考虑以下SQLAalchemy http://www.sqlalchemy.org/ / GeoAlchemy2 http://geoalchemy-2.readthedocs.org/en/0.2.6/index.html具有几何字段的 ORM:
from geoalchemy2 import Geometry, WKTElement
class Item(Base):
__tablename__ = 'item'
id = Column(Integer, primary_key=True)
...
geom = Column(Geometry(geometry_type='POINTZ', srid=4326))
当我更新 PostgreSQL shell 中的项目时:
UPDATE item SET geom = st_geomFromText('POINT(2 3 0)', 4326) WHERE id = 5;
获取字段:
items = session.query(Item).\
filter(Item.id == 3)
for item in items:
print item.geom
Gives:
01e9030000000000000000004000000000000008400000000000000000
这不是一个正确的 WKB - 至少,它不解析身材匀称的loads https://pypi.python.org/pypi/Shapely.
我如何获得lat
/lon
of the geom
field?
正在获取lat
, lon
via ST_X http://postgis.net/docs/ST_X.html and ST_Y http://postgis.org/docs/ST_Y.html可能不是最优雅的方法,但它有效:
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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)