New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot get xy coordinates of a raster object imported with terra #29
Comments
Another reproducible example that reads-in the vector dataset (terra's read functionality seems very efficient btw). Note: I need estimated elevations at each vertex of the linestring, but was also having issues with the # Aim: get gradients at xy locations with bilinear interpolation
remotes::install_github("rspatial/terra")
#> Using github PAT from envvar GITHUB_PAT
#> Skipping install of 'terra' from a github remote, the SHA1 (a36cf92e) has not changed since last install.
#> Use `force = TRUE` to force installation
library(terra)
#> This is version 0.5.12 of the "terra" package, for evaluation only
download.file("https://github.com/geocompr/d/releases/download/1/r1.zip", "r1.zip")
unzip("r1.zip")
dem = terra::rast("r1/hdr.adf")
download.file("https://github.com/geocompr/d/releases/download/1/example_route_2d.gpkg", "example_route_2d.gpkg", mode="wb")
v = vect("example_route_2d.gpkg")
plot(v) # terra::extract(dem, v, method = "bilinear") # fails Created on 2020-04-25 by the reprex package (v0.3.0) |
In the first chunk you probably want
|
It's working 🎉 # Aim: get gradients at xy locations with bilinear interpolation
# See https://github.com/rspatial/terra/issues/29
remotes::install_github("rspatial/terra")
#> Using github PAT from envvar GITHUB_PAT
#> Skipping install of 'terra' from a github remote, the SHA1 (35f7d051) has not changed since last install.
#> Use `force = TRUE` to force installation
library(terra)
#> This is version 0.6.1 of the "terra" package, for evaluation only
download.file("https://github.com/geocompr/d/releases/download/1/r1.zip", "r1.zip")
unzip("r1.zip")
dem = terra::rast("r1/hdr.adf")
download.file("https://github.com/geocompr/d/releases/download/1/example_route_2d.gpkg", "example_route_2d.gpkg")
v = vect("example_route_2d.gpkg")
plot(v) g = geom(v)
head(g)
#> id part x y hole
#> 1 1 1 -92539.1 -106077.5 0
#> 2 1 1 -92529.4 -106078.7 0
#> 3 1 1 -92519.7 -106079.9 0
#> 4 1 1 -92510.0 -106081.1 0
#> 5 1 1 -92500.3 -106082.4 0
#> 6 1 1 -92490.6 -106083.6 0
z = terra::extract(dem, g[, c("x", "y")], method = "bilinear") # fails
z
#> hdr
#> [1,] 90.943
#> [2,] 90.462
#> [3,] 90.283
#> [4,] 90.246
#> [5,] 90.200
#> [6,] 90.144
#> [7,] 90.087
#> [8,] 90.031
#> [9,] 90.000
#> [10,] 90.000
#> [11,] 88.687
#> [12,] 87.367
dem_matrix = xyFromCell(dem, 1:ncell(dem))
head(dem_matrix)
#> x y
#> [1,] -113450 -79570
#> [2,] -113440 -79570
#> [3,] -113430 -79570
#> [4,] -113420 -79570
#> [5,] -113410 -79570
#> [6,] -113400 -79570 Created on 2020-04-26 by the reprex package (v0.3.0) |
@Robinlovelace idea - create some benchmarks |
Equivalent code in raster with timings # Aim: get gradients at xy locations with bilinear interpolation
library(raster)
#> Loading required package: sp
download.file("https://github.com/geocompr/d/releases/download/1/r1.zip", "r1.zip")
unzip("r1.zip")
tictoc::tic()
dem = raster("r1/hdr.adf")
tictoc::toc()
#> 0.072 sec elapsed
download.file("https://github.com/geocompr/d/releases/download/1/example_route_2d.gpkg", "example_route_2d.gpkg")
tictoc::tic()
v = rgdal::readOGR("example_route_2d.gpkg")
#> Warning in OGRSpatialRef(dsn, layer, morphFromESRI = morphFromESRI, dumpSRS =
#> dumpSRS, : Discarded datum European_Terrestrial_Reference_System_1989 in CRS
#> definition: +proj=tmerc +lat_0=39.6682583333333 +lon_0=-8.13310833333333 +k=1
#> +x_0=0 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs
#> OGR data source with driver: GPKG
#> Source: "/tmp/RtmpUnnj6J/reprex58e81cf15c2c/example_route_2d.gpkg", layer: "example_route_2d"
#> with 1 features
#> It has 0 fields
plot(v) g = geom(v)
head(g)
#> object part cump x y
#> [1,] 1 1 1 -92539.1 -106077.5
#> [2,] 1 1 1 -92529.4 -106078.7
#> [3,] 1 1 1 -92519.7 -106079.9
#> [4,] 1 1 1 -92510.0 -106081.1
#> [5,] 1 1 1 -92500.3 -106082.4
#> [6,] 1 1 1 -92490.6 -106083.6
tictoc::toc()
#> 0.192 sec elapsed
tictoc::tic()
z = extract(dem, g[, c("x", "y")], method = "bilinear") # fails
tictoc::toc()
#> 0.1 sec elapsed
tictoc::tic()
dem_matrix = xyFromCell(dem, 1:ncell(dem))
tictoc::toc()
#> 0.258 sec elapsed
head(dem_matrix)
#> x y
#> [1,] -113450 -79570
#> [2,] -113440 -79570
#> [3,] -113430 -79570
#> [4,] -113420 -79570
#> [5,] -113410 -79570
#> [6,] -113400 -79570 Created on 2020-04-26 by the reprex package (v0.3.0) |
Result: # Aim: get gradients at xy locations with bilinear interpolation
# See https://github.com/rspatial/terra/issues/29
remotes::install_github("rspatial/terra")
#> Using github PAT from envvar GITHUB_PAT
#> Skipping install of 'terra' from a github remote, the SHA1 (35f7d051) has not changed since last install.
#> Use `force = TRUE` to force installation
library(terra)
#> This is version 0.6.1 of the "terra" package, for evaluation only
download.file("https://github.com/geocompr/d/releases/download/1/r1.zip", "r1.zip")
unzip("r1.zip")
tictoc::tic()
dem = terra::rast("r1/hdr.adf")
tictoc::toc()
#> 0.003 sec elapsed
download.file("https://github.com/geocompr/d/releases/download/1/example_route_2d.gpkg", "example_route_2d.gpkg")
tictoc::tic()
v = vect("example_route_2d.gpkg")
plot(v) g = geom(v)
head(g)
#> id part x y hole
#> 1 1 1 -92539.1 -106077.5 0
#> 2 1 1 -92529.4 -106078.7 0
#> 3 1 1 -92519.7 -106079.9 0
#> 4 1 1 -92510.0 -106081.1 0
#> 5 1 1 -92500.3 -106082.4 0
#> 6 1 1 -92490.6 -106083.6 0
tictoc::toc()
#> 0.062 sec elapsed
tictoc::tic()
z = terra::extract(dem, g[, c("x", "y")], method = "bilinear") # fails
tictoc::toc()
#> 0.003 sec elapsed
tictoc::tic()
dem_matrix = xyFromCell(dem, 1:ncell(dem))
tictoc::toc()
#> 0.99 sec elapsed
head(dem_matrix)
#> x y
#> [1,] -113450 -79570
#> [2,] -113440 -79570
#> [3,] -113430 -79570
#> [4,] -113420 -79570
#> [5,] -113410 -79570
#> [6,] -113400 -79570 Created on 2020-04-26 by the reprex package (v0.3.0) |
Heads-up @rhijmans tictoc::tic()
dem_matrix = xyFromCell(dem, 1:ncell(dem))
tictoc::toc()
#> 0.99 sec elapsed |
Context: I have discovered that raster's
extract()
function can exactly reproduce the elevation profiles generated by proprietary software in functionality to calculate the gradient, slope, of linestrings. I plan to implement this, as per ropensci/stplanr#392The problem with the current implementation: it takes an hour to run single threaded on a regional example and could take days on national road datasets. I'm hoping
terra
could be the tool for the job, reasonable hope?Reproducible example that I hope supports development efforts (I know this is work in progress):
Created on 2020-04-25 by the reprex package (v0.3.0)
The text was updated successfully, but these errors were encountered: