微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Django / GeoDjango / PostGis - 过滤 FeatureCollection

如何解决Django / GeoDjango / PostGis - 过滤 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","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('\001\007\000\000 \346 \020\000\000\002\000\000\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 )

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。