Uploaded image for project: 'Python Driver'
  1. Python Driver
  2. PYTHON-1155

Deserialize GeoJSON data as Python GeoJSON type

    • Type: Icon: Improvement Improvement
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • None

      Wouldn't it be nice to have GeoJSON data deserialized as GeoJSON objects in pymongo, just like bson.ObjectId or datetime.datetime instances?

      Consider following code:

      import bson
      import datetime
      import geojson
      
      d = datetime.datetime(2016, 1, 1)
      o = bson.ObjectId('0123456789ab0123456789ab')
      g = geojson.Point([1, 2])
      print(type(d))
      print(type(o))
      print(type(g))
      
      data = bson.BSON.encode({
          'a': 1,
          'd': d,
          'o': o,
          'g': g
      })
      decoded_doc = bson.BSON.decode(data)
      
      print(decoded_doc)
      
      print(type(decoded_doc['d']))
      print(type(decoded_doc['o']))
      print(type(decoded_doc['g']))
      

      decoded_doc prints as:

      {'a': 1, 'g': {'type': 'Point', 'coordinates': [1, 2]}, 'o': ObjectId('0123456789ab0123456789ab'), 'd': datetime.datetime(2016, 1, 1, 0, 0)}
      

      where g is a dict. It could be nice to get a GeoJSON object.

      This could rely on already existing GeoJSON library: https://github.com/frewsxcv/python-geojson.

      Unless adding this kind of dependency is an issue.

      I realize that this library might not have reached required quality / stability. On the other hand, duplicating it inside pymongo would be a shame.

      I figured pymongo's bson might be the good place for that rather than application code.

      Just an idea. Feedback welcome.

            Assignee:
            prashant.mital Prashant Mital (Inactive)
            Reporter:
            lafrech Jérôme
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: