使用Python 将shapefile导入mongodb
随着big data时代的到来,各个行业都在考虑能不能把big data的思路、方法引入进来,GIS行业也不能免俗。
下面就介绍一下如何将shapefile导入mongodb中
1首先安装pyshp 和pymongo 库
2 安装mongodb,并正确运行
3 执行下面的python脚本
import pymongo
from pymongo.connection import Connection
def readSHPPoint(append):
fileP = u'E:\\data\\supermarket_webMercator\\supermarket.shp'
sf = shapefile.Reader(fileP)
shapeRecs = sf.shapeRecords()
mongodb_server='192.168.120.100'
mongodb_port = 27017
mongodb_collection ='supermarket'
mongodb_db = 'gisdb'
connection = Connection(mongodb_server, mongodb_port)
print 'Getting database %s' % mongodb_db
db = connection[mongodb_db]
print 'Getting the collection %s' % mongodb_collection
collection = db[mongodb_collection]
if append == False:
print 'Removing features from the collection...'
collection.remove({})
print 'Starting loading features...'
for shaperec in shapeRecs:
mongofeat = {}
#'{x='',y=''}'
strX = "%.3f" % shaperec.shape.points[0][0]
strY = "%.3f" % shaperec.shape.points[0][1]
mongogeom = '{x="'+strX+'",y="'+strY+'"}'
print mongogeom
mongofeat['geom'] = mongogeom
mongofeat['name'] = shaperec.record[1].decode('GB2312').encode('utf-8')
collection.insert(mongofeat)
#create 2d index
collection.create_index([("geom", pymongo.GEO2D)])
if __name__ == "__main__":
readSHPPoint(False)
目前mongodb只支持点类型的数据,并提供空间索引。
使用mongodb可以很方便的满足高并发的并且简单的需求,例如POI的周边查询API查询
本文转载自:http://www.giser.net/?p=1076