Distance query help

167 views
Skip to first unread message

Matt Waite

unread,
Sep 1, 2010, 11:45:19 AM9/1/10
to geodjango
Good folks, I'm begging your help. I've run up against a distance
query that I can get to work right.

I've got a view that takes a lat, lon pair, turns it into a point, and
queries a list of locations based on that point. Pretty basic stuff:

pnt = Point(float(request.GET['lon']), float(request.GET['lat']))

places = Model.objects.filter(place__point__distance_lte=(pnt,
D(mi=2)))

Now, what I want to do is sort places by their distance to pnt. I
thought it would be something like:

places = Model.objects.filter(place__point__distance_lte=(pnt,
D(mi=2))).distance(pnt).order_by('distance')

But I get:

TypeError: ST_Distance output only available on GeometryFields.

I'm sure there's a way to do this and I'm just not seeing it. Can
anyone point me in a direction?

Matt

Trevor Wiens

unread,
Sep 1, 2010, 12:35:21 PM9/1/10
to geod...@googlegroups.com, geodjango
My guess is that geodjango is representing your data as a geography field and not a geometry field. Distance is only been supported for geometry types instead of geography fields in previous versions of postgis. I don't know about the latest release. I'm a newbie with geodjango so I don't know if it is possible to transform geography to geometry within the framework which would then allow you to do your distance query.

TSW
------------------
Trevor Wiens
Sent from my mobile

> --
> You received this message because you are subscribed to the Google Groups "geodjango" group.
> To post to this group, send email to geod...@googlegroups.com.
> To unsubscribe from this group, send email to geodjango+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/geodjango?hl=en.
>

Justin Bronn

unread,
Sep 1, 2010, 12:56:43 PM9/1/10
to geod...@googlegroups.com
On 9/1/10 11:35 AM, Trevor Wiens wrote:
> My guess is that geodjango is representing your data as a geography field and not a geometry field.

At first I thought this might be case, but geography fields are opt-in
(GeoDjango won't use them unless explicitly told so via `geography=True`
keyword). Also, ST_Distance exists for geography columns.

>> places = Model.objects.filter(place__point__distance_lte=(pnt,
>> D(mi=2))).distance(pnt).order_by('distance')
>>
>> But I get:
>>
>> TypeError: ST_Distance output only available on GeometryFields.
>>
>> I'm sure there's a way to do this and I'm just not seeing it. Can
>> anyone point me in a direction?

It's failing because there's no geographic field on `Model`.
GeoQuerySet methods operate on the first geographic field in the model
by default. It looks like you want to perform the query on the point
column of a _related_ model -- thus, you'll have to specify this with
the `field_name` keyword your `distance()` call [1]:

places =
Model.objects.filter(place__point__distance_lte=(pnt,D(mi=2))).distance(pnt,
field_name='place__point').order_by('distance')

Note that for this to work, you'll need to have `objects = GeoManager()`
on Model, which I'm assuming you don't. [2]

[1]
http://docs.djangoproject.com/en/dev/ref/contrib/gis/geoquerysets/#geoqueryset-methods

[2]
http://docs.djangoproject.com/en/dev/ref/contrib/gis/model-api/#geomanager

-Justin

Matt Waite

unread,
Sep 1, 2010, 3:17:10 PM9/1/10
to geodjango
You know, it's far too rare that I get to tell Justin Bronn he's a
damned genius. Because it's true. He is. Thanks Justin. I did have
objects=GeoManager() on the model, but I was missing the field_name
bits and that worked like a charm.

By the way, if you're curious where this is getting applied, this site
is Django -- and more specifically GeoDjango -- powered:
http://www.tampabay.com/things-to-do/

A more specific example of the use of geo queries:
http://www.tampabay.com/things-to-do/neighborhoods/downtown-st-petersburg/

Matt

Paolo Corti

unread,
Sep 1, 2010, 6:03:39 PM9/1/10
to geod...@googlegroups.com
> .... I'm a newbie with geodjango so I don't know if it is possible to transform geography to geometry within the framework which would then allow you to do your distance query.

you may do that easily
best regards
P

--
Paolo Corti
GIS Architect and Developer
web: http://www.paolocorti.net
twitter: @paolo_corti

Ivan

unread,
Feb 18, 2014, 5:44:20 PM2/18/14
to geod...@googlegroups.com
Bringing an old topic to life... The solution Justin posted works great for ForeignKey relationship. But is the same thing somehow possible for many2many relationship? Right now, the same approach gives the ST_Distance output only available on GeometryFields error.

Cheers,
Ivan
Reply all
Reply to author
Forward
0 new messages