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

Unhelpful error message when routes have no CRS #36

Closed Robinlovelace closed 3 years ago

Robinlovelace commented 3 years ago

Example:

remotes::install_github("itsleeds/slopes")
#> Using github PAT from envvar GITHUB_PAT
#> Skipping install of 'slopes' from a github remote, the SHA1 (fe5f12da) has not changed since last install.
#>   Use `force = TRUE` to force installation
library(slopes)
library(sf)
#> Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
routes = lisbon_road_segments[1:3, ]
st_crs(routes)
#> Coordinate Reference System:
#>   User input: ETRS89 / Portugal TM06 
#>   wkt:
#> PROJCRS["ETRS89 / Portugal TM06",
#>     BASEGEOGCRS["ETRS89",
#>         DATUM["European Terrestrial Reference System 1989",
#>             ELLIPSOID["GRS 1980",6378137,298.257222101,
#>                 LENGTHUNIT["metre",1]]],
#>         PRIMEM["Greenwich",0,
#>             ANGLEUNIT["degree",0.0174532925199433]],
#>         ID["EPSG",4258]],
#>     CONVERSION["Portugual TM06",
#>         METHOD["Transverse Mercator",
#>             ID["EPSG",9807]],
#>         PARAMETER["Latitude of natural origin",39.6682583333333,
#>             ANGLEUNIT["degree",0.0174532925199433],
#>             ID["EPSG",8801]],
#>         PARAMETER["Longitude of natural origin",-8.13310833333333,
#>             ANGLEUNIT["degree",0.0174532925199433],
#>             ID["EPSG",8802]],
#>         PARAMETER["Scale factor at natural origin",1,
#>             SCALEUNIT["unity",1],
#>             ID["EPSG",8805]],
#>         PARAMETER["False easting",0,
#>             LENGTHUNIT["metre",1],
#>             ID["EPSG",8806]],
#>         PARAMETER["False northing",0,
#>             LENGTHUNIT["metre",1],
#>             ID["EPSG",8807]]],
#>     CS[Cartesian,2],
#>         AXIS["easting (X)",east,
#>             ORDER[1],
#>             LENGTHUNIT["metre",1]],
#>         AXIS["northing (Y)",north,
#>             ORDER[2],
#>             LENGTHUNIT["metre",1]],
#>     USAGE[
#>         SCOPE["unknown"],
#>         AREA["Portugal - mainland - onshore"],
#>         BBOX[36.95,-9.56,42.16,-6.19]],
#>     ID["EPSG",3763]]
st_crs(routes) = NA
dem = dem_lisbon_raster
(s = slope_raster(routes, dem))
#> Error in if (lonlat) {: missing value where TRUE/FALSE needed

Created on 2021-08-17 by the reprex package (v2.0.1)

Robinlovelace commented 3 years ago

Updated error message is clearer:

remotes::install_github("itsleeds/slopes")
#> Using github PAT from envvar GITHUB_PAT
#> Skipping install of 'slopes' from a github remote, the SHA1 (d7869d0e) has not changed since last install.
#>   Use `force = TRUE` to force installation
library(slopes)
library(sf)
#> Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
routes = lisbon_road_segments[1:3, ]
st_crs(routes)
#> Coordinate Reference System:
#>   User input: ETRS89 / Portugal TM06 
#>   wkt:
#> PROJCRS["ETRS89 / Portugal TM06",
#>     BASEGEOGCRS["ETRS89",
#>         DATUM["European Terrestrial Reference System 1989",
#>             ELLIPSOID["GRS 1980",6378137,298.257222101,
#>                 LENGTHUNIT["metre",1]]],
#>         PRIMEM["Greenwich",0,
#>             ANGLEUNIT["degree",0.0174532925199433]],
#>         ID["EPSG",4258]],
#>     CONVERSION["Portugual TM06",
#>         METHOD["Transverse Mercator",
#>             ID["EPSG",9807]],
#>         PARAMETER["Latitude of natural origin",39.6682583333333,
#>             ANGLEUNIT["degree",0.0174532925199433],
#>             ID["EPSG",8801]],
#>         PARAMETER["Longitude of natural origin",-8.13310833333333,
#>             ANGLEUNIT["degree",0.0174532925199433],
#>             ID["EPSG",8802]],
#>         PARAMETER["Scale factor at natural origin",1,
#>             SCALEUNIT["unity",1],
#>             ID["EPSG",8805]],
#>         PARAMETER["False easting",0,
#>             LENGTHUNIT["metre",1],
#>             ID["EPSG",8806]],
#>         PARAMETER["False northing",0,
#>             LENGTHUNIT["metre",1],
#>             ID["EPSG",8807]]],
#>     CS[Cartesian,2],
#>         AXIS["easting (X)",east,
#>             ORDER[1],
#>             LENGTHUNIT["metre",1]],
#>         AXIS["northing (Y)",north,
#>             ORDER[2],
#>             LENGTHUNIT["metre",1]],
#>     USAGE[
#>         SCOPE["unknown"],
#>         AREA["Portugal - mainland - onshore"],
#>         BBOX[36.95,-9.56,42.16,-6.19]],
#>     ID["EPSG",3763]]
st_crs(routes) = NA
dem = dem_lisbon_raster
(s = slope_raster(routes, dem))
#> Error in slope_raster(routes, dem): CRS of routes not known. Set the CRS, e.g. as follows:
#> sf::st_crs(routes) = 4326 # if the routes are in lon/lat coordinates
cor(routes$Avg_Slope, s)
#> Error in is.data.frame(y): object 's' not found

Created on 2021-08-17 by the reprex package (v2.0.1)