GeoAlchemy2: Get the lat, lon of a point

て烟熏妆下的殇ゞ 提交于 2019-12-06 05:41:14

Fetching the lat, lon via ST_X and ST_Y might not be the most elegant approach, but it works:

from sqlalchemy import func

items = session.query(Item, 
                      func.st_y(Item.geom), 
                      func.st_x(Item.geom)).\
    filter(Item.id == 3)

for item in items:
    print item.geom

Gives:

(<Item 3>, 3.0, 2.0)

geoalchemy2 to_shape function to convert a :class:geoalchemy2.types.SpatialElement to a Shapely geometry.

in Item class :

from geoalchemy2.shape import to_shape

point = to_shape(self.geo)

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