Shortest path across Spain: the squirrel problem

(Jump straight to the solution.)

This post answers the following simple question. Can a squirrel cross from north to south Spain without touching the ground?

The answer is: obviously not. But, another question arises, and this one is not so easy to answer:If a squirrel had to go from the north of Spain to the south, touching the ground as little as possible: which way would it follow?

The answer is not trivial.

The history

The legend says that Spain was once so thickly-forested that a squirrel could cross the peninsula hopping from tree to tree. Even though we all learned this at school as a ground truth, it seems that, in fact, this was just a legend. Spain was in fact “land of rabbits”, as the Romans used to call it.

Much has been said about that squirrel, but none have proved it analytically. Here you have found the answer!

The procedure

I used the GlovCover maps from the ESA to get information of the land use. The resolution is around 200m, accurate enough for this purpose. The map spans the whole world but here we are just interested in Spain. So the first step is to load the image raster and crop it.

Screenshot of the solution

The cropped raster image needs to be converted to a directed graph:

Having such a graph, the last step is to calculate the shortest path between two nodes - and there we have the path the smart squirrel would follow!

The coding

The code is purely R and uses several packages: raster, rgdal, Matrix, igraph… Creating the network matrix runs really slow, so some optimization in terms of code to be done.

The solution

The solution is presented HERE as a interactive dashboard. Unfortunately, the poor squirrel needs to touch the ground at several places as it gets south.

Download path coordinates

Download them here