MongoDB导入shapefile数据的具体方法是什么
Admin 2022-05-27 群英技术资讯 600 次浏览
两种解决方案:
一、将整个shapefile转为GeoJSON然后直接导入mongoDB数据库中
首先,将shapefile数据转为WGS84地理坐标,然后使用GDAL的命令行工具ogr2ogr进行格式的转换,转换命令如下:
ogr2ogr -f geoJSON continents.json continents.shp
删除生成JSON文件的前两行{ "type": "FeatureCollection",和最后一行}。
最后,使用mongodb的mongoimport工具进行导入:
mongoimport --db world --collection continents < continents.json
这样子整个shapefile文件在mongodb中是以一个document存在的。
二、更加细粒度的存储方法是将shapefile中的每个feature取出来转为GeoJSON存入mongodb
具体实现代码入下(Java版本):
package cn.tzy.mongodb; import java.io.File; import java.io.IOException; import java.io.StringWriter; import org.bson.Document; import org.geotools.data.FileDataStore; import org.geotools.data.FileDataStoreFinder; import org.geotools.data.simple.SimpleFeatureIterator; import org.geotools.data.simple.SimpleFeatureSource; import org.geotools.geojson.feature.FeatureJSON; import org.opengis.feature.simple.SimpleFeature; import com.mongodb.MongoClient; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; public class MongoEx { public static void main(String[] args) throws IOException { final String IP_ADDRESS = "127.0.0.1"; final String DB_NAME = "SpatialFeatures"; final String COLLECTION_NAME = "continents"; final String SHAPE_FILE = "/home/theone/Data/World/continent.shp"; MongoClient client = new MongoClient(IP_ADDRESS, 27017); MongoDatabase db = client.getDatabase(DB_NAME); db.createCollection(COLLECTION_NAME); MongoCollection<Document> coll = db.getCollection(COLLECTION_NAME); File shapeFile = new File(SHAPE_FILE); FileDataStore store = FileDataStoreFinder.getDataStore(shapeFile); SimpleFeatureSource sfSource = store.getFeatureSource(); SimpleFeatureIterator sfIter = sfSource.getFeatures().features(); // 依次取出每一个Feature转为GeoJSON格式,然后插入到collection中 while (sfIter.hasNext()) { SimpleFeature feature = (SimpleFeature) sfIter.next(); FeatureJSON fjson = new FeatureJSON(); StringWriter writer = new StringWriter(); fjson.writeFeature(feature, writer); String sjson = writer.toString(); Document doc = Document.parse(sjson); coll.insertOne(doc); } client.close(); } }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章给大家分享的是有关MongoDB中skip和limit的作用和使用的内容。小编觉得挺实用的,因此分享给大家做个参考,下面一起跟随小编来学习一下吧。
MongoDB $type 操作符 描述 在本章节中,我们将继续讨论MongoDB中条件操作符 $type。 $type操作符是基于BSON类型来检索集合中匹配的数据类型,并返回结果。 MongoDB 中可以使用的类型如下表所示: 类型 数字 备注 Double 1 String 2 Object 3 Array 4 Binary da..
关闭mongodb端口的方法:1、使用ps -ef | grep mongo命令查看mongo相关进程。2、使用“kill <进程号>”命令关闭mongodb进程。
在MongoDB中索引就像书本的目录一样,可以帮助我们在没有目录的情况下查找内容,若是没有索引我们就只能全篇查找翻阅,那么你知道MongoDB如何使用索引吗?
使用mongodb做集群的方法:1、使用Replica Set副本实现集群。2、使用Sharding分片方式实现。3、使用Master-slave主备方式实现。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008