-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelevation.R
74 lines (58 loc) · 1.8 KB
/
elevation.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
p <-
c("tidyverse",
"httr2",
"jsonlite",
"sf")
library(xfun)
pkg_attach2(p)
rm(p)
# pasted from he-2022.xls
source_tbl <- tibble::tribble(
~siteid, ~easting, ~northing,
685L, 361143, 175464,
686L, 361152, 175673,
687L, 361225, 175818,
688L, 360907, 174857,
689L, 360997, 174898,
690L, 361136, 175114,
691L, 361104, 175051,
692L, 360459, 174495,
693L, 360427, 174439,
694L, 360515, 174431,
695L, 360467, 174302,
696L, 360232, 174386,
697L, 360303, 174380
)
#https://github.com/Jorl17/open-elevation/blob/master/docs/api.md
points_sf <- source_tbl %>%
st_as_sf(coords = c("easting", "northing"), crs = 27700) %>%
st_transform(crs = 4326)
locations <- points_sf %>%
mutate(longitude = unlist(map(.$geometry,1)),
latitude = unlist(map(.$geometry,2))) %>%
st_drop_geometry() %>%
select(-siteid)
locs <- locations %>%
transpose()
l <- tojson(list(locations = locs))
l
# Make the API call
base_url <- "https://api.open-elevation.com/api/v1/lookup"
req <- request(base_url) %>%
req_headers("Accept" = "application/json",
"Content-Type" = "application/json") %>%
req_body_raw(l)
req %>%
req_dry_run()
resp <- req_perform(req)
elevations_tbl <- resp %>%
resp_body_json() %>%
pluck("results") %>%
map_df(~bind_rows(.x))
elevations_tbl %>%
write_csv("data/he_elevations.csv")
# Check if in AQMA ----
aqma_sf <- st_read("https://opendata.bristol.gov.uk/explore/dataset/air-quality-management-areas/download/?format=geojson&timezone=Europe/London&lang=en")
in_aqma_tbl <- st_within(points_sf, aqma_sf) %>%
apply(1, any) %>%
enframe()