[R-sig-Geo] Extracting one cell only with raster's rasterToPoints()

Daniel Rodolphe Schlaepfer dschlaep at uwyo.edu
Fri Sep 5 09:49:26 CEST 2014


Hello all,

I converted a large set of rasters to spatial points. One of the rasters had only one cell that was to be converted; the raster package function rasterToPoints() failed, but the function would work for spatial=FALSE and of course also if there is more than one cell to convert.

Example:
r <- raster(nrow=5, ncol=5)
r[] <- 0
r[1] <- 1
p <- rasterToPoints(x=r, fun=function(x) x >= 1, spatial=TRUE) #Error in (function (classes, fdef, mtable)  : unable to find an inherited method for function ‘coordinates’ for signature ‘"numeric"’
p <- rasterToPoints(x=r, fun=function(x) x >= 1, spatial=FALSE) #This works

r[2] <- 1
p <- rasterToPoints(x=r, fun=function(x) x >= 1, spatial=TRUE) #This works


It appears that there is a small solution to fix the rasterToPoints() function: add drop=FALSE to the xyv object in the return call of the if(spatial){}, i.e., change
return(SpatialPointsDataFrame(coords = xyv[, 1:2], data = v, proj4string = crs))
to
return(SpatialPointsDataFrame(coords = xyv[, 1:2, drop=FALSE], data = v, proj4string = crd))


Sincerely,
Daniel Schlaepfer


My sessionInfo():
#R version 3.1.1 (2014-07-10)
#Platform: x86_64-apple-darwin13.3.0 (64-bit)
#
#locale:
#[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#
#attached base packages:
#[1] stats     graphics  grDevices
#[4] utils     datasets  methods
#[7] base
#
#other attached packages:
#[1] raster_2.2-48 sp_1.0-15
#
#loaded via a namespace (and not attached):
#[1] grid_3.1.1      lattice_0.20-29




-------------------------------------------------------
Daniel Schlaepfer
University of Wyoming, Laramie, WY 82071
-------------------------------------------------------


	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list