-
-
Save percursoaleatorio/5294032 to your computer and use it in GitHub Desktop.
OGR buffer vector data. From http://goo.gl/TjjQk Via https://gist.github.com/jgomezdans/808194
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#buffer.py | |
import sys | |
import os | |
from osgeo import ogr | |
def buffer(infile,outfile,buffdist): | |
try: | |
ds_in=ogr.Open( infile ) | |
lyr_in=ds_in.GetLayer( 0 ) | |
drv=ds_in.GetDriver() | |
if os.path.exists( outfile ): | |
drv.DeleteDataSource(outfile) | |
ds_out = drv.CreateDataSource( outfile ) | |
lyr_out = ds_out.CreateLayer( lyr_in.GetLayerDefn().GetName(), lyr_in.GetSpatialRef(), ogr.wkbPolygon) | |
for i in xrange ( lyr_in.GetLayerDefn().GetFieldCount() ): | |
field_in = lyr_in.GetLayerDefn().GetFieldDefn( i ) | |
field_out = ogr.FieldDefn( field_in.GetName(), field_in.GetType() ) | |
lyr_out.CreateField ( field_out ) | |
for feat_in in lyr_in: | |
geom = feat_in.GetGeometryRef() | |
feat_out = feat_in.Clone() | |
feat_out.SetGeometry(geom.Buffer(float(buffdist))) | |
lyr_out.CreateFeature(feat_out) | |
del geom | |
ds_out.Destroy() | |
except: | |
return False | |
return True | |
if __name__=='__main__': | |
usage='usage: buffer <infile> <outfile> <distance>' | |
if len(sys.argv) == 4: | |
if buffer(sys.argv[1],sys.argv[2],sys.argv[3]): | |
print 'Buffer succeeded!' | |
sys.exit(0) | |
else: | |
print 'Buffer failed!' | |
sys.exit(1) | |
else: | |
print usage | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment