mongodb導入shapefile數(shù)據(jù)的方法-創(chuàng)新互聯(lián)

創(chuàng)新互聯(lián)www.cdcxhl.cn八線動態(tài)BGP香港云服務器提供商,新人活動買多久送多久,劃算不套路!

成都創(chuàng)新互聯(lián)于2013年開始,是專業(yè)互聯(lián)網(wǎng)技術服務公司,擁有項目成都做網(wǎng)站、網(wǎng)站設計網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元寧洱做網(wǎng)站,已為上家服務,為寧洱各地企業(yè)和個人服務,聯(lián)系電話:028-86922220

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

兩種解決方案:

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

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

ogr2ogr -f geoJSON continents.json continents.shp

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

最后,使用mongodb的mongoimport工具進行導入:

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

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

二、更加細粒度的存儲方法是將shapefile中的每個feature取出來轉(zhuǎn)為GeoJSON存入mongodb

具體實現(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();
        // 依次取出每一個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();
    }
}

看完了這篇文章,相信你對mongodb導入shapefile數(shù)據(jù)的方法有了一定的了解,想了解更多相關知識,歡迎關注創(chuàng)新互聯(lián)-成都網(wǎng)站建設公司行業(yè)資訊頻道,感謝各位的閱讀!

分享名稱:mongodb導入shapefile數(shù)據(jù)的方法-創(chuàng)新互聯(lián)
分享URL:http://muchs.cn/article6/cdccig.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、品牌網(wǎng)站建設、網(wǎng)站設計、微信公眾號、網(wǎng)站營銷網(wǎng)站收錄

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設