Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am using Postgresql 9.0 and PostGIS 2.0. I am using Django 1.3 and GeoDjango.I have model with id,latitude,longitude and placename. All the places are in 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?

Please guide me. thanks

share|improve this question

1 Answer 1

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.name, 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:

distance_lt
distance_lte
distance_gt
distance_gte
dwithin

Dont forget, qs is in array and it filter 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

 
discard

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.