|
Project Information
Members
Featured
Downloads
Links
|
OverviewThis 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
OR Just run: easy_install pyshp If you are looking for information on .sbn and .sbx file formats some documentation is available here. Latest News2/28/2012 - Added a brief description of the Reader.shapeRecords() method in the Wiki 10/01/2011 - Thanks to a lot of work from memedough, pyshp now has a single code base for Python 2 and 3. I haven't tested Jython and IronPython yet but they should be covered. More about this topic is in Issue 19 . 9/27/2011 - You can now read shapefiles from file-like objects in the Python 2 version as well as write to file-like objects. The documentation has been updated to reflect this change. There is also a proposal in place to create a common version of the library that runs in both Python 2 and Python 3. The "usage.py" file has been moved to a restructured text README.txt file for a cleaner installation from pypi. For now the Python 3 version is a separate branch in subversion and lags behind the Python 2 version in both features and bug fixes. This difference will be remedied soon one way or another. 9/4/2011 - Significant bug related to blank dbf attributes fixed ( Issue 15 ). Please upgrade to at least 1.0.6 because sooner or later this bug will bite you. Also implemented an improvement to robustly handle improperly formatted date fields in dbf files which happens way more than you'd think ( Issue 6 ). 9/2/2011 - Thanks to prodding and assistance from several users pyshp is now registered on PyPi using setuptools. So you can install it using any of the setuptools methods (i.e. easy_install). The setup.py script is also in the subversion respository. LicenseThis 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. UsageThe "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. Important: For information about map projections, shapefiles, and Python please visit: http://code.google.com/p/pyshp/wiki/MapProjections Reading ShapefilesReading 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')TestsThe 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 |