Skip to content

Instantly share code, notes, and snippets.

@mpuig
Last active June 13, 2020 10:39
Show Gist options
  • Save mpuig/4502507 to your computer and use it in GitHub Desktop.
Save mpuig/4502507 to your computer and use it in GitHub Desktop.
How to calculate the (real) distance of a linestring object, in km (or any other distance unit)
from django.contrib.gis.geos import Point
from django.contrib.gis.geos import LineString
from models import Track
# [...]
points = []
for lat,lon in coords:
pnt = Point(float(lon), float(lat))
points.append(pnt)
track.geom = Linestring(points)
track.save()
track.length = Track.objects.length().get(id=track.id).length.km
track.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment