GeoDjango通过coords查询PointField

时间:2014-03-02 16:30:29

标签: python django geodjango

是否可以通过PointField的lng,lat值查询django模型?

<---example--->

Foo.objects.filter(bar=(-73, 34)) 

## bar == name of PointField in the django model.
## -73 == longitude
## 34 == latitude

1 个答案:

答案 0 :(得分:4)

检查与坐标的完全匹配可能不是一个好主意。您应该使用提供的坐标的距离进行查询。无论如何,你可以做到这两点。 From the documentation,您可以在PointField上执行many lookups。你可以像这样使用距离滤镜 -

>>> from django.contrib.gis.geos import *
>>> from django.contrib.gis.measure import D # ``D`` is a shortcut for ``Distance``
>>> from geoapp import SouthTexasCity
# Distances will be calculated from this point, which does not have to be projected.
>>> pnt = fromstr('POINT(-96.876369 29.905320)', srid=4326)
# If numeric parameter, units of field (meters in this case) are assumed.
>>> qs = SouthTexasCity.objects.filter(point__distance_lte=(pnt, 7000))
# Find all Cities within 7 km, > 20 miles away, and > 100 chains  away (an obscure unit)
>>> qs = SouthTexasCity.objects.filter(point__distance_lte=(pnt, D(km=7)))
>>> qs = SouthTexasCity.objects.filter(point__distance_gte=(pnt, D(mi=20)))
>>> qs = SouthTexasCity.objects.filter(point__distance_gte=(pnt, D(chain=100)))

同样,您可以执行exact查找。