2016-11-19 6 views
1

propertiesカラムを使用してgeojsonに渡すべきものについては、DataFieldの解を探したいと思います。現在、以下のgeojson形式では、StringField()PointField()しか必要ないので、問題はありません。Geojson to MongoDB/Mengoengineのプロパティフォーマット

にGeoJSON形式は次のようになります。

from mongoengine import * 
from colorful.fields import RGBColorField 

class Geometry(Document): 
    type = StringField() 
    id = StringField() 
    geometry = LineStringField() 
    name = StringField() color= RGBColorField() ***OR*** properties = ??? 
:以下のいずれかのよう モデルを使用して

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

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

しかし、この形式を持つにGeoJSONのための

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

どのように私は、モデル内にGeoJSONフォーマットを保存することができます。私はそれが"properties": [{"name": "Dummy Name","color": "#000000"}]

ないにつながるEmbeddedDocumentFieldを使用して、別のプロパティフィールドを作成しますか?

答えて

1

DictFieldまたはDynamicDocumentを試してください。

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"} 
     } 
} 

または、単にEmbeddedDocumentFieldindex 0を使用してアクセス?

print(properties[0]) 
{"name": "Dummy Name","color": "#000000"} 
+0

私は実際にドキュメントを再読し、DictFieldを見ました。 :)私はこれが完璧にすべてを通して動作することを願っています。ありがとう! – Reiion