当前位置: 首页 > news >正文

java使用geotools解析矢量数据kml、geojson、shp文件

geotools解析kml、geojson

  • geotools环境准备
  • 公共获取属性方法
  • 解析kml
  • 解析geojson
  • 解析shp

geotools环境准备

这里使用的是maven引用geotools包,引用geotools包需要添加maven仓库,pom.xml文件如下:

<properties><!-- geotools版本 --><geotools-version>28.2</geotools-version>
</properties><!-- geotools-->
<dependency><groupId>org.geotools</groupId><artifactId>gt-main</artifactId><version>${geotools-version}</version>
</dependency>
<dependency><groupId>org.geotools</groupId><artifactId>gt-geojson</artifactId><version>${geotools-version}</version>
</dependency><!-- geotools-geojson核心包 -->
<dependency><groupId>org.geotools</groupId><artifactId>gt-geojson-core</artifactId><version>${geotools-version}</version>
</dependency>
<dependency><groupId>org.geotools.xsd</groupId><artifactId>gt-xsd-kml</artifactId><version>${geotools-version}</version>
</dependency><!-- geotools仓库-->
<repositories><repository><id>osgeo</id><name>Open Source Geospatial Foundation Repository</name><url>https://repo.osgeo.org/repository/release/</url></repository><repository><id>osgeo-snapshot</id><name>OSGeo Snapshot Repository</name><url>https://repo.osgeo.org/repository/snapshot/</url></repository>
</repositories>

公共获取属性方法

@Slf4j
public class AnalysisUtil {public static void readProperty(SimpleFeature simpleFeature) {Collection<Property> properties = simpleFeature.getProperties();Iterator<Property> iterator = properties.iterator();int index = 0;while (iterator.hasNext()) {Property property = iterator.next();//kml拿到属性值会有自带的9个属性if (index++ > 8) {log.info("GeoJSONReader解析geojson -->> 属性名:【{}】,属性值:【{}】,属性类型:【{}】", property.getName().toString(),property.getValue(), property.getType().getBinding());}}Object defaultGeometry = simpleFeature.getDefaultGeometry();//wkt格式geometryGeometry geometry = (Geometry) defaultGeometry;log.info("wkt格式geometry:{}", geometry);}
}

解析kml

KML(Keyhole Markup Language,Keyhole 标记语言)是一种基于XML 的标记语言,利用XML 语法格式描述地理空间数据(如点、线、面、多边形和模型等)。

public class AnalysisKml {public static void main(String[] args) {String fileUrl = "D:\\workspace\\vector\\vector\\KML.kml";try (FileInputStream fileInputStream = new FileInputStream(fileUrl)) {PullParser parser = new PullParser(new KMLConfiguration(), fileInputStream, SimpleFeature.class);SimpleFeature simpleFeature = (SimpleFeature) parser.parse();//kml文件声明了坐标系才能获取到,没声明获取就是nullCoordinateReferenceSystem coordinateReferenceSystem = simpleFeature.getFeatureType().getCoordinateReferenceSystem();log.info("解析kml获取坐标系:{}", coordinateReferenceSystem);readKml(simpleFeature, parser);} catch (XMLStreamException | IOException | SAXException e) {throw new UtilException(e.getMessage());}}//递归方式获取每个地块信息public static void readKml(SimpleFeature simpleFeature, PullParser parser) throws XMLStreamException, IOException,SAXException {//读取属性AnalysisUtil.readProperty(simpleFeature);//获取下一个simpleFeaturewhile (simpleFeature != null && simpleFeature.getDefaultGeometry() != null) {simpleFeature = (SimpleFeature) parser.parse();readKml(simpleFeature, parser);}}
}

解析geojson

GeoJSON 是一种用于编码各种地理数据结构的格式。
GeoJSON 支持以下几何类型:Point、LineString、Polygon、MultiPoint、MultiLineString 和 MultiPolygon。 具有附加属性的几何对象是特征对象。 要素集包含在FeatureCollection 对象中。

下面介绍2种解析geojson的方法:

