使用django.contrib.gis.measure.D时的GeoDjango dwithin错误 [英] GeoDjango dwithin errors when using django.contrib.gis.measure.D
问题描述
首先:RHEL 6.5上的Python 2.7.6,Django 1.6.5,Postgres 9.3.4,PostGIS 2.1.3,psycopg2 2.5.3
First off: Python 2.7.6, Django 1.6.5, Postgres 9.3.4, PostGIS 2.1.3, psycopg2 2.5.3 on RHEL 6.5
以下是相关模型:
class Location(models.Model):
name = models.CharField(max_length=255)
geometry = models.MultiPolygonField(blank=True, default=None, null=True)
objects = models.GeoManager() # override the default manager with a GeoManager instance
parent = models.ForeignKey('self', blank=True, default=None, null=True)
def __unicode__(self):
return self.name
此查询应根据文档:
touching_locations = Location.objects.filter(geometry__dwithin=(location.geometry, D(km=5)))
logging.debug(type(touching_locations))
logging.debug(len(touching_locations))
但事实并非如此.第一个调试调用有效,但是第二个调试调用抛出ValueError
:
But it doesn't. The first debug call works, but the second throws a ValueError
:
<class 'django.contrib.gis.db.models.query.GeoQuerySet'>
ValueError: Only numeric values of degree units are allowed on geographic DWithin queries.
如果我通过将D(km=5)
更改为5
进行了小的更改:
If I make a small change by changing D(km=5)
to 5
:
touching_locations = Location.objects.filter(geometry__dwithin=(location.geometry, 5))
logging.debug(type(touching_locations))
logging.debug(len(touching_locations))
突然间它起作用了.我得到的输出是这样的:
All of a sudden it works. The output I get is this:
<class 'django.contrib.gis.db.models.query.GeoQuerySet'>
54
有人知道为什么这没有按预期进行吗?这可能是一个错误,还是我犯了一个我看不见的错误?
Does anyone know why this isn't working as expected? Is this perhaps a bug, or am I making a mistake I just don't see?
我认为这可能是Django错误.我继续在此处打开了一张票.确定正确的解决方法后,我将在此处添加答案.
[edit]
I think this may be a Django bug. I went ahead and opened a ticket here. Once I figure out what the proper fix is, I'll add the answer here.
推荐答案
I got a response to the ticket I submitted (https://code.djangoproject.com/ticket/22830). Apparently, I found a seemingly undocumented (or at least not clearly documented) problem that dwithin
queries have with Distance
objects. A dev says this:
由于您的对象位于地理坐标中(默认为几何字段 到WGS84),则必须以度单位提供距离.这是 例如匹配PostGIS定义:
As your objects are in geographic coordinates (geometry fields default to WGS84), you have to provide the distance as degree units. This is for example matching the PostGIS definition:
布尔值ST_DWithin(几何g1,几何g2,双精度 distance_of_srid);
boolean ST_DWithin(geometry g1, geometry g2, double precision distance_of_srid);
distance_of_srid是WGS84的度数.所以这5个适合您的 示例表示5度,而不是5公里!
distance_of_srid being degrees for WGS84. So the 5 that works in your example means 5 degrees, not 5 km!
似乎他们将澄清文档以使其更清楚(非常好!).
It looks like they're going to clarify the documentation to make this clearer (great!).
因为我想要的是5km,所以我需要将5km转换为度. 1度是大约111.325km.因此,1km = 1/111.325度.因此5km约为0.0449或约0.05度.因此,我只需要将此呼叫更改为:
Since what I want is 5km, I need to convert 5km to degrees. 1 degree is approximately 111.325km. Therefore, 1km = 1/111.325 degrees. 5km is therefore approximately 0.0449 or about 0.05 degrees. So I just need to change my call to this:
touching_locations = Location.objects.filter(geometry__dwithin=(location.geometry, 0.05))
这篇关于使用django.contrib.gis.measure.D时的GeoDjango dwithin错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!