to_shape() 失败并显示 ParseException



我一直在尝试GeoAlchemy2,我在解析其geom字段时遇到了一些麻烦。

我尝试在 WKB 元素上使用内置to_shape函数:

示例如下:

lake = Session.query(Lake).get(1)
polygon = to_shape(lake.geom)

我使用过:

house = config.database.db_session.query(House)
        .filter_by(house_id=1).first()
print "geom:", house.geom
01e90300009aea561e53634140ffb86b0da20a40400000000000000000
from geoalchemy2 import shape
print "to_shape:", shape.to_shape(house.geom)
to_shape:
Traceback (most recent call last):
  File "ranker_tester.py", line 40, in <module>
    print "to_shape:", shape.to_shape(house.geom)
  File ".../lib/python2.7/site-packages/GeoAlchemy2-0.2.4-py2.7.egg/geoalchemy2/shape.py", line 24, in to_shape
    return shapely.wkb.loads(bytes(element.data))
  File ".../lib/python2.7/site-packages/shapely/wkb.py", line 16, in loads
    return reader.read(data)
  File ".../lib/python2.7/site-packages/shapely/geos.py", line 361, in read
    raise ReadingError("Could not create geometry because of errors "
shapely.geos.ReadingError: Could not create geometry because of errors while reading input.

知道我如何解析这个GeoAlchemy2几何字段吗?数据库值有效。

我不

使用GeoAlchemy2,所以我不能评论它是如何生成WKB的,只能说它是一种与Shapely/GEOS使用的方言不同的方言。

您提供的WKB是OGC/ISO(PostGIS使用encode(ST_AsBinary(geom), 'hex')):

01e90300009aea561e53634140ffb86b0da20a40400000000000000000

这是 WKT 等效物(即 ST_AsText(geom) ):

POINT Z (34.7759740757367 32.0830704475393 0)

但是,Shapely 和 GEOS 不支持更高维度几何形状的 OGC/ISO WKB。它只支持EWKB,它是为PostGIS指定的(并且早于OGC/ISO规范),看起来像这样(即 encode(ST_AsEWKB(geom), 'hex') ):

01010000809aea561e53634140ffb86b0da20a40400000000000000000

所以这适用于 Shapely,

from shapely.wkb import loads
pt = loads('01010000809aea561e53634140ffb86b0da20a40400000000000000000', hex=True)
pt.wkt  # 'POINT Z (34.77597407573667 32.08307044753928 0)'
pt.wkb_hex  # '01010000809AEA561E53634140FFB86B0DA20A40400000000000000000'

我不确定这对您的问题有何帮助,但可能会对正在发生的事情表现出一些煽动。如果您不使用Z尺寸,则可以坚持使用2D几何图形,其中WKB与OGC/ISO WKB和EWKB规格相同。

最新更新