library(rworldmap)
library(fields)

latmax <- NULL
latmin <- NULL

for( i 1:30) {
  a <- Selbox()
  a$lat
  latmax <- cbind(latmax, a$lat[1])
  latmin
  lonmin <- cbind(lonmin, a$lon[length(a$lon)])
  lonmax
}

# Testing:
lonmin <- seq(-100,90, 50)
lonmax <- seq(-55, 100, 50)
latmin <- seq(-80, 79, 40)
latmax <- seq(-55, 80, 40)


map <- getMap()
plot(map, axis = TRUE)
map <- getMap()
plot(map, axis = TRUE)
# For each region change to 1:30
lapply(1:4, function(x){
             polygon(x = c(lonmin[x],lonmax[x], lonmax[x], lonmin[x]),
                      y = c(latmin[x], latmin[x], latmax[x], latmax[x]),
                      border = "darkgreen", col = x)})
# Scale:
image.plot(x = 1:3, y = 1:4, val, legend.only = TRUE)
# Binary:
legend("top", c("region1", "region2"), fill = c("pink", "lightgreen"), h = TRUE, bty = 'n')

# testing: 
polygon(x = c(x1 = 0, x2 = 50, x3 = 50, x4 = 0),
        y = c(y1 = 0, y2 = 0, y3 = 50, y4 = 50), border = "red",
        col = "pink")
polygon(x = c(x1 = -30, x2 = -70, x3 = -70, x4 = -30),
        y = c(y1 = -80, y2 = -80, y3 = -20, y4 = -20), border = "darkgreen",
        col = "lightgreen")
legend("top", c("region1", "region2"), fill = c("pink", "lightgreen"), h = TRUE, bty = 'n')
val <- 1:(3*4)
dim(val) <- c(x = 3, y = 4)
image.plot(x = 1:3, y = 1:4, val, legend.only = TRUE)
