Offline Maps with RgoogleMaps and leaflets



Offline Maps with RgoogleMaps and leaflets








New version of RgoogleMaps now fetches map tiles

Until version 1.3.0 RgoogleMaps only downloaded static maps as provided by the static maps APIs from e.g. Google, bing and OSM. While there are numerous advantages to this strategy such as full access to the extensive feature list provided by those APIs, the limitations are also clear:

  1. unlikely reusability of previously stored static maps,
  2. limits on the maximum size of the map (640,640),
  3. and the requirement to be online.

Beginning with version 1.4.1 (which is now on CRAN ) , we added the functions GetMapTiles and PlotOnMapTiles which fetch individual map tiles and store them locally.

For example, if we wanted to fetch 20 tiles (in each direction) at zoom level 16 around Washington Square Park in Manhattan, we would simply run

library(RgoogleMaps)
(center=getGeoCode("Washington Square Park;NY"))
##       lat       lon 
##  40.73082 -73.99733
GetMapTiles(center, zoom=16,nTiles = c(20,20))

Note that the default server is taken to be openstreetmap and the default local directory “~/mapTiles/OSM”. We could have also passed the location string directly and saved several zoom levels at once (note the constant radius adaptation of the number of tiles):

for (zoom in 13:15)
  GetMapTiles("Washington Square Park;NY", zoom=zoom,nTiles = round(c(20,20)/(17-zoom)))

Before requesting new tiles, the function checks if that map tile exists already which avoids redundant downloads.

We can repeat the process with Google map tiles and plot them:

for (zoom in 13:16)
  GetMapTiles("Washington Square Park;NY", zoom=zoom,nTiles = round(c(20,20)/(17-zoom)),
              urlBase = "http://mt1.google.com/vt/lyrs=m", tileDir= "~/mapTiles/Google/")

#just get 3x3 tiles:

#mt= GetMapTiles(center = c(lat = 40.73082, lon =-73.99733), zoom=16,nTiles = c(3,3), urlBase = "http://mt1.google.com/vt/lyrs=m", tileDir= "~/mapTiles/Google/", returnTiles = TRUE)

mt= GetMapTiles("Washington Square Park;NY", zoom=16,nTiles = c(3,3),
              urlBase = "http://mt1.google.com/vt/lyrs=m", tileDir= "~/mapTiles/Google/", returnTiles = TRUE)
PlotOnMapTiles(mt)

unnamed-chunk-3-1

Interactive Web Maps with the JavaScript ‘Leaflet’ Library

While the original motivation of GetMapTiles was to enable offline creation of static maps within the package RgoogleMaps, combining this feature with the interactivity of the leaflet library leads to an effective offline maps version of leaflet!

We only need to replace the default server specified by the parameter urlTemplate by a local server obliging with the file naming scheme zoom_X_Y.png set by GetMapTiles Any simple local Web service will suffice, but the following two solutions work best for me

  1. (http://stackoverflow.com/questions/5050851/best-lightweight-web-server-only-static-content-for-windows) “To use Python as a simple web server just change your working directory to the folder with your static content and type python -m SimpleHTTPServer 8000, everything in the directory will be available at http:/localhost:8000/

  2. (https://github.com/yihui/servr) Use the R package servr: Rscript -e ‘servr::httd()’ -p8000

So assuming (i) successful execution of the map tileabove and (ii) the correct launch of the server (in the parent dirtectory of mapTiles/), the following code will have leaflet dynamically load them (from the local repository) for zooming and panning abilities:

library(leaflet)
  m = leaflet::leaflet() %>% 
    addTiles( urlTemplate = "http:/localhost:8000/mapTiles/OSM/{z}_{x}_{y}.png")
  m = m %>% leaflet::setView(-73.99733, 40.73082 , zoom = 16)
  m = m %>% leaflet::addMarkers(-73.99733, 40.73082 )
  m

And for google map tiles:

library(leaflet)
  m = leaflet::leaflet() %>% 
    addTiles( urlTemplate = "http:/localhost:8000/mapTiles/Google/{z}_{x}_{y}.png")
  m = m %>% leaflet::setView(-73.99733, 40.73082 , zoom = 16)
  m = m %>% leaflet::addMarkers(-73.99733, 40.73082 )
  m



Alternative to the Hypergeometric Formula

The Hypergeometric Distribution is usually explained via an urn analogy and formulated as the ratio of “favorable outcomes” to all possible outcomes:

\[
\displaystyle
\boxed{P(x=a;N,A,n,a) = \frac{{A \choose a} \cdot {N-A \choose n-a} }{{N \choose n}}}
\]

where \(N\) is the total number of balls in, \(A\) the number of “red” balls in the urn, and \(n\) the sample size. The question answered by the above expression is the probability of finding \(x=a\) red balls in the sample.

However, a different – equally worthy – viewpoint is that of a tree with conditional probabilities.

Here is an example for \(N=10, A=4, n=3\)

So there are 3 leafs with exactly one red ball in the sample. Following the tree we multiply the conditional probabilities of the tree egdes to get to the “and” probability of the leafs. It should be clear that all three probabilites are identical – but for the order of multiplication:

\[
P (\mbox{one red}) = 3 \cdot P_{leaf} = 3 \cdot \frac{4}{10} \cdot \frac{6}{9} \cdot \frac{5}{8} = 3 \cdot \frac{4 \cdot 6 \cdot 5}{10 \cdot 9 \cdot 8}
\]

Alternative formula

How many leafs contain exactly one red ball? Exactly \({n \choose a} = {3 \choose 1} = 3\). The probability for the precise event “a red balls followed by n-a blue balls”is:

\[
\frac{A}{N} \cdot \frac{A-1}{N-1} \cdots \frac{A-a+1}{N-a+1} \cdot \frac{N-A}{N-a} \cdot \frac{N-A-1}{N-a-1} \cdots \frac{N-A-(n-a-1)}{N-a-(n-a-1)}
\]
The last denominator is simply \(N-n+1\), i.e. the full denominator is \(_NV_n=N!/(N-n)!\)

The left numerator is simply \(_AV_n = A!/(A-a)!\) in analogy the right numerator \(_{N-A}V_{n-a} = (N-A)!/(N-A-(n-a))!\) So, all in all \[
{n \choose a} \cdot \frac{_AV_n \cdot _{N-A}V_{n-a}}{_NV_n} = {n \choose a} \cdot \frac{A! \cdot (N-A)! \cdot (N-n)!}{(A-a)! \cdot (N-A-(n-a))! \cdot N!}
\]

\[
= {n \choose a} \cdot \frac{\frac{A!}{(A-a)!} \cdot \frac{(N-A)!}{(N-A-(n-a))!} }{\frac{N!}{(N-n)!}} = \frac{{A \choose a} \cdot {N-A \choose n-a} }{{N \choose n}}
\]

which leaves us with \[
\displaystyle
\boxed{P(x=a;N,A,n,a) = {n \choose a} \cdot \frac{_AV_n \cdot _{N-A}V_{n-a}}{_NV_n} }
\]