我的问题是这样的。我正在为一些数据创建一个模型。
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'对象不可调用
(涉及 IT 技术)
return str(cable.route)
返回 0102000020e610000002000000b34fd4d9bca351c032e14d5134c240c0d24f8055e0a351c0dedea9f4dcbf40c0
,这意味着我有一个几何对象。
return cable.route
返回 TypeError: 'WKBElement' 对象不可调用
如果我打印路由类型,
print(type(cable.route))
返回结果
<class 'geoalchemy2.elements.WKBElement'>
我认为它应该返回这个类的一个对象,而不是类本身。我现在感到困惑,不知道该怎么办了。你有什么建议吗?