【发布时间】:2020-07-16 09:20:25
【问题描述】:
我的数据库中有一个 GeoDjango GeometryField:
class Place(models.Model):
name = CharField()
geometry = models.GeometryField()
我正在尝试将 shapefile 的某些部分加载到我的数据库中。
from django.contrib.gis.gdal import DataSource
datasource = DataSource("file.shp")
layer = datasource[0]
for each in layer:
name = each.get("NAME") # To obtain the name
... (some more logic to add properties and validate which objects should be imported)
Place.objects.create(
name = name,
geometry = each.geom
)
但是,这会返回此错误:
无法使用类型值设置 Place SpatialProxy (GEOMETRY):
我不确定我必须如何转换这个gdal.geometries.MultiPolygon 对象,以便它可以存储在数据库中。我试过使用:
from django.contrib.gis.gdal import OGRGeometry
geometry = OGRGeometry(each.geom)
但这行不通。
【问题讨论】: