liste

Bonjour et bienvenue dans mon site ! Willkommen auf meiner Seite! welcome in my homepage! http://assistance-en-sig.blogspot.com/ SIG ; Bases de données ; Géomatique; Python; ArcGIS; QGIS;

recherche

Sunday, November 10, 2013

pyshp: Python Shapefile Library

This library reads and writes ESRI Shapefiles in pure Python. You can read and write shp, shx, and dbf files with all types of geometry. Everything in the public ESRI shapefile specification is implemented. This library is compatible with Python versions 2.4 to 3.x.

Overview

This library reads and writes ESRI Shapefiles in pure Python. You can read and write shp, shx, and dbf files with all types of geometry. Everything in the public ESRI shapefile specification is implemented. This library is compatible with Python versions 2.4 to 3.x.

Get Started Instantly

  1. Download shapefile.py
  2. Start Python
  3. import shapefile
  4. Try one of the examples below
OR
Just run: easy_install pyshp
OR
pip install pyshp
If you are looking for information on .sbn and .sbx file formats some documentation is available here.

License

This library is released under the MIT license allowing it to be used for both commercial and non-commercial use. If your organization requires a different license for legal or policy reasons please contact the author through GeospatialPython.com.

Usage

The "Source" tab contains the SVN trunk with the latest release. Tagged releases representing relatively stable versions are also in the repository. The python module "shapefile.py" contains the complete library.
Detailed documentation and examples can be found in the Wiki as well as the "Download" tab. These files are different formats for the same information.
Below are minimal examples to give you a quick idea of what using the library "feels" like. More examples can also be found onGeospatialPython.com.
Important: For information about map projections, shapefiles, and Python please visit: http://code.google.com/p/pyshp/wiki/MapProjections

Reading Shapefiles

Reading Points in Shapes

>>> import shapefile>>> sf = shapefile.Reader("shapefiles/blockgroups")
>>> shapes = sf.shapes()
>>> # Read the bounding box from the 4th shape
>>> shapes[3].bbox[-122.485792, 37.786931000000003, -122.446285, 37.811019000000002]
>>>#  Read the 8th point in the 4th shape
>>> shapes[3].points[7]
[-122.471063, 37.787402999999998]

Reading Database Attributes

>>> # Read the field descriptors for the database file
>>> sf.fields[("DeletionFlag", "C", 1, 0), ["AREA", "N", 18, 5], ... ["BKG_KEY", "C", 12, 0], ["POP1990", "N", 9, 0], ["POP90_SQMI", "N", 10, 1], ... ["HOUSEHOLDS", "N", 9, 0], ... ["MALES", "N", 9, 0], ["FEMALES", "N", 9, 0]]
>>> # Read the 2nd and 3rd field values of the 4th database record
>>> sf.records[3][1:3]
['060750601001', 4715]

Writing Shapefiles

>>> import shapefile>>> # Make a point shapefile
>>> w = shapefile.Writer(shapefile.POINT)
>>> w.point(90.3, 30)
>>> w.point(92, 40)
>>> w.point(-122.4, 30)
>>> w.point(-90, 35.1)
>>> w.field('FIRST_FLD')
>>> w.field('SECOND_FLD','C','40')
>>> w.record('First','Point')
>>> w.record('Second','Point')
>>> w.record('Third','Point')
>>> w.record('Fourth','Point')
>>> w.save('shapefiles/test/point')
>>> # Create a polygon shapefile
>>> w = shapefile.Writer(shapefile.POLYGON)
>>> w.poly(parts=[[[1,5],[5,5],[5,1],[3,3],[1,1]]])
>>> w.field('FIRST_FLD','C','40')
>>> w.field('SECOND_FLD','C','40')
>>> w.record('First','Polygon')
>>> w.save('shapefiles/test/polygon')

Tests

The above examples are very simple. There are many other shortcuts/features listed in the longer usage examples.
The file "README.txt" is a doctest module containing basic tests which are called if you run "shapefile.py" directly instead of importing it.
The sample shapefile used in the test and the output directory structure are in the "shapefiles" directory in the trunk.
Bug reports, fixes, improvements are welcome
Commercial support is available from NVision Solutions

No comments:

Post a Comment