• 使用Python 将shapefile导入mongodb


    使用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 

     

  • 相关阅读:
    3n+1问题
    判断x的m次方和y的m次方末尾三位数是否相等
    OpenJudge 计算概论1007:点评赛车
    整数划分问题【转】
    证明:平面内有5个整点,必有两个点连线的中点为整点【本资源整理自网络】
    欧几里德算法的证明
    导出本地和远程SVN项目, Export remote SVN repository
    Centos7的firewalld配置
    ESXi5.5下的Centos7虚机配置静态IP
    Dubbo消费端错误: ClassNotFoundException: org.apache.zookeeper.proto.WatcherEvent
  • 原文地址:https://www.cnblogs.com/3echo/p/3262415.html
Copyright © 2020-2023  润新知