
```{r theHeader, echo=FALSE, result='asis'}
if(requireNamespace("Pmisc", quietly=TRUE)) {
  Pmisc::markdownHeader(
    title="Examples of map layers in the mapmisc package",    
    author='Patrick Brown'
    )
} else {
cat("# Examples of map layers in the mapmisc package")  
}
```

The full document is at
[diseasemapping.r-forge.r-project.org](http://diseasemapping.r-forge.r-project.org).

See [mc.bbbike.org/mc](http://mc.bbbike.org/mc/) for examples more map tiles 
(not all of which are compatible with `mapmisc`).


```{r setup, include=FALSE}
library('knitr')
library('mapmisc')

fact = 2
allTiles = FALSE

knitr::opts_chunk$set(dpi=100, dev.args=list(bg='white'),
    fig.path = 'mapLayersFigures/', out.width='80%')

theNames = names(osmTiles())
cachePath = file.path(getwd(), 'mapLayersCache')

```

```{r theExtent, include=FALSE}
africaExtent = c(25, 25)
trafalgarSquare=c(-0.128056,51.508056)
stjohns=c(-52.683320, 47.566285)
```


```{r downloadTiles,include=FALSE}

  africaTiles = openmap(africaExtent, path=theNames,zoom=2,
    verbose=TRUE, 
    crs = crsMerc, 
    cachePath = cachePath)
  
  londonTiles = openmap(trafalgarSquare,zoom=12,path=theNames,
    verbose=TRUE, 
    crs = crsMerc, 
    cachePath = cachePath)
  
  nfTiles = openmap(stjohns, zoom=14, path=theNames,
    verbose=TRUE, 
    crs = crsMerc, 
    cachePath = cachePath)
```


The following are some examples of maps produced with `mymap=openmap(x, path="something")`


```{r someTiles, echo=FALSE, results='asis', fig.height=5, fig.width=5}

mytiles = list(africaTiles, londonTiles, nfTiles)

for(D in theNames){
  Dsub = gsub("-",".", D)
  
  threeTiles = length(
    grep(
      paste("^", Dsub, "([rR]ed|[gG]reen|[bB]lue)$", sep=""), 
      names(mytiles[[1]])
    )
  ) == 3
  
  cat("\n\n# ", D, "\n\n")
  cat("`plot", "RGB"[threeTiles], 
    "(openmap(x,path=\"", D, "\"))`\n\n", 
    sep='')
  cat(openmapAttribution(D, 'html'), '\n\n')
  
  cat("\n|     |     |     |\n|-----|-----|-----|\n")
  for(Dplot in 1:length(mytiles)) {
    cat("|")
    
    oneTileName = grep(paste("^", Dsub, "$", sep=""), 
      names(mytiles[[Dplot]]), value=TRUE)
    threeTilesName = grep(paste("^", Dsub, 
        "([rR]ed|[gG]reen|[bB]lue)$", sep=""), 
      names(mytiles[[Dplot]]),value=TRUE)
    
    if(length(threeTilesName)==3) { # plotRGB
      map.new(mytiles[[Dplot]])
      
      plotRGB(mytiles[[Dplot]][[
          paste(Dsub, c("Red","Green","Blue"),sep="")
        ]],add=TRUE)
      
    } else if (length(oneTileName)==1){
      
      map.new(mytiles[[Dplot]])
      plot(mytiles[[Dplot]][[oneTileName]],add=TRUE)
      
    } else {
      
      plot(0.5, 0.8)
      text(0.5, 0.8, paste(D, "is\n not currently\navailable"))
      
    }
    
  }
  cat("|")
  
}

```

# Additional map layers

See [mc.bbbike.org/mc](http://mc.bbbike.org/mc/) 

Maps shown because it is not clear how they should be attributed.

Michelin
    
```{r additionalMichelin, echo=TRUE, purl=FALSE, eval=FALSE}

mPath = osmTiles(paste(
  'http://map3.viamichelin.com/map/mapdirect?map=',
  c('light','viamichelin'), '&', sep=''),
  xyz='zxy=', suffix = 
    '&format=png&version=201503191157&layer=background')

africaMichelin = openmap(
  africaExtent, path=mPath,
  zoom=2,
  verbose=TRUE, 
  crs = crsMerc)


for(D in names(africaMichelin)) {
  map.new(africaMichelin)
  plot(africaMichelin[[D]], add=TRUE)
  mtext(D, side=3, line=-2, outer=FALSE)
}

```
Stamen retina

   
```{r stamenRetina, echo=TRUE, purl=FALSE, eval=FALSE}

sPath = osmTiles('stamen-toner', 
  suffix = '@2x.png')

africaS = openmap(
  africaTiles, 
  buffer=-1,
  zoom=attributes(africaTiles)$tiles$zoom+1,
  path=sPath, verbose=TRUE)
africaS2 = tonerToTrans(africaS)

map.new(africaTiles)
plot(africaTiles[['opentopomap']], add=TRUE)
plot(africaS2, add=TRUE)

mtext(sPath, side=3, line=-2, outer=FALSE)
```


Komoot, Yandex, ESRI, F4map, and Sigma

```{r aditionalEsri, echo=TRUE, purl=FALSE, eval=FALSE}

otherPaths = c(
  'https://a.tile.hosted.thunderforest.com/komoot-2/',
  'https://sat01.maps.yandex.net/tiles?l=sat&v=1.35.0&',
  'https://vec02.maps.yandex.net/tiles?l=map&v=4.40&',
  'https://server.arcgisonline.com/ArcGIS/rest/services/World_Physical_Map/MapServer/tile/',
  'https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/',
  'https://services.arcgisonline.com/ArcGIS/rest/services/Ocean/World_Ocean_Base/MapServer/tile/',
  'https://tile2.f4map.com/tiles/f4_2d/',
  'http://tiles1.sigma-dc-control.com/layer8/')

africaExtra = openmap(
  africaExtent, path=otherPaths,
  zoom=2,
  verbose=TRUE, 
  crs = crsMerc)

Spaths = unique(
  gsub("(red|green|blue|trans)$", "",
    names(africaExtra), ignore.case=TRUE)
)

for(D in Spaths) {
  
  oneTileName = grep(paste("^", D, "$", sep=""), 
    names(africaExtra), value=TRUE)
  
  threeTilesName = grep(paste("^", D, 
      "([rR]ed|[gG]reen|[bB]lue)$", sep=""), 
    names(africaExtra),value=TRUE)
  
  map.new(africaExtra)
  if(length(threeTilesName)==3) {
    plotRGB(africaExtra[[threeTilesName]], add=TRUE)
  } else {
    plot(africaExtra[[oneTileName]], add=TRUE)
  }
  mtext(D, side=3, line=-2, outer=FALSE)
}

``` 


