Saturday, June 12, 2010

Buffering Features with GeoScript Python

In a previous post, Jared buffered points and added them to a polygon shapefile. Here's the workflow in Python GeoScript.

from geoscript import proj
from geoscript import geom
from geoscript.layer import Shapefile
from geoscript.feature import schema

def changeGeometryType(strName,geomType,shpParent):

#Get all shapefile schema attributes except 'the_geom'
parentS = [(str(,i.typ) for i in shpParent.schema.fields if not issubclass(i.typ,geom.Geometry)]

#Get shapefile Projection
pPrj = shpParent.schema.get('the_geom').proj

#Reinsert geomType into schema

#Create centroid buffer schema
centroidSchema = schema.Schema(strName,parentS)

return centroidSchema

if __name__=='__main__':

#Get centroid shapefile
shp = Shapefile('/home/gregcorradini/GeoTools/geoscript_wrk/centroid_buffer/data/centroids.shp')

#Create a geom.Polygon schema from centroid schema
centroidSchema = changeGeometryType('centroid_buffer',geom.Polygon,shp)

#Get shapefile workspace
ws = shp.workspace

#Create a buffered centroid layer based on centroid schema
buffCentLayer = ws.create(schema=centroidSchema)

#For each centroid feature, create a buffered centroid, add it to buffered layer
for f in shp.features():

#Copy all feature attributes into a dictionary
dictFeat = dict(f.iteritems())

#Buffer 'the_geom' 3 degrees
dictFeat['the_geom'] = f.geom.buffer(3)

print dictFeat

#Create a buffered centroid feature with new dictionary attributes
buffFeature = buffCentLayer.schema.feature(dictFeat,

#Add new feature to buffered layer

No comments:

Post a Comment

Introducing GeoScript

GeoScript adds geo capabilities to dynamic scripting languages such as JavaScript, Python, Scala and Groovy.