How to get lng lat value from query results of geoalchemy2

落花浮王杯 提交于 2020-08-02 06:11:10

问题


For exammple,

class Lake(Base):
     __tablename__ = 'lake'
     id = Column(Integer, primary_key=True)
     name = Column(String)
     geom = Column(Geometry('POLYGON'))
     point = Column(Geometry('Point'))


lake = Lake(name='Orta', geom='POLYGON((3 0,6 0,6 3,3 3,3 0))', point="POINT(2 9)")
query = session.query(Lake).filter(Lake.geom.ST_Contains('POINT(4 1)'))
for lake in query:
     print lake.point

it returned <WKBElement at 0x2720ed0; '010100000000000000000000400000000000002240'>

I also tried to do lake.point.ST_X() but it didn't give the expected latitude neither

What is the correct way to transform the value from WKBElement to readable and useful format, say (lng, lat)?

Thanks


回答1:


You can parse WKB (well-known binary) points, and even other geometry shapes, using shapely.

from shapely import wkb
for lake in query:
    point = wkb.loads(bytes(lake.point.data))
    print point.x, point.y



回答2:


http://geoalchemy-2.readthedocs.org/en/0.2.4/spatial_functions.html#geoalchemy2.functions.ST_AsText is what you are looking for. This will return 'POINT (lng, lat)'. ST_X ought to work, though, so you may have another issue if it isn't returning the correct value.



来源:https://stackoverflow.com/questions/24092817/how-to-get-lng-lat-value-from-query-results-of-geoalchemy2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!