将 KML 文件添加到 GeoDjango 字段中
Adding a KML file into a GeoDjango field
我正在尝试将 KML 文件添加到 GeoDjango 中的字段。 Link to KML file. I tried to follow the answer on this question 但大部分都是错误的。
我的模特:
class School(models.Model):
boundaries = models.PolygonField(null=True)
i = School.objects.get(...)
ds = DataSource('school.aspx')
layer = ds[0] #The file only has 1 layer
geom = layer.get_geoms()
boundary = GEOSGeometry(geom[0])
i.boundaries = boundary
i.save()
上面的代码给我以下错误:
TypeError: Improper geometry input type: <class 'django.contrib.gis.gdal.geometries.Polygon'>
当我尝试直接添加字段时,像这样:
i = School.objects.get(...)
ds = DataSource('school.aspx')
layer = ds[0]
geom = layer.get_geoms()
i.boundaries = geom[0]
i.save()
我收到此错误:TypeError: Cannot set School SpatialProxy (POLYGON) with value of type: <class 'django.contrib.gis.gdal.geometries.Polygon'>
如何将 KML 文件中的多边形形状保存到我的数据库中?我被难住了。
尝试使用 gdal geoms 的 .geos
属性:
ds = DataSource('school.kml')
o = School(boundaries=ds[0][0].geom.geos)
o.save()
我正在尝试将 KML 文件添加到 GeoDjango 中的字段。 Link to KML file. I tried to follow the answer on this question 但大部分都是错误的。
我的模特:
class School(models.Model):
boundaries = models.PolygonField(null=True)
i = School.objects.get(...)
ds = DataSource('school.aspx')
layer = ds[0] #The file only has 1 layer
geom = layer.get_geoms()
boundary = GEOSGeometry(geom[0])
i.boundaries = boundary
i.save()
上面的代码给我以下错误:
TypeError: Improper geometry input type: <class 'django.contrib.gis.gdal.geometries.Polygon'>
当我尝试直接添加字段时,像这样:
i = School.objects.get(...)
ds = DataSource('school.aspx')
layer = ds[0]
geom = layer.get_geoms()
i.boundaries = geom[0]
i.save()
我收到此错误:TypeError: Cannot set School SpatialProxy (POLYGON) with value of type: <class 'django.contrib.gis.gdal.geometries.Polygon'>
如何将 KML 文件中的多边形形状保存到我的数据库中?我被难住了。
尝试使用 gdal geoms 的 .geos
属性:
ds = DataSource('school.kml')
o = School(boundaries=ds[0][0].geom.geos)
o.save()