Extract in R fails for small polygons and raster
Asked Answered
U

0

1

This example should be reproducible, the first part (with large polygons) works, the second fails:

library(raster)

USA.altitude <- getData('alt', country='USA')
lower48 <- USA.alt[[1]]

Srs1 = Polygons(list(Polygon(cbind(c(-96,-95,-95,-96),c(40,40,41,40)))), "s1")
Srs2 = Polygons(list(Polygon(cbind(c(-97,-96,-96,-97),c(40,40,41,40)))), "s2")

spdf.large <- SpatialPolygonsDataFrame( SpatialPolygons(list(Srs1,Srs2)), 
                              data.frame( z=1:2, row.names=c("s1","s2") ) )
plot(spdf.large)
usa.average.elev<-extract(lower48, spdf.large, fun=mean)

Yay! everything seems ok, yes? Now with small polys:

Srs3 = Polygons(list(Polygon(cbind(c(-95.01,-94.99,-94.99,-95.01),c(40,40,40.01,40)))), "s3")
Srs4 = Polygons(list(Polygon(cbind(c(-94.01,-93.99,-93.99,-94.01),c(40,40,40.01,40)))), "s4")


spdf.small <- SpatialPolygonsDataFrame( SpatialPolygons(list(Srs3,Srs4)), 
                              data.frame( z=1:2, row.names=c("s3","s4") ) )
plot(spdf.small)
usa.average.elev<-extract(lower48, spdf.small, fun=mean)

Fails with error:

Error in pmax(ceiling(10/dim(r)[1:2]), c(1, 1)) : object 'r' not found

Unsaid answered 4/1, 2014 at 0:19 Comment(6)
Would you please construct a reproducible example that does not require a data set that takes several minutes to download? Also, have you investigated the small= and weights= arguments to extract(), both documented in ?extract?Titlark
Isn't this duplicate of extract() data from raster with small polygons - rounded weights too small?Effect
@Effect did Robert Hijmans' answer to that question turn out to be helpful to you?Titlark
Sorry about the data size, it only took 2 seconds here, so sometimes I forget not all have cable internet. I will edit the datasource and repost soon. I have tried small = TRUE and messed with weights having seen Tomas's question. This seemed more severe though as it throws an error and prevents analysis outright.Unsaid
This is a bug introduced in the last version (earlier this week); as a result of trying to improve the function to better deal with the issue Tomas brought up. It was not supposed to be in the CRAN version yet, but it got there anyway, prematurely. Should go away in the next version.Twister
@JoshO'Brien: I don't know, I haven't tried it yet.Effect

© 2022 - 2024 — McMap. All rights reserved.