Skip to content

Commit

Permalink
Merge pull request #31 from ITSLeeds/rev2
Browse files Browse the repository at this point in the history
Respond to rev2
  • Loading branch information
Robinlovelace authored Jun 21, 2021
2 parents d55a491 + 03a963b commit f127ac1
Show file tree
Hide file tree
Showing 27 changed files with 517 additions and 355 deletions.
10 changes: 7 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,23 @@ RoxygenNote: 7.1.1
Imports:
sf,
raster,
methods
methods,
pbapply
Depends:
R (>= 2.10)
Suggests:
geodist,
pbapply,
terra,
colorspace,
knitr,
rmarkdown,
ceramic,
bookdown,
covr,
testthat
testthat,
osmextract,
stplanr,
dplyr,
rgdal
VignetteBuilder: knitr
Config/testthat/edition: 3
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

export(elevation_extract)
export(elevations_get)
export(plot_dz)
export(plot_slope)
export(sequential_dist)
export(slope_3d)
Expand Down
34 changes: 14 additions & 20 deletions R/plot_slope.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#' Plot slope data for a 3d linestring with base R graphics
#'
#' @param r A linestring with xyz dimensions
#' @inheritParams slope_raster
#' @inheritParams plot_dz
#' @inheritParams sequential_dist
#'
#' @export
#' @examples
#' plot_slope(lisbon_route_3d)
#' r = lisbon_road_segment_3d
#' plot_slope(r)
#' r = lisbon_road_segment_3d
plot_slope = function(r,
lonlat = sf::st_is_longlat(r),
#' routes = lisbon_road_segment_3d
#' plot_slope(routes)
#' routes = lisbon_road_segment_3d
plot_slope = function(routes,
lonlat = sf::st_is_longlat(routes),
fill = TRUE,
horiz = FALSE,
p = ifelse(
Expand All @@ -27,7 +27,7 @@ plot_slope = function(r,
title = "Slope colors (percentage gradient)",
s = 3:18,
ncol = 4) {
dz = distance_z(r, lonlat = lonlat)
dz = distance_z(routes, lonlat = lonlat)
plot_dz(dz$d, dz$z, fill = fill)
}
#' Plot a digital elevation profile based on xyz data
Expand All @@ -48,14 +48,14 @@ plot_slope = function(r,
#' @param ncol Number of columns in legend
#' @param horiz Should the legend be horizontal (`FALSE` by default)
#' @param ... Additional parameters to pass to legend
#' @export
#' @examples
#' r = lisbon_road_segment_3d
#' m = sf::st_coordinates(r)
#' routes = lisbon_road_segment_3d
#' m = sf::st_coordinates(routes)
#' d = cumsum(sequential_dist(m, lonlat = FALSE))
#' d = c(0, d)
#' z = m[, 3]
#' plot_dz(d, z)
#' # not exported
#' # plot_dz(d, z)
plot_dz = function(d,
z,
fill = TRUE,
Expand All @@ -78,7 +78,7 @@ plot_dz = function(d,
if (fill) {
b = make_breaks(brks)
pal = make_pal(p, b)
g = slope_vector(x = d, e = z)
g = slope_vector(x = d, elevations = z)
colz = make_colz(g, b, pal)
lapply(seq(d)[-(length(d))], function(i) {
graphics::polygon(
Expand All @@ -95,15 +95,9 @@ plot_dz = function(d,
ncol = ncol, cex = cex)
}
}
# g = slope_matrix(m)
# abline(h = 0, lty = 2)
# points(x[-length(x)], gx, col = "red")
# points(x[-length(x)], gxy, col = "blue")
# title("Distance (in x coordinates) elevation profile",
# sub = "Points show calculated gradients of subsequent lines")

distance_z = function(r, lonlat) {
m = sf::st_coordinates(r)
distance_z = function(routes, lonlat) {
m = sf::st_coordinates(routes)
d = cumsum(sequential_dist(m, lonlat = lonlat))
d = c(0, d)
z = m[, 3]
Expand Down
31 changes: 11 additions & 20 deletions R/slope_get.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,35 @@
#' @examples
#' # time-consuming, and see
#' # https://github.com/ITSLeeds/slopes/runs/648128378#step:18:107
#' r = cyclestreets_route
#' # e = elevations_get(r)
#' routes = cyclestreets_route
#' # e = elevations_get(routes)
#' # class(e)
#' # e
#' # plot(e)
#' # plot(sf::st_geometry(r), add = TRUE)
elevations_get = function(r, file = NULL, ...) {
#' # plot(sf::st_geometry(routes), add = TRUE)
elevations_get = function(routes, file = NULL, ...) {
if(requireNamespace("ceramic")) {
mid_ext = sf_mid_ext_lonlat(r)
mid_ext = sf_mid_ext_lonlat(routes)
bw = max(c(mid_ext$width, mid_ext$height)) / 1 # buffer width
e = ceramic::cc_elevation(loc = mid_ext$midpoint, buffer = bw, ...)
} else {
message("Install the package ceramic")
}
# issue: cannot convert CRS currently
# cr = sf::st_crs(r)
# 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(r)[[2]])
# raster::projectRaster(e, sf::st_crs(routes)[[2]])
e
}
#
# trace_projected = sf::st_transform(trace, 3857)
# plot(e)
# plot(trace_projected$geometry, add = TRUE)
#
# # aim: get max distance from centrepoint
# bb = sf::st_bbox(sf::st_transform(trace, 4326))

# # max of those 2 and divide by 2

sf_mid_ext_lonlat = function(r) {
sf_mid_ext_lonlat = function(routes) {
res = list()
if(!sf::st_is_longlat(r)) {
r = sf::st_transform(r, 4326)
if(!sf::st_is_longlat(routes)) {
routes = sf::st_transform(routes, 4326)
}
bb = sf::st_bbox(r)
bb = sf::st_bbox(routes)
res$midpoint = c(mean(c(bb[1], bb[3])), mean(c(bb[2], bb[4])))
res$width = geodist::geodist(c(x = bb[1], y = bb[2]), c(x = bb[3], y = bb[2]))
res$height = geodist::geodist(
Expand Down
Loading

0 comments on commit f127ac1

Please sign in to comment.