当前位置:Gxlcms > mysql > PythonMongoDBSpatialQuery

PythonMongoDBSpatialQuery

时间:2021-07-01 10:21:17 帮助过:27人阅读

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

人气教程排行