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");
}
}
读取数据显示:


JAVA用geotools读取shape格式文件的更多相关文章
- JAVA用geotools读写shape格式文件
转自:http://toplchx.iteye.com/blog/1335007 JAVA用geotools读写shape格式文件 (对应geotools版本:2.7.2) (后面添加对应geotoo ...
- Java使用Geotools读取shape矢量数据
作为GIS开发者而言,矢量数据是我们经常要用到的,而shape数据是矢量数据中最常用的格式,因此解析shape数据也是作为GIS软件开发人员必备的基础技能,而GeoTools无疑是Java最好用来处理 ...
- 使用jsp读取TXT格式文件
<%@page import="java.io.BufferedReader"%> <%@page import="java.io.FileReader ...
- 读取siftgeo格式文件的matlab程序
% This function reads a siftgeo binary file %读取siftgeo格式的二进制文件 % % Usage: [v, meta] = siftgeo_read ( ...
- java通过CLASSPATH读取包内文件
读取包内文件,使用的路径一定是相对的classpath路径,比如a,位于包内,此时可以创建读取a的字节流:InputStream in = ReadFile.class.getResourceAsSt ...
- SHELL读取 ini 格式文件做配置文件
ini文件格式一般都是由节.键.值三部分组成 格式: [第一节 ] 第一个键 = 值 第二个键 = 第二个值 [第二节 ] 第一个键 = val1,val2,val3 例子: [COM] KINGGO ...
- Java读取UTF-8格式文件第一行出现乱码——问号“?”及解决 And Java读带有BOM的UTF-8文件乱码原因及解决方法
測试样例: Java读取UTF-8的txt文件第一行出现乱码"?"及解决 test.txt文件内容: 1 00:00:06,000 --> 00:00:06,010 < ...
- [转]World Wind Java开发之五——读取本地shp文件
World Wind Java 使用IconLayer图层类表现点和多点数据,使用RenderableLayer图层表现线和面数据,一个图层只能对应一组shape文件.World Wind Java首 ...
- World Wind Java开发之五——读取本地shp文件(转)
http://blog.csdn.net/giser_whu/article/details/41484433 World Wind Java 使用IconLayer图层类表现点和多点数据,使用Ren ...
随机推荐
- MySQL rand(随机数)、floor(保留整数)、char(ASCII 转字符)、concat(字符串连接)
一.MySQL的rand()函数 select rand(); rand()函数,随机0-1之间的数. 二.获得0-10之间的整数(包含0,不包含10) ; 其中floor()去掉小数. 三.获得指定 ...
- flutter实践 - plsy
项目背景 项目需要从钉钉微应用跳转 WPS 打开 word 文档,但是 WPS 只提供了 StartActivity 方式携带参数跳转应用,deeplink 只能打开应用,而钉钉微应用只支持 deep ...
- Linux设置redis密码登录
第一种:永久方式 redis设置密码访问 你的redis在真是环境中不可以谁想访问就可以访问,所以必须要设置密码 设置密码的流程如下: vim /etc/redis.conf 找到 #require ...
- Proto3:风格
本文介绍.proto文件的编码风格.遵循下面的惯例,可以使你的protocol buffer消息定义和它们对应的类连贯且已读. 注意,protocol buffer风格随时间变化一直在进步,所以可能你 ...
- OpenSSL编程之摘要
说明: 数字摘要是将任意长度的消息变成固定长度的短消息,它类似于一个自变量是消息的函数,也就是Hash函数.数字摘要就是采用单向Hash函数将需要加密的明文“摘要”成一串固定长度(128位)的密文这一 ...
- zookeeper 实战 - Pymjer 的博客
下载 $ wget http://apache.forsale.plus/zookeeper/zookeeper-3.4.9/zookeeper-3.4.9.tar.gz 配置conf/zoo.cfg ...
- 【Android TimeCat】 解决cannot resolve symbol R
莫名其妙出现了,鬼知道怎么来的. 解决方法总结 1. 推荐 解决90%的情况: Build->Clean ProjectBuild->Rebuild Project 2. 不常见 Andr ...
- 5种方法获取url中文件的扩展名
/** * strrchr - 查找指定字符在字符串中的最后一次出现 * strrpos — 计算指定字符串在目标字符串中最后一次出现的位置 * end — 将数组的内部指针指向最后一个单元 * pa ...
- clearstatcache清除文件状态缓存
当使用下列任何函数时stat(),lstat(),file_exists(),is_writable(),is_readable(),is_executable(),is_file(),is_dir( ...
- 对话|人工智能先驱Yoshua Bengio
Bengio"> 今年1月份,微软收购深度学习初创公司Maluuba时,Maluuba公司德高望重的顾问.深度学习先驱Yoshua Bengio也接手了微软的人工智能研究顾问 ...