Skip to Content

Nous adressons toutes nos pensées à la famille de notre ami Jérôme !

http://www.forumsig.org/showthread.php/43488-Disparition-de-Phoenix

requêtes spatiales avec PyMongo

  1. from pymongo import Connection, GEO2D
  2. db = Connection().monspatial
  3.  
  4. # création de l'index spatial
  5. db.montest.create_index([("loc", GEO2D)])
  6.  
  7. # insertion des points (simple)
  8. db.montest.insert({"loc": [50.52, 4.42]})
  9. db.montest.insert({"loc": [50.52, 5.42]})
  10. db.montest.insert({"loc": [51.52, 3.42]})
  11.  
  12. # recherche find near, limitée
  13. for doc in db.montest.find({"loc": {"$near": [50.00, 5.00]}}).limit(3):
  14. repr(doc)
  15. "{u'loc': [50.520000000000003, 5.4199999999999999], u'_id': ObjectId('4e5bc3c5c32e370848000001')}"
  16. "{u'loc': [50.520000000000003, 4.4199999999999999], u'_id': ObjectId('4e5bc361c32e370848000000')}"
  17. "{u'loc': [51.520000000000003, 3.4199999999999999], u'_id': ObjectId('4e5bc3d2c32e370848000002')}"
  18.  
  19.  
  20. # recherche find within box
  21. for doc in db.montest.find({"loc": {"$within": {"$box": [[50, 4.5], [51, 6.0]]}}}):
  22. repr(doc)
  23. "{u'loc': [50.520000000000003, 5.4199999999999999], u'_id': ObjectId('4e5bc3c5c32e370848000001')}"
  24.  
  25. # recherche geoNear
  26. from bson.son import SON
  27. >>> db.command(SON([('geoNear', 'montest'), ('near', [50.52, 4.42])]))
  28. {u'ok': 1.0, u'near': u'1100100000001111101011001011000011101100000011001101', u'ns': u'monspatial.montest', u'stats': {u'btreelocs': 2, u'avgDistance': 0.80473870022961835, u'objectsLoaded': 3, u'time': 38, u'maxDistance': 1.4142134780844851, u'nscanned': 3}, u'results': [{u'obj': {u'loc': [50.520000000000003, 4.4199999999999999], u'_id': ObjectId('4e5bc361c32e370848000000')}, u'dis': 0.0}, {u'obj': {u'loc': [50.520000000000003, 5.4199999999999999], u'_id': ObjectId('4e5bc3c5c32e370848000001')}, u'dis': 1.0000026226043701}, {u'obj': {u'loc': [51.520000000000003, 3.4199999999999999], u'_id': ObjectId('4e5bc3d2c32e370848000002')}, u'dis': 1.4142134780844851}]}