Skip to content

Instantly share code, notes, and snippets.

@dgreyling
Forked from bmcbride/geojson-length.py
Created June 10, 2016 20:00
Show Gist options
  • Save dgreyling/85eb74040d1f3f0f04837138f8502d16 to your computer and use it in GitHub Desktop.
Save dgreyling/85eb74040d1f3f0f04837138f8502d16 to your computer and use it in GitHub Desktop.
Calculate the length of a GeoJSON linestring using the Python GDAL/OGR API
from osgeo import ogr
from osgeo import osr
source = osr.SpatialReference()
source.ImportFromEPSG(4326)
target = osr.SpatialReference()
target.ImportFromEPSG(3857)
transform = osr.CoordinateTransformation(source, target)
geojson = """{ "type": "LineString", "coordinates": [ [ -75.313585, 43.069271 ], [ -75.269426, 43.114027 ], [ -75.158731, 43.114325 ] ] }"""
geom = ogr.CreateGeometryFromJson(geojson)
geom.Transform(transform)
print "Length = %d" % geom.Length() + " meters"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment