Java使用Geotools读取shape矢量数据
作为GIS开发者而言,矢量数据是我们经常要用到的,而shape数据是矢量数据中最常用的格式,因此解析shape数据也是作为GIS软件开发人员必备的基础技能,而GeoTools无疑是Java最好用来处理GIS数据的三方库,当然这只是GeoTools的冰山一角,后面我也会慢慢的去分享GeoTools的更多用法。
今天就简单来梳理下shape数据的解析获取,环境是maven工程,首先是maven依赖:

<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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.dudu.liuyang</groupId>
<artifactId>gis</artifactId>
<version>0.0.1-SNAPSHOT</version> <repositories>
<repository>
<id>central</id>
<name>aliyun maven</name>
<url>https://maven.aliyun.com/repository/central</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository> <repository>
<id>osgeo-releases</id>
<name>OSGeo Nexus Release Repository</name>
<url>https://repo.osgeo.org/repository/release/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository> <repository>
<id>osgeo-snapshots</id>
<name>OSGeo Nexus Snapshot Repository</name>
<url>https://repo.osgeo.org/repository/snapshot/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository> <repository>
<id>geosolutions</id>
<name>geosolutions repository</name>
<url>https://maven.geo-solutions.it/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories> <dependencies>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-shapefile</artifactId>
<version>25.2</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.2.8.RELEASE</version>
</plugin>
</plugins>
</build> </project>
下面就是简单的代码操作,其实如果只是简单的读取,就很简单,下面是主要代码。

