Skip to content

Instantly share code, notes, and snippets.

@bmcbride
Created March 31, 2014 20:41
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bmcbride/9901745 to your computer and use it in GitHub Desktop.
Save bmcbride/9901745 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"
@erenerdilli
Copy link

erenerdilli commented Dec 9, 2021

What is the actual length formula it is using? It should be Sqrt(Abs(X1-X2)^2+Abs(Y1-Y2)^2)*111139 right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment