From 4476a381ff7150fa7d2ad8fcd5d3eb6f39dd47ae Mon Sep 17 00:00:00 2001 From: Robin Lovelace Date: Fri, 3 Sep 2021 23:38:18 +0100 Subject: [PATCH] Reproject result of elevation_get() for #37 --- R/slope_get.R | 22 ++++++++++++++-------- man/elevation_get.Rd | 6 +++++- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/R/slope_get.R b/R/slope_get.R index c17f00c..54f0213 100644 --- a/R/slope_get.R +++ b/R/slope_get.R @@ -15,6 +15,8 @@ #' [README](https://github.com/hypertidy/ceramic#local-caching-of-tiles). #' #' @param ... Options passed to `cc_elevation()` +#' @param output_format What format to return the data in? +#' Accepts `"raster"` (the default) and `"terra"`. #' @inheritParams slope_raster #' @return A raster object with cell values representing elevations in the #' bounding box of the input `routes` object. @@ -27,11 +29,12 @@ #' routes = cyclestreets_route #' e = elevation_get(routes) #' class(e) +#' crs(e) #' e #' plot(e) #' plot(st_geometry(routes), add = TRUE) #' } -elevation_get = function(routes, ...) { +elevation_get = function(routes, ..., output_format = "raster") { if(requireNamespace("ceramic")) { mid_ext = sf_mid_ext_lonlat(routes) bw = max(c(mid_ext$width, mid_ext$height)) / 1 # buffer width @@ -41,13 +44,16 @@ elevation_get = function(routes, ...) { } else { message("Install the package ceramic") } - # issue: cannot convert CRS currently - # cr = sf::st_crs(routes) - # cr$wkt - # raster::crs(e) = raster::crs("+init=epsg:3857") - # raster::projectRaster(e, "+init=epsg:4326") - # raster::projectRaster(e, sf::st_crs(routes)[[2]]) - e + crs_routes = sf::st_crs(routes) + if(!requireNamespace("terra", quietly = TRUE)) { + message('install.packages("terra") # for this to work') + } + et = terra::rast(e) + res = terra::project(et, y = crs_routes$wkt) + if(output_format == "raster") { + res = raster::raster(res) + } + res } sf_mid_ext_lonlat = function(routes) { diff --git a/man/elevation_get.Rd b/man/elevation_get.Rd index ae6852b..497630b 100644 --- a/man/elevation_get.Rd +++ b/man/elevation_get.Rd @@ -4,13 +4,16 @@ \alias{elevation_get} \title{Get elevation data from hosted maptile services} \usage{ -elevation_get(routes, ...) +elevation_get(routes, ..., output_format = "raster") } \arguments{ \item{routes}{Routes, the gradients of which are to be calculated. The object must be of class \code{sf} with \code{LINESTRING} geometries.} \item{...}{Options passed to \code{cc_elevation()}} + +\item{output_format}{What format to return the data in? +Accepts \code{"raster"} (the default) and \code{"terra"}.} } \value{ A raster object with cell values representing elevations in the @@ -39,6 +42,7 @@ library(raster) routes = cyclestreets_route e = elevation_get(routes) class(e) +crs(e) e plot(e) plot(st_geometry(routes), add = TRUE)