user1617676
user1617676

Reputation: 361

rasterFromXYZ generates an unexpected blank line

I converted a raster to a dataframe by rasterToPoints, then converted it back to raster using rasterFromXYZ. An unexpected blank line appeared on the second raster as shown below. Any way to avoid this?

Original raster vs. the raster from rasterFromXYZ

Upvotes: 1

Views: 281

Answers (1)

Robert Hijmans
Robert Hijmans

Reputation: 47071

For example you can do (reproducible example)

library(raster)
f <- system.file("external/test.grd", package="raster")
r <- raster(f)
v <- as.matrix(r)
v <- v * 2
x <- setValues(r, v)

But in most cases it is not advisable to go this route as you can do almost any operation on the Raster objects themselves.

With your data I do:

r <- raster("bv2.tif")
x <- rasterToPoints(r)
z <- rasterFromXYZ(x)
e <- extent(-121.4,-114.6,49,50.4)
zoom(z, e)

And there is no white line. Zooming in further does not show an irregularity either. So this may be a problem with the tool you used to produce your maps.

This is confirmed by

sum(values(z) != values(r), na.rm=TRUE)
# [1] 0
cellStats(r, 'countNA')
# [1] 2130763
cellStats(z, 'countNA')
# [1] 2130763

Upvotes: 3

Related Questions