2017-01-16 9 views
1

제 문제는 이것입니다. 일부 데이터에 대한 모델을 만들고 있습니다.geometry를 geojson으로 변환 할 수 없습니다.

class Cables(Base): 
    __tablename__ = 'cables' 

    id = Column(Integer, nullable=False) 
    route = Column(Geometry(geometry_type='LINESTRING', srid=4326), nullable=False) 

이제 이러한 경로를 GeoJSON으로 변환하고 싶습니다. 내가 반환 변경하는 경우

것들 내가

@app.route("/api/cable/<int:id>", methods=['GET']) 
def get_cable(id): 
    cable = session.query(Cables).filter(Cables.id == id).first() 
    return str(geoalchemy2.functions.ST_AsGeoJSON(cable.route)) 

반환 ST_AsGeoJSON(ST_GeomFromEWKB(:ST_GeomFromEWKB_1))

을 시도했다 :

return geoalchemy2.functions.ST_AsGeoJSON(cable.route) 

수익을 TypeError: 'ST_AsGeoJSON' object is not callable

return str(cable.route) 

반환 0102000020e610000002000000b34fd4d9bca351c032e14d5134c240c0d24f8055e0a351c0dedea9f4dcbf40c0 이것은 내가 기하학 객체를 가지고 있다는 신호 것입니다.

return cable.route 

반환 TypeError: 'WKBElement' object is not callable 내가 경로 유형을 인쇄 할 경우

,

print(type(cable.route)) 

반환

<class 'geoalchemy2.elements.WKBElement'> 

나는이 같은 클래스의 객체를 반환하고,하지 않은한다고 생각 클래스 그 자체. 나는이 시점에서 당혹스럽고 지금 무엇을해야하는지 모른다.

제안 사항?

답변

1

ST_AsGeoJSON을 호출하는 올바른 방법은 쿼리 내부에있는 것으로 보입니다. 예를 들어 , 나는 육각 bytestring을 읽은 다음 사전을 JSON으로 변환 할 수있는 쉬운 방법으로, 사전에 변환하기 위해 (매끈한) 새로운 라이브러리를 설치 한 일을 결국 무엇

ruta = session.query(Cables.route.ST_AsGeoJSON()).filter(Cables.id == id).first() 

. 내 문자열 방법 다음

def ewkb_route_to_dict(self): 
    """ 
    returns the cable's route as a dictionary. 
    It uses shapely.wkb.loads. The first argument is the route as a bytestring, 
    and the second one (True) is to tell loads that 
    the input wkb will be represented as a hex string 
    """ 
    return mapping(shapely.wkb.loads(str(self.route), True)) 

: 제대로 GeoJSON에 형상을 변화시킬

def __str__(self): 
    d = dict() 
    ... 
    d["route"] = self.ewkb_route_to_dict() 
    ... 
    return dumps(d) 

.