前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ShapeFile数据到mongodb的导入

ShapeFile数据到mongodb的导入

作者头像
卡尔曼和玻尔兹曼谁曼
发布2019-01-22 10:59:31
1.2K0
发布2019-01-22 10:59:31
举报

开发环境为: 系统环境 Linux 4.4.0-36-generic #55~14.04.1-Ubuntu x86_64 x86_64 x86_64 GNU/Linux mongodb版本 当前最新版本3.2.9 但是下面的代码同样适用Windows环境!

具体mongodb的安装参照官方文档,强烈建议参照官方安装文档。网上博客这种资料良莠不齐,而且新版本可能和老板的安装略有区别,博客中的安装方法不一定适合你。所以,一句话,参照官方文档进行安装。

开发语言为Java,开发工具为GeoTools和mongodb的Java Driver。

具体实现思想是:首先使用GeoTools读取shapefile文件,然后遍历每个feature,将feature转为GeoJSON的字符串。每个GeoJSON的字符串作为mongodb的collection中的一个document。

使用Maven构建项目工程,核心pom文件如下:

代码语言:javascript
复制
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>cn.tzy</groupId>
  <artifactId>mongodb</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>mongodb</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <geotools.version>15.1</geotools.version>
  </properties>

  <repositories>
      <repository>
          <id>repo2.maven.org</id>
          <name>maven2 repository</name>
          <url>http://repo2.maven.org/maven2/</url>
      </repository>
      <repository>
          <id>osgeo</id>
          <name>Open Source Geospatial Foundation Repository</name>
          <url>http://download.osgeo.org/webdav/geotools/</url>
      </repository>
    </repositories>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongodb-driver</artifactId>
        <version>3.3.0</version>
    </dependency>
    <dependency>
      <groupId>org.geotools</groupId>
      <artifactId>gt-shapefile</artifactId>
      <version>${geotools.version}</version>
    </dependency>
    <dependency>
      <groupId>org.geotools</groupId>
      <artifactId>gt-geojson</artifactId>
      <version>${geotools.version}</version>
    </dependency>
  </dependencies>

  <build>
        <plugins>
            <plugin>
                <inherited>true</inherited>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

具体代码如下(mongodb的API从3.x版本进行了比较大的变化,好些API标记为过时。下面代码是使用最新的API写的):

代码语言:javascript
复制
package cn.tzy.mongodb;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;

import org.bson.Document;
import org.geotools.data.shapefile.ShapefileDataStore;
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 = "world"; // 数据库名称
        final String COLLECTION_NAME = "continents"; // Collection名称
        final String SHAPE_FILE = "/home/zytan/data/continent.shp"; // ShapeFile全路径

        // 初始化mongodb
        MongoClient client = new MongoClient(IP_ADDRESS);
        MongoDatabase db = client.getDatabase(DB_NAME);
        db.createCollection(COLLECTION_NAME);
        MongoCollection<Document> coll = db.getCollection(COLLECTION_NAME);

        // 使用GeoTools读取ShapeFile文件
        File shapeFile = new File(SHAPE_FILE);
        ShapefileDataStore store = new ShapefileDataStore(shapeFile.toURI().toURL());
        SimpleFeatureSource sfSource = store.getFeatureSource();
        SimpleFeatureIterator sfIter = sfSource.getFeatures().features();
        // 从ShapeFile文件中遍历每一个Feature,然后将Feature转为GeoJSON字符串,最后将字符串插入到mongodb的Collection中
        while (sfIter.hasNext()) {
            SimpleFeature feature = (SimpleFeature) sfIter.next();
            // Feature转GeoJSON
            FeatureJSON fjson = new FeatureJSON();
            StringWriter writer = new StringWriter();
            fjson.writeFeature(feature, writer);
            String sjson = writer.toString();
            System.out.println(sjson);
            // 插入到Collection中
            Document doc = Document.parse(sjson);
            coll.insertOne(doc);
        }

        client.close(); // 关闭数据库连接
        System.out.println("数据导入完毕!");

    }
}

接下来,我们使用命令行进入mongodb建立索引:

db.countries.createIndex({"geometry":"2dsphere"}) 因为我们的数据是WGS84地理坐标系,所以我们使用2dsphere索引在geometry字段上建立索引。

下面是一个地理查询的示例: 查询在给定点的最大距离约束下的地理实体

代码语言:javascript
复制
db.continents.find( { geometry :
                         { $near :
                           { $geometry :
                              { type : "Point" ,
                                coordinates : [ 108 , 34 ] } ,
                             $maxDistance : 2000
                      } } } )
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016年09月17日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 MongoDB
腾讯云数据库 MongoDB(TencentDB for MongoDB)是腾讯云基于全球广受欢迎的 MongoDB 打造的高性能 NoSQL 数据库,100%完全兼容 MongoDB 协议,支持跨文档事务,提供稳定丰富的监控管理,弹性可扩展、自动容灾,适用于文档型数据库场景,您无需自建灾备体系及控制管理系统。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档