项目作者: Sean-McVeigh

项目描述 :
Shapely integration for MongoEngine
高级语言: Python
项目地址: git://github.com/Sean-McVeigh/mongoshapes.git
创建时间: 2018-09-19T16:40:50Z
项目社区:https://github.com/Sean-McVeigh/mongoshapes

开源协议:MIT License

下载


MongoShapes

Build Status
Coverage Status

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:

  • Point
  • LineString
  • Polygon
  • MultiPoint
  • MultiLineString
  • MultiPolygon
  • GeometryCollection

For substituting the MongoEngine field types:

  • PointField
  • LineStringField
  • PolygonField
  • MultiPointField
  • MultiLineStringField
  • MultiPolygonField

And additionally providing:

  • GeometryCollectionField

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

Usage is intended to be consistent with the MongoEngine documentation:

  1. import random
  2. import mongoengine as me
  3. import mongoshapes as ms
  4. from shapely.geometry import box, mapping
  5. from shapely.geometry.base import BaseGeometry
  6. class Doc(me.Document):
  7. point = ms.PointField()
  8. def bbox(geometry: BaseGeometry) -> ms.PolygonDict:
  9. buffered = geometry.buffer(5)
  10. bounds = buffered.bounds
  11. polygon = box(*bounds)
  12. mapped = mapping(polygon)
  13. return mapped
  14. me.connect('mydb')
  15. new = ms.Point(map(lambda x: random.uniform(-x, x), (180, 90)))
  16. doc = Doc()
  17. doc.point = new
  18. doc.validate()
  19. doc.save()
  20. qry = doc.objects(point__geo_within=bbox(new))
  21. assert qry.first() is not None

Testing

Using provided examples from MongoDB’s GeoJSON documentation as fixtures, a full py.test suite exists:

  • unit for testing GeoJSON fixtures with Shapely and MongoEngine.
  • func for testing validation and querying with new field types.