Skip to content

Instantly share code, notes, and snippets.

@springmeyer
Created December 14, 2012 21:20
Show Gist options
  • Save springmeyer/4288759 to your computer and use it in GitHub Desktop.
Save springmeyer/4288759 to your computer and use it in GitHub Desktop.
Convert bbox to polygon and encode as geo csv for TileMill
#!/usr/bin/env python
'''
Usage:
# edit the script below "main" then:
python bbox2csv.py > bounds.csv
'''
def bbox2wkt(minx, miny, maxx, maxy):
# first validate bbox values
assert isinstance(minx,float) or isinstance(minx,int)
assert isinstance(miny,float) or isinstance(miny,int)
assert isinstance(maxx,float) or isinstance(maxx,int)
assert isinstance(maxy,float) or isinstance(maxy,int)
assert (minx < maxx), 'failed: %s is not < %s' % (minx,maxx)
assert (miny < maxy), 'failed: %s is not < %s' % (miny,maxy)
return "Polygon((%(minx)s %(miny)s, %(minx)s %(maxy)s, %(maxx)s %(maxy)s, %(maxx)s %(miny)s, %(minx)s %(miny)s))" % locals()
if __name__ == "__main__":
bbox = '-122,45,-100,48'
print 'wkt,name'
print '"%s","%s"' % (bbox2wkt(*map(float,bbox.split(','))),'bounds')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment