Flask将shapefile上传到postgis中

时间:2018-05-09 17:18:12

标签: flask postgis geoalchemy2 pyshp

我正在尝试将shapefile上传到PostGIS。我正在使用Flask并从用户(.shp,.shx,.dbf和.prj)获取文件,并在使用PYSHP python库访问形状集合之前将它们保存到临时文件中。

r = shapefile.Reader(tempfile_name + '.shp')
shapes = r.shapes()

我想要做的是将形状集合中的多边形组合在一起,并将其作为一条记录添加到数据库中。基本上将多边形合并为多​​边形。 PostGIS表的相关列定义为:

footprint = db.Column(Geometry(geometry_type='MULTIPOLYGON', srid=27700))

我不知道如何继续合并多边形并将它们转换为可以使用SQLAlchemy和GeoAlchemy2写入PostGIS数据库的格式。将非常感激地收到关于如何进行的建议。

我已经看过地形和身材匀称,但似乎可以找到相关的信息

我正在使用

  • Flask 1.0.2
  • Postgresql 9.6
  • SQLAlchmey 1.2.7
  • Geoalchemy2 0.4.2

更新1。

找到一个似乎有效的解决方案,但它看起来相当混乱。

我使用shapefile库的cascaded_union循环遍历shapefile.Reader的shapes()并将每个多边形保存到数组中。然后使用pygeoif库转换要保存在postgis数据库中的信息。

poly = []
for shp in r.shapes():
  if shp.shapeType == 5:
    poly.append(shape(shp.__geo_interface__))
footprint_poly = cascaded_union(poly)

gshape = pygeoif.MultiPolygon(pygeoif.geometry.as_shape(footprint_poly))
project.footprint = 'SRID=27700;{0}'.format(gshape.wkt)

0 个答案:

没有答案
相关问题