I need the ability to calculate walking distance between 2 points without calling an outside webservice like Google Maps. Google Maps provides an API called the Distance Matrix API, which is perfect for what I need to do, but it has limits and requires that I make a call to a web service. Fortunately, my longitude and latitude points will be limited to one city, so it is not as if I need to be able to query any 2 points in the world and calculate walking distance as Google Maps Distance Matrix API would provide.
I realize that this would probably require a lot of storage on the backend to maintain street routes and I don’t know if this is a totally crazy request, but if anyone knows of a way to do this or a library that provides similar functionality any help would be greatly appreciated!
You can get the data you’d need from http://www.openstreetmap.org in the form of .osm files.
These are xml files that contains the road you’ll need. You can read more about osm and on their wiki page.
If you only want distances and don’t care about the actual routes, that’ll make your task a lot easier.
Write a script to parse the .osm file. Extract the nodes and edges that are relevant, no highways, rivers, or anything else that can’t be walked on. Then take this data and put into a graph data structure of some sort.
From there, if you want the distance between two points, just run Dijkstra’s Algorithm. If you want to store all possible distances, just run Dijkstra once from each node in graph, storing the distances each time.
As you can see, a way can have more than 2 nodes within it. You’ll need to break up each way into sets of pairs of nodes, so this file would become something like this
Frm, To, Weight
111, 112, dist(111,112)
112, 111, “
112, 543, dist(112,543)
543, 112, “
…
You’d also have to deal with one way streets, and other complicating factors. Not all ways are roads, so you’d have to check that.
These files can also be very large, depending on the bounds of the region and how much stuff is in that particular region.
You’re absolutely correct about having to write a ton of custom code to scrape the data. I’ve done it myself.