package gis; import java.io.File;
import java.io.IOException;
import java.util.List; import org.geotools.data.FeatureSource;
import org.geotools.data.Query;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.type.AttributeDescriptor;
import org.opengis.feature.type.GeometryType;
import org.opengis.referencing.crs.CoordinateReferenceSystem; /**
* shape文件读取
* @author ly
*
*/
public class ShapeFileReaderTest { private static final String FILE_PATH = "F:\\data\\vector\\shape\\line\\xian_sheng.shp"; public static void main(String[] args) {
File file = new File(FILE_PATH);
readShapeFile(file);
} /**
*
* @param shpFile 传递的是shape文件中的.shp文件
*/
private static void readShapeFile(File shpFile) {
/**
* 直接使用shapefileDatastore,如果不知道,也可以使用工厂模式(见下个方法)
* 建议,如果确定是shape文件,就直使用shapefileDatastore
*/
try {
ShapefileDataStore shapefileDataStore = new ShapefileDataStore(shpFile.toURI().toURL());
//这个typeNamae不传递,默认是文件名称
FeatureSource featuresource = shapefileDataStore.getFeatureSource(shapefileDataStore.getTypeNames()[0]);
//读取bbox
ReferencedEnvelope bbox =featuresource.getBounds();
//读取投影
CoordinateReferenceSystem crs = featuresource.getSchema().getCoordinateReferenceSystem();
//特征总数
int count = featuresource.getCount(Query.ALL);
//获取当前数据的geometry类型(点、线、面)
GeometryType geometryType = featuresource.getSchema().getGeometryDescriptor().getType();
//读取要素
SimpleFeatureCollection simpleFeatureCollection = (SimpleFeatureCollection) featuresource.getFeatures();
//获取当前矢量数据有哪些属性字段值
List<AttributeDescriptor> attributes = simpleFeatureCollection.getSchema().getAttributeDescriptors();
//
SimpleFeatureIterator simpleFeatureIterator = simpleFeatureCollection.features();
//
while(simpleFeatureIterator.hasNext()) {
SimpleFeature simpleFeature = simpleFeatureIterator.next();
attributes.stream().forEach((a) -> {
//依次读取这个shape中每一个属性值,当然这个属性值,可以处理其它业务
System.out.println(simpleFeature.getAttribute(a.getLocalName()));
});
}
} catch (IOException e) {
e.printStackTrace();
} } }
当然GeoTools对于shape数据的操作非常多,除了简单的读取,还有高级的过滤查询,当然这个就需要借助ECSQL(其实在我看来就是sql语句的条件查询,只是用一种标准把它定义了出来),还有就是对矢量数据的增删改查,这些我都会在后期逐个分享。
Java使用Geotools读取shape矢量数据的更多相关文章
- JAVA用geotools读取shape格式文件
Shapefile属于一种矢量图形格式,它能够保存几何图形的位置及相关属性.但这种格式没法存储地理数据的拓扑信息. 其中,要组成一个Shapefile,有三个文件是必不可少的,它们分别是". ...
- JAVA用geotools读写shape格式文件
转自:http://toplchx.iteye.com/blog/1335007 JAVA用geotools读写shape格式文件 (对应geotools版本:2.7.2) (后面添加对应geotoo ...
- GeoJson的生成与解析,JSON解析,Java读写geojson,geotools读取shp文件,Geotools中Geometry对象与GeoJson的相互转换
GeoJson的生成与解析 一.wkt格式的geometry转成json格式 二.json格式转wkt格式 三.json格式的数据进行解析 四.Java读写geojson 五.geotools读取sh ...
- World Wind Java开发之六——解析shape文件(转)
http://blog.csdn.net/giser_whu/article/details/41647117 最近一直忙于导师项目的事情了,几天没更新了,昨天和今天研究了下WWJ解析shp文件的源代 ...
- [html5+java]文件异步读取及上传核心代码
html5+java 文件异步读取及上传关键代码段 功能: 1.多文件文件拖拽上传,file input 多文件选择 2.html5 File Api 异步FormData,blob上传,图片显示 3 ...
- java使用poi读取ppt文件和poi读取excel、word示例
java使用poi读取ppt文件和poi读取excel.word示例 http://www.jb51.net/article/48092.htm
- JAVA使用POI读取EXCEL文件的简单model
一.JAVA使用POI读取EXCEL文件的简单model 1.所需要的jar commons-codec-1.10.jarcommons-logging-1.2.jarjunit-4.12.jarlo ...
- 关于java.util.Properties读取中文乱码的正确解决方案(不要再用native2ascii.exe了)
从Spring框架流行后,几乎根本不用自己写解析配置文件的代码了,但近日一个基础项目(实在是太基础,不能用硕大繁琐的Spring), 碰到了用java.util.Properties读取中文内容(UT ...
- silverlight用Encoding.UTF8读取shape文件的中文属性值 出现乱码
最近用Silverlight读取shape文件,读出的属性居然是乱码. 原因是:Silverlight不支持GB2312. 解决方案: 下载该地址的代码http://encoding4silverli ...
随机推荐
- 异步回调实现- Guava Retryer
为什么要使用重试利器Retryer 在实际开发中我们经常会遇到需要轮询查询一个接果,实现轮询的方式有很多种,我们经常要写许多代码,有时还会怕写出的代码有bug,如果已经有轮子了,我们就没必要重复造轮子 ...
- TCP三次握手和四次挥手【转】
一. TCP/IP协议族 TCP/IP是一个协议族,通常分不同层次进行开发,每个层次负责不同的通信功能.包含以下四个层次: 1. 链路层,也称作数据链路层或者网络接口层,通常包括操作系统中的设备驱动程 ...
- Lvs+Keepalived+MySQL Cluster架设高可用负载均衡Mysql集群
------------------------------------- 一.前言 二.MySQL Cluster基本概念 三.环境 四.配置 1.LB-Master及LB-Backup配置 2.M ...
- KubeSphere单节点(all-in-one)平台搭建记录
KubeSphere单节点(all-in-one)平台搭建记录 目录 KubeSphere单节点(all-in-one)平台搭建记录 一.主机准备 1.1 主机配置 1.2 环境准备 二.下载kube ...
- linux_3
1.统计出/etc/passwd文件中其默认shell为非/sbin/nologin的用户个数,并将用户都显示出来 [root@lhq ~]#echo "total:`cat /etc/pa ...
- Java数据库连接池--DBCP浅析.
一. 为何要使用数据库连接池假设网站一天有很大的访问量,数据库服务器就需要为每次连接创建一次数据库连接,极大的浪费数据库的资源,并且极易造成数据库服务器内存溢出.拓机.数据库连接是一种关键的有限的昂贵 ...
- 小程序入门心得(不谈api)
小程序入门 一.准备 首先先去微信公众平台注册一个小程序账号,去拿到一个AppID(没AppID也可以开发,只是有些功能会受限),注册成功后到开发设置获取自己的AppID,即使有AppID有些功能还是 ...
- 从零开始, 开发一个 Web Office 套件(4):新的问题—— z-index
<从零开始, 开发一个 Web Office 套件>系列博客目录 这是一个系列博客, 最终目的是要做一个基于HTML Canvas 的, 类似于微软 Office 的 Web Office ...
- Vue 源码解读(2)—— Vue 初始化过程
当学习成为了习惯,知识也就变成了常识. 感谢各位的 点赞.收藏和评论. 新视频和文章会第一时间在微信公众号发送,欢迎关注:李永宁lyn 文章已收录到 github 仓库 liyongning/blog ...
- Hyperledger Fabric 2.x Java区块链应用
一.说明 在上一篇文章中 <Hyperledger Fabric 2.x 自定义智能合约> 分享了智能合约的安装并使用 cli 客户端进行合约的调用:本文将使用 Java 代码基于 fab ...