Shapely integration for MongoEngine
This project is a complete replacement of the GeoJSON field functionality of MongoEngine which exploits the
geo_interface protocol to handle BSON serialization and deserialization from MongoDB. This is accomplished by
integrating Shapely‘s corresponding GeoJSON-like geometry types:
For substituting the MongoEngine field types:
And additionally providing:
Wherein the field value stored in memory is the corresponding geometry instance rather than a GeoJSON mapping. This
provides direct access to all of Shapely‘s geometry operations and manipulations as well as access to an
array_interface protocol for NumPy.
Field validation is overridden with Shapely‘s implementation of OpenGIS standards. Geo-querying via MongoDB’s
2dsphere is also supported (with exception of GeometryCollection) in the usual manner.
Usage is intended to be consistent with the MongoEngine documentation:
import random
import mongoengine as me
import mongoshapes as ms
from shapely.geometry import box, mapping
from shapely.geometry.base import BaseGeometry
class Doc(me.Document):
point = ms.PointField()
def bbox(geometry: BaseGeometry) -> ms.PolygonDict:
buffered = geometry.buffer(5)
bounds = buffered.bounds
polygon = box(*bounds)
mapped = mapping(polygon)
return mapped
me.connect('mydb')
new = ms.Point(map(lambda x: random.uniform(-x, x), (180, 90)))
doc = Doc()
doc.point = new
doc.validate()
doc.save()
qry = doc.objects(point__geo_within=bbox(new))
assert qry.first() is not None
Using provided examples from MongoDB’s GeoJSON documentation as fixtures, a full py.test suite exists: