如何在WHERE子句中使用ST_Intersects
问题描述
我想知道如何执行以下操作
WHERE NOT ST_Intersects(dataSource.LIDARDataAsSinglePolygon,geom)
查询:
def exec(self,geometryAsGeoJSONObjectAsString,geom):
query="""
WITH dataSource As(
SELECT DISTINCT
ST_AsGeoJSON(ST_Union(ST_GeomFromGeoJSON(feature->>'geometry'))) As LIDARDataAsSinglePolygonUsingST_UnionAsGeoJSONInEPSG4326,
ST_AsText(ST_Union(ST_GeomFromGeoJSON(feature->>'geometry'))) As LIDARDataAsSinglePolygon
)
FROM
(SELECT json_array_elements('{geometryAsGeoJSONObjectAsString}'::json->'features') AS feature) a WHERE NOT ST_Intersects(dataSource.LIDARDataAsSinglePolygon,geom);
""".format(geometryAsGeoJSONObjectAsString=geometryAsGeoJSONObjectAsString)
print(query)
data = self.connection.query(query,[])
# print(data)
return data
小提琴:
https://dbfiddle.uk/?rdbms=postgres_12&fiddle=8dc0ccc1017a220098022131e8d41063
解决方案
您最有可能尝试将ST_Intersects
与表达式的别名一起使用,而不是与列或几何图形一起使用。您的逻辑是颠倒的:您需要首先解析和转储要素集合中的几何,然后应用过滤器,而不是反过来:
WITH datasource (feature) AS (
SELECT json_array_elements('{json_string}'::json->'features')
)
SELECT
ST_AsGeoJSON(ST_Union(ST_GeomFromGeoJSON(feature->>'geometry'))),
ST_AsText(ST_Union(ST_GeomFromGeoJSON(feature->>'geometry')))
FROM datasource
WHERE NOT ST_Intersects(ST_GeomFromGeoJSON(feature->>'geometry'),'{a geometry}');
相关文章