如何在 WHERE 子句中使用 ST_Intersects

How to use ST_Intersects in WHERE clause

我想知道如何进行下面的操作

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

fiddle:

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}');