Advertisement
Guest User

Untitled

a guest
Jan 30th, 2013
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Coordinates(object):
  2.  
  3.     def __init__(self, lng, lat):
  4.         super(Coordinates, self).__init__()
  5.         self.lng = float(lng)
  6.         self.lat = float(lat)
  7.  
  8.     def to_sql(self):
  9.         return "GeomFromText('POINT(%s %s)')" % (self.lng, self.lat)
  10.  
  11.     def __unicode__(self):
  12.         return 'Coordinates(lat=%s, lng=%s)' % (self.lat, self.lng)
  13.  
  14.     @staticmethod
  15.     def to_python(value):
  16.         return value
  17.         # write the rest when i know what to do
  18.         return Coordinates(0, 0)
  19.  
  20.  
  21. class GeometryField(models.Field):
  22.  
  23.     __metaclass__ = models.SubfieldBase
  24.  
  25.     def __init__(self,  *args, **kwargs):
  26.         #self.max_length = 68
  27.         super(GeometryField, self).__init__(*args, **kwargs)
  28.         self.spatial = True
  29.  
  30.     def db_type(self, connection):
  31.         return 'GEOMETRY'
  32.  
  33.     def to_python(self, value):
  34.         if isinstance(value, Coordinates):
  35.             return value
  36.  
  37.         return Coordinates.to_python(value)
  38.  
  39.     def get_prep_value(self, value):
  40.         return self.to_python(value)
  41.  
  42.     def get_db_prep_save(self, value, connection):
  43.         return self.to_python(value).to_sql()
  44.  
  45.     def value_to_string(self, obj):
  46.         value = self._get_val_from_obj(obj)
  47.         return self.get_db_prep_value(value)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement