Closed alexyshr closed 5 months ago
The problem seems to be limited to a certain type of stars
geometries. For example, the following outputs the correct raster extent when performing an IDW interpolation.
library(sf)
library(stars)
library(gstat)
point <- st_as_sf(st_as_sfc( "MULTIPOINT ((20 20), (20 80), (80 80), (80 20))" ))
point <- st_cast(point, "POINT")
point$z <- c(1,2,3,2)
extent <- st_as_sfc( "POLYGON ((0 0, 0 100, 100 100, 100 0, 0 0))" )
extent <- st_as_sf(extent)
r <- st_rasterize(extent, nx = 100, ny = 100)
out <- idw(z ~ 1, point, newdata = r)
The output looks like this:
image(out, axes = TRUE)
plot(point, pch = 16, col = "red", add = TRUE)
grid()
The original raster overlaps properly, as expected. (It's shown in a semi-transparent blue raster in the following image).
image(r, add = TRUE, col=rgb(0,0,1,0.3))
The extents match, as expected.
st_bbox(out)
xmin ymin xmax ymax
0 0 100 100
st_bbox(r)
xmin ymin xmax ymax
0 0 100 100
Now, a change in a couple of corners of the raster's extent forces a shift in the interpolated raster's y-axis
#' The following geometry generates an interpolated raster that is shifted down
extent <- st_as_sfc( "POLYGON ((10 20, 15 85, 100 100, 100 0, 10 20))" )
extent <- st_as_sf(extent)
r <- st_rasterize(extent, nx = 100, ny = 100)
out <- idw(z ~ 1, point, newdata = r)
# Here's the interpolated raster
image(out, axes = TRUE , ylim = c(-20,100))
plot(point, pch = 16, col = "red", add = TRUE)
grid()
#'The output raster is shifted down
image(r, add = TRUE, col=rgb(0,0,1,0.3))
The extents differ.
st_bbox(out)
xmin ymin xmax ymax
10.00 -9.95 100.00 90.05
st_bbox(r)
xmin ymin xmax ymax
10 0 100 100
I've played with a few other simple geometries, but can't nail down a pattern. For the example, the following extents work:
extent <- st_as_sfc( "POLYGON ((10 20, 0 100, 100 100, 100 0, 10 20))" ) #works
extent <- st_as_sfc( "POLYGON ((0 0, 10 80, 100 100, 100 0, 0 0))" ) #works
and the following extents fail:
extent <- st_as_sfc( "POLYGON ((10 20, 20 50, 10 90, 100 100, 80 50, 100 0, 10 20))" ) # fails
extent <- st_as_sfc( "POLYGON ((20 0, 50 100, 100 0, 20 0))" ) # fails
Versions:
gstat
= 2.1.1
stars
= 0.6.1
sf
= 1.0.13
@mgimond I'm sorry for not getting back to you sooner. I need to apologize because my code has errors (that can be avoided). Your code is easier and better to explain the issue. Thank you!
Thanks so much for the great reports, and persistence. The virtue of copy-and-paste. This happened when a stars
object had non-square cells.
Dear Dr. Edzer,
I hope your Python learning curve goes fast to improve your wonderful R packages. I knew about your new academic path some weeks ago by checking your GitHub account. Wonderful!
I am simulating a spatial random process from the Gaussian model given an exponential covariance model. Then I am extracting a sample to perform geostatistics and check the resulting covariance vs. the real one. I am using only
sf
andstars
. The issue is that the final map is shifted on the y-axis. I did it first with noCRS
(lat, long numbers), and the displacement was even bigger, then I changed to projected coordinates, and the issue persist!Thank you for your support!
Best regards
Created on 2023-03-08 with reprex v2.0.2