GeoDjango查询:包含在多面体中的所有点 [英] GeoDjango query: all point that are contained into a multi polygon

查看:90
本文介绍了GeoDjango查询:包含在多面体中的所有点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型:

Model_A that contains a GeoDjango Point;

Model_B that contains a GeoDjnago MultiPololygon;

对于Model_A中的每个元素,我都必须检查该点是否包含在Model_B元素的某些m_polygon中;

For every element in Model_A I have to check if the point is contained into some m_polygon of Model_B element;

我能够进行这个简单的查询。

I'm able to make this simple query.

但我还认为:
我在Model_A中有很多元素,在Model_B中有几个元素。
因此,迭代访问Model_B中的所有元素并检查是否包含在当前Model_B元素中的Model_A中是否存在某个元素可能更有效。

But I also thought: I have a lot of elements in Model_A and few elements in Model_B. So, probably is more efficient to iterate all elements in Model_B and check if exist some element in Model_A that it is contained into the current Model_B element.

那么,有什么方法可以进行此GeoDjango查询吗?

So, is there any way to make this GeoDjango query?

类似这样的东西:

Model_A.objects.filter(*point_is_contained_into*=a_model_b_mpolygon);

------------------编辑-----------------

我尝试使用此功能:

result = Model_A.objects.filter(position__intersects=a_model_b_mpolygon)

这对我有用。
在我的情况下是否有禁忌使用这种类型的查询?

And this works for me. Are there contraindications to use this type of query in my case?

推荐答案

从Django 1.11版开始,您已经优化了解决该查询的选项。

From Django version 1.11 you have an optimized option to solve this query.

假设:


  1. Model_A 的几何字段为: model_a_point

  2. Model_B 有一个几何字段,称为: model_b_poly

  1. Model_A has a geometry field called: model_a_point.
  2. Model_B has a geometry field called: model_b_poly.

使用的方法:


  1. Subquery() ,新Django 1.11中的方法,该方法允许使用子查询部分来定义查询。

  1. Subquery(), new method in Django 1.11 which allows the definition of queries with a subquery part.

OuterRef() ,Django 1.11中的新方法

OuterRef(), new method in Django 1.11 which is used:

当子查询中的查询集需要引用外部
查询中的字段时。

when a queryset in a Subquery needs to refer to a field from the outer query.


  • within () ,其中:


    测试几何字段是否在查找几何空间内。

    Tests if the geometry field is spatially within the lookup geometry.


  • annotate() ,它将为查询集中的每个项目,一个新字段(在我们的示例中,它将包含多边形所包含的点。)

  • annotate(), which will generate for each item in a queryset, a new field (in our case it will contain the points contained by a polygon.)

    查询:

    Model_B.objects.annotate(
        contained_points=Subquery(
            Model_A.objects.filter(
                model_a_point__within=OuterRef('model_b_poly')
            )  # Ref: 1, referenced below, keep reading
        )
    )
    

    结果及更多:

    上面的查询将为每个字段包含一个 contained_points 字段 Model_B 中的多边形,包含该多边形包含的 Model_A 中的每个点。

    The query above will have a field contained_points for every polygon in Model_B which contains every point from Model_A contained by this polygon.

    如果只想保留这些点的几何字段( lon,lan ),请在子查询调用(参考号:1),使用 values() 方法。

    If you only want to keep geometry field of those points (lon, lan), at the end of the Subquery call (Ref: 1), use the values() method.

    这篇关于GeoDjango查询:包含在多面体中的所有点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