Guest User

Untitled

a guest
Jan 30th, 2013
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.49 KB | None | 0 0
  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 Coordinates(0, 0)# value
  17.         lat = None
  18.         lng = None
  19.  
  20.         if isinstance(value, (list, tuple)):
  21.             lat, lng = value
  22.             return Coordinates(lat, lng)
  23.  
  24.         if isinstance(value, (str, unicode)):
  25.             lat, lng = value.split(' ')
  26.             return Coordinates(lat, lng)
  27.  
  28.  
  29. class GeometryField(models.Field):
  30.  
  31.     __metaclass__ = models.SubfieldBase
  32.  
  33.     def __init__(self,  *args, **kwargs):
  34.         #self.max_length = 68
  35.         super(GeometryField, self).__init__(*args, **kwargs)
  36.         self.spatial = True
  37.  
  38.     def db_type(self, connection):
  39.         return 'GEOMETRY'
  40.  
  41.     def to_python(self, value):
  42.         if isinstance(value, Coordinates):
  43.             return value
  44.  
  45.         return Coordinates.to_python(value)
  46.  
  47.     def get_prep_value(self, value):
  48.         return self.to_python(value)
  49.  
  50.     def get_db_prep_save(self, value, connection):
  51.         return self.to_python(value).to_sql()
  52.  
  53.     def value_to_string(self, obj):
  54.         value = self._get_val_from_obj(obj)
  55.         return self.get_db_prep_value(value)
Advertisement
Add Comment
Please, Sign In to add comment