Group Abstract Group Abstract

Message Boards Message Boards

Do All Roads Lead to Rome? The Answer is Surprisingly Beautiful

POSTED BY: Bernat Espigulé
13 Replies

enter image description here - another post of yours has been selected for the Staff Picks group, congratulations !

We are happy to see you at the top of the "Featured Contributor" board. Thank you for your wonderful contributions, and please keep them coming!

POSTED BY: EDITORIAL BOARD

Hi again,

one of the little problems when you want to do these travel directions within cities is that the documentation only shows how to calculate TravelDirections between cities and/or landmark buildings.museums etc. For house to house (or street to street) directions you need the exact geolocations of the start and endpoint.

This little function might come in handy:

location[str_String] :=
GeoPosition@(ToExpression[{StringSplit[#, {"<lat>", "</lat>"}][[2]], StringSplit[#, {"<lng>", "</lng>"}][[2]]}] & @
    StringJoin@Flatten@Import["https://maps.googleapis.com/maps/api/geocode/xml?address=" <> StringReplace[str, " " -> "+"]]) 

With it you can enter two addresses as strings like so:

loc1 = location["21 Union Street Aberdeen UK"]; 
loc2 = location["University of Aberdeen UK"];

Then you can -as always- use:

TravelDirections[{loc1, loc2}]["Dataset"]

to get:

enter image description here

Or else:

TravelDirections[{loc1, loc2}]["TravelDistance"]
(*Quantity[1.98282, "Miles"]*)

Cheers,

Marco

POSTED BY: Marco Thiel

That's very handy indeed. Marco, thanks for sharing it!

POSTED BY: Bernat Espigulé
POSTED BY: Marco Thiel

Hi Marco,

Thanks for the reply; In fact it IS a Voronoi diagram! The measure is just not the usual Euclidean norm, but the a custom norm (TravelDistance)! Given the great profusion of roads throughout Europe, I think the 'normal' Voronoi (using GeoDistance) and custom Voronoi (Using TravelDistance) will be quite the same in those places that are not bordered by masses of water. There is not a lot of CPU necessary right? Memory and internet connection is more crucial I think...

A more general question about TravelDirections: Does it use shortest distance, fastest time, most economic, a mix?

POSTED BY: Sander Huisman

Dear Sander,

yes, it is sort of a Voronoi Diagram. One of the problems is that the metric is not symmetric. It has been mentioned that the distance from A to B is not necessarily the same as the one from B to A, because of e.g. one way streets; this might however not be a problem in this particular case of inter-city movements.

Also, the distances lead to interesting effects on the edges between "Voronoi cells".

I think that I would just overlay a standard (Euclidean) Voronoi diagram and see what happens; it should be quite close to the catchment areas. I also think that the sea areas are not such a big problem. I guess one would use the entire square region for the analysis and then just use a mask to black out the sea.

Great work!

Marco

PS: Even larger computers I have keep crashing on Bernat's program. So I have not got any good data to work with yet.

POSTED BY: Marco Thiel

You might have to use a fast equivalent of GeoDistance, as the earth is curved rather than flat: Norm[{lat1,lon1}-{lat2,lon2}] probably won't give nice results. I'm not sure how to do Voronoi tesselations (quickly) with different norms (Manhattan norm for example).

POSTED BY: Sander Huisman

Hi Marco, I'm really sorry to hear that. I should have warn you about the difficulty of rendering 3.96GB of travel directions data in a single GeoGraphics. My suggestion is to decrease the resolution of the GeoPaths. Here is a simple example that tries to find all the travel directions to Rome from all the other capitals of the World. 131 out of 239 capital cities can be reached by car from Rome, being Singapore the capital that takes the longest to reach ( 671220 seconds, almost 8 days driving straight ).

directions=TravelDirections[{CountryData[#,"CapitalCity"],Entity["City",{"Rome","Lazio","Italy"}]}]&/@DeleteCases[CountryData[],Entity["Country","Italy"]];
directions = DeleteCases[directions, _Symbol];
(* Merge the GeoPaths, decrease the resolution by taking a point every 500 points, and color the paths with VertexColors normalized according to the longest travel time: 671220 seconds *)
paths=GeoPath[GeoPosition[pts=Take[Flatten[#["TravelPath"][[1,1]],1],{1,-1,500}]],VertexColors->Table[ColorData["Rainbow"][QuantityMagnitude[#["TravelTime"],"Seconds"]/671220-i/Length[pts]],{i,Length[pts]}]]&/@directions;
(* Render the colored geopaths in GeoGraphics *)
GeoGraphics[{Inset[BarLegend[{"Rainbow",{0,8}},LegendLayout->"Row",LegendLabel->Style["Days",Black,Bold,12],LegendMarkerSize->300],GeoPosition[{-11.45,77.13}]],Thickness[.003],paths,Red,PointSize[.008],Point[Entity["City",{"Rome","Lazio","Italy"}]]},GeoProjection->"Mercator",GeoBackground->GeoStyling["StreetMapNoLabels"],ImageSize->1000]

All capitals to Rome

Attached is the notebook with the colored paths exported as an m file.

Attachments:
POSTED BY: Bernat Espigulé

Very nice! To speed up the visualisation you can use the travelpath option:

pts=CirclePoints[{45.0,5.0},3,10000];
AbsoluteTiming[GeoGraphics[GeoPath[pts,"TravelPath"]]]
AbsoluteTiming[GeoGraphics[GeoPath[pts]]]

roughly 90x faster!

POSTED BY: Sander Huisman

Great tip, thanks!

POSTED BY: Bernat Espigulé

I'm not sure what the difference is, but I think it just draws straight lines rather then curves (Geodesic). So you can just as well use:

GeoPath[pts,"TravelPath"]
Line[Reverse/@pts]

look the same to me...

POSTED BY: Sander Huisman
POSTED BY: Sander Huisman

Sander, that's really well-executed! Benoit Mandelbrot would be happy to see that not only nature but human-made roads can generate beautiful fractals.

POSTED BY: Bernat Espigulé
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard