-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathget_geodata.R
138 lines (97 loc) · 4.03 KB
/
get_geodata.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
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
# Packages -----------------------------------------------------------------------------
require(httr)
require(stringr)
# Functions ----------------------------------------------------------------------------
check_geolevel <- function(geolevel, available_geolevels) {
if (!geolevel %in% available_geolevels) stop("Please select valid 'geolevel'.")
}
check_api_call <- function(call_res) {
if (httr::http_error(call_res)) stop("The API does not respond properly. Do you have an internet connection and an open proxy?")
}
call_api_geodata <- function() {
# Call
res <- httr::GET("https://opendata.swiss/api/3/action/package_show?id=geodaten-zu-den-eidgenoessischen-abstimmungsvorlagen")
# Check
check_api_call(res)
# Return
return(res)
}
get_geodata <- function(geolevel = "municipality", latest = T, verbose = F, call_res) {
# Check input
check_geolevel(geolevel, available_geolevels = c("national", "canton", "district", "municipality", "zh_counting_districts", "lakes"))
# Call geodata api
if (missing(call_res)) call_res <- call_api_geodata()
cnt <- httr::content(call_res)
# Get info
if (latest) {
gdInfo <- cnt[["result"]][["resources"]][[2]][["title"]]
gdUrl <- cnt[["result"]][["resources"]][[2]][["download_url"]]
gdLayers <- sf::st_layers(gdUrl)[1][["name"]]
} else {
gdInfo <- cnt[["result"]][["resources"]][[1]][["title"]]
gdUrl <- cnt[["result"]][["resources"]][[1]][["download_url"]]
gdLayers <- sf::st_layers(gdUrl)[1][["name"]]
}
if (verbose) cat(paste0(gdInfo[!gdInfo == ""], collapse = "\n"), "\n\n")
# Load geodata and join votes
if (geolevel == "municipality") {
# Load
gd <- sf::st_read(gdUrl, layer = gdLayers[stringr::str_detect(gdLayers, "voge_")], quiet = T)
# Mutate if variable vogenr exists
if ("vogenr" %in% names(gd)) {
gd <- gd %>%
dplyr::mutate(id = vogenr) %>%
dplyr::select(-vogenr)
}
if ("vogeId" %in% names(gd)) {
gd <- gd %>%
dplyr::mutate(id = vogeId) %>%
dplyr::select(-vogeId)
}
# Adjust variable mun_id
gd <- gd %>%
dplyr::rename(mun_id = id) %>%
dplyr::mutate(mun_id = as.character(mun_id)) %>%
dplyr::select(mun_id, geometry)
}
if (geolevel == "district") {
# Load
gd <- sf::st_read(gdUrl, layer = gdLayers[stringr::str_detect(gdLayers, "bezk_")], quiet = T)
# Mutate if variable vogenr exists
if ("bezkId" %in% names(gd)) {
gd <- gd %>%
dplyr::mutate(id = bezkId) %>%
dplyr::select(-bezkId)
}
# Adjust variable district_id
gd <- gd %>%
dplyr::rename(district_id = id) %>%
dplyr::mutate(district_id = as.character(district_id)) %>%
dplyr::select(district_id, geometry)
}
if (geolevel == "canton") {
# Load
gd <- sf::st_read(gdUrl, layer = gdLayers[stringr::str_detect(gdLayers, "kant_")], quiet = T) %>%
dplyr::rename(canton_id = id) %>%
dplyr::rename(canton_name = name) %>%
dplyr::mutate(canton_id = as.character(canton_id)) %>%
dplyr::select(canton_id, geometry)
}
if (geolevel == "zh_counting_districts") {
gd <- sf::st_read(gdUrl, layer = gdLayers[stringr::str_detect(gdLayers, "zaelhlkreise_")], quiet = T) %>%
dplyr::rename(mun_id = id) %>%
dplyr::rename(mun_name = name) %>%
dplyr::mutate(mun_id = as.character(mun_id)) %>%
dplyr::select(mun_id, geometry)
}
if (geolevel == "lakes") {
gd <- sf::st_read(gdUrl, layer = gdLayers[stringr::str_detect(gdLayers, "seen_")], quiet = T) %>%
dplyr::select(id, geometry)
}
if (geolevel == "national") {
gd <- sf::st_read(gdUrl, layer = gdLayers[stringr::str_detect(gdLayers, "suis_")], quiet = T) %>%
dplyr::select(id, geometry)
}
# Return
return(gd)
}