首页 技术 正文
技术 2022年11月14日
0 收藏 454 点赞 3,243 浏览 1267 个字

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,083
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,558
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,407
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,180
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,817
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,900