简体   繁体   English

如何根据位置坐标过滤Django查询集?

[英]How to filter a django queryset based on Location coordinates?

Let's say that I have a photo model. 假设我有一个照片模型。 In the photo model, I have longitude and latitude fields in my photo model. 在照片模型中,我的照片模型中有经度和纬度字段。

 class Photo(models.Model):
      photographer = models.ForeignKey(Photographer, related_name = 'shot_owner')
      title = models.CharField(max_length=140, blank=True)
      description = models.CharField(max_length, blank=True)
      longitude = models.DecimalField(max_digits=16, decimal_places = 14, null=True, blank=True)
      latitude = models.DecimalField(max_digits=16, decimal_places = 14, null=True, blank=True)

I use Django Tastypie as my rest framework. 我使用Django Tastypie作为我的其余框架。 Let's say a user decides they'd like to see all the photos within a 10 km radius. 假设某个用户决定要查看10公里半径内的所有照片。 How can one achieve this? 一个人怎么能做到这一点? Here is my resource below: 这是我的以下资源:

class PhotosNearMe(ModelResource):
photographer = fields.ForeignKey(PhotographerResource, 'photographer', full=True)
class Meta:
    queryset = Photo.objects.all()
    resource_name = 'photos-near-me'
    fields = ['id', 'title', 'description', 'latitude','longitude','photographer']
    authentication = BasicAuthentication()
    authorization = DjangoAuthorization()
    serializer = Serializer(formats=['json'])
    include_resource_uri = False
    filtering = {
            'photographer' : ALL_WITH_RELATIONS,

}

def get_object_list(self, request):
        return super(PhotosNearMe, self).get_object_list(request).filter(....)

This is where I am having trouble. 这是我遇到麻烦的地方。 As I mentioned before, the user will be able to send me their coordinates and I can save them. 如前所述,用户可以向我发送他们的坐标,然后我可以保存它们。 Something like: 就像是:

 lati = bundle.obj.latitude
 longi = bundle.obj.longitude

I can later use lat and long to filter through all the images in the database that are within a 10 km radius. 稍后,我可以使用lat和long来过滤数据库中半径10 km以内的所有图像。 The question is, how? 问题是,如何? am I suppose to filter by some sort of range? 我想按某种范围进行过滤吗?

EDIT** 编辑**

I have found something that I could perhaps use, Find items within a certain range of given coordinate 我找到了可能可以使用的东西, 在给定坐标的一定范围内查找项目

Is there anyway I can implement this? 无论如何,我可以实现吗?

If you are going to be handling a lot of data that is geographic in nature, you may consider GeoDjango, which has support for spatial lookups . 如果您要处理大量本质上是地理的数据,则可以考虑使用GeoDjango,它支持空间查找 However, it only works with certain backends, so it may require a lot more to get set up if your stack doesn't meet the requirements. 但是,它仅适用于某些后端,因此如果您的堆栈不符合要求,则可能需要进行大量设置。

Otherwise, the other option would be to do a little geometry and calculate the bounding circle around your point, and filter on that. 否则,另一个选择是做一些几何图形,然后计算出您的点周围的边界圆,然后对其进行过滤。 Here's an example , and it looks like there are plenty of other writeups on how to do this. 这是一个示例 ,看来还有很多其他有关如何执行此操作的文章。

EDIT: in response to your question on how to do this, I am assuming you mean the second part. 编辑:针对您关于如何执行此操作的问题,我假设您是指第二部分。 I am not an expert in TastyPie, but it looks like you will have to this in your view: 我不是TastyPie的专家,但是看来您将不得不这样做:

  1. Get the user's lat an longitude coordinates 获取用户的经度坐标
  2. Calculate a distance -- it looks like you can do this natively in SQL ( or here ), but I think this is more complicated than it initially appears. 计算距离-看起来您可以在SQL中或在此处本地完成此操作,但是我认为这比最初看起来要复杂得多。 It may be easier to do a square, since it's easy to calculate min and maxs here 进行平方可能会更容易,因为在这里计算最小值和最大值很容易
  3. Apply a filter to get_object_list based on the min and max coordinates. 根据最小和最大坐标将过滤器应用于get_object_list。

It seems like all of these would belong in ModelResource.build_filters . 似乎所有这些都属于ModelResource.build_filters

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM