简体   繁体   English

在 GeoDjango 中将多边形层与多边形相交

[英]Intersecting multipolygon layer with polygon in GeoDjango

I have a Region GeoDjango model (deriving from django.contrib.gis.db.models.Model ), with a geom field, which is a django.contrib.gis.db.models.MultiPolygonField . I have a Region GeoDjango model (deriving from django.contrib.gis.db.models.Model ), with a geom field, which is a django.contrib.gis.db.models.MultiPolygonField .

I'd like to compute the intersection of this model (representing a set of polygonal regions stored in PostGIS) with a GeoJSON polygonal "query":我想计算这个 model(代表存储在 PostGIS 中的一组多边形区域)与 GeoJSON 多边形“查询”的交集:

from django.contrib.gis.geos import GEOSGeometry

query = GEOSGeometry(
    '{"type":"Polygon","coordinates":[[[-80.983786,43.929011],[-80.511513,43.778458],[-80.291852,44.079184],[-80.775108,44.232127],[-80.983786,43.929011]]]}'
)

I tried many things:我尝试了很多事情:

results = Region.objects.all().intersection(query) 

or或者

from django.contrib.gis.db.models.functions import Intersection

results = Intersection(Region.objects.all(), query)

but it seems I don't have the right approach.但似乎我没有正确的方法。

Note that I do not want to simply compute the subset of regions which intersect with the query, but rather their exact intersection (in other words the resulting set of polygons should have the exact same shape as the query).请注意,我不想简单地计算与查询相交的区域子集,而是计算它们的确切交集(换句话说,生成的多边形集应该与查询具有完全相同的形状)。

Essentially we need to annotate the intersection (if any) of each Region 's geom with the given query .本质上,我们需要用给定的query annotate每个Regiongeomintersection (如果有的话)。 Doing so with a DB query look like this:使用 DB 查询执行此操作如下所示:

from django.db.models import F
from django.contrib.gis.db.models.functions import Intersection
from django.contrib.gis.geos import GEOSGeometry

query = GEOSGeometry(
    '{"type":"Polygon","coordinates":[[[-80.983786,43.929011],[-80.511513,43.778458],[-80.291852,44.079184],[-80.775108,44.232127],[-80.983786,43.929011]]]}'
) 

results = Region.objects.filter(geom__intersects=query).annotate(
    intersection_geom=Intersection(F('geom'), query)
)

Query Explanation:查询说明:

  1. Filter the Regions that intersect the query , using the intersects spatial lookup.使用intersects空间查找过滤与query相交Regions
  2. Calculate the result of Intersection between a Region's geom field and the query , using the F() expression to access the geom (an explanation on F() usage can be found in my Q&A example: How to execute arithmetic operations between Model fields in django )使用F()表达式访问geom计算区域的geom字段和queryIntersection的结果(关于F()用法的说明可以在我的问答示例中找到: 如何在 django 中的 Model 字段之间执行算术运算)
  3. annotate the calculated Intersection into each corresponding Region as a field named intersection_geom .将计算出的 Intersection annotate到每个对应的Region中,作为名为intersection_geom的字段。

After the query execution, the results will contain each Region that intersects the query geom, with the intersection_geom field containing the exact geometry of the corresponding intersection.查询执行后, results将包含与query几何相交的每个Regionintersection_geom字段包含相应相交的确切几何。

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

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