1

I'l like to be able to calculate the length in meters of a LineString, in python. I came across this question: Get LineString length in meters (Python, GEODjango) but the recommended method seems to disagree with the calculation given by ST_Length (spatialite in this example) - which also agrees the actual length of the path.

I don't want to use ST_Length, I need to find the length in python. But I can't figure out why these calculations differ. To exemplify:

My Model:

class TestRoute(models.Model):
    path = models.LineStringField(null=True)

Using GeoDjango's Length annotation after creating an instance with an example path returns the correct length in meters:

In [1]: TestRoute.objects.annotate(length=Length('path'))[0].length.m
Out[1]: 41345.49910715875

But using the same instance:

In [2]: path = TestRoute.objects.all()[0].path
In [3]: path.srid
Out[3]: 4326
In [4]: path.length
Out[4]: 0.416972813296872
In [5]: path.transform(3857)
In [6]: path.length
Out[6]: 51830.398931961485

The difference is about 10.5k meters.

How can I get the total length of a LineString without needing to save it to a model?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.