I am using geotools in Java to get the Shortest Path from node to node1.
Below is my Java code to retrieve the Path:
AStarShortestPathFinder finder = new AStarShortestPathFinder(g, node, node1, null);
Path path = finder.getPath();
I tried to surround the "WrongPathException" try catch in the above line of code, but error message "The type org.geotools.graph.path.WrongPathException is not visible".
Any one encountered this error before? Is there any workaround?
I have used maven and here is my pom.xml. It is my first time using maven, please let me know if there are any mistakes.
<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>
Below is my code, it is my first time using geotools as well, so I am still testing the code.
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();
}
}