为道路修建十字路口时出错
Error in building crossroads for roads
我试图将原始道路数据分成几段。首先,我创建了 road_segments table.Then 我使用 shapely 将道路 table 的内容转换为一系列相连的路段。
对于每条道路,我首先将道路的名称和几何图形加载到内存中。
到这里的代码工作正常。
我得到了道路的 LineString 几何图形,我想在它接触或穿过另一条道路的每个点处分割。为此,我尝试为这条路建立一个十字路口列表。
crossroads = []
cursor.execute("SELECT ST_AsText(centerline) FROM ROADS" +
"WHERE ST_Touches(roads.centerline, " +
"ST_GeomFromText(%s)) OR ST_Crosses(" +
"roads.centerline, ST_GeomFromText(%s))",
(wkt, wkt))
for row in cursor:
crossroad = shapely.wkt.loads(row[0])
crossroads.append(crossroad)
但是报错如下:
---------------------------------------------------------------------------
ProgrammingError Traceback (most recent call last)
<ipython-input-9-0d71c4db883b> in <module>()
4 "ST_GeomFromText(%s)) OR ST_Crosses(" +
5 "roads.centerline, ST_GeomFromText(%s))",
----> 6 (wkt, wkt))
ProgrammingError: syntax error at or near "."
LINE 1: ...sText(centerline) FROM ROADSWHERE ST_Touches(roads.centerlin...
当我编辑如下代码时:
crossroads = []
cursor.execute("SELECT ST_AsText(centerline) FROM ROADS WHERE ST_Touches(roads.centerline, " +
"ST_GeomFromText(%s)) OR ST_Crosses(" +
"roads.centerline, ST_GeomFromText(%s))",
(wkt, wkt))
for row in cursor:
crossroad = shapely.wkt.loads(row[0])
crossroads.append(crossroad)
以下错误:
---------------------------------------------------------------------------
InternalError Traceback (most recent call last)
<ipython-input-5-b50bc9a8488c> in <module>()
3 "ST_GeomFromText(%s)) OR ST_Crosses(" +
4 "roads.centerline, ST_GeomFromText(%s))",
----> 5 (wkt, wkt))
6 for row in cursor:
7 crossroad = shapely.wkt.loads(row[0])
InternalError: current transaction is aborted, commands ignored until end of transaction block
第一个错误是因为"ROADSWHERE"之间没有space,第二个InternalError是因为事务需要回滚,即conn.rollback()
.
首先,WKB 是一种在 PostGIS 和 Shapely 之间传输几何的更快的无损方式。您可以使用 .wkb_hex
属性 和 PostGIS 从匀称几何图形中获取此信息,而无需执行任何特殊操作。其次,我发现最好使用三引号来编写 SQL 表达式,如下所示:
from shapely import wkb
cursor.execute("""\
SELECT centerline FROM ROADS
WHERE ST_Touches(roads.centerline, %(geom)s)
OR ST_Crosses(roads.centerline, %(geom)s;
""", {'geom': geom.wkb_hex})
for row in cursor:
crossroad = wkb.loads(row[0], hex=True)
crossroads.append(crossroad)
最后,我不确定它是否有帮助,但为了更好地理解 "Touches" 和 "Crosses" 等谓词运算符的工作原理,您可能希望看到 DE-9IM. You can make custom predicates with ST_Relate,而不是调用两个不同的谓词。
我试图将原始道路数据分成几段。首先,我创建了 road_segments table.Then 我使用 shapely 将道路 table 的内容转换为一系列相连的路段。
对于每条道路,我首先将道路的名称和几何图形加载到内存中。
到这里的代码工作正常。
我得到了道路的 LineString 几何图形,我想在它接触或穿过另一条道路的每个点处分割。为此,我尝试为这条路建立一个十字路口列表。
crossroads = []
cursor.execute("SELECT ST_AsText(centerline) FROM ROADS" +
"WHERE ST_Touches(roads.centerline, " +
"ST_GeomFromText(%s)) OR ST_Crosses(" +
"roads.centerline, ST_GeomFromText(%s))",
(wkt, wkt))
for row in cursor:
crossroad = shapely.wkt.loads(row[0])
crossroads.append(crossroad)
但是报错如下:
---------------------------------------------------------------------------
ProgrammingError Traceback (most recent call last)
<ipython-input-9-0d71c4db883b> in <module>()
4 "ST_GeomFromText(%s)) OR ST_Crosses(" +
5 "roads.centerline, ST_GeomFromText(%s))",
----> 6 (wkt, wkt))
ProgrammingError: syntax error at or near "."
LINE 1: ...sText(centerline) FROM ROADSWHERE ST_Touches(roads.centerlin...
当我编辑如下代码时:
crossroads = []
cursor.execute("SELECT ST_AsText(centerline) FROM ROADS WHERE ST_Touches(roads.centerline, " +
"ST_GeomFromText(%s)) OR ST_Crosses(" +
"roads.centerline, ST_GeomFromText(%s))",
(wkt, wkt))
for row in cursor:
crossroad = shapely.wkt.loads(row[0])
crossroads.append(crossroad)
以下错误:
---------------------------------------------------------------------------
InternalError Traceback (most recent call last)
<ipython-input-5-b50bc9a8488c> in <module>()
3 "ST_GeomFromText(%s)) OR ST_Crosses(" +
4 "roads.centerline, ST_GeomFromText(%s))",
----> 5 (wkt, wkt))
6 for row in cursor:
7 crossroad = shapely.wkt.loads(row[0])
InternalError: current transaction is aborted, commands ignored until end of transaction block
第一个错误是因为"ROADSWHERE"之间没有space,第二个InternalError是因为事务需要回滚,即conn.rollback()
.
首先,WKB 是一种在 PostGIS 和 Shapely 之间传输几何的更快的无损方式。您可以使用 .wkb_hex
属性 和 PostGIS 从匀称几何图形中获取此信息,而无需执行任何特殊操作。其次,我发现最好使用三引号来编写 SQL 表达式,如下所示:
from shapely import wkb
cursor.execute("""\
SELECT centerline FROM ROADS
WHERE ST_Touches(roads.centerline, %(geom)s)
OR ST_Crosses(roads.centerline, %(geom)s;
""", {'geom': geom.wkb_hex})
for row in cursor:
crossroad = wkb.loads(row[0], hex=True)
crossroads.append(crossroad)
最后,我不确定它是否有帮助,但为了更好地理解 "Touches" 和 "Crosses" 等谓词运算符的工作原理,您可能希望看到 DE-9IM. You can make custom predicates with ST_Relate,而不是调用两个不同的谓词。