Open temospena opened 3 years ago
Due the api request limits, it needs another loop to make one request at a time per linestring.
So, for the full lisbon_road_segments
dataset, we can do:
library(tidyverse)
library(sf)
library(slopes)
library(googleway)
set_key(Sys.getenv("GOOGLE_KEY"))
Roads = st_transform(lisbon_road_segments, 4326)
# mapview::mapview(Roads)
#a df with only corddinates in WGS84 (google does not like others)
Roads_coords = data.frame(st_coordinates(Roads$geom))
names(Roads_coords) = c("lon", "lat", "l")
#a df without geometries
Roads_xyz = Roads
Roads_xyz$geom= NULL
Roads_xyz$bbox= NULL
Roads_xyz$geom= NA
#get elevarion per point, and transform each line in a xyz linestring again
for (i in 1:nrow(Roads)){
google = google_elevation(Roads_coords[Roads_coords$l == i,])
Roads_coords$elev[Roads_coords$l == i] = google$results$elevation
Roads_coords$resol[Roads_coords$l == i] = google$results$resolution
line = Roads_coords[Roads_coords$l == i,]
Roads_xyz$geom[i] = st_as_sf(line, coords = c("lon","lat","elev")) %>% st_combine() %>% st_cast("LINESTRING")
}
Roads_xyz = st_as_sf(Roads_xyz, dim = "XYZ", crs=st_crs(Roads))
Roads$slope = slope_xyz(Roads_xyz) #to store the values in the original dataset
summary(Roads$slope)
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> 0.0004856 0.0064922 0.0279617 0.0455289 0.0780702 0.1995000
summary(Roads_coords$resol) #check average resolution
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> 1.193 1.193 1.193 1.193 1.193 1.193
mapview::mapview(Roads["slope"])
Fantastic, I think that could be the basis of a new function to get slopes from Google's API. Hypothesis: will be faster and more accurate than the current approach that downloads raster DEMs covering the entire area.
Maybe to add as an alternative to ceramic/mapbox? It has a very good resolution depending on the place on earth.
Using googleway to retrieve google elevation: