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 ...
随机推荐
- react组件中的类调用construcor、super方法你知道多少?
constructor:在类中作为一个钩子函数,有constructor钩子函数的时候,可以定义state,如果用户不定义state的话,有无constructor钩子函数没啥区别: super:
- SharedPreferences介绍
sharedPreferences是通过xml文件来做数据存储的. 一般用来存放一些标记性的数据,一些设置信息. 使用sharedPreferences存储数据 ...
- 浮动float、浮动影响和清除浮动
普通流(normal flow) 这个单词很多人翻译为 文档流 , 字面翻译 普通流 或者标准流都可以. 前面我们说过,网页布局的核心,就是用CSS来摆放盒子位置.如何把盒子摆放到合适的位置? CSS ...
- ldconfig及LD_LIBRARY_PATH
ldconfig是一个动态链接库管理命令,为了让动态链接库为系统所共享,还需运行动态链接库的管理命令:ldconfig.ldconfig 命令的用途,主要是在默认搜寻目录 (/lib和/usr/lib ...
- Block基本概念
1.什么是Block Block是iOS中一种比较特殊的数据类型 Block是苹果官方特别推荐使用的数据类型, 应用场景比较广泛 动画 多线程 集合遍历 网络请求回调 Block的作用 用来保存某一段 ...
- MySQL--事件/定时器
原创:转载需注明原创地址 https://www.cnblogs.com/fanerwei222/p/11777311.html MySQL--事件/定时器 : 多个SQL的集合, 定时执行任务. 查 ...
- maven项目pom文件下载的包放在哪
右击项目configure bulid path ----->libraries--->maven dependencies 可以看到每个jar的存放路径
- push自定义动画
// // ViewController.m // ViewControllerAnimation // // Created by mac on 15/5/26. // Copyright ...
- 实现基于MYSQL验证的vsftpd虚拟用户访问
一.配置mysql服务器 1.1 安装mysql # yum -y install mariadb-server # systemctl enable --now mariadb.service &a ...
- Linux重定向输出到以当前时间命名的文件 / date命令格式化输出
1. 利用date命令重定向到以当前时间命名的文件 例如: ls -l > mylog_$(date +"%Y-%m-%d_%H-%M-%S").log 或: ls -l & ...