I would like to find a solution on what DataField
I should give to a geojson with a properties
column. Currently a geojson format below would not have any issues since it it would only need the StringField()
and PointField()
.
A geojson format looks like this:
{
name : "Timmy's Taco Truck",
loc : {
type : "Point",
coordinates : [ 37.7577 , -122.4376 ]
}
}
However for a geojson with this format:
{
"type" : "Feature",
"id" : "ID80001",
"geometry":{"type": "LineString", "coordinates":[[122.332,14.241],[125.332,13.532]]},
"properties":{ "name":"Dummy Name", "color":"#000000" }
}
Using a model like the one below:
from mongoengine import *
from colorful.fields import RGBColorField
class Geometry(Document):
type = StringField()
id = StringField()
geometry = LineStringField()
name = StringField() color= RGBColorField() ***OR*** properties = ???
If I use EmbeddedDocumentField
and create a separate properties field it would result to "properties": [{"name": "Dummy Name","color": "#000000"}]
not:
"properties": {"name": "Dummy Name","color": "#000000"}
How can I preserve the geojson format in the models?
Try DictField or 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"}
}
}
or, just access it using index 0
of EmbeddedDocumentField
?
print(properties[0])
{"name": "Dummy Name","color": "#000000"}