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
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With