#!/usr/bin/env python # shpUtils.py # Original version by Zachary Forest Johnson # http://indiemaps.com/blog/index.php/code/pyShapefile.txt # This version modified by Michael Geary from struct import unpack import dbfUtils XY_POINT_RECORD_LENGTH = 16 db = [] def loadShapefile( filename ): # open dbf file and get features as a list global db dbfile = open( filename[0:-4] + '.dbf', 'rb' ) db = list( dbfUtils.dbfreader(dbfile) ) dbfile.close() fp = open( filename, 'rb' ) # get basic shapefile configuration fp.seek(32) filetype = readAndUnpack('i', fp.read(4)) bounds = readBounds( fp ) # fetch Records fp.seek(100) features = [] while True: feature = createRecord(fp) if feature == False: break getPolyInfo( feature ) features.append( feature ) return { 'type': filetype, 'bounds': bounds, 'features': features } record_class = { 0:'RecordNull', 1:'RecordPoint', 8:'RecordMultiPoint', 3:'RecordPolyLine', 5:'RecordPolygon' } def createRecord(fp): # read header record_number = readAndUnpack('>L', fp.read(4)) if record_number == '': return False content_length = readAndUnpack('>L', fp.read(4)) rectype = readAndUnpack('