I'm learning how to use Geo Django. When a user registers I save the latitude and longitude information as seen below:
from django.contrib.gis.db import models
from django.contrib.gis.geos import Point
class GeoModel(models.Model):
"""
Abstract model to provide GEO fields.
"""
latitude = models.FloatField(blank=True, null=True, verbose_name='Latitude')
longitude = models.FloatField(blank=True, null=True, verbose_name='Longitude')
location = models.PointField(blank=True, null=True)
class Meta:
abstract = True
def save(self, *args, **kwargs):
if self.latitude and self.longitude:
self.location = Point(float(self.latitude), float(self.longitude))
super(GeoModel, self).save(*args, **kwargs)
Next I want to add a new field called city
where **I want to store the city "name" i.e. London based the long and lat details given.
I have looked at django-cities
where is saves a database of cities from Geo Name, but this seems overkill and not Python 3 compatible so a no go for me. I have seen a package called geopy but this seems to completely replace Geo Django and uses Google API which comes with restrictions.
I want to stick with Geo Django and somehow do a lookup to find out the City name based on long and lat so what are my options? i.e..
- Find a way to install http://www.geonames.org/ data django-cities does not work for Python 3 and Django 1.8.x
- Somehow use Geo Django to do this lookup using the data? (unsure ow to do this)
Is above the correct way to approach this? If so how do I get the data into my DB then do a lookup on it using Geo Django to find the city name?