1

I want to rasterize a SpatialPolygonsDataframe derived from a tessellation algorithm and get the following error:

Error in if (x2a < rxmn) { : missing value where TRUE/FALSE needed

The data for creating the tessellation comes from a random percolation map and contains only 0 and 1. The particular thing here is, that the error seems to appear only for a certain arrangement for the 0 and 1 - and it is not quite obvious to me what is causing it. It appears that .polygonsToRaster assumes that the polygon is showing holes in its geometry?

The code to reproduce the error:

library(dismo); library(deldir); library(igraph)

m <- matrix(c(1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0,
  0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1,
  0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 
  0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 
  1, 1, 0, 0), ncol=10, nrow=10)

r <- raster::raster(m)

# Cluster identification (clustering of adjoining pixels) ----
clusters <- raster::clump(r, direction = 4)
# Convert cells with values to points
cluster_points <- raster::rasterToPoints(clusters, spatial = TRUE)
# Create voronoi polygons
tess <- dismo::voronoi(cluster_points)

# Convert to raster
newr <- raster::raster(nrow = 10, ncol = 10, ext = raster::extent(c(0,1,0,1)))
tess_r <- raster::rasterize(tess, newr, field ='clumps', fun = "mean")

Any ideas on how to fix it or another way to rasterize the polygon?

EDIT Spacedman solved the error, but his solution leads to another problem:

The initial number of cells is 10x10 - after his rasterization, there are 11x11 cells. When I now try to crop the spatialpolygonsdataframe to an extent of c(0,1,0,1) (the same as the raster has) and try rasterize:

randomcluster_spdf <- raster::crop(r, extent(0, 1, 0, 1))
# Convert to raster ----
randomcluster_raster <- raster::rasterize(
  tess,
  raster::raster(nrow = 10, ncol = 10, resolution = c(1/10, 1/10), ext = 
  raster::extent(c(0,1,0,1))),
  field = tess@data[, 1],
  fun = "mean"
)

**Error in if (x2a < rxmn) { : missing value where TRUE/FALSE needed**

... is back. Is there the possibility to rasterize and keep the original dimensions of 10x10?

  • See edit below. Suspect it might be a bug in raster... – Spacedman Aug 16 '17 at 14:50
2

The extent of your polygons is a bit larger than (0,1,0,1):

> extent(randomcluster_spdf)
class       : Extent 
xmin        : -0.04 
xmax        : 1.04 
ymin        : -0.04 
ymax        : 1.04 

So when you rasterize to a slightly smaller raster, you get that error:

> rr = raster(ncol=10,nrow=10, resolution = 1/10, ext = extent(c(0,1,0,1)))
> ra = rasterize(tess, rr,"clumps")
Error in if (x2a < rxmn) { : missing value where TRUE/FALSE needed

If instead you use the extent of the polygon object explicitly:

> rr = raster(ncol=10,nrow=10, resolution = 1/10, ext = extent(tess))
> ra = rasterize(tess, rr, "clumps")

It works!

The source code of rasterize has this commented out section:

  #if (is.na(x2)) { 
  #   txt <- paste('something funny at row:', r, 'polygon:',j)
  #   stop(txt)
  #}

stepping through the code shows me this (is.na(x2)) is what is triggering the error in your example. If this chunk above wasn't commented out you'd get "something funny at row...". So I suspect the author hit this problem at some point and either thought they'd fixed it or decided stopping with "something funny" was not a good idea. Might be worth a bug report, its nicely reproducible with your code above. My hunch is that its a problem with intersections of polygons and grids.

Specifically, its polygon number 24. If you run rasterize on randomcluster_spdf[-24,] it works fine (except for the hole left by missing polygon 24). Running on polygon 24 alone triggers the error.

  • 1
    Yes, this was a bug in raster, Fixed in version 2.6-1 (forthcoming) – Robert Hijmans Nov 2 '17 at 1:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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