  1. org.geotools.data.geojson.GeoJSONReader解析给geojson,但是它不会读取坐标系,所以不管什么坐标系的geojson文件读出来都是默认的坐标系WGS84.
  2. org.geotools.geojson.feature.FeatureJSON解析geojson可以读到文件对应的坐标系。
@Slf4j
public class GeoJsonAnalysis {public static void main(String[] args) {String fileUrl = "D:\\workspace\\vector\\vector\\福田路网geojson.geojson";geoJSONReader(fileUrl);featureJson(fileUrl);}public static void featureJson(String fileUrl) {// 指定GeometryJSON构造器,15位小数FeatureJSON featureJson = new FeatureJSON(new GeometryJSON(15));try {FeatureCollection featureCollection = featureJson.readFeatureCollection(new FileInputStream(fileUrl));//获取坐标系CoordinateReferenceSystem coordinateReferenceSystem =featureCollection.getSchema().getCoordinateReferenceSystem();log.info("解析geojson获取坐标系:{}", coordinateReferenceSystem);FeatureIterator featureIterator = featureCollection.features();while (featureIterator.hasNext()) {SimpleFeature simpleFeature = (SimpleFeature) featureIterator.next();AnalysisUtil.readProperty(simpleFeature);}} catch (IOException e) {e.printStackTrace();}}public static void geoJSONReader(String fileUrl) {GeoJSONReader reader = null;try {reader = new GeoJSONReader(new FileInputStream(fileUrl));SimpleFeatureCollection featureCollection = reader.getFeatures();//创建图层数据迭代器FeatureIterator<SimpleFeature> simpleFeatureFeatureIterator = featureCollection.features();while (simpleFeatureFeatureIterator.hasNext()) {AnalysisUtil.readProperty(simpleFeatureFeatureIterator.next());}} catch (IOException e) {e.printStackTrace();} finally {if (reader != null) {try {reader.close();} catch (IOException e) {e.printStackTrace();}}}}
}

解析shp

shp文件解析请看往期文章:geotoolsu解析shp文件

http://www.lryc.cn/news/226371.html

相关文章:

  • 原生 JS DOM 常用操作大全
  • 昇腾CANN 7.0 黑科技:DVPP硬件加速训练数据预处理,友好解决Host CPU预处理瓶颈
  • Aria2 任意文件写入漏洞复现
  • 思维模型 多看效应
  • 持续集成交付CICD:Jenkins Pipeline与远程构建触发器
  • 【无标题(PC+WAP)花卉租赁盆栽绿植类pbootcms站模板
  • pytorch 学习率衰减策略
  • Flink SQL -- 概述
  • Spring RabbitMQ那些事(1-交换机配置消息发送订阅实操)
  • C++动态库
  • 【教3妹学编程-算法题】2923. 找到冠军 I
  • 矢量图形编辑软件Boxy SVG mac中文版软件特点
  • 神经网络遗传算法函数极值寻优
  • 剑指JUC原理-16.读写锁
  • 文件改名:避免繁琐操作,利用筛选文件批量重命名技巧优化文件管理
  • 【CocoaPods安装环境和流程以及各种情况】
  • canvas与svg区别与实际应用
  • rasa train nlu详解:1.1-train_nlu()函数
  • 使用ResponseSelector实现校园招聘FAQ机器人
  • ENVI IDL:如何基于气象站点数据进行反距离权重插值?
  • 实战Leetcode(四)
  • C语言——个位数为 6 且能被 3 整除但不能被 5 整除的三位自然数共有多少个,分别是哪些?
  • 基于Docker容器DevOps应用方案
  • Apinto 网关进阶教程,使用 API Mock 生成模拟数据
  • 笔记:AI量化策略开发流程-基于BigQuant平台(一)
  • Spring Cloud 微服务入门篇
  • 使用Go语言搭建区块链基础
  • 手搓MyBatis框架(原理讲解)
  • FRC-EP系列--你的汽车数据一站式管家
  • 【ARM Trace32(劳特巴赫) 使用介绍 3 - trace32 访问运行时的内存】