我如何用Tastype和geoDjango测量距离?

2024-03-29 09:16:01 发布

您现在位置:Python中文网/ 问答频道 /正文

使用tastype和GeoDjango,我试图返回位于一个点1英里范围内的建筑物的结果。在

TastyPie documentation声明目前还不支持距离查找,但是我发现了一些人们可以使用它的例子,比如StackOverflow上的this discussion和{a3},但是没有可以应用的工作代码示例。在

我尝试使用的想法是,如果我在URL的末尾附加一个GET命令,则会返回附近的位置,例如:

http://website.com/api/?format=json&building_point__distance_lte=[{"type": "Point", "coordinates": [153.09537, -27.52618]},{"type": "D", "m" : 1}]

但当我尝试的时候,我得到的只是:

^{pr2}$

我已经在Tastype文档上翻了好几天了,只是不知道如何实现这一点。在

我会提供更多的例子,但我知道它们都很糟糕。感谢所有的建议,谢谢!在


Tags: 代码声明距离示例documentationtypethistastype
1条回答
网友
1楼 · 发布于 2024-03-29 09:16:01

成功了,给后人举个例子:

在api.py文件,创建如下所示的资源:

from django.contrib.gis.geos import *

class LocationResource(ModelResource):
    class Meta:
        queryset = Building.objects.all()
        resource_name = 'location'

    def apply_sorting(self, objects, options=None):
        if options and "longitude" in options and "latitude" in options:
            pnt = fromstr("POINT(" + options['latitude'] + " " + options['longitude'] + ")", srid=4326)
            return objects.filter(building_point__distance_lte=(pnt, 500))

        return super(LocationResource, self).apply_sorting(objects, options)

“building”字段定义为模型.py. 在

然后在资源的URL中,附加以下内容,例如:

^{pr2}$

这将返回500米范围内的所有对象。在

相关问题 更多 >