//引入Pymongo
>>> from pymongo import MongoClient,GEO2D
// 链接数据库gis
>>> db = MongoClient().gis
//创建索引
>>> db.places.create_index([(“loc”,GEO2D)])
‘loc_2d’
//插入数据
>>> db.places.insert({“loc”:[120,30]})
ObjectId(‘520e3893421aa91ddc7a8239’)
>>> db.places.insert({“loc”:[80,39]})
ObjectId(‘520e38b6421aa91ddc7a823a’)
>>> db.places.insert({“loc”:[112.25,56]})
ObjectId(‘520e38de421aa91ddc7a823b’)
>>> db.places.insert({“loc”:[125.23,56]})
ObjectId(‘520e3909421aa91ddc7a823c’)
//附近查询 limit 查询前三个
>>>for doc in db.places.find({“loc”:{“$near”:[115.20,35]}}).limit(3):
doc
{‘loc’: [120, 30], ‘_id’: ObjectId(‘520e3893421aa91ddc7a8239’)} {‘loc’: [112.25, 56], ‘_id’: ObjectId(‘520e38de421aa91ddc7a823b’)} {‘loc’: [125.23, 56], ‘_id’: ObjectId(‘520e3909421aa91ddc7a823c’)}
//拉框查询
>>> for doc in db.places.find({“loc”:{“$within”:{“$box”:[[75.23,20.32],[152.23,60]]}}}):
doc
{‘loc’: [120, 30], ‘_id’: ObjectId(‘520e3893421aa91ddc7a8239’)} {‘loc’: [125.23, 56], ‘_id’: ObjectId(‘520e3909421aa91ddc7a823c’)} {‘loc’: [80, 39], ‘_id’: ObjectId(‘520e38b6421aa91ddc7a823a’)} {‘loc’: [112.25, 56], ‘_id’: ObjectId(‘520e38de421aa91ddc7a823b’)}
//点缓冲区查询
>>> for doc in db.places.find({“loc”:{“$within”:{“$center”:[[120.2,30.3],10]}}}):
doc
{‘loc’: [120, 30], ‘_id’: ObjectId(‘520e3893421aa91ddc7a8239’)}
//——参考文档
http://api.mongodb.org/python/current/examples/geo.html
转载请注明出处:http://www.cnblogs.com/likehua
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/tech/bigdata/9615.html