Both OGR and PyShp
read and write the dbf
files because they are part of the shapefile specification. The dbf
files contain the attributes and fields for the shapefiles. However, both libraries have very basic dbf
support. Occasionally, you will need to do some heavy duty dbf
work. The dbfpy3
module is a pure Python module dedicated to working with dbf
files. It is currently hosted on github.com
only. You can force easy_install
to find the download by specifying the download file:
easy_install -f https://github.com/GeospatialPython/dbfpy3/archive/master.zip
If you are using pip
to install packages, use the following command:
pip install https://github.com/GeospatialPython/dbfpy3/archive/master.zip
The following shapefile has over 600 dbf records representing U.S. Census Bureau tracts which make it a good sample for trying out dbfpy
:
https://github.com/GeospatialPython/Learn/raw/master/GIS_CensusTract.zip
Let's open up the dbf
file of this shapefile and look at the first record:
>>> from dbfpy3 import dbf >>> db = dbf.Dbf("GIS_CensusTract_poly.dbf") >>> db[0] GEODB_OID: 4029 (<type 'int'>) OBJECTID: 4029 (<type 'int'>) PERMANE0: 61be9239-8f3b-4876-8c4c-0908078bc597 (<type 'str'>) SOURCE_1: NA (<type 'str'>) SOURCE_2: 20006 (<type 'str'>) SOURCE_3: Census Tracts (<type 'str'>) SOURCE_4: Census Bureau (<type 'str'>) DATA_SE5: 5 (<type 'str'>) DISTRIB6: E4 (<type 'str'>) LOADDATE: 2007-03-13 (<type 'datetime.date'>) QUALITY: 2 (<type 'str'>) SCALE: 1 (<type 'str'>) FCODE: 1734 (<type 'str'>) STCO_FI7: 22071 (<type 'str'>) STATE_NAME: 22 (<type 'str'>) COUNTY_8: 71 (<type 'str'>) CENSUST9: 22071001734 (<type 'str'>) POPULAT10: 1760 (<type 'int'>) AREASQKM: 264.52661934 (<type 'float'>) GNIS_ID: NA (<type 'str'>) POPULAT11: 1665 (<type 'int'>) DB2GSE_12: 264526619.341 (<type 'float'>) DB2GSE_13: 87406.406192 (<type 'float'>)
The module very quickly and easily gives us both the column names and data values. Now let's increment the population field contained in POPULAT10
by 1
:
>>> rec = db[0] >>> field = rec["POPULAT10"] >>> rec["POPULAT10"] = field + 1 >>> rec.store() >>> del rec >>> db[0]["POPULAT10"] 1761
Keep in mind that both OGR and PyShp can do this same procedure, but dbfp3y
makes it a little easier if you are making a lot of changes to the dbf
files only.