zoukankan      html  css  js  c++  java
  • JAVA用geotools读取shape格式文件

    Shapefile属于一种矢量图形格式,它能够保存几何图形的位置及相关属性。但这种格式没法存储地理数据的拓扑信息。

    其中,要组成一个Shapefile,有三个文件是必不可少的,它们分别是".shp", ".shx"与 ".dbf"文件

    • .shp— 图形格式,用于保存元素的几何实体。

    • .shx— 图形索引格式。几何体位置索引,记录每一个几何体在shp文件之中的位置,能够加快向前或向后搜索一个几何体的效率。

    • .dbf— 属性数据格式,以dBase IV的数据表格式存储每个几何形状的属性数据。

    下面将介绍如何通过Java读取Shape文件中的内容信息

    我们的pom文件

    <?xml version="1.0" encoding="UTF-8"?>
    <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>com.herbert.geotool</groupId>
        <artifactId>geo</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <dependencies>
            <dependency>
                <groupId>org.geotools</groupId>
                <artifactId>gt-shapefile</artifactId>
                <version>19.2</version>
                <scope>system</scope>
            </dependency>
    
            <dependency>
                <groupId>org.geotools</groupId>
                <artifactId>gt-opengis</artifactId>
                <version>19.2</version>
            </dependency>
    
            <dependency>
                <groupId>org.geotools</groupId>
                <artifactId>gt-data</artifactId>
                <version>19.2</version>
            </dependency>
    
            <dependency>
                <groupId>org.geotools</groupId>
                <artifactId>gt-api</artifactId>
                <version>19.2</version>
            </dependency>
    
            <dependency>
                <groupId>org.geotools</groupId>
                <artifactId>gt-main</artifactId>
                <version>19.2</version>
            </dependency>
    
            <dependency>
                <groupId>org.geotools</groupId>
                <artifactId>gt-metadata</artifactId>
                <version>19.2</version>
            </dependency>
    
            <dependency>
                <groupId>org.geotools</groupId>
                <artifactId>gt-referencing</artifactId>
                <version>19.2</version>
            </dependency>
    
            <dependency>
                <groupId>org.geotools</groupId>
                <artifactId>gt-geojson</artifactId>
                <version>19.2</version>
            </dependency>
    
            <dependency>
                <groupId>org.json.simple</groupId>
                <artifactId>json-simple</artifactId>
                <version>1.1</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-pool</artifactId>
                <version>1.5.4</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang</artifactId>
                <version>2.6</version>
            </dependency>
    
            <dependency>
                <groupId>com.vividsolutions</groupId>
                <artifactId>jts</artifactId>
                <version>1.13</version>
            </dependency>
        </dependencies>
    
    </project>

    具体Java代码

    package com.herbert.geotoool.util;
    
    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 java.io.File;
    import java.io.IOException;
    import java.io.StringWriter;
    import java.nio.charset.Charset;
    
    /**
     * @author :Herbert
     * @date :Created in 2019/12/26 17:01
     * @description:
     * @modified By:
     * @version: $
     */
    
    public class ShapeModel {
    
    
        public static void main(String[] args) throws IOException {
            long start = System.currentTimeMillis();
    
            String SHAPE_FILE = "F:\MapData\gisMap\xian\街道界线.shp"; // ShapeFile全路径
    
            // 使用GeoTools读取ShapeFile文件
            File shapeFile = new File(SHAPE_FILE);
            ShapefileDataStore store = new ShapefileDataStore(shapeFile.toURI().toURL());
            //设置编码
            Charset charset = Charset.forName("GBK");
            store.setCharset(charset);
            SimpleFeatureSource sfSource = store.getFeatureSource();
            SimpleFeatureIterator sfIter = sfSource.getFeatures().features();
            // 从ShapeFile文件中遍历每一个Feature,然后将Feature转为GeoJSON字符串
            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=====  >>>>  "  + sjson);
            }
            System.out.println("数据导入完成,共耗时"+(System.currentTimeMillis() - start)+"ms");
        }
    }

    读取数据显示:

  • 相关阅读:
    HashTable, HashMap,TreeMap区别
    redis的多线程
    五种I/O模型介绍
    SpringBoot的优点
    spring注解
    三种方法求解两个数组的交集
    数据库七种传播行为
    BETA 版冲刺前准备
    Python学习笔记(二)--变量和数据类型
    事后诸葛亮
  • 原文地址:https://www.cnblogs.com/zhaixingzhu/p/12562689.html
Copyright © 2011-2022 走看看