Skip to main content

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() 


view.py

pointlist is array of the points containing geo data:

 for point in pointlist:

            plist.append(point.Point)

        try:

            linestring = LineString(plist)

            linestring.srid=4326

            linestring.transform(22186)

            dis = linestring.length

        except:

            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.

Comments

Popular posts from this blog

Small script for one of the plarium game

few notes about google games. Goggle informed that access to the all games be closed after June, 30. I played "Pirates: Tides of fortune" and decided to limit my game time using small script. Detail is below. Some history First internet games were updated network games only. 15 years ago internet connection requires a lot of money and nobody had a problem with lagging of the other players. Usually games uses personal communication protocol. Warbirds, Aces High and Eve online use this way. Next part use a browser and trivial HTTP protocol. One of the popular game in this area is travian . Travian player uses browser and every browser (PC, cell phone, tablet) can be used for playing. Of course, popularity of the game is related to graphics. Trivial HTTP does not have good power in this and other technology is used for this side. One of them is Flash . Unfortunately flash requires a lot of CPU : "Pirates" was near to froze at my old celeron with ...

Small python script for monitoring MySQL performance

I have few services which use MySQL as database server. I would like to have information about load in PNG  image  or  in Cacti app. MySQL   has  performance information at 'SHOW STATUS' command. Values  which  are monitored :   threads_running, threads_connected, thread_cached, slow_queries  Of course,  it is really easy to add more variables. Connection to MySQL is accomplished by MySQLdb  module. Typical example of usage is below : import MySQLdb mydb = MySQLdb.connect(host = 'hostname', user = 'username', password = 'secret', database = 'mysatabase' ) mycursor = mydb.cursor() mycursor.execute('SQL command') sqlresult = cur.fetchall() Storing data in rrd file is aviable via rrdtools package. This one is present in debian and Centos OS. example of creating file is below: import rrdtool rrdtool.create("myfile.rrd" , "DS:value1:data...

checking host hardware using linux kernel

How to collect information about  host without trivial system command   I'm using a lot of hardware boxes with no limit on distro types and versions.  In this case using system command such as ifconfig has a limited usage.  Also,  system config files have a different path and best way for me is getting information from the kernel. Of course,  I'm using cat command for doing this. Next table has information about hardware. Let me know if something is interesting for you and you know something interesting hardware information in the linux kernel file path information /proc/cpuinfo CPU /proc/meminfo memory /sys/block List of block devices. HDD has sd or hd at the beginning /sys/class/block/{digit}/device/model HDD drive and vendor /sys/block/{digit}/size HDD size  /sys/block List of block devices. HDD has sd or hd at the beginning /sys/bus/pci/devices/0000:{digit}/net System name of the network card /sys/device...