How can I use osm2po to calculate distance matrix in Km? There is an example that calculates distance matrix in time but I cannot make it turn into kilometers. I dont understand how should I use the int[] path parameter into the calcPathLength function.

1 Answer 1


The answer is laid out by Carsten in this post and comments: How to speed up osm2po when using as java lib?

I used the same code from Carsten's answer and just modified the last for loop to look like this. Works like a charm.

for (int z = 0; z < n; z++) {
          int x = z + y;
            if (x >= n) x -= n;

            matrix[y][x] = -1;
            if (router.isVisited(vertexIds[x])) {
                int[] path=router.makePath(vertexIds[x]);
                matrix[y][x] = graph.calcPathLength(path);  

So instead of getting the cost to the vertexIds[x], what we do is get the path (which is just an array of all the vertices visited to reach vertexIds[x], and then get the length of that path from the calcPathLength() function. Note: the matrix called matrix must be declared as double.


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.