Courtesy: http://environmentalinformatics-marburg.github.io/mapview/popups/html/popups.html

This document was produced on Tue Feb 27 2018 using mapview version 2.3.0


mapview provides a few convenience functions for popup creation. These popups can be used with both mapview and leaflet.


library(tidyverse)
library(mapview)
library(sp)
library(lattice)

Load stations

gUS             <- read.csv("Station_US.csv")
gCA             <- read.csv("Station_CA.csv")
gUS$County      <- "USGS"
gUS$Station       <- paste0("0", gUS$Station)
gCA$County      <- "ECCC"

  
pnt             <- rbind(gUS, gCA)
colnames(pnt)[2]<- "gages"
pnt$Avail       <- FALSE

Load hydrographs

somehow local images cannot be popuped by mapview

#img   <- character(npnt)
rpnt  <- nrow(pnt)
for (i in 1 : rpnt) {
  if(pnt$County[i] == "USGS"){
    filename <- paste0("P:\\ipemf\\wrf\\NWM_GL\\Precip\\US/Hydrograpy_MAP_Stream_", pnt$gages[i], ".png")
  } else{
    filename <- paste0("P:\\ipemf\\wrf\\NWM_GL\\Precip\\CA/Hydrograpy_MAP_Stream_", pnt$gages[i], ".png")
  }
  if (file.exists(filename)){
    pnt$Avail[i] <- TRUE
  }
}

pnt <- pnt[pnt$Avail, ]
npnt  <- nrow(pnt)
img <- paste0("http://www-personal.umich.edu/~cxiao/NWM_GL_Hydrograph/Hydrograpy_MAP_Stream_", 
              pnt$gages, ".png")

Spatial points

coordinates(pnt) <- ~ Lon + Lat
proj4string(pnt) <- "+init=epsg:4326"

Add the Great Lakes watershed polygon

library(rgdal)
rMaumee       <- readOGR(dsn = "glin_glwatershed_outline", layer = "glwatershed_outline")
## OGR data source with driver: ESRI Shapefile 
## Source: "glin_glwatershed_outline", layer: "glwatershed_outline"
## with 1 features
## It has 2 fields
rMaumee.proj  <- spTransform(rMaumee, CRSobj = CRS(proj4string(pnt)))

Draw plot

mapview(pnt, zcol = "County", label = pnt$gages, layer.name = "Hydrographs", 
        popup = popupImage(img, src = "remote"), cex = 5) +
  mapview(rMaumee.proj, layer.name = "Watershed")

Use different colors to differenciate stations with and without hydrographs

Change the popup Image size