【问题标题】:Distance Spatial Queries with TastypieTastypie 的距离空间查询
【发布时间】:2014-06-19 04:54:09
【问题描述】:

不确定如何将 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)."}

【问题讨论】:

    标签: tastypie geodjango


    【解决方案1】:

    来自tastepie 源代码:

    # 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 不是理想的解决方案,但它有效。

    【讨论】:

      【解决方案2】:

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

      它不会包含“距离”,因此您会收到错误。

      除了包含之外,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 作为获取参数传递的文档开始变得正确。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-08-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-21
        • 2017-04-17
        相关资源
        最近更新 更多