Skip to main content

Posts

Showing posts with the label Django

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

Passing values from the view to the form.

Passing values from the view to the form it is really popular. Ex: form should be different for different kind of the user. Unfortunately I have not seen a documentation how to do this with class based view. This task is separated in the two parts: 1) Prepare values in the view class Myview(): """ This is only example view """ # set form form_class = myformwithadditionallogic # our function which pass some values to the form def get_initial(self): """ Passing initial values to the form""" # default initial should be saved super(Myview, self).get_initial() self.initial = {"ourvalue":1} return self.initial That all for view. Second part is retreive this data in the form: class myformwithadditionallogic(): """ the example form . has no field only constructor """ def __init__(self, *args, **kwargs): """ Constructor """ myvalues = kwargs.pop( ...