Login

Comma Seprated Character Field to store Geographic Coordinates

Author:
vijay.shanker
Posted:
December 17, 2012
Language:
Python
Version:
1.4
Score:
0 (after 0 ratings)

in models, import GeoCoordinateField:

class Place(models.Model): geocoordn = GeoCoordinateField(verbose_name="geocordfield", null=True, blank = True)

place = Place.objects.geocoordn #gives you a Geocoordinate object place.geocoordn.latitude, place.geocoordn.longitude #gives latitude and longitude of place

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from django.db import models
from math import acos, cos, sin

class GeoCoordinate(object):
    def __init__(self,latitude=0.00,longitude=0.00):
        self.latitude = latitude
        self.longitude = longitude
                
    def within_radius(self,r,geocoordn_obj):
        print geocoordn_obj.__dict__
        if acos(sin(geocoordn_obj.latitude)*sin(self.latitude) + \
            cos(geocoordn_obj.latitude)*cos(self.latitude)*cos(self.longitude -(geocoordn_obj.longitude)))*6371 <=r:
            return True
        else:
            return False
        
        
    def __repr__(self):
        return str(self.latitude) + ',' + str(self.longitude)
    
    
class GeoCoordinateField(models.Field):
    description = "A latitude,longitude pair"
    __metaclass__ = models.SubfieldBase
    
    def __init__(self,help_text=("A comma-separated latitude longitude pair"),verbose_name='geocordfield', *args,**kwargs):
        self.name="GeoCoordinateField",
        self.through = None
        self.help_text = help_text
        self.blank = True
        self.editable = True
        self.creates_table = False
        self.db_column = None
        self.serialize = False
        self.null = True
        self.creation_counter = models.Field.creation_counter
        models.Field.creation_counter += 1
        super(GeoCoordinateField, self).__init__(*args, **kwargs)
        
        
    def db_type(self, connection):
        return 'varchar(100)'
    
    def to_python(self,value):
        if value in ( None,''):
            return GeoCoordinate()
        else:
            if isinstance(value, GeoCoordinate):
                return value
            else:
                args = [float(value.split(',')[0]),float(value.split(',')[1])]
                if len(args) != 2 and value is not None:
                    raise ValidationError("Invalid input for a GeoCoordinate instance")
                return GeoCoordinate(*args)
        
    
    def get_internal_type(self):
        return 'CharField'
    
    def value_to_string(self, obj):
        value = self._get_val_from_obj(obj)
        return self.get_prep_value(value)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 3 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 11 months ago
  5. Help text hyperlinks by sa2812 11 months, 3 weeks ago

Comments

Please login first before commenting.