Tuesday, March 18, 2008

OGR python projection

OGR Projection

If you're using shapely and you need to do projections, you'll either have a lot of boilerplate or a function like this one. Actually, even in OGR, there's a lot of bioler plate involved in transforming....

from osgeo import ogr
from shapely.wkb import loads

def project(geom, to_epsg=900913, from_epsg=4326):
"""utility function to do quick projection with ogr,
to and from shapely objects
>>> from shapely.geometry import LineString
>>> l = LineString([[-121, 43], [-122, 42]])
>>> lp = project(l, from_epsg=4326, to_epsg=26910)
>>> lp.wkt
'LINESTRING (663019.0700828594854102 4762755.6415722491219640, 582818.0692490270594135 4650259.8474613213911653)'

to_srs = ogr.osr.SpatialReference()

from_srs = ogr.osr.SpatialReference()

ogr_geom = ogr.CreateGeometryFromWkb(geom.wkb)

return loads(ogr_geom.ExportToWkb())

import doctest

No comments: