django - 在 GeoDjango 上剥离 Z 维度(强制 2D 几何)

标签 django gis postgis geodjango geos

在我的项目中,我需要从 导入一些几何图形。形状文件 .

其中一些是 MULTIPOLYGON Z类型,但所有 Z 坐标都是 0 值。

当我尝试保存几何图形时,出现错误:

"Geometry has Z dimension but column does not"



剥离 Z 维度的最佳方法是什么?

我的代码:
ds = DataSource(file_path, encoding='ISO-8859-1')
layers = ds[0]

#need something HERE to coerce geometry to 2D

obj=MyModel(geometry=GEOSGeometry(layers[0].geom.hex))
obj.save()

最佳答案

感谢您的回复,迈克 T。

问题是我需要使用 GeoDjango framework 来实现它,无需直接访问 Postgis 数据库 .

其实经过努力,我找到了解决办法。我需要使用 .clone()方法来自 OGRGeometry 目的。现在,我可以改变 coord_dim属性(property)。如果我改变 coord_dim在原始对象中,什么也没有发生。

这是我的代码:

ds = DataSource(file_path, encoding='ISO-8859-1')
layers = ds[0]

#HERE IS THE TRICK
new_layer = layers[0].geom.clone() 
new_layer.coord_dim = 2

obj=MyModel(geometry=GEOSGeometry(layers[0].geom.hex))
obj.save()

关于django - 在 GeoDjango 上剥离 Z 维度(强制 2D 几何),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35851577/

相关文章:

c# - GIS 查找表示为长纬度集合的多边形所包含的点(长纬度)

MySQL VS Postgres/POSTGIS 空间数据库支持

postgresql - 使用空间查询选择点要素

python - django模板嵌套字典,按键获取值

python - 覆盖 django rest 泛型 CreateAPIView 中的创建方法

php - 将 GIS 点批量转换为纬度/经度

javascript - 传单 + EPSG :31466 WMS layer

geospatial - 将 MapInfo 数据导入 PostGIS 时如何避免几何问题?

python - 使用基于类的(通用) View 在 Django 中重定向

Django DateTime 字段生成带 OUT 时区的时间戳字段