Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Shapely将PostGIS几何体类型作为几何体类型导入Python?_Python_Postgresql_Gis_Postgis_Shapely - Fatal编程技术网

从Shapely将PostGIS几何体类型作为几何体类型导入Python?

从Shapely将PostGIS几何体类型作为几何体类型导入Python?,python,postgresql,gis,postgis,shapely,Python,Postgresql,Gis,Postgis,Shapely,所以我有一种情况,我有一条断开的路线的大量线串,我需要使用Shapely的LineMerge或Union或PostGIS ST_Union将它们合并在一起 我现在的想法是使用Shapely导入as几何体类型。使用Shapely合并或合并它们,然后导出回数据库中的结果表 然而,PostGIS数据库中的几何体类型只是一堆胡言乱语。像 01020000020e61000.... 如何使用Shapely将其从数据库转换为Python几何体类型,进行一些操作,然后将其导出回数据库 目前这是我的代码,它只

所以我有一种情况,我有一条断开的路线的大量线串,我需要使用Shapely的LineMerge或Union或PostGIS ST_Union将它们合并在一起

我现在的想法是使用Shapely导入as几何体类型。使用Shapely合并或合并它们,然后导出回数据库中的结果表

然而,PostGIS数据库中的几何体类型只是一堆胡言乱语。像

01020000020e61000....
如何使用Shapely将其从数据库转换为Python几何体类型,进行一些操作,然后将其导出回数据库

目前这是我的代码,它只是从数据库导入geom对象字符串,并抛出错误,因为它不是几何体类型

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
编辑:

PostGIS将几何图形存储为十六进制值。使用Shapely的loads函数,使用hex=True参数加载该函数

具体地说

geom = shapely.wkb.loads(hex_geom[0], hex=True)
不要使用PostGIS ST_Union,因为您必须一次又一次地卸载和加载才能使其正常工作。Shapely也配置了这一功能

PostGIS将几何图形存储为十六进制值。使用Shapely的loads函数,使用hex=True参数加载该函数

具体地说

geom = shapely.wkb.loads(hex_geom[0], hex=True)

不要使用PostGIS ST_Union,因为您必须一次又一次地卸载和加载才能使其正常工作。Shapely还配置了

,而不是字符串连接值到SQL查询,请使用占位符。使代码更具可读性,无需手动“处理”引用,并且总体上降低了注入风险。请使用占位符,而不是字符串连接值到SQL查询。使代码更具可读性,消除了手动“处理”引用的需要,并且总体上降低了注入的风险。