-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata-wrangling.qmd
102 lines (81 loc) · 2.37 KB
/
data-wrangling.qmd
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
---
title: "neotomalakes-data+wrangling"
author: "Florencia D'Andrea"
format: html
---
# NeotomaLakes app data wrangling
```{r echo = FALSE}
library(tidyverse) #not a good idea for renv
library(sf)
```
## Countries
```{r}
# I select some countries to try the app.
mdg <- st_read('data/gadm41_MDG.gpkg') |>
st_simplify(preserveTopology = TRUE)
prt <- st_read('data/gadm41_PRT.gpkg') |>
st_simplify(preserveTopology = TRUE)
arg <- st_read('data/gadm41_ARG.gpkg') |>
st_simplify(preserveTopology = TRUE)
jpn <- st_read('data/gadm41_JPN.gpkg') |>
st_simplify(preserveTopology = TRUE)
countries = countries_sf = rbind(jpn, prt, mdg)
# Save country polygons
write_sf(countries_sf, 'data/countries.gpkg')
```
```{r}
st_geometry(countries_sf) <- NULL
```
## NEOTOMADB SITES
```{r}
sites <- read.csv('../Dropbox/neotoma_project/sites_20231101_.csv')
#Some sites are points and other ones are polygons
pointsites <-
sites[stringr::str_detect(sites$geog, pattern = "POINT"),] |>
sf::st_as_sf(wkt = 'geog') |>
select(siteid, sitename, sitedescription, notes, geog) |>
mutate(lonc = sf::st_coordinates(geog)[, 1],
# need this to setView in leaflet
latc = sf::st_coordinates(geog)[, 2])
polysites <-
sites[stringr::str_detect(sites$geog, pattern = "POLYGON"),] |>
sf::st_as_sf(wkt = 'geog') |>
select(siteid, sitename, sitedescription, notes, geog) |>
st_simplify(preserveTopology = TRUE,
dTolerance = 100) |>
mutate(
centroid = st_point_on_surface(geog),
# need this to setView in leaflet
lonc = sf::st_coordinates(centroid)[, 1],
latc = sf::st_coordinates(centroid)[, 2]
) |>
select(-centroid)
datasf <- rbind(pointsites, polysites) |> st_set_crs(4326)
#extra step= removing sites of countries that aren't in the demo
datasf_country = st_filter(datasf, countries_sf)
```
## HYDROLAKESDB
```{r}
lakes <-
st_read('../Dropbox/neotoma_project/HydroLAKES_polys_v10_shp/HydroLAKES_polys_v10_shp') |>
select(
Hylak_id,
Lake_name,
Country,
Vol_total,
Lake_area,
Shore_len,
Depth_avg,
Elevation,
geometry
)
lakes_country <- countries_sf |>
left_join(lakes, by = c('COUNTRY' = 'Country'))
write_sf(lakes_country, "data/lakes_country.gpkg")
```
```{r}
sites_country <-
datasf |> st_join(countries_sf, left = TRUE) |>
tidyr::drop_na(COUNTRY)
write_sf(datasf_country, "data/sites_country.gpkg")
```