Я использую геоинструменты в Java, чтобы получить кратчайший путь от узла к узлу 1.
Ниже мой код Java для извлечения пути:
AStarShortestPathFinder finder = new AStarShortestPathFinder(g, node, node1, null);
Path path = finder.getPath();
Я попытался окружить «WrongPathException» попытаться перехватить в приведенной выше строке кода, но сообщение об ошибке «Тип org.geotools.graph.path.WrongPathException не отображается ".
Кто-нибудь сталкивался с этой ошибкой раньше? Есть ли способ обхода?
Я использовал maven, а вот мой pom. xml. Я впервые использую 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.geotools</groupId>
<artifactId>tutorial</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>tutorial</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<geotools.version>24-SNAPSHOT</geotools.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-shapefile</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-swing</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-graph</artifactId>
<version>${geotools.version}</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>osgeo</id>
<name>OSGeo 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-snapshot</id>
<name>OSGeo Snapshot Repository</name>
<url>https://repo.osgeo.org/repository/snapshot/</url>
<snapshots><enabled>true</enabled></snapshots>
<releases><enabled>false</enabled></releases>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
Ниже мой код, я также впервые использую геоинструменты, поэтому я все еще тестирую код.
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.geotools.data.FeatureSource;
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.feature.FeatureCollection;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.graph.build.feature.FeatureGraphGenerator;
import org.geotools.graph.build.line.LineStringGraphGenerator;
import org.geotools.graph.path.AStarShortestPathFinder;
import org.geotools.graph.path.Path;
import org.geotools.graph.path.WrongPathException; //ERROR IN THIS LINE
import org.geotools.graph.structure.Graph;
import org.locationtech.jts.geom.Coordinate;
import org.opengis.feature.Feature;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.GeometryFactory;
import org.geotools.graph.structure.Node;
import org.geotools.graph.structure.line.BasicDirectedXYNode;
import org.geotools.graph.traverse.standard.AStarIterator;
public class App2 {
public static void main(String[] args) {
//Import shapefile
File shapeFile = new File("shape.shp");
//import all features in the shape.shp into FeatureCollection
FeatureCollection fCollection = null;
//.....other code to import the shapefile to FeatureCollection.......
Graph g = featureGen.getGraph();
//Using AStarShortestPathFinder to find the Shortest Path between node and node1 with the graph from the Shapefile
AStarShortestPathFinder finder = new AStarShortestPathFinder(g, node, node1, null);
Path path = finder.getPath();
}
}