匀称地阅读Postgis几何


11

我正在ipython Notebook中使用geopandas和shape进行小型工作流,​​有时从shapefile,有时从postgis(执行更昂贵的处理)中提取一堆地理空间数据。

现在,我使用将该postgis表拖入python中sqlalchemy,并一路将几何体转换为WKT,得到如下结果:

sql = """
SELECT ST_AsText(ST_Transform(the_geom,4326)) as newgeom,* 
  FROM public.parcels2010_small limit 5;
 """
parcels = pd.read_sql(sql, engine)
parcels

+----+---------------------------------------------------+---------------------------------------------------+-----------+-------------+
| id | newgeom                                           | the_geom                                          | parcel_id | osm_node_id |
+----+---------------------------------------------------+---------------------------------------------------+-----------+-------------+
| 0  | MULTIPOLYGON(((-122.991093691444 38.4878691106... | 01060000209C0E00000100000001030000000100000097... | 1805792   | 66237       |
+----+---------------------------------------------------+---------------------------------------------------+-----------+-------------+
| 1  | MULTIPOLYGON(((-122.444576448624 37.7346386006... | 01060000209C0E0000010000000103000000010000008A... | 10435     | 123826      |
+----+---------------------------------------------------+---------------------------------------------------+-----------+-------------+
| 2  | MULTIPOLYGON(((-122.796785208193 38.5427593334... | 01060000209C0E0000010000000103000000010000007D... | 1817842   | 313047      |
+----+---------------------------------------------------+---------------------------------------------------+-----------+-------------+
| 3  | MULTIPOLYGON(((-122.695538506163 38.3618570798... | 01060000209C0E0000010000000103000000010000009B... | 1934612   | 63776       |
+----+---------------------------------------------------+---------------------------------------------------+-----------+-------------+
| 4  | MULTIPOLYGON(((-122.223424422869 37.8416019090... | 01060000209C0E00000100000001030000000100000072... | 861785    | 26369       |
+----+---------------------------------------------------+---------------------------------------------------+-----------+-------------+

从shapefile加载时,它看起来很像几何图形,但是它不会转换为形状良好的几何图形。我无法找到规范的方式来执行此操作,要么单独使用匀称,要么使用Descartes

Answers:



16

PostGIS几何的默认格式是十六进制编码的WKB(熟知二进制)。Shapely可以shapely使用其wkb模块将该格式转换为几何对象:

from shapely import wkb

# ....

sql = """SELECT * FROM public.parcels2010_small LIMIT 5;"""
parcels = pd.read_sql(sql, engine)

for parcel in parcels:
    parcel.the_geom = wkb.loads(parcel.the_geom, hex=True)

如果要打印几何图形,则应如下所示:

print parcels[0].the_geom

<shapely.geometry.multipolygon.MultiPolygon object at ...>

此处查看shapely.wkb模块上的文档。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.