Maps

library(ggmap)
## Loading required package: ggplot2
odin.deployment.sites <- c(odin.100=12, odin.101=11, odin.102=10, odin.103=17,
                           odin.104=13, odin.105=1, odin.106=6, odin.107=15,
                           odin.108=16, odin.109=18, odin.110=14, odin.111=3,
                           odin.112=4, odin.113=9, odin.114=8, odin.115=2,
                           odin.116=5, odin.117=7)

site.coords <- readRDS("site_coords.rds")

draw.map <- function(ids, label, zoom) {
  # Print a map illustrating the geographic location of each of a set of points
  # label is a vector of the points to be labelled
  # zoom is the zoom level of the map
  
  # Compile lats and longs
  points <- data.frame(id=NA, lon=NA, lat=NA, serial=NA)
  for (i in 1:length(ids)) {
    points[i, 1] <- odin.deployment.sites[[paste0('odin.',ids[i])]]
    points[i, 2:3] <- site.coords[which(site.coords$id == points[i, 1]), 2:3]
    if (ids[i] %in% label)
      points[i, 4] <- ids[i]
    else
      points[i, 4] <- ''
  }
  
  # Unlabelled map
  map <- get_map(location = c(mean(points$lon), mean(points$lat)), zoom = zoom)
  
  # Labelled map
  map.lab <- ggmap(map) +
    geom_point(data=points, aes(x=lon, y=lat), col="red", size=1) +
    geom_text(data=points, aes(label=serial), hjust=0, vjust=0) +
    xlab('') + ylab('')
  
  print(map.lab)
}

draw.map(100:117, c(116, 117), 10)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-43.308478,172.575732&zoom=10&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false

draw.map(100:115, 100:115, 14)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-43.304553,172.587441&zoom=14&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false