I recently posted my first question on StackOverflow, but I think the question was quite extensive and full of potential errors. So I'd begin again, step-by-step, with a much shorter and simplier question :

The purpose is to structure my models.py in my django website to store datas in a Mongo database as GeoJson objects.

Do you think this code is correct, I'm not very sure about the syntax ?

Thanks a lot !


models.py :

# -*- coding: utf-8 -*-

from mongoengine import *
connect(‘mongodb_jsons’)

import datetime

class GeoJson(Document):
 # from a geojson object
 # save it in Mongodb with a geojson structure

    Date_created = DateTimeField(default=datetime.datetime.now)
    Latitude = FloatField()
    Longitude = FloatField()
    Content1 = CharField()
    Content2 = CharField()

    meta = {'db_alias': 'mongodb_jsons', # save in DB ‘mongodb_jsons’
            'indexes': [                 # the geojson structure
                {'type' : 'Feature', {
                   'geometry':{
                        'type' : 'Point',
                        'coordinates' : ['Longitude','Latitude']}, 
                        }, 
                   'properties':{
                       'content1' : 'Content1',
                       'content2' : 'Content2',    
                       'date_creation' : 'Date_created'
                        }
                    }
                }]
            }

up vote 2 down vote accepted

I'm not sure your GeoJSON is correct. 1) I don't think you want to use parenthesis, 2) your geometry coordinates should be [longitude, latitude] (you have it reversed). GeoJSONlint.com is a great resource for validating the syntax.

Like this:

{
    "type": "Feature",
    "geometry": {
        "type": "Point",
        "coordinates": [-105,35]
    },
    "properties": {
        "name": "Plaza Road Park",
        "content1": "Content1",
        "content2": "Content2",
        "date_creation": 06/11/2014
    }
}
  • I used the parenthesis based on the mongoengine documentation, but still not sure how to understand well the syntax : docs.mongoengine.org/guide/defining-documents.html#indexes – Julien_ Jun 11 '14 at 16:00
  • and I corrected the syntax in the question – Julien_ Jun 11 '14 at 18:59
  • Great. I corrected the inside of the 'indexes' part in the question, but my concern is still about if this way to specify MongoEngine indexes gets well the document (here called 'GeoJson') properties : namely Latitude, Longitude, Content1 and Content2. – Julien_ Jun 11 '14 at 21:35
  • I'm not certain what your question is now. Is your code working or are you not getting the result you are expecting? – mtn.biker Jun 12 '14 at 0:35
  • What I first wanted was to integrate datas into my MongoDB with a GeoJson structure. But while I was starting to try to make it work locally, I thought that a simplier solution was to get back the datas from Mongo without any indexes, but to 'recreate' the GeoJson structure once calling the datas in Python itself. Like : from .models import GeoJson collection = GeoJson.objects.all() for data in collection : GeoJsons = { Tweets_json = {"type": "FeatureCollection", "features": [{"geometry": {"type": "Point", "coordinates": data.coordinates (etc..) – Julien_ Jun 12 '14 at 1:21

I found a way to avoid the meta indexes, which were not exactly fitting the task. I tried it in local and it seems to work. In my models.py the code would be more like :

models.py

    # -*- coding: utf-8 -*-

    from django.db import models

    from mongoengine import *
    connect ('mongo_db')

    import datetime

    # Create your models here.
    class Geometry(EmbeddedDocument):
        type_ = StringField() 
        # as 'type' is already a function in Python we have to use a different name here
        Coordinates = ListField(FloatField())

    class Properties(EmbeddedDocument):
        Date_created = DateTimeField(default=datetime.datetime.now)
        IntegerData = IntField()
        StringData = StringField()
        DecimalData = DecimalField()
        ListData = ListField(StringField(max_length=30))

    class GeoJson(Document):       
        type_ = StringField()
        geometry = ListField(EmbeddedDocumentField(Geometry))
        properties = ListField(EmbeddedDocumentField(Properties))

The only problem is that is not exactly a GeoJson because 'geometry' and 'properties' are stocked as lists, and not as dictionnaries like in a proper GeoJson. so :

"properties": [{
    "name": "Plaza Road Park", ... ]}

instead of :

"properties": {
    "name": "Plaza Road Park", ... }

I also found another way to do so (from this link) if the source of datas are GeoJson objects :

direct2mongo.py

    import json
    from .models import GeoJson
    from mongoengine import *
    connect ('mongo_db')

    # datas are received as geojson objects
    decoded = json.loads(data)
    geojsonObject = GeoJson(**decoded).save()

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.