Skip to content

Commit

Permalink
Fix a bug in map_stations.
Browse files Browse the repository at this point in the history
  • Loading branch information
ConorIA committed Jun 27, 2018
1 parent 0e4a1c0 commit 72d1e68
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 60 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: senamhiR
Type: Package
Title: A Collection of Functions to Obtain Peruvian Climate Data
Version: 0.6.2
Date: 2018-06-25
Version: 0.6.3
Date: 2018-06-27
Authors@R: c(person(given = c("Conor", "I."), family = "Anderson",
role = c("aut","cre"), email = "[email protected]"),
person(given = c("William", "A."), family = "Gough", role = "ths",
Expand Down
82 changes: 24 additions & 58 deletions R/map_stations.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
##' @description Show the stations of interest on an interactive map using Leaflet. Zoom levels are guessed based on an RStudio plot window.
##'
##' @param station character; one or more station id numbers to show on the map.
##' @param zoom numeric; the level to zoom the map to.
##' @param type character; either "osm" for OpenStreetMap tiles, or "sentinel" for cloudless satellite by EOX IT Services GmbH (\url{https://s2maps.eu}).
##'
##' @importFrom dplyr "%>%" filter
Expand All @@ -14,14 +13,12 @@
##' @author Conor I. Anderson
##'
##' @examples
##' # Make a map of all the stations.
##' \dontrun{map_stations(catalogue$StationID, zoom = 4)}
##' # Make a map of all stations in Cusco.
##' \dontrun{map_stations(catalogue$StationID[catalogue$Region == "CUSCO"])}
##' ##' # Make a map from station search results.
##' \dontrun{map_stations(station_search(region = "SAN MARTIN", baseline = 1981:2010))}
##' # Map a single station
##' map_stations(401)
##' # Make a map from station search results.
##' map_stations(station_search(region = "SAN MARTIN", baseline = 1981:2010))

map_stations <- function(station, zoom, type = "osm") {
map_stations <- function(station, type = "osm") {

catalogue <- .get_catalogue()

Expand All @@ -39,79 +36,48 @@ map_stations <- function(station, zoom, type = "osm") {
station <- filter(catalogue, StationID %in% station)
}

hilat <- ceiling(max(station$Latitude))
lolat <- floor(min(station$Latitude))
hilon <- ceiling(max(station$Longitude))
lolon <- floor(min(station$Longitude))
lats <- (hilat + lolat)/2
lons <- (hilon + lolon)/2
if (missing(zoom)) {
latrng <- (hilat - lolat)
if (latrng >= 16) {
zoom = 4
} else if (latrng >= 8) {
zoom = 5
} else if (latrng >= 5) {
zoom = 6
} else if (latrng >= 2) {
zoom = 7
} else if (latrng == 1) {
zoom = 8
} else zoom = 10
}

defIcons <-function(dat) {
sapply(dat$Configuration, function(Configuration) {
if(Configuration %in% c("M", "M1", "M2")) {
"thermometer"
} else {
"waterdrop"
} })
}

defColours <-function(dat) {
sapply(dat$Configuration, function(Configuration) {
if(Configuration %in% c("M", "M1", "M2")) {
"orange"
} else {
"blue"
} })
}

icons <- awesomeIcons(
icon = defIcons(station),
icon = unname(sapply(station$Configuration, function(x) {
if (x %in% c("M", "M1", "M2")) "thermometer" else "waterdrop"
})),
iconColor = 'black',
library = 'ion',
markerColor = defColours(station)
markerColor = unname(sapply(station$Configuration, function(x) {
if (x %in% c("M", "M1", "M2")) "orange" else "blue"
}))
)

map <- if (type == "sentinel") {
leaflet(station, options = leafletOptions(crs = leafletCRS("L.CRS.EPSG4326"))) %>%
setView(lng = lons, lat = lats, zoom = zoom) %>%
addWMSTiles(
"https://tiles.maps.eox.at/wms?service=wms",
layers = "s2cloudless",
options = WMSTileOptions(format = "image/jpeg"),
attribution = "Sentinel-2 cloudless - https://s2maps.eu by EOX IT Services GmbH (Contains modified Copernicus Sentinel data 2016 & 2017)"
attribution = paste("Sentinel-2 cloudless - https://s2maps.eu by EOX",
"IT Services GmbH (Contains modified Copernicus",
"Sentinel data 2016 & 2017)")
)
} else {
if (type != "osm") warning("Unrecognized map type. Defaulting to osm.")
leaflet(station) %>%
addTiles() %>%
setView(lng = lons, lat = lats, zoom = zoom)
leaflet(station) %>% addTiles()
}

map <- map %>%
addAwesomeMarkers(~Longitude, ~Latitude, icon = icons,
label = paste0(station$StationID,
" - ", station$Station,
" (", station$Configuration, ")"))
label = paste0(station$StationID, " - ",
station$Station,
" (", station$Configuration, ")",
" @ ", round(station$Latitude, 2), ", ",
round(station$Longitude, 2)))

# Add a target if it exists
target <- c(attr(station, "target_lon"), attr(station, "target_lat"))
if (!is.null(target)) {
map <- map %>% addCircleMarkers(lng = target[1], lat = target[2],
color = "red", label = "target")
color = "red", label = paste0("Target: ",
target[2],
", ",
target[1]))
}

map
Expand Down

0 comments on commit 72d1e68

Please sign in to comment.