I am computing a shortest path with A* algorithm of GeoTools:

AStarShortestPathFinder pf = new AStarShortestPathFinder(
                                     myGraph, nodeFrom, nodeTo, myAstarfun);
Path p = pf.getPath();

How could I then retrieve the cost of the computed path p ?

With DijkstraShortestPathFinder, this can be obtained easily with a pf.getCost(nodeTo) but no similar method seems available with A*. I did not find information in the documentation to do that.

1 Answer 1


It appears that AStarNodes store the cost so far in their g field so something like:

cost = ((AStarNode) p.getLast()).getG();

should work or if you intend to use the whole path anyway you could do something like:

List<Edge> edges = p.getEdges();
double cost = 0;
for (Edge e : edges) {
  cost += myAstarfun.cost((AStarNode)e.getNodeA(),(AStarNode)e.getNodeB());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.