我正在使用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文件版本(编辑 here ,为了简洁起见,删除了一大块中间坐标):
- {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'>
对于loaddata来说,YAML文件的多边形(此处为&#39; shape&#39;)字段应该是什么样的?
答案 0 :(得分:1)
GEOS正在寻找WKT(Well Known Text)格式,它与上面的geoJSON / YAML格式接近但不相同。
实际的解决方案是使用geomet将JSON输入转换为WKT,然后可以舒适地驻留在YAML文件中......带括号的乐趣!
那么成功的格式如下所示:
- fields: {external_id: toluca-lake, name: Toluca Lake, region: 9, shape: 'MULTIPOLYGON
(((-118.357158 34.164806, -118.357154 34.163097, -118.357151 34.161287, -118.356036
34.161287, -118.354978 34.161288, -118.354682 34.161288, -118.354020 34.161288,
-118.353103 34.161289, -118.353034 34.161128, -118.352925 34.160873, -118.352156
34.159076, -118.352138 34.159033, -118.351812 34.158271, -118.351554 34.157668,
-118.351235 34.156925, -118.350751 34.155794, -118.350196 34.154497, -118.349988
34.154012, -118.349958 34.153941, -118.349830 34.153812, -118.349756 34.153629,
-118.349673 34.153425, -118.349643 34.153350, -118.349664 34.153256, -118.349216
34.152209, -118.348450 34.150419, -118.348067 34.149523, -118.347680 34.148618,
-118.347555 34.148327, -118.347308 34.147748, -118.346800 34.146562, -118.346767
34.146485, -118.346624 34.146151, -118.346446 34.145735, -118.346430 34.145696,
-118.345949 34.144573, -118.345903 34.144218, -118.345691 34.142572, -118.345678
34.142466, -118.345665 34.142367, -118.345665 34.142367, -118.345698 34.142356,
-118.346425 34.142207, -118.346907 34.142174, -118.347168 34.142177, -118.347168
34.142177, -118.347522 34.142180, -118.348435 34.142262, -118.351608 34.142887,
-118.352051 34.142964, -118.354160 34.143211, -118.354603 34.143227, -118.357641
34.143337, -118.357783 34.143342, -118.357967 34.143348, -118.358339 34.143269,
-118.358930 34.143143, -118.359082 34.143111, -118.359198 34.143088, -118.361136
34.142712, -118.361258 34.142688, -118.361267 34.142686, -118.361264 34.142680,
-118.361340 34.142669, -118.361411 34.142907, -118.361711 34.143307, -118.362211
34.144107, -118.362911 34.145107, -118.363350 34.145900, -118.363863 34.146805,
-118.364611 34.148106, -118.365011 34.148806, -118.365312 34.149206, -118.365512
34.149606, -118.366012 34.150406, -118.366612 34.151206, -118.367112 34.152206,
-118.367609 34.153023, -118.367885 34.153506, -118.368500 34.154522, -118.368612
34.154706, -118.369145 34.155691, -118.369652 34.156644, -118.370237 34.157613,
-118.369112 34.157606, -118.368112 34.157606, -118.367012 34.157606, -118.365929
34.157604, -118.364912 34.157606, -118.363793 34.157613, -118.362712 34.157606,
-118.361601 34.157613, -118.361623 34.159427, -118.361612 34.161206, -118.361612
34.163106, -118.361612 34.164806, -118.360512 34.164806, -118.359412 34.164806,
-118.358211 34.164806, -118.357211 34.164806, -118.357158 34.164806)))'}
model: geo.neighborhood