I am attempting to do a raster to polygon conversion using the stars package. The workflow i've seen elsewhere (and have used successfully previously) is on my "zones" raster is:
zones_stars <- st_as_stars(zones) %>% st_as_sf(merge = TRUE)
When I break this pipeline apart for diagnostics, the first step (st_as_stars) seems to result in maintenance of original object extents in raster format.
(Original) Zones raster:
class : RasterLayer
dimensions : 2744, 5907, 16208808 (nrow, ncol, ncell)
resolution : 0.02, 0.02 (x, y)
extent : 488532.1, 488650.3, 4424048, 4424103 (xmin, xmax, ymin, ymax)
crs : +proj=utm +zone=18 +datum=NAD83 +units=m +no_defs
source : memory
names : layer
values : 172, 12263 (min, max)
Output of st_as_stars:
attribute(s), summary of first 1e+05 cells:
Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
layer NA NA NA NaN NA NA 1e+05
dimension(s):
from to offset delta refsys point values x/y
x 1 5907 488532 0.02 +proj=utm +zone=18 +datum... NA NULL [x]
y 1 2744 4424103 -0.02 +proj=utm +zone=18 +datum... NA NULL [y]
So far so good. The second step (st_as_sf), converts to vector polygons but introduces a several meter offset:
geometry type: POLYGON
dimension: XY
bbox: xmin: 488532.1 ymin: 4424051 xmax: 488650.3 ymax: 4424102
CRS: +proj=utm +zone=18 +datum=NAD83 +units=m +no_defs
First 10 features:
layer geometry
1 332 POLYGON ((488648.2 4424102,...
2 335 POLYGON ((488647.7 4424102,...
3 172 POLYGON ((488646.1 4424101,...
4 338 POLYGON ((488646.9 4424101,...
Note particularly, the ymin value shift from original to final output. Any thoughts on this?
Here is the original zones data, if you'd like to try to re-create: https://bit.ly/31yPfhz
EDIT: The issue appears to be related to the inclusion of NA values in the raster. If you convert those to a number (e.g., 0) then there is no issue. I do not know if this applies more generally or is data-specific.