Amrmsmb
Amrmsmb

Reputation: 1

How to use ST_Intersects in WHERE clause

i would like to know how to perform the following operation

WHERE NOT ST_Intersects(dataSource.LIDARDataAsSinglePolygon,geom)

query:

  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

Upvotes: 0

Views: 650

Answers (1)

Jim Jones
Jim Jones

Reputation: 19613

You're most likely trying to use ST_Intersects with an alias of an expression, not a column or a geometry. And you're logic is inverted: you need to first parse and dump the geometries from your feature collection and then apply the filter, not the other way around:

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

Upvotes: 0

Related Questions