I'm getting a strange error when trying to convert a raster with only 1 non-NA pixel to SpatialPixelsDataFrame.

r0 <- raster( matrix( 1, 1,1))
as(r0, 'SpatialPixelsDataFrame')

Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘coordinates’ for signature ‘"numeric"’

And it doesn't seem to depend on the size of the raster

r1 <- raster( matrix( c(1, NA), 1,2))
as(r1, 'SpatialPixelsDataFrame')

Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘coordinates’ for signature ‘"numeric"’

r2 <- raster( matrix( c(1, rep(NA,99)), 10,10))
as(r1, 'SpatialPixelsDataFrame')

Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘coordinates’ for signature ‘"numeric"’

Yet when I include 2 or more non-NA pixels ...

r3 <- raster( matrix( c(1,1, rep(NA,98) ), 10, 10))

as(r3, 'SpatialPixelsDataFrame')

Everything works fine.

Object of class SpatialPixelsDataFrame
Object of class SpatialPixels
Grid topology:
   cellcentre.offset cellsize cells.dim
s1              0.05      0.1        10
s2              0.05      0.1        10
        x    y
[1,] 0.05 0.95
[2,] 0.05 0.85
Coordinate Reference System (CRS) arguments: NA
...blah blah blah

I realize this is a boundary case, but any idea what is going on here?

I'm getting this error only occasionally as part of a larger automated process, so it would be great if there was a way to convert my 1-pixel raster to a SPDF without an elaborate hack.

closed as off-topic by PolyGeo Mar 6 at 11:37

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions that relate to the business of vendors and product teams, such as licensing, pricing, release dates, submission of bug reports and enhancement requests, etc, are off-topic here and should be directed instead to the official software support teams." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Thanks for reporting (and to @Spacdeman for passing this on). Fixed in the development version. – Robert Hijmans Mar 6 at 11:09
  • @PolyGeo - don't know why this question was closed as a bug report, it was a user getting an error that turned out to be a bug. User did not know it was a bug at the time. – Spacedman Mar 7 at 20:46
  • @Spacedman It is closed as a bug report because that is how it appears to have been resolved and that resolution has even been accepted by the asker. Both the question and answer have upvotes that keep it on the site so being closed does not affect its visibility to future searchers. – PolyGeo Mar 7 at 21:04

Bug in the raster package. If your raster has only one non-zero cell, then this line:


 sp <- SpatialPoints(v[,1:2], proj4string= crs(r))

will drop a dimension at v[,1:2]. Changing to v[,1:2, drop=FALSE] should fix it. There's a few other places that this might happen also.

I'll report it via email to the maintainer....

Not the answer you're looking for? Browse other questions tagged or ask your own question.