-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path01_vector_kml.Rmd
149 lines (101 loc) · 5.26 KB
/
01_vector_kml.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
---
pagetitle: "spatialkml"
editor_options:
chunk_output_type: console
---
<br>
## Working with `kml` Files
The `sf` package can read many different file types in, and `kml` is no exception. The difference is `kml` files are the unzipped versions of `kmz` files. You'll want to unzip with any sort of zip package you have on your computer (Keka, 7zip, etc.). Data used in this example live in the github repo [here](https://github.com/ryanpeek/mapping-in-R-workshop/blob/master/data/streamgages_06.kml). Importantly, `kml` can contain multiple layers because they use a sort of nested format, so it's good to check first to see which layer you may want. This example only has a single *layer* so we don't need to specify a layer in our `st_read` function.
```{r loadlibraries, eval=T, echo=T}
suppressPackageStartupMessages({
library(ggplot2)
library(dplyr)
library(sf);
library(stringi)
})
# https://waterwatch.usgs.gov/?m=stategage
# check layers first:
st_layers("data/streamgages_06.kml")
# read in
gages <- st_read("data/streamgages_06.kml") # reads kml but not kmz
```
<br>
### Cleaning/Removing `html` tags
Once we have the data in, we still need to clean up some of the `html` bits that we don't want. We can use some text/string functions to deal with this in R. `gsub` and `substr` are great functions for fixing/replacing text.
```{r cleanhtml, eval=T, echo=T}
# pull out relevant info (remove html tags)
gages$name <- gsub(pattern="<.*?>", "", gages$Name)
gages$details <- gsub(pattern="<.*?>", "", gages$Description)
gages$site_no <- substr(gages$name, start = 1, stop = 8)
gages$site <- substr(gages$name, start = 10, stop = nchar(gages$name))
gages$date_s <- substr(gages$details, start=13, stop=22)
gages$date_e <- substr(gages$details, start=32, stop=nchar(gages$details)-1)
gages <- gages %>% dplyr::select(site_no:date_e)
gages$gageID <- paste0("T", gages$site_no)
# grep all sites with "AMERICAN", "DEER" or "BEAR" in site name
gages_FILTER <- filter(gages, grepl(pattern = "AMERICAN|DEER|BEAR",site))
```
### A Basic `ggplot`
Here we can make a basic `ggplot` with no specific background to our spatial data. We can still sort of make out an outline of California, but it would be nice to add some more details.
```{r simpleMapCA, echo=T, eval=F}
# simple map
ggplot() +
labs(x="Longitude (WGS84)", y="Latitude",
title="USGS Gages in CA") +
geom_sf(data=gages, col="blue", lwd=0.4, pch=21) +
theme_bw()
```
```{r simpleMapHidden, eval=F, echo=F}
ggplot() +
labs(x="Longitude (WGS84)", y="Latitude",
title="USGS Gages in CA") +
geom_sf(data=gages, col="blue", lwd=0.4, pch=21) +
theme_bw(base_family = "Roboto Condensed")
ggsave(filename = "img/usgs_gages_in_ca.png", width = 6, height = 8, units = "in", dpi = 150)
```
```{r simpleMapPlot, echo=F, out.width = "80%"}
library(knitr)
include_graphics(path = "img/usgs_gages_in_ca.png")
```
### Use `ggspatial` for Map Background/Details
Now we can add a few additional details here. Let's use the `USAboundaries` package go get a county layer, and the `ggspatial` package to add a scale bar, north arrow, and open source map background. Note, we could also just throw this `sf` layer into `mapview` (see the mapview page) and have an interactive map.
First let's get the county layer:
<!--
Not including this for now as we don't need to label this example:
counties <- us_counties(resolution = "high", states="CA") %>% # list states
# add centroid values for labels
mutate(lon=map_dbl(geometry, ~st_centroid(.x)[[1]]),
lat=map_dbl(geometry, ~st_centroid(.x)[[2]]))
-->
```{r getCounties, eval=F, echo=T, message=FALSE, warning=FALSE}
# now with background
library(ggspatial)
library(USAboundaries)
library(purrr)
counties <- us_counties(resolution = "high", states="CA")
```
Next we use the `ggspatial` package to add background (change the `zoom=` to a different value to change level of detail, but beware higher values take longer to download) using the `annotation_map_tile` function. Then add `annotation_scale` and `annotation_north_arrow` options to make the map look complete!
```{r FancyMapping, eval=F, echo=T, message=FALSE, warning=FALSE}
# water color version
ggplot() +
annotation_map_tile(zoom = 8) + # this can take a few seconds
geom_sf(data=counties, fill=NA, col="gray50", alpha=0.7) +
scale_color_viridis_d() +
labs(x="Longitude (WGS84)", y="Latitude",
title="Selected USGS Gages with American, Deer, or Bear In Name") +
geom_sf(data=gages_FILTER, fill="dodgerblue", alpha=0.9, pch=21, size=2) +
# spatial-aware automagic scale bar
annotation_scale(location = "bl",style = "ticks") +
theme_bw() +
# spatial-aware automagic north arrow
annotation_north_arrow(width = unit(.3,"in"),
pad_y = unit(.3, "in"),location = "bl",
which_north = "true")
```
```{r FancyMappingOut, eval=T, echo=F, out.height="100%", out.width="100%", fig.retina = 1.5}
include_graphics("img/selected_usgs_gages_w_deer_amer_bear_zoom8.png")
# ggsave(filename = "img/selected_usgs_gages_w_deer_amer_bear_zoom8.pdf",
# width=6, height = 8, units = "in", dpi = 300, device = cairo_pdf)
# ggsave(filename = "img/selected_usgs_gages_w_deer_amer_bear_zoom8.png",
# width=6, height = 8, units = "in", dpi = 300, type = "cairo")
```