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");
}
}

读取数据显示:

JAVA用geotools读取shape格式文件的更多相关文章

  1. JAVA用geotools读写shape格式文件

    转自:http://toplchx.iteye.com/blog/1335007 JAVA用geotools读写shape格式文件 (对应geotools版本:2.7.2) (后面添加对应geotoo ...

  2. Java使用Geotools读取shape矢量数据

    作为GIS开发者而言,矢量数据是我们经常要用到的,而shape数据是矢量数据中最常用的格式,因此解析shape数据也是作为GIS软件开发人员必备的基础技能,而GeoTools无疑是Java最好用来处理 ...

  3. 使用jsp读取TXT格式文件

    <%@page import="java.io.BufferedReader"%> <%@page import="java.io.FileReader ...

  4. 读取siftgeo格式文件的matlab程序

    % This function reads a siftgeo binary file %读取siftgeo格式的二进制文件 % % Usage: [v, meta] = siftgeo_read ( ...

  5. java通过CLASSPATH读取包内文件

    读取包内文件,使用的路径一定是相对的classpath路径,比如a,位于包内,此时可以创建读取a的字节流:InputStream in = ReadFile.class.getResourceAsSt ...

  6. SHELL读取 ini 格式文件做配置文件

    ini文件格式一般都是由节.键.值三部分组成 格式: [第一节 ] 第一个键 = 值 第二个键 = 第二个值 [第二节 ] 第一个键 = val1,val2,val3 例子: [COM] KINGGO ...

  7. Java读取UTF-8格式文件第一行出现乱码——问号“?”及解决 And Java读带有BOM的UTF-8文件乱码原因及解决方法

    測试样例: Java读取UTF-8的txt文件第一行出现乱码"?"及解决 test.txt文件内容: 1 00:00:06,000 --> 00:00:06,010 < ...

  8. [转]World Wind Java开发之五——读取本地shp文件

    World Wind Java 使用IconLayer图层类表现点和多点数据,使用RenderableLayer图层表现线和面数据,一个图层只能对应一组shape文件.World Wind Java首 ...

  9. World Wind Java开发之五——读取本地shp文件(转)

    http://blog.csdn.net/giser_whu/article/details/41484433 World Wind Java 使用IconLayer图层类表现点和多点数据,使用Ren ...

随机推荐

  1. SpringMVC 数据校验(JSR-303)

    项目中,通常使用较多的是前端的校验,比如页面中js校验以及form表单使用bootstrap校验.然而对于安全要求较高点建议在服务端进行校验. 服务端校验: 控制层controller:校验页面请求的 ...

  2. btrace简单使用

    基本安装 在github上btrace项目的release下 下载最新的btracehttps://github.com/btraceio/btrace/releases 解压完后,将btrace的b ...

  3. unittest(20)- 自动更新表格中的数据(3)

    通过函数获取表格中的手机号 # 1. get_data.py from tools import project_path import pandas as pd class GetData: Coo ...

  4. POJ 3249 Test for Job(拓扑排序+dp优化空间)

    Description Mr.Dog was fired by his company. In order to support his family, he must find a new job ...

  5. SSM框架三分钟搞定分页查询

    使用的国产第三方jar   pagehelper 里面的基本属性值 //当前页 private int pageNum; //每页的数量 private int pageSize; //当前页的数量 ...

  6. 吴裕雄--天生自然KITTEN编程:画三角形

  7. java使用jackson生成和解析JSON

    java使用jackson生成和解析JSON 1.导包 2.生成json和解析json package test; import com.fasterxml.jackson.core.JsonProc ...

  8. 前端学习之路CSS基础学习二

    CSS属性相关 样式操作: (1)width:为元素设置宽度 (2)height:为元素设置高度 ps:块儿级标签才能设置长宽行内标签设置长宽没有任何影响 p{ width: 30px; height ...

  9. Angular4——7.表单处理

    在Angular中存在两种表单处理方式: 模版驱动式表单 表单的数据模型是通过组件模版中的相关指令来定义的.由于使用这种方式定义表单的数据模型时,我们会受限于HTML的语法,所以,模版驱动方式只适用于 ...

  10. 伪元素 before 和 after 初探

    伪元素 before 和 after 初探 使用了 CodePen 做演示,欢迎点击预览 定义 首先来看 MDN 的定义: ::before 创建一个伪元素,作为已选中元素的第一个子元素,常通过 co ...