Geojson到MongoDB / Mongoengine的属性格式

时间:2016-11-19 05:01:24

标签: python django mongodb geojson mongoengine

我想找到一个解决方案DataField我应该给一个properties列的geojson。目前,下面的geojson格式不会有任何问题,因为它只需要StringField()PointField()

geojson格式如下所示:

{
name : "Timmy's Taco Truck",
  loc : {
    type : "Point",
    coordinates : [ 37.7577 , -122.4376 ]
  }
}

但是对于这种格式的geojson:

{
   "type" : "Feature",
   "id" : "ID80001",
   "geometry":{"type": "LineString", "coordinates":[[122.332,14.241],[125.332,13.532]]},
   "properties":{ "name":"Dummy Name", "color":"#000000" }
}

使用 模型 ,如下所示:

from mongoengine import *
from colorful.fields import RGBColorField

class Geometry(Document):
   type = StringField()
   id = StringField()
   geometry = LineStringField()
   name = StringField() color= RGBColorField() ***OR*** properties = ???

如果我使用EmbeddedDocumentField并创建单独的属性字段,则会产生"properties": [{"name": "Dummy Name","color": "#000000"}]

"properties": {"name": "Dummy Name","color": "#000000"}

如何在模型中保留geojson格式?

1 个答案:

答案 0 :(得分:1)

尝试DictFieldDynamicDocument

mySelectedItem.id

或者,只需使用from mongoengine import * class Geometry(Document): type = StringField() id = StringField() geometry = LineStringField() properties = DictField() g = Geometry() # Assuming id is unique g.properties['id'] = {"name": "Dummy Name","color": "#000000"} g.save() db.Geometry.findOne() { "_id": <some_id> "properties": { "<some_id>": { {"name": "Dummy Name","color": "#000000"} } } 的{​​{1}}访问它?

index 0