MongoDB导入shapefile数据的具体方法是什么
这篇文章主要讲解了“MongoDB导入shapefile数据的具体方法是什么”,文中的讲解内容简单、清晰、详细,对大家学习或是工作可能会有一定的帮助,希望大家阅读完这篇文章能有所收获。下面就请大家跟着小编的思路一起来学习一下吧。
以上就是关于“MongoDB导入shapefile数据的具体方法是什么”的介绍了,感谢各位的阅读,如果大家想要了解更多相关的内容,欢迎关注博信,小编每天都会为大家更新不同的知识。
两种解决方案:
一、将整个shapefile转为GeoJSON然后直接导入mongoDB数据库中
首先,将shapefile数据转为WGS84地理坐标,然后使用GDAL的命令行工具ogr2ogr进行格式的转换,转换命令如下:
ogr2ogr-fgeoJSONcontinents.jsoncontinents.shp
删除生成JSON文件的前两行{ "type": "FeatureCollection",和最后一行}。
最后,使用mongodb的mongoimport工具进行导入:
mongoimport--dbworld--collectioncontinents<continents.json
这样子整个shapefile文件在mongodb中是以一个document存在的。
二、更加细粒度的存储方法是将shapefile中的每个feature取出来转为GeoJSON存入mongodb
具体实现代码入下(Java版本):
packagecn.tzy.mongodb; importjava.io.File; importjava.io.IOException; importjava.io.StringWriter; importorg.bson.Document; importorg.geotools.data.FileDataStore; importorg.geotools.data.FileDataStoreFinder; importorg.geotools.data.simple.SimpleFeatureIterator; importorg.geotools.data.simple.SimpleFeatureSource; importorg.geotools.geojson.feature.FeatureJSON; importorg.opengis.feature.simple.SimpleFeature; importcom.mongodb.MongoClient; importcom.mongodb.client.MongoCollection; importcom.mongodb.client.MongoDatabase; publicclassMongoEx{ publicstaticvoidmain(String[]args)throwsIOException{ finalStringIP_ADDRESS="127.0.0.1"; finalStringDB_NAME="SpatialFeatures"; finalStringCOLLECTION_NAME="continents"; finalStringSHAPE_FILE="/home/theone/Data/World/continent.shp"; MongoClientclient=newMongoClient(IP_ADDRESS,27017); MongoDatabasedb=client.getDatabase(DB_NAME); db.createCollection(COLLECTION_NAME); MongoCollection<Document>coll=db.getCollection(COLLECTION_NAME); FileshapeFile=newFile(SHAPE_FILE); FileDataStorestore=FileDataStoreFinder.getDataStore(shapeFile); SimpleFeatureSourcesfSource=store.getFeatureSource(); SimpleFeatureIteratorsfIter=sfSource.getFeatures().features(); //依次取出每一个Feature转为GeoJSON格式,然后插入到collection中 while(sfIter.hasNext()){ SimpleFeaturefeature=(SimpleFeature)sfIter.next(); FeatureJSONfjson=newFeatureJSON(); StringWriterwriter=newStringWriter(); fjson.writeFeature(feature,writer); Stringsjson=writer.toString(); Documentdoc=Document.parse(sjson); coll.insertOne(doc); } client.close(); } }
以上就是关于“MongoDB导入shapefile数据的具体方法是什么”的介绍了,感谢各位的阅读,如果大家想要了解更多相关的内容,欢迎关注博信,小编每天都会为大家更新不同的知识。
版权声明
本文仅代表作者观点,不代表博信信息网立场。