2

我正在尝试将查询转换为 sqlalchemy 和 geoalchemy2 中的相等 orm 语句。我正在努力如何将 p1.geom 的引用获取到 not exists 子句中的子查询中。

SELECT p1.geom FROM polygons as p1 
WHERE p1.column1 = 'something' AND NOT EXISTS(
   SELECT 1 FROM polygons  AS p2
   WHERE p2.column2 = 'something else' AND ST_Intersects(p1.geom, p2.geom)
)

我已经尝试了一些,但我得到的最接近的是这样的:

p1 = self._session.query(polygons.geom).filter_by(column1='something').subquery()
p2 = ~self._session.query(polygons.geom).filter(polygons.column2 == 'something else')\
.filter(polygons.geom.intersects(p1.c.geom)).exists()
result = self._session.query(polygons.geom)\
        .filter_by(column1='something')\
        .filter(p2)

我从中得到了什么:

SELECT polygons.geom FROM polygons 
WHERE polygons.column1 = 'something' AND NOT (EXISTS (
   SELECT 1 FROM (
      SELECT polygons.geom AS geom FROM polygons 
      WHERE polygons.column1 = 'something') AS anon_1 
   WHERE polygons.column2 = 'something else' AND (polygons.geom && anon_1.geom)))

有什么可以帮助的想法吗?

4

0 回答 0