[R-sig-Geo] Extract from raster produces NA when polygon has holes

Brooks E.G.E. Emma.Brooks at soton.ac.uk
Tue Dec 16 17:07:46 CET 2014


Apologies, I tried it with a couple of my own different data sets and I thought the holes were the problem, however I've just tried to reproduce the example:

library(maptools)
library(raster)
data(wrld_simpl)
Afr2<-wrld_simpl[which(wrld_simpl at data$NAME=='South Africa'|wrld_simpl at data$NAME=='Namibia'),]
ras<-raster(extent(Afr2), nrows = 10, ncols = 10)
values(ras) = 1:100
Afr2$Score<-extract(ras,Afr2,na.rm=T,progress='text',fun=sum)
data.frame(Afr2)

The equivalent problem in my own data would give South Africa a $Score of NA, however it seems to be working fine here, so I'll have to go back to the drawing board to pinpoint the problem.

Thanks for your time,
Emma

-----Original Message-----
From: b.rowlingson at gmail.com [mailto:b.rowlingson at gmail.com] On Behalf Of Barry Rowlingson
Sent: 16 December 2014 15:30
To: Brooks E.G.E.
Cc: r-sig-geo at r-project.org
Subject: Re: [R-sig-Geo] Extract from raster produces NA when polygon has holes

On Tue, Dec 16, 2014 at 3:07 PM, Brooks E.G.E. <Emma.Brooks at soton.ac.uk> wrote:
> Dear all
>
> I am using extract to 'extract' the values from raster to polygons, which is working fine until I noticed a couple of values are coming up as NA. On closer inspection these polygons have holes; it is a lake layer so these are representing islands. I don't know why expressly the extract function doesn't like holes - is there any way round this, or do I need to manually remove these? (I don't want to remove them as ideally I don't want to extract the data which belongs to the islands rather than the lakes).
>
> I am thinking this may be obvious for people in the know, but if it needs clarifying, or dummy data, apologies and please let me know.

 I think we need dummy data.... I would expect NA where there is a hole, since extract doesn't know what the right value is there. If you want to get the value of the surrounding polygon when extracting at a hole then you'll have to remove the holes and just use the outer polygon, assuming you only have one outer polygon...

 However, if you are getting NA all over any polygon that does have a hole somewhere then I wonder if your polygons are coded the right way round....

 Barry


More information about the R-sig-Geo mailing list