Car*_*son 1 python gis postgresql postgis shapely
所以我有这样一种情况,我有很多断路的线串,我需要使用 Shapely 的 LineMerge 或 Union OR PostGIS ST_Union 将它们联合在一起。
我现在的想法是使用 Shapely 将Linestrings导入为 Geometry 类型。使用 Shapely 将它们联合或合并,然后导出回数据库中的结果表。
但是,PostGIS 数据库中的几何类型只是一堆乱码。喜欢...
01020000020e61000....
Run Code Online (Sandbox Code Playgroud)
如何使用 Shapely 将其从数据库转换为 Python 几何类型,进行一些操作,然后将其导出回数据库?
目前这是我的代码,它现在只是从数据库中导入 geom 对象字符串并抛出错误,因为它不是 Geometry 类型。
def create_shortest_route_geom(shortest_routes):
conn = connect_to_database()
cur = conn.cursor()
shortest_route_geoms = []
for route in shortest_routes:
source = str(int(route[1]))
target = str(int(route[2]))
query = 'SELECT the_geom FROM public.ways WHERE target_osm = ' + target + ' AND source_osm = ' + source + ' OR target_osm = ' + source + ' AND source_osm = ' + target + ';'
cur.execute(query)
total_geom = cur.fetchone()
for index, node in enumerate(route):
try:
source = str(int(node))
target = str(int(route[index + 1]))
query = 'SELECT the_geom FROM public.ways WHERE target_osm = ' + target + ' AND source_osm = ' + source + ' OR target_osm = ' + source + ' AND source_osm = ' + target + ';'
cur.execute(query)
geom = cur.fetchone()
query = "SELECT ST_Union("+str(geom[0])+","+str(total_geom[0])+")"
cur.execute(query)
total_geom = cur.fetchone()
except IndexError:
print "Last element"
shortest_route_geoms.insert(total_geom)
return shortest_route_geoms
Run Code Online (Sandbox Code Playgroud)
PostGIS 将几何存储为十六进制值。使用 Shapely 的加载函数以 hex=True 的参数加载它。
具体来说...
geom = shapely.wkb.loads(hex_geom[0], hex=True)
Run Code Online (Sandbox Code Playgroud)
不要使用 PostGIS ST_Union,因为您必须一遍又一遍地转储和加载才能使其工作。Shapely 也配置了linemerge
| 归档时间: |
|
| 查看次数: |
3088 次 |
| 最近记录: |