ropensci / slopes

Package to calculate slopes of roads, rivers and trajectories
https://docs.ropensci.org/slopes/
GNU General Public License v3.0
70 stars 6 forks source link

Error message when running readme #25

Closed Robinlovelace closed 3 years ago

Robinlovelace commented 3 years ago

I get this:

image

Robinlovelace commented 3 years ago

Reproducible example:

remotes::install_github("itsleeds/slopes")
#> Using github PAT from envvar GITHUB_PAT
#> Skipping install of 'slopes' from a github remote, the SHA1 (4e2ebc5f) has not changed since last install.
#>   Use `force = TRUE` to force installation
library(slopes)
lisbon_route_3d_auto = slope_3d(r = lisbon_route)
#> Loading required namespace: ceramic
#> Error in find_xy_cols(obj): Unable to determine longitude and latitude columns; perhaps try re-naming columns.

Created on 2021-02-02 by the reprex package (v1.0.0)

Robinlovelace commented 3 years ago

Going to try running the contents of the function line-by-line:

slope_3d = function(r, e = NULL, method = "bilinear", terra = has_terra() && methods::is(e, "SpatRaster")) {
  # if("geom" %in% names(r)) {
  #   rgeom = r$geom
  # } else if("geometry" %in% names(r)) {
  #   rgeom = r$geometry
  # } else {
  #   rgeom = sf::st_geometry(r)
  # }
  if(is.null(e)) {
    e = elevations_get(r)
    r_original = r # create copy to deal with projection issues
    r = sf::st_transform(r, raster::crs(e))
    suppressWarnings({sf::st_crs(r) = sf::st_crs(r_original)})
    # plot(e)
    # plot(r$geometry, add = TRUE)
    m = sf::st_coordinates(r)
    mo = sf::st_coordinates(r_original)
    z = as.numeric(elevation_extract(m, e, method = method, terra = terra))
    m_xyz = cbind(mo[, 1:2], z)
  } else {
    m = sf::st_coordinates(r)
    z = as.numeric(elevation_extract(m, e, method = method, terra = terra))
    m_xyz = cbind(m[, 1:2], z)
  }
  n = nrow(r)

  if(n == 1) {
    # currently only works for 1 line, to be generalised

    rgeom3d_line = sf::st_linestring(m_xyz)
    rgeom3d_sfc = sf::st_sfc(rgeom3d_line, crs = sf::st_crs(r))
    # message("Original geometry: ", ncol(rgeom[[1]]))
    sf::st_geometry(r) = rgeom3d_sfc
    # message("New geometry: ", ncol(r$geom[[1]]))
  } else {
    linestrings = lapply(seq(n), function(i){
      rgeom3d_line = sf::st_linestring(m_xyz[m[, 3] == i, ])
    })
    rgeom3d_sfc = sf::st_sfc(linestrings, crs = sf::st_crs(r))
    sf::st_geometry(r) = rgeom3d_sfc
  }
  r
}
Robinlovelace commented 3 years ago

Seems it's an issue with elevations_get():

# slope_3d = function(r, e = NULL, method = "bilinear", terra = has_terra() && methods::is(e, "SpatRaster")) {

r = slopes::lisbon_route
method = "bilinear"
terra = FALSE

  # if("geom" %in% names(r)) {
  #   rgeom = r$geom
  # } else if("geometry" %in% names(r)) {
  #   rgeom = r$geometry
  # } else {
  #   rgeom = sf::st_geometry(r)
  # }
  # if(is.null(e)) {
    e = elevations_get(r)
#> Error in elevations_get(r): could not find function "elevations_get"

Created on 2021-02-02 by the reprex package (v1.0.0)

Robinlovelace commented 3 years ago

And from that it seems the error is:

r = slopes::lisbon_route
mid_ext = slopes:::sf_mid_ext_lonlat(r)
#> Error in find_xy_cols(obj): Unable to determine longitude and latitude columns; perhaps try re-naming columns.

Created on 2021-02-02 by the reprex package (v1.0.0)

Robinlovelace commented 3 years ago

And from that function...

r = slopes::lisbon_route
# mid_ext = slopes:::sf_mid_ext_lonlat(r)
res = list()
if(!sf::st_is_longlat(r)) {
  r = sf::st_transform(r, 4326)
}
bb = sf::st_bbox(r)
res$midpoint = c(mean(c(bb[1], bb[3])), mean(c(bb[2], bb[4])))
res$width = geodist::geodist(c(bb[1], bb[2]), c(bb[3], bb[2]))
res$height = geodist::geodist(c(bb[1], bb[2]), c(bb[1], bb[4]))
r = slopes::lisbon_route
r = sf::st_transform(r, 4326)
bb = sf::st_bbox(r)
geodist::geodist(c(bb[1], bb[2]), c(bb[3], bb[2]))
#>          [,1]
#> [1,] 1329.777
geodist::geodist(c(bb[1], bb[2]), c(bb[1], bb[4]))
#> Error in find_xy_cols(obj): Unable to determine longitude and latitude columns; perhaps try re-naming columns.

Created on 2021-02-02 by the reprex package (v1.0.0)