【问题标题】:GeoDjango query PointField by coordsGeoDjango 通过坐标查询 PointField
【发布时间】:2014-03-02 16:30:28
【问题描述】:

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

<---example--->

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

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

【问题讨论】:

    标签: python django geodjango


    【解决方案1】:

    检查与坐标的完全匹配可能不是一个好主意。您应该查询与提供的坐标的距离。无论如何,你可以两者兼得。 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 查找。

    【讨论】:

      猜你喜欢
      • 2011-11-13
      • 2017-01-07
      • 2012-10-01
      • 2011-12-01
      • 2016-04-29
      • 1970-01-01
      • 1970-01-01
      • 2019-06-05
      • 2014-08-16
      相关资源
      最近更新 更多