<!--
%\VignetteEngine{knitr::docco_linear}
%\VignetteIndexEntry{Space Station Map Projections}
-->

# Space Station Map Projections

```{r setup}
library('mapmisc', quietly=TRUE)
haveRgdal = requireNamespace('rgdal', quietly=TRUE)
haveGeosphere = requireNamespace('geosphere', quietly=TRUE)

if(!exists('mapmiscCachePath'))
  mapmiscCachePath = system.file('extdata', package='mapmisc')

if(!exists('mapmiscCacheReadOnly'))
  mapmiscCacheReadOnly = TRUE

if(!exists('testing'))
  testing=FALSE

if(!exists('fact'))
  fact = 0.6
# uncomment for nicer images
# fact = 1 

options(
  mapmiscCachePath = mapmiscCachePath,
  mapmiscCacheReadOnly = mapmiscCacheReadOnly
  )
```

```{r mapmiscCache, echo=FALSE, purl=FALSE}
  getOption('mapmiscCachePath')
  getOption('mapmiscCacheReadOnly')
if(testing) {
  options(mapmiscVerbose = TRUE)
}
```

```{r data}
okinawa = mapmisc::geocode("Okinawa")
hk = y = mapmisc::geocode("Hong Kong")
guam = mapmisc::geocode("Guam")
x = SpatialPoints(cbind(130,15), proj4string=crsLL)
```

```{r projection}
if(haveGeosphere & haveRgdal) {
  myProj = tpers(x=bind(x,y), hKm = 2*1000,
      tilt =-15, axis='seu')
  xProj = spTransform(guam, myProj)
  yProj = spTransform(hk, myProj)
  okinawaProj = spTransform(okinawa, myProj)
}
```

```{r background}
if(haveGeosphere & haveRgdal)
  myMap = openmap(myProj, path='landscape', zoom=2,fact=3)
```

```{r backgroundOverDateline, include=FALSE, eval=FALSE, purl=FALSE}
if(length(attributes(myProj)$ellipseSafeLL)>2 & haveRgdal) {
  
  myMap1 = openmap(
      x=attributes(myProj)$regionLL[1,],
      crs=crsMerc, zoom=3,
      path='https://sat01.maps.yandex.net/tiles?l=sat&v=1.35.0&')
  
  
  myMap2 = openmap(
      x=attributes(myProj)$regionLL[2,],
      zoom=attributes(myMap1)$tiles$zoom,
      crs=crsMerc, zoom=3,
      path='https://sat01.maps.yandex.net/tiles?l=sat&v=1.35.0&')
  
  mapRaster = raster(
      extent(attributes(myProj)$ellipse), 
      res = mean(res(myMap1)/1.5),
      crs=myProj
  )
  
  myMap1a = projectRaster(
      from=myMap1, to=mapRaster, method='ngb')
  myMap2a = projectRaster(
      from=myMap2, to=mapRaster, method='ngb')
  
  myMap = mosaic(myMap1a, myMap2a, fun=mean)
}
```



```{r map}
if(haveGeosphere & haveRgdal) {
  map.new(myProj, bg='black', buffer=2*100*1000)
  plot(myMap, add=TRUE)
  
  if(requireNamespace('maptools')) {
    data("wrld_simpl", package='maptools')
    wrld4 = wrapPoly(x=wrld_simpl, crs=myProj)
    plot(wrld4, add=TRUE)
  }
  
  gridlinesWrap(myProj, col='yellow', 
      north=seq(-40,40,by=10), easts=seq(-150,180,by=10),
      lty=2)
  
  points(xProj, col='red', pch=16, cex=2)
  text(xProj@coords, 
      as.character(xProj$originalPlace),
      pos=2, col='red')
  
  points(okinawaProj, col='red', pch=16, cex=2)
  text(okinawaProj@coords, 
      as.character(okinawaProj$originalPlace),
      pos=2, col='red')
  
  points(yProj, col='red', pch=16, cex=2)
  text(yProj@coords, 
      as.character(yProj$originalPlace),
      pos=2, col='red')
 
}
```
