java - 从 kml 获取坐标时出错
问题描述
我试图从 KML 文件中获取一些坐标,代码没有给我任何错误,但在运行时我得到了这个:
Exception in thread "main" java.lang.ClassCastException: class de.micromata.opengis.kml.v_2_2_0.Document cannot be cast to class de.micromata.opengis.kml.v_2_2_0.Placemark (de.micromata.opengis.kml.v_2_2_0.Document and de.micromata.opengis.kml.v_2_2_0.Placemark are in unnamed module of loader 'app')
at user.it.User_Ex.Open_File.open(Open_File.java:18)
at user.it.User_Ex.UserExApplication.main(UserExApplication.java:13)
这是我的代码:
public class Open_File{
public void open(){
System.out.println("This is KML test");
Kml kml = Kml.unmarshal(new File("C:\\Users\\USER\\Desktop\\Sab\\zone_monopattini\\Fisciano.kml"));
Feature feature = kml.getFeature();
parseFeature(feature);
Placemark placemark = (Placemark) kml.getFeature();
Polygon geom = (Polygon) placemark.getGeometry();
LinearRing linearRing = geom.getOuterBoundaryIs().getLinearRing();
List<Coordinate> coordinates = linearRing.getCoordinates();
for (Coordinate coordinate : coordinates) {
System.out.println(coordinate.getLatitude());
System.out.println(coordinate.getLongitude());
System.out.println(coordinate.getAltitude());
}
}
private static void parseFeature(Feature feature) {
if(feature != null) {
if(feature instanceof Document) {
Document document = (Document) feature;
List<Feature> featureList = document.getFeature();
for(Feature documentFeature : featureList) {
if(documentFeature instanceof Placemark) {
Placemark placemark = (Placemark) documentFeature;
Geometry geometry = placemark.getGeometry();
parseGeometry(geometry);
}
}
}
}
}
private static void parseGeometry(Geometry geometry) {
if(geometry != null) {
if(geometry instanceof Polygon) {
Polygon polygon = (Polygon) geometry;
Boundary outerBoundaryIs = polygon.getOuterBoundaryIs();
if(outerBoundaryIs != null) {
LinearRing linearRing = outerBoundaryIs.getLinearRing();
if(linearRing != null) {
List<Coordinate> coordinates = linearRing.getCoordinates();
if(coordinates != null) {
for(Coordinate coordinate : coordinates) {
parseCoordinate(coordinate);
}
}
}
}
}
}
}
private static void parseCoordinate(Coordinate coordinate) {
if(coordinate != null) {
System.out.println("Longitude: " + coordinate.getLongitude());
System.out.println("Latitude : " + coordinate.getLatitude());
System.out.println("Altitude : " + coordinate.getAltitude());
System.out.println("");
}
}
}
这是我的 KML:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<name>Fisciano</name>
<description/>
<Style id="icon-1899-0288D1-nodesc-normal">
<IconStyle>
<color>ffd18802</color>
<scale>1</scale>
<Icon>
<href>https://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png</href>
</Icon>
<hotSpot x="32" xunits="pixels" y="64" yunits="insetPixels"/>
</IconStyle>
<LabelStyle>
<scale>0</scale>
</LabelStyle>
<BalloonStyle>
<text><![CDATA[<h3>$[name]</h3>]]></text>
</BalloonStyle>
</Style>
<Style id="icon-1899-0288D1-nodesc-highlight">
<IconStyle>
<color>ffd18802</color>
<scale>1</scale>
<Icon>
<href>https://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png</href>
</Icon>
<hotSpot x="32" xunits="pixels" y="64" yunits="insetPixels"/>
</IconStyle>
<LabelStyle>
<scale>1</scale>
</LabelStyle>
<BalloonStyle>
<text><![CDATA[<h3>$[name]</h3>]]></text>
</BalloonStyle>
</Style>
<StyleMap id="icon-1899-0288D1-nodesc">
<Pair>
<key>normal</key>
<styleUrl>#icon-1899-0288D1-nodesc-normal</styleUrl>
</Pair>
<Pair>
<key>highlight</key>
<styleUrl>#icon-1899-0288D1-nodesc-highlight</styleUrl>
</Pair>
</StyleMap>
<Style id="icon-1899-DB4436-nodesc-normal">
<IconStyle>
<color>ff3644db</color>
<scale>1</scale>
<Icon>
<href>https://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png</href>
</Icon>
<hotSpot x="32" xunits="pixels" y="64" yunits="insetPixels"/>
</IconStyle>
<LabelStyle>
<scale>0</scale>
</LabelStyle>
<BalloonStyle>
<text><![CDATA[<h3>$[name]</h3>]]></text>
</BalloonStyle>
</Style>
<Style id="icon-1899-DB4436-nodesc-highlight">
<IconStyle>
<color>ff3644db</color>
<scale>1</scale>
<Icon>
<href>https://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png</href>
</Icon>
<hotSpot x="32" xunits="pixels" y="64" yunits="insetPixels"/>
</IconStyle>
<LabelStyle>
<scale>1</scale>
</LabelStyle>
<BalloonStyle>
<text><![CDATA[<h3>$[name]</h3>]]></text>
</BalloonStyle>
</Style>
<StyleMap id="icon-1899-DB4436-nodesc">
<Pair>
<key>normal</key>
<styleUrl>#icon-1899-DB4436-nodesc-normal</styleUrl>
</Pair>
<Pair>
<key>highlight</key>
<styleUrl>#icon-1899-DB4436-nodesc-highlight</styleUrl>
</Pair>
</StyleMap>
<Style id="line-1267FF-5000-nodesc-normal">
<LineStyle>
<color>ffff6712</color>
<width>5</width>
</LineStyle>
<BalloonStyle>
<text><![CDATA[<h3>$[name]</h3>]]></text>
</BalloonStyle>
</Style>
<Style id="line-1267FF-5000-nodesc-highlight">
<LineStyle>
<color>ffff6712</color>
<width>7.5</width>
</LineStyle>
<BalloonStyle>
<text><![CDATA[<h3>$[name]</h3>]]></text>
</BalloonStyle>
</Style>
<StyleMap id="line-1267FF-5000-nodesc">
<Pair>
<key>normal</key>
<styleUrl>#line-1267FF-5000-nodesc-normal</styleUrl>
</Pair>
<Pair>
<key>highlight</key>
<styleUrl>#line-1267FF-5000-nodesc-highlight</styleUrl>
</Pair>
</StyleMap>
<Folder>
<name>Fisciano</name>
<Placemark>
<name>Fisciano</name>
<styleUrl>#icon-1899-0288D1-nodesc</styleUrl>
<Point>
<coordinates>
14.796561,40.773194,0
</coordinates>
</Point>
</Placemark>
</Folder>
<Folder>
<name>Percorso 1 - Terminal - Casa</name>
<Placemark>
<name>Percorso 1 - Terminal - Casa</name>
<styleUrl>#line-1267FF-5000-nodesc</styleUrl>
<LineString>
<tessellate>1</tessellate>
<coordinates>
14.79184,40.77456,0
14.79175,40.77461,0
14.79174,40.77461,0
14.79164,40.77467,0
14.79147,40.77476,0
14.79152,40.77495,0
14.7916,40.77531,0
14.79164,40.77543,0
14.79169,40.77566,0
14.79173,40.77584,0
14.79174,40.77586,0
14.79176,40.77591,0
14.79177,40.77592,0
14.79179,40.77593,0
14.79188,40.77595,0
14.79209,40.77598,0
14.79259,40.77601,0
14.79275,40.77602,0
14.79283,40.77602,0
14.79306,40.77602,0
14.79328,40.77602,0
14.79352,40.77601,0
14.79365,40.776,0
14.79377,40.77598,0
14.79387,40.77597,0
14.794,40.77594,0
14.79422,40.77588,0
14.79444,40.77581,0
14.7953,40.77554,0
14.79563,40.77539,0
14.79578,40.7753,0
14.79587,40.77523,0
14.79589,40.77521,0
14.79593,40.77517,0
14.79635,40.7747,0
14.79653,40.77467,0
14.79684,40.77461,0
14.79707,40.77457,0
14.7973,40.77453,0
14.79753,40.77449,0
14.79755,40.77448,0
14.79771,40.77444,0
14.79773,40.77443,0
14.79775,40.77442,0
14.79776,40.77442,0
14.79781,40.77436,0
14.79785,40.77432,0
14.79787,40.77427,0
14.79788,40.7742,0
14.79788,40.77414,0
14.79787,40.77409,0
14.79785,40.77391,0
14.79782,40.77373,0
14.7978,40.77359,0
14.79748,40.7734,0
14.79743,40.77337,0
14.79742,40.7733,0
14.79735,40.77256,0
14.79735,40.77245,0
14.79746,40.77242,0
14.7975,40.77241,0
14.79752,40.77239,0
14.79752,40.77237,0
14.79753,40.77235,0
14.79754,40.77231,0
14.79758,40.77222,0
14.7976,40.77221,0
14.79761,40.7722,0
14.79762,40.7722,0
14.79764,40.7722,0
14.7977,40.77219,0
14.79791,40.77215,0
14.79795,40.77215,0
14.79815,40.77212,0
14.79816,40.77212,0
14.79821,40.77211,0
14.79824,40.7721,0
14.79825,40.77208,0
14.79825,40.77207,0
14.79826,40.77205,0
14.79828,40.7716,0
14.79834,40.7714,0
14.79834,40.77137,0
14.79835,40.77136,0
14.79836,40.77134,0
14.79837,40.77134,0
14.79837,40.77133,0
14.79847,40.77129,0
14.7986,40.77125,0
14.79863,40.77123,0
14.79865,40.77122,0
14.79867,40.77121,0
14.79868,40.7712,0
14.79868,40.77118,0
14.79868,40.77117,0
14.79869,40.771,0
14.79869,40.77089,0
14.7987,40.77086,0
14.79871,40.77084,0
14.79873,40.77083,0
14.79876,40.77082,0
14.79908,40.77077,0
14.7993,40.77072,0
14.79934,40.7704,0
14.79936,40.77022,0
14.80005,40.77033,0
14.80044,40.7704,0
14.80117,40.77058,0
14.80185,40.77069,0
14.80187,40.7707,0
14.8021,40.77074,0
14.80249,40.77078,0
14.80259,40.77089,0
14.80264,40.771,0
14.80267,40.77105,0
14.80271,40.77114,0
14.80277,40.7712,0
14.80281,40.77124,0
14.80298,40.7713,0
14.80313,40.77136,0
14.80324,40.77142,0
14.80335,40.77146,0
14.80344,40.77149,0
14.80351,40.7715,0
14.80364,40.77151,0
14.80403,40.77151,0
14.80428,40.77151,0
14.80444,40.77151,0
14.8054,40.77155,0
14.80561,40.77155,0
</coordinates>
</LineString>
</Placemark>
<Placemark>
<name>Fisciano-unisa, 84084 Fisciano SA, Italia</name>
<styleUrl>#icon-1899-DB4436-nodesc</styleUrl>
<Point>
<coordinates>
14.791835,40.7745627,0
</coordinates>
</Point>
</Placemark>
<Placemark>
<name>Via della Rimembranza, 45, 84084 Fisciano SA, Italia</name>
<styleUrl>#icon-1899-DB4436-nodesc</styleUrl>
<Point>
<coordinates>
14.8056062,40.7715473,0
</coordinates>
</Point>
</Placemark>
</Folder>
<Folder>
<name>Percorso 2 - Terminal - Biblioteca</name>
<Placemark>
<name>Percorso 2 - Terminal - Biblioteca</name>
<styleUrl>#line-1267FF-5000-nodesc</styleUrl>
<LineString>
<tessellate>1</tessellate>
<coordinates>
14.79109,40.77498,0
14.7912,40.77493,0
14.79124,40.7749,0
14.79147,40.77476,0
14.79164,40.77467,0
14.79144,40.77455,0
14.79125,40.77444,0
14.79118,40.7744,0
14.79109,40.77437,0
14.79104,40.77436,0
14.79102,40.77437,0
14.791,40.77437,0
14.79053,40.77455,0
14.79047,40.77449,0
14.7904,40.7744,0
14.7903,40.77426,0
14.79029,40.77424,0
14.79008,40.77392,0
14.79001,40.77382,0
14.78996,40.77374,0
14.7898,40.77352,0
14.78979,40.77351,0
14.7898,40.77349,0
14.79001,40.7732,0
14.7901,40.77324,0
14.79012,40.77324,0
14.79014,40.77323,0
14.7903,40.77301,0
14.79048,40.77275,0
14.79057,40.77264,0
14.79057,40.77262,0
14.79057,40.77261,0
14.79055,40.7726,0
14.7905,40.77258,0
14.79046,40.77256,0
14.79044,40.77254,0
14.7904,40.77251,0
14.79004,40.77236,0
14.78994,40.77232,0
14.78981,40.77226,0
14.78961,40.77217,0
14.7905,40.77094,0
14.79085,40.77045,0
14.79075,40.77042,0
14.79064,40.77038,0
14.79057,40.77036,0
14.79053,40.77034,0
14.79032,40.77026,0
14.79015,40.77018,0
14.79026,40.77017,0
14.79032,40.77015,0
14.79037,40.77014,0
14.79043,40.77011,0
14.7905,40.77006,0
14.79056,40.77,0
14.7906,40.76992,0
14.79064,40.76985,0
14.79064,40.7698,0
14.79065,40.76975,0
14.79064,40.76969,0
14.79111,40.76962,0
14.79116,40.76962,0
14.79137,40.76934,0
14.79149,40.76918,0
14.7916,40.76902,0
14.79163,40.76898,0
</coordinates>
</LineString>
</Placemark>
<Placemark>
<name>Via delle Stelle, 2, 84084 Fisciano SA, Italia</name>
<styleUrl>#icon-1899-DB4436-nodesc</styleUrl>
<Point>
<coordinates>
14.7910921,40.7749781,0
</coordinates>
</Point>
</Placemark>
<Placemark>
<name>Rettorato, 84084 Fisciano SA, Italia</name>
<styleUrl>#icon-1899-DB4436-nodesc</styleUrl>
<Point>
<coordinates>
14.7916337,40.7689811,0
</coordinates>
</Point>
</Placemark>
</Folder>
</Document>
</kml>
这是我的 pom.xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>user.it</groupId>
<artifactId>User_Ex</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>User_Ex</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
在构建路径中,我有这些用于使用 KML:
- JavaApiforKml-2.2.1.jar
- jax-b-2.3.0.jar
我该如何解决?或者我可以用另一种方法代替这个方法吗?
解决方案
推荐阅读
- sql - 如何从 Eloquent Laravel 的组合框中获取一到三个值?
- mod-rewrite - 与 mod_rewrite 斗争
- spring-boot - 从 Spring Security OAuth2 迁移到 Keycloak 时,如何解决“permitAll 仅适用于 HttpSecurity.authorizeRequests()”?
- python - 根据先前的值对 pandas 数据框中的值进行分类
- hyperledger - Hyperledger Indy:什么是受托人和管家?
- three.js - 在增强现实中的应用
- python - SyntaxError:可迭代解包不能用于理解。问题是什么?
- django - 如何仅限制在 Django 频道中显示新消息框
- java - 在我的 Java 程序中读取第二个用户输入不起作用
- spring - NoSuchBeanDefinitionException - SpringBoot 自动装配构造函数包含具有未生成的自动装配声明的参数