-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_db.R
68 lines (57 loc) · 1.9 KB
/
init_db.R
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
library(countrycode)
library(dplyr)
source("config.R")
wdpa_db <- src_postgres(dbname = DBNAME, host = HOST, port = PORT, user = USER,
password = PASSWORD, options="-c search_path=wdpa")
wdpa_poly <- tbl(wdpa_db, "wdpa_poly_geom")
wdpa_point <- tbl(wdpa_db, "wdpa_point_geom")
summarize_land_pa_per_iso3 <- function(data){
summary_data <- data %>%
filter(marine == "0") %>%
group_by(iso3, iucn_cat, status) %>%
summarise(
count = n(),
area_km = sum(rep_area)
) %>%
ungroup() %>%
as.data.frame()
return(summary_data)
}
pa_per_iso3_poly <- summarize_land_pa_per_iso3(wdpa_poly)
pa_per_iso3_poly$type <- "polygon"
pa_per_iso3_point <- summarize_land_pa_per_iso3(wdpa_point)
pa_per_iso3_point$type <- "point"
pa_per_iso3 <- bind_rows(pa_per_iso3_poly, pa_per_iso3_point)
# Get real names
country_names <- countrycode(pa_per_iso3$iso3, "iso3c", "country.name")
for (i in 1:length(country_names)){
if (is.na(country_names[i])) {
country_names[i] <- pa_per_iso3$iso3[i]
} else {
country_names[i] <- paste0(pa_per_iso3$iso3[i], " (", country_names[i],
")")
}
}
pa_per_iso3$iso3_country_name <- country_names
pa_per_iso3 <- pa_per_iso3 %>%
select(iso3_country_name, type, iucn_cat, status, count, area_km) %>%
arrange(iso3_country_name, type, iucn_cat)
pa_cat_global <- pa_per_iso3 %>%
group_by(iucn_cat, type) %>%
summarise(
count = sum(count),
area_km = sum(area_km)
) %>%
ungroup() %>%
mutate(iso3_country_name = "Global") %>%
select(iso3_country_name, type, iucn_cat, count, area_km)
pa_stat_global <- pa_per_iso3 %>%
group_by(status) %>%
summarise(
count = sum(count),
area_km = sum(area_km)
) %>%
ungroup() %>%
mutate(perc = round(area_km / sum(area_km) * 100, 2),
iso3_country_name = "Global") %>%
select(iso3_country_name, status, count, area_km, perc)