gpt4 book ai didi

python - Geojson 到 MongoDB/Mongoengine 的属性格式

转载 作者:太空宇宙 更新时间:2023-11-03 15:57:04 24 4
gpt4 key购买 nike

我想找到一个解决方案,告诉我应该为带有 properties 列的 geojson 提供什么 DataField 。目前,下面的 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": "虚拟名称","color": "#000000"}

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

最佳答案

尝试DictFieldDynamicDocument

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

关于python - Geojson 到 MongoDB/Mongoengine 的属性格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40689399/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com