[Fixed]-Geo Django get cities from latitude and longitude

10👍

You have several solutions to do this:

  1. Create another model City

    from django.contrib.gis.db import models
    class City(models.Model):
        name = models.CharField(max_lenght=255)
        geometry = models.MultiPolygonField()
    
        objects = models.GeoManager()
    

Then you can find the name of the city containing your location with:

    geomodel = GeoModel(...)
    city = City.objects.get(geometry__contains=geomodel.location)
    city_name = city.name

Depending on the country/region you are interested in, you may find city geometries to fill the City table in OpenData (eg http://www.gadm.org/country (not tested but seems to propose open shapefiles))

  1. Use an inverse geocoder. Google can provide such service or look at http://www.geonames.org/export/reverse-geocoding.html if something can match your needs.

Leave a comment