Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get lng lat value from query results of geoalchemy2

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

like image 798
Chung Avatar asked Jun 07 '14 01:06

Chung


2 Answers

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
like image 75
Enrico Tiongson Avatar answered Nov 04 '22 06:11

Enrico Tiongson


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.

like image 44
John Powell Avatar answered Nov 04 '22 07:11

John Powell