具有Tastypie的距离空间查询

时间:2014-05-02 13:23:36

标签: tastypie geodjango

不确定如何使用带有美味馅饼的distance_lte空间过滤器。我可以使用包含空间过滤器我无法找出distance_lte过滤器的格式。

以下是我的尝试:

http://www.domain.com/myapp/api/v1/location/?format=json&coord__distance_lte={"type": "Point", "coordinates": [153.09537, -27.52618]},D(m=5)

返回{"error": "Invalid resource lookup data provided (mismatched type)."}

2 个答案:

答案 0 :(得分:0)

来自tastypie源代码:

# If we are filtering on a GeometryApiField then we should try
# and convert this to a GEOSGeometry object.  The conversion
# will fail if we don't have value JSON, so in that case we'll
# just return ``value`` as normal.

您的D(m = 3)无效JSON。 这是正在翻译字段的代码:

if isinstance(self.fields[field_name], GeometryApiField):
        try:
            value = GEOSGeometry(unquote(value))
        except ValueError:
            pass
    return value

由于以下代码应在内部工作: Location.objects.filter(location__distance_lte=(fromstr('POINT(153.09537 -27.52618)', srid=4326), D(m=5)))

我可以想象它需要看起来像:

[{"type": "Point", "coordinates": [153.09537, -27.52618]},{"type": "D", "m" : 5}]

我还没有让它运行。希望你有更多的运气!

修改 由于我无法运行,因此我自己使用它实现了它 Django Tastypie Advanced Filtering: How to do complex lookups with Q objects 不是理想的解决方案,但它确实有效。

答案 1 :(得分:0)

这是因为Tastypie使用querysets query.query_terms属性错误地查找有效过滤器

它不会包含'距离'因此您会收到错误。

除了contains之外,TastyPie在这些GIS搜索中几乎不起作用(至少不添加你自己的特殊酱汁。)

例如,您可以通过覆盖build_filters并添加“距离”来实现距离工作。到有效的过滤器集:

def build_filters(self, filters=None):
   '''
   Add in some filters so spatial queries will work.
   '''
   self._meta.queryset.query.query_terms.update(set(['distance','distance_lte']))
   return super(MyResource, self).build_filters(filters)

之后,关于如何将WKT和/或GeoJSON作为get参数传递,文档开始变得正确。