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 ...
随机推荐
- Net6 DI源码分析Part2 Engine,ServiceProvider
ServiceProvider ServiceProvider是对IServiceProvider实现,它有一个internal的访问修饰符描述的构造,并需要两个参数IServiceCollectio ...
- Maven仓库的目录结构
_remote.repositories文件 本地库中的包都有一个_remote.repositories文件,示例: #NOTE: This is an Aether internal implem ...
- JVM学习九-(复习)HotSpot 垃圾收集器
HotSpot 虚拟机提供了多种垃圾收集器,每种收集器都有各自的特点,虽然我们要对各个收集器进行比较,但并非为了挑选出一个最好的收集器.我们选择的只是对具体应用最合适的收集器. 新生代垃圾收集器 Se ...
- Media Player播放
转载请注明来源:https://www.cnblogs.com/hookjc/ <object id="player" height="64" width ...
- Activity有多个启动图标
(1)如果你想让你的Activity有多个启动图标 需要这样配置 <intent-filter> <action android:name="android.intent. ...
- 可能用得上的UI控件
为了便于开发者打造各式各样的优秀App,UIKit框架提供了非常多功能强大又易用的UI控件以下列举一些在开发中可能用得上的UI控件: 红色表明最常用,蓝色代表一般,黑色代表几乎不用(这不是绝对的, ...
- node.js中的fs.appendFile方法使用说明
方法说明: 该方法以异步的方式将 data 插入到文件里,如果文件不存在会自动创建.data可以是任意字符串或者缓存. 语法: 代码如下: fs.appendFile(filename, data, ...
- 数值分析:最小二乘与岭回归(Pytorch实现)
Chapter 4 1. 最小二乘和正规方程 1.1 最小二乘的两种视角 从数值计算视角看最小二乘法 我们在学习数值线性代数时,学习了当方程的解存在时,如何找到\(\textbf{A}\bm{x}=\ ...
- .NET6: 开发基于WPF的摩登三维工业软件 (2)
在<.NET6: 开发基于WPF的摩登三维工业软件 (1)>我们创建了一个"毛坯"界面,距离摩登还差一段距离.本文将对上一阶段的成果进行深化,实现当下流行的暗黑风格UI ...
- 通过shell脚本统计elasticsearch indices每天的数量以及大小
前情提要: 最近elasticsearch集群总出问题,之前虽然修复了,现在又出现新的问题,于是PM要求拉取elasticsearch每天建立的索引有多少,索引有多大,需要对机器进行评估 客户现场无法 ...