mongodb导入shapefile数据的方法
                                            小编给大家分享一下MongoDB导入shapefile数据的方法,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨方法吧!

成都创新互联主要从事成都网站设计、成都做网站、网页设计、企业做网站、公司建网站等业务。立足成都服务金凤,十多年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18982081108
两种解决方案:
一、将整个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 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();
    }
} 看完了这篇文章,相信你对mongodb导入shapefile数据的方法有了一定的了解,想了解更多相关知识,欢迎关注创新互联行业资讯频道,感谢各位的阅读!
网站标题:mongodb导入shapefile数据的方法
URL链接:http://www.scyingshan.cn/article/isjgds.html

 建站
建站
 咨询
咨询 售后
售后
 建站咨询
建站咨询 
 