本文介绍了GeoAlchemy2:获取点的纬度和经度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下 SQLAalchemy / GeoAlchemy2 具有几何字段的ORM:

Consider the following SQLAalchemy / GeoAlchemy2 ORM with a geometry field:

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中的项目时:

When I update an item in the 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

赠予:

01e9030000000000000000004000000000000008400000000000000000

这不是适当的WKB-至少,它不使用 Shapely的loads .

This isn't a proper WKB - at least, it does not parse with Shapely's loads.

如何获取geom字段的lat/lon?

How do I get the lat/lon of the geom field?

推荐答案

通过 ST_X ST_Y 可能不是最优雅的方法,但它可以:

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

赠予:

(<Item 3>, 3.0, 2.0)

这篇关于GeoAlchemy2:获取点的纬度和经度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-12 20:54