使用Python 將shapefile導入mongodb

使用Python 將shapefile導入mongodbpython

隨着big data時代的到來,各個行業都在考慮能不能把big data的思路、方法引入進來,GIS行業也不能免俗。web

下面就介紹一下如何將shapefile導入mongodb中mongodb

1首先安裝pyshp 和pymongo 庫併發

2 安裝mongodb,並正確運行app

3 執行下面的python腳本高併發

import pymongospa

from pymongo.connection import Connection.net

def readSHPPoint(append):code

    fileP = u'E:\\data\\supermarket_webMercator\\supermarket.shp'server

    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 

相關文章
相關標籤/搜索