Python MongoDB Spatial Query

WBOY
发布: 2016-06-07 17:38:50
原创
1105 人浏览过

Python MongoDB Spatial Query //引入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('52

Python MongoDB Spatial Query

//引入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')}

//------参考文档

转载请注明出处:

posted on

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板