[R] problem with extract raster with polygon

Jacob Strunk strunky at gmail.com
Mon Mar 20 22:30:18 CET 2017

```Hello, I am having some troubles extracting pixels from a raster using
polygons. When I attempt to do so, pixels which are only partially
intersected by polygons are not included.

In the example below the number of pixels returned is less than the number
of pixels which can be seen intersecting polygons.

As an aside I also encountered strange behavior in my example below after
buffering polygons by 10 units. After buffering polygons by 10 units, the
number of pixels return by extract was actually reduced.

Example
r <- raster(ncol=5, nrow=5)
r[] <- 1:ncell(r)

cds1 <- rbind(c(-180,-20), c(-160,5), c(-60, 0), c(-160,-70), c(-180,-20))
cds2 <- rbind(c(80,0), c(100,60), c(120,0), c(120,-55), c(80,0))
polys <- spPolygons(cds1, cds2)

plot(r)

extract(r, polys)
extract(r, buffer(polys,10,dissolve=F))
extract(r, buffer(polys,30,dissolve=F))
extract(r, buffer(polys,100,dissolve=F))

my R output:
> extract(r, polys)
[[1]]
[1] 11 12 16

[[2]]
[1] 14  4 15 25

> extract(r, buffer(polys,10,dissolve=F))
[[1]]
[1] 11 12 16

[[2]]

14

> extract(r, buffer(polys,30,dissolve=F))
[[1]]
[1] 11 12 16 17 21

[[2]]
[1]  9 14 15 19 20 25

> extract(r, buffer(polys,100,dissolve=F))
[[1]]
[1]  1  2  3  6  7  8 11 12 13 16 17 18 21 22 23

[[2]]
[1]  3  4  5  8  9 10 13 14 15 18 19 20 24 25

> R.Version()
\$platform
[1] "x86_64-w64-mingw32"

\$arch
[1] "x86_64"

\$os
[1] "mingw32"

\$system
[1] "x86_64, mingw32"

\$status
[1] ""

\$major
[1] "3"

\$minor
[1] "3.2"

\$year
[1] "2016"

\$month
[1] "10"

\$day
[1] "31"

\$`svn rev`
[1] "71607"

\$language
[1] "R"

\$version.string
[1] "R version 3.3.2 (2016-10-31)"

\$nickname
[1] "Sincere Pumpkin Patch"

"Documentation for package ‘raster’ version 2.5-8" from Raster help pages

Thanks for any help

--
Jacob

[[alternative HTML version deleted]]

```