2014-12-30 61 views
0

我一直在尝试GeoAlchemy2和I have some trouble with parsing its geom fieldto_shape()失败并出现ParseException

我试图use the built-in to_shape function on a WKB element

的例子如下:

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 GEOM场?数据库值是有效的。

回答

1

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

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

01e90300009aea561e53634140ffb86b0da20a40400000000000000000 

这里是WKT当量(即ST_AsText(geom)):

POINT Z (34.7759740757367 32.0830704475393 0) 

然而,身材匀称和GEOS做不支持更高维度的OGC/ISO WKB。它只支持EWKB,这对PostGIS的(和预日期OGC/ISO规范)规定,它看起来像这样(即encode(ST_AsEWKB(geom), 'hex')):

01010000809aea561e53634140ffb86b0da20a40400000000000000000 

所以这一点也适用身材匀称,

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尺寸相同。

相关问题