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 ...
随机推荐
- 支付宝同步请求检查appid,以及公钥,私钥是否正确
第一步:下载支付宝Demo 下载地址:https://opendocs.alipay.com/open/270/106291#%E8%BF%90%E8%A1%8C%E8%AF%B4%E6%98%8E ...
- jsp 九大内置对象和其作用以及四大域对象
感谢作者:Fangcf. 链接:https://blog.csdn.net/qq_39320833/article/details/80818442 一.jsp 九大内置对象 方法简单介绍:https ...
- svn使用规范、在Windows下使用svn命令行工具、svn命令行的解释
以前在公司一直使用git,现在公司有用svn,一时间还真的不知道如何下手,在网上搜寻了很多大神和官网文档的指导,总结了下面一份教程,希望能够帮助大家快速上手,如果想更细致的了解相关内容,可以点击每个小 ...
- python官网导航翻译
- Netty高级应用及聊天室实战
Netty 高级应用 1. 编解码器 概念:在网络应用中,需要实现某种编解码器.将原始字节数据与自定义消息数据进行相互转换.网络中都是以字节码的形式传输的. 对Netty而言,编解码器由两部分组成:编 ...
- MySQL基本数据类型与约束条件
昨日内容回顾 数据存储的演变 # 方向: 朝着更加统一和方便管理 数据库的发展史 # 由本地保存逐步演变为线上保存 数据库的本质 # 本质上就是一款CS架构的软件 """ ...
- c++ 指针数组与指向数组的指针
指针数组与指向数组的指针 1.int (*a)[10]-->指向数组的指针 a是一个二级指针,可认为是一个二维数组的首地址,指向一个一维数组,数组存储了10个int数据. int arr1[10 ...
- html特殊字符(css3 content)
由于偶尔用到,又经常忘记,所以把网上的资料考下来记录一下. <!DOCTYPE html> <html lang="en"> <head> &l ...
- 从Spring容器的角度理解Dubbo扩展点的加载时机
对于Dubbo提供的扩展点,主程序执行的过程中并没有显示调用加载的过程,无论是自激活的Filter还是自适应的ThreadPool.那么这样的扩展点在程序运行的哪个节点调用的呢?跟踪之前性能监控扩展点 ...
- 使用IDEA新建一个Spring Boot项目
本文使用Spring Initializer来创建 开发环境 操作系统:Windows 10 IDEA:2020.3.2 JDK:1.8 1. 启动IDEA,选择New Project(新建工程): ...