【发布时间】:2014-08-29 12:43:29
【问题描述】:
我正在使用 pyyaml(以及其他库)制作一个夹具集,我想使用 manage.py loaddata 将其加载到我的 django 项目中。当我运行 loaddata 时,我正在生成的 YAML(或 GeoDjango 或其他东西)无法正常工作。
我收到此错误:Cannot set Neighborhood GeometryProxy (MULTIPOLYGON) with value of type: <type 'list'>
这是我尝试加载的数据示例。下面是模型定义
- fields:
external_id: unincorporated-catalina-island
name: Unincorporated Catalina Island
region: 5
shape:
- - - [-118.604432, 33.47871]
....
- [-118.604375, 33.478642]
- [-118.604325, 33.478558]
- [-118.603603, 33.47794]
model: geo.neighborhood
型号:
from django.db import models
from django.contrib.gis.db import models as geo_models
class Area(ABase):
"""
An area defines a geographic area of any size. This abstract class is
subclassed in order to define the type of area being modeled, e.g.
a wide region or a smaller neighborhood.
"""
name = models.CharField(max_length=200)
external_id = models.CharField(max_length=200, blank=True, unique=True,
help_text="The ID of this area in a third party datasource")
shape = geo_models.MultiPolygonField()
def __unicode__(self):
return self.name
class Meta(ABase.Meta):
abstract = True
ordering = ['name']
我想我没有正确格式化形状字段。 JSON 源代码在这里:
然后我尝试为 YAML 文件提供整个几何字典,如下所示:
- fields:
external_id: lake-los-angeles
name: Lake Los Angeles
region: 2
shape:
coordinates:
- - - [-117.845055, 34.631392]
...
- [-117.845055, 34.631392]
type: MultiPolygon
model: geo.neighborhood
但这会产生与上面几乎相同的错误:
Cannot set Neighborhood GeometryProxy (MULTIPOLYGON) with value of type: <type 'dict'>
为了继续这个过程,我尝试了最小的手工制作的 YAML 文件版本(为简洁起见,在 此处 删除了大部分中间坐标):
- {model: geo.region, fields: {province: 1, id: &angeles-forest 1,
name: Angeles Forest, external_id: angeles-forest, shape: [ [ [ [ -118.298947, 34.157699 ], [ -118.298830, 34.157683 ], [ -118.298638, 34.157808 ], [ -118.298481, 34.157914 ], [ -118.298172, 34.158122 ], [ -118.297935, 34.158293 ], [ -118.297878, 34.158342 ], [ -118.297854, 34.158477 ], [ -118.297837, 34.158577 ], [ -118.297744, 34.158575 ], [ -118.299082, 34.157728 ], [ -118.298947, 34.157699 ] ] ] ]}}
这会产生与上面相同的错误,抱怨<type 'list'>
YAML 文件的多多边形(此处为“形状”)字段应该是什么样子才能让 loaddata 接受它?
【问题讨论】:
-
我对 YAML 了解不多,但是在 GeoJSON 或 WKT 格式中,您使用三方括号(或括号)来描绘多面体:每个点一组,每个组成多边形周围一组,另一个设置为在 MultiPolygon 中分隔每个多边形,例如 [[[x1, y1],[x2,y2],[x3,y3],[xn,yn]],[[polygon 2....]]]]
-
我认为这只是 YAML 语法的问题。我使用流语法进行了尝试(参见最后一次编辑),它看起来更像 JSON(我相信是 YAML 的正确子集)并得到了完全相同的结果。正如其他地方所建议的那样,我也尝试切换到多边形,但结果是一样的。
标签: django yaml postgis geodjango pyyaml