Django / GeoDjango / PostGis - 过滤特征集合

Django / GeoDjango / PostGis - Filter FeatureCollection

目前我有一个包含 PointField 的非常基本的 django 模型:

from django.contrib.gis.db import models
from django.utils.translation import gettext_lazy as _

from api.models import AbstractModel

class SampleEntity(AbstractModel):
    point = models.PointField(_('point'))

我的目标是过滤所有行并检查该点是否在某个多边形中...

为了创建一个多边形,我收到了以下负载 (FeatureCollection):

{
    "type": "FeatureCollection",
    "crs": {
        "type": "name",
        "properties": {
            "name": "urn:ogc:def:crs:OGC:1.3:CRS84"
        }
    },
    "source": "© GeoBasis-DE / BKG 2013",
    "features": [{
        "type": "Feature",
        "properties": {},
        "geometry": {
            "type": "Polygon",
            "coordinates": [
                [
                    [10.453980128926366, 47.55557895879648],
                    [10.438876535127962, 47.52349211089603],
                    [10.44055084477551, 47.5135694350795],
                    [10.431323512106337, 47.5036776164676],
                    [10.442767953986108, 47.4846168990274],
                    [10.45131095387671, 47.485685093946636],
                    [10.463220692620368, 47.48277492286606],
                    [10.468022327337152, 47.47691846929882],
                    etc..
                ]
            ]
        }
    }, {
        "type": "Feature",
        "properties": {},
        "geometry": {
            "type": "Polygon",
            "coordinates": [
                [
                    [9.617342617593987, 47.568803310179476],
                    [9.628007474077956, 47.570911279233016],
                    [9.628870213746309, 47.56638028461108],
                    [9.638605938479984, 47.56693417152559],
                    [9.653552559008789, 47.55698746615904],
                    [9.679038885003902, 47.55702134414172],
                    [9.682804387548277, 47.55244003193477],
                    [9.675623645853232, 47.54522412435771],
                    etc..
                ]
            ]
        }
    }]
}

接下来我需要以某种方式将我的 FeatureCollection 转换为有效的“GeoDjango”多边形,以便在数据库中进行过滤:

geoms = []
for feature in payload["features"]:
    geoms.append(GEOSGeometry(str(feature["geometry"])))
geometry = GeometryCollection(geoms)

rows = SampleEntity.objects.filter(point__within=geometry)
print(rows) # <- Error

加薪:

django.db.utils.InternalError: Relate Operation called with a LWGEOMCOLLECTION type.  This is unsupported.
HINT:  Change argument 1: 'GEOMETRYCOLLECTION(MULTIPOLYGON(((13.8005705219365 53.5582546199631,13.798463...'

可能是一些内部错误...

谁能帮我解决这个问题? 我真的不知道在这里做什么..

谢谢和问候!

编辑 1:

查询:

SELECT "api_table"."id", "api_event"."point"::bytea FROM "api_table" WHERE ST_Within ("api_event"."point", ST_GeomFromEWKB('[=38=]1[=38=]7[=38=]0[=38=]0 60[=39=]0[=39=]0[=39=]2[=39=]0[=39=]0\etc

为了过滤有效 geodjango 几何中的点,您必须将 geojson 转换为适当的几何类型。在您的情况下,geojson 多边形将被组合以创建多多边形对象,而不是几何集合。

from django.contrib.gis.geos import  MultiPolygon, GEOSGeometry
import json
polygonlist = [GEOSGeometry(json.dumps(feature["geometry"])) \
               for feature in  payload["features"]]

mpoly = MultiPolygon(*polygonlist)
rows = SampleEntity.objects.filter(point__within=mpoly )