Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using Postgresql 9.0 and PostGIS 2.0. I am using Django 1.3 and GeoDjango. I have a model with id, latitude, longitude and placename. All the places are in the USA.

My requirement is following:

  • User will give his address. We use his address to get latitude and longitude, and then find places within 'x' miles.

So, should I use PointField in the model? Also, how do I convert users latitude\longitude into geometry object to use dwithin?

share|improve this question

Your model must be like this:

# Import django module
from django.contrib.gis.db import models

class Point(models.Model):
    placename = models.CharField(max_length=50)
    geom = models.PointField(srid=4326)
    objects = models.GeoManager()

    def __unicode__(self):
        return '%s %s %s' % (, self.geom.x, self.geom.y)

your view is :

from django.contrib.gis.geos import *
from adress.models import Point
from django.http import HttpResponse
from django.contrib.gis.measure import D # ``D`` is a shortcut for ``Distance``

def AdressFinder(request):
    adress = request.POST.get('sentAdress', '')
    getAdressCoord = Point.objects.get(placename = str(adress))
    po = POINT(getAdressCoord.x getAdressCoord.y)
    pnt = fromstr(str(po), srid=4326)
    qs = Point.objects.filter(point__distance_lte=(pnt, D(km=5)))

    return HttpResponse(your choose for returning object)

it will give you objects in 5 km square.

Geodjango Distance Lookups
Availability: PostGIS, Oracle, SpatiaLite

The following distance lookups are available:


Don't forget, qs is in array and it filters all objects for your request that you have to loop for getting all objects one by one..

Now you can convert your result to GeoJSON or anything for your app...
I hope it helps you....

share|improve this answer
I have already done this but getting error when firing distance queries – carlsen Apr 26 '12 at 6:28
What errors are you getting? – blah238 Apr 26 '12 at 6:34
first of all, you have to make geometry column in your db from your x and y. because of spaial query, you will need this...and give me some detail about your error... – Aragon Apr 26 '12 at 6:46
Very helpful, thanks! – Mark Bell Mar 23 '13 at 9:31

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.