Shapefile.py
Introduction
Shapefile.py is an Open Source module for reading and writing Esri-format shapefiles. Shapefile.py is interesting both because it is very easy to use and an example of how to read and write binary files with Python.
Shapefile.py is much easier to use than the "cursors" provided by ArcGIS but Shapefile.py does not provide the ability to filter shapefiles by attributes. There is minimal documentation available for Shapefile.py but a good tutorial is available under "Additional Resources" below.
Below is an example of using shapefile.py to read the bounding boxes for the shapes in a shapefile and then read the attribute values.
# import Tkinter, Tkinter's FileDialog, the shapefile module, and os.path import Tkinter import tkFileDialog import shapefile import os.path # initialize Tkinter root = Tkinter.Tk() # Get a full path to a shapefile from the user FilePath = tkFileDialog.askopenfilename(filetypes=[("allfiles","*")]) # split the full path into a path without an extension and the extension FileNameWithoutExtension,Extension=os.path.splitext(FilePath) # splitext() returns a unicode string which is not supported by shapefile.py # so we need to convert it to an ASCII string FileNameWithoutExtension=str(FileNameWithoutExtension) # open the shapefile TheShapefile=shapefile.Reader(FileNameWithoutExtension) # get the list of shapes (points, polygons, or polines) in the file TheShapes=TheShapefile.shapes() # find the number of shapes from the length of the list NumShapes=len(TheShapes) print("Number of Shapes:"+format(NumShapes)) # iterate through each shape in the list and print out it's bounding box for TheShape in TheShapes: print("Box: "+format(TheShape.bbox)) # bbox stands for bounding box # get the list of fields (attribute column headings) from the file TheFields=TheShapefile.fields # print the field names print(TheFields) # get the records (attribute rows) from the shapefiles dbf file TheRecords=TheShapefile.records() # find the number of records NumRecords=len(TheRecords) print("Number of records: "+format(NumRecords)) # print each of the records (attribute rows) in the shapefile Count=0 while (Count<NumRecords): print("Records: "+format(TheRecords[Count])) Count+=1
Additional Resources