2017-10-18 23 views
0

그래서 저는 Shapely 's LineMerge 나 Union 또는 PostGIS ST_Union을 사용하여 함께 묶어야하는 부서진 루트의 선 스트링을 가지고 있습니다.Shapely에서 PostGIS 지오메트리 형식을 파이썬으로 지오메트리 형식으로 가져 오시겠습니까?

제 아이디어는 바로 모양을 사용하여 Linestrings을 기하학 유형으로 가져 오는 것입니다. Shapely를 사용하여 병합하거나 병합 한 다음 데이터베이스의 결과 테이블로 다시 내 보냅니다.

그러나 PostGIS 데이터베이스의 지오메트리 유형은 단지 횡설수설에 지나지 않습니다. 처럼 ...

01020000020e61000.... 

어떻게, 매끈한를 사용하여 파이썬 지오메트리 유형으로 데이터베이스에서이 번역 일부 조작을 수행 한 후 다시 데이터베이스로 내보낼 수 있습니까?

현재이 코드는 데이터베이스에서 해당 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 

EDIT : HEX 값으로 I may have found my answer here, looking more into it and will update my question with an answer if I figure this out.

+0

문자열 대신 SQL 쿼리에 값을 연결하면 자리 표시자를 사용하십시오. 더 읽기 쉬운 코드를 작성하고, 수동으로 "인용 부호를 처리"할 필요가 없으며 일반적으로 주사의 위험을 줄입니다. –

답변

1

Shapely already has libraries for this specific problem.

PostGIS와 저장 형상. Shapely의로드 함수를 사용하여 hex = True의 매개 변수로로드하십시오. 당신이 덤프 이상로드 이상이 위해 일해야 원인을 구체적으로

...

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

은 PostGIS와 ST_Union은 사용하지 마십시오. 모양이 정확하게 구성되어 있습니다 linemerge