-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path04_vig_mapview_leaflet.Rmd
158 lines (107 loc) · 6.32 KB
/
04_vig_mapview_leaflet.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
---
pagetitle: "mapview_and_leaflet"
---
## Using `mapview`
This package is pretty cool, and has a bunch of functionality. There are a number of [vignettes online already](https://r-spatial.github.io/mapview/articles/index.html), in particular check out the basic overview vignette [here](https://r-spatial.github.io/mapview/articles/articles/mapview_01-basics.html).
### Main Functions
- `mapview` - view potentially multiple spatial objects on a set of background maps
- `viewExtent` - view extent / bounding box of spatial objects
- `viewRGB` - view RGB true- or false-color images of raster objects
- `latticeView` & `sync` - view small multiples of several potentially synchronised maps
- `slideView` - view two overlaid raster objects with a horizontal slider to hide/reveal one of them
- `cubeView` - 3D view of raster data cubes (e.g. space-time cube) with keyboard control over position of slices
- `plainview` - view large rasters on a plain background (i.e. no background map)
- `mapshot` - easily save maps (including leaflet maps) as html or png (or other image formats)
### Spatial Data Supported in `mapview`
It is natively set to work with `sf`, which is great. Note it will plot `raster` objects as well!
- `sf` (XY dimension only)
- `raster` (Layer, Stack, Brick and SpatialPixels* / SpatialGridDataFrame)
- `sp` (Points, Polygons, Lines and their DataFrame version)
- `satellite`
### Leaflet Compatible Functions
In addition, mapview provides some extra leaflet functionality:
- `addHomeButton` - add zoom-to-layer buttons
- `addLogo` - add a logo (or any image really) to a map
- `addFeatures` - add features to a map (sf or sp). This is features type agnostic, meaning the same function can be used for points, lines and polygons
- `addMouseCoordinates` - add mouse cursor postion information when moving over the map
- `addLargeFeatures` - add large data to a map that would otherwise make the leaflet map irresponsive
- `popupTable, popupGraph & popupImage` - popups of tables, grpahs and images for use with the leaflet add* functions
### Advanced `mapview` Options
https://r-spatial.github.io/mapview/articles/articles/mapview_02-advanced.html
## Using `leaflet`
You can also create `leaflet` maps, and they can be shared via html. This is a good way to provide dynamic spatial data, though bear in mind these html files can get large if you are trying to show a lot of data points or polygons.
### The Basics
The [`leaflet` website](https://rstudio.github.io/leaflet/) is excellent and should be the go-to for figuring out details. Below is a simple example building on some of the functions/data we worked with earlier in this workshop.
The main things to identify are:
- What type of spatial data do you want to plot? (i.e., polygons, lines, markers, etc)
- What format is that data? (works with `sf` or `sp` objects, or simple X/Y coordinates)
- What base layers would you like to include? (see [here for the possibilities](http://leaflet-extras.github.io/leaflet-providers/preview/index.html))
- How fancy do you want it to look? There are many extra options you can add using [built in plugins](https://rstudio.github.io/leaflet/morefeatures.html), or custom Javascript.
<hr>
## Example: California Snow Course Stations
Let's look at California snow course stations (check out the [CDEC website](http://cdec.water.ca.gov/snow/current/snow/index.html)). There's a nice package called "**`sharpshooter`**" we can use to pull the locations and metadata for all these stations and plot them with a `mapview` and `leaflet` map.
```{r GetData, echo=T, eval=T, purl=FALSE}
library(sharpshootR) # CDEC.snow.courses, CDECquery, CDECsnowQuery
# GET DATA AND PREP
data(CDEC.snow.courses)
snw<-CDEC.snow.courses
# make a few changes for plotting purposes
snw$id<-as.factor(snw$id)
snw$latitude<-as.numeric(snw$latitude)
snw$longitude<-as.numeric(snw$longitude)*-1
snw$apr1avg_in<-snw$april.1.Avg.inches
snw<-dplyr::select(snw, course_number, id, elev_feet:longitude,apr1avg_in)
# make longitude negative!
snw$longitude <- abs(snw$longitude)*-1
str(snw) # check out data
```
### `mapview` Map
The nice thing is a mapview map is very simple to code compared to a leaflet map. See below.
```{r mapviewMap, echo=T, eval=T, purl=FALSE, warnings=FALSE, message=FALSE}
library(mapview)
library(sf)
mapviewOptions(fgb = FALSE)
# make a sf type object:
snw_sf <- st_as_sf(snw, coords = c("longitude", "latitude"), crs=4326, remove = FALSE)
# the default color scheme is viridis
mapview(snw_sf, zcol="apr1avg_in", layer.name="CDEC SNOW STATIONS")
```
<hr>
### `leaflet` Map
A leaflet map takes a bit more formatting, but the sky is the limit. We can make the map look pretty much exactly as we'd like, there are far fewer limitations.
```{r leafletMap, fig.width=7, echo=TRUE, eval=T, purl=F}
library(leaflet)
# add color palette
pal <- colorNumeric(
palette = "GnBu",# can change to whatever: "RdBu", "GnBu"
domain = snw$apr1avg_in
)
# Make a leaflet map!
m <- leaflet() %>% addTiles() %>%
#setView(lng = -120.8, lat = 39, zoom = 8) %>% if you want to preset the view/zoom default
addProviderTiles("Esri.WorldImagery", group = "ESRI Aerial") %>%
addProviderTiles("Esri.WorldTopoMap", group = "Topo") %>%
# add scale bar
addMeasure(position = "topright",
primaryLengthUnit = "meters",
primaryAreaUnit = "sqmeters",
activeColor = "#3D535D",
completedColor = "#7D4479") %>%
# CDEC SNOW STATIONS
addCircleMarkers(data=snw, group="CDEC Snow",
lng= ~longitude, lat= ~latitude, # the spatial data, requires "~" here because it's NOT sp or sf object
popup=paste0("<strong>","Course ID: ","</strong>",
snw$course_number, "<br><strong>", "Name: ",
"</strong>", snw$id, "<br><strong>", "Elev (ft): ",
"</strong>", snw$elev_feet, "<br><strong>",
"Apr-1 Avg: ", "</strong>", snw$apr1avg_in),
stroke=TRUE, weight=0.6,radius=8,
fillOpacity = 0.5, color="black",
fillColor= ~pal(apr1avg_in)) %>% # mapping to the color palette
# add controls for basemaps and data
addLayersControl(
baseGroups = c("ESRI Aerial", "Topo"),
overlayGroups = c("CDEC Snow"),
options = layersControlOptions(collapsed = T))
m
```