溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

mongodb導(dǎo)入shapefile數(shù)據(jù)的方法

發(fā)布時(shí)間:2020-06-29 16:44:31 來(lái)源:億速云 閱讀:274 作者:清晨 欄目:編程語(yǔ)言

小編給大家分享一下mongodb導(dǎo)入shapefile數(shù)據(jù)的方法,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討方法吧!

兩種解決方案:

一、將整個(gè)shapefile轉(zhuǎn)為GeoJSON然后直接導(dǎo)入mongoDB數(shù)據(jù)庫(kù)中

首先,將shapefile數(shù)據(jù)轉(zhuǎn)為WGS84地理坐標(biāo),然后使用GDAL的命令行工具ogr2ogr進(jìn)行格式的轉(zhuǎn)換,轉(zhuǎn)換命令如下:

ogr2ogr -f geoJSON continents.json continents.shp

刪除生成JSON文件的前兩行{ "type": "FeatureCollection",和最后一行}。

最后,使用mongodb的mongoimport工具進(jìn)行導(dǎo)入:

mongoimport --db world --collection continents < continents.json

這樣子整個(gè)shapefile文件在mongodb中是以一個(gè)document存在的。

二、更加細(xì)粒度的存儲(chǔ)方法是將shapefile中的每個(gè)feature取出來(lái)轉(zhuǎn)為GeoJSON存入mongodb

具體實(shí)現(xiàn)代碼入下(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();
        // 依次取出每一個(gè)Feature轉(zhuǎn)為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();
    }
}

看完了這篇文章,相信你對(duì)mongodb導(dǎo)入shapefile數(shù)據(jù)的方法有了一定的了解,想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI