Friday, February 5, 2016

Django PostGIS How to calculate length of linestring stored in Lon Lat format

I'm working on simple GTS system  and one of the task is calculate distance.  Usually  GPS tracker sends coordinates as Lon , Lat.  It is easy to understands by navigator,  but not a citizen. Brute force conversation requires knowledge of geography, mathematic etc.  Django   and PostGIS have simple and elegant solution:

 example model:

from django.contrib.gis.db import models as gismodels
class Points(models.Model):

    """ Simple class with one Point only """

    Point    = gismodels.PointField()

pointlist is array of the points containing geo data:

 for point in pointlist:



            linestring = LineString(plist)



            dis = linestring.length


            dis = 0

        return  dis/1000


Thats all!

Prepare array of point  as Lon, Lat.
Transform it  to meters changing SRID
Calculate distance

All job is done by PostGIS and DB level.  I like this one.