Skip to content

extract by attributes from raster-image to polygons

3 messages · Rafael Wüest, BennyB, Robert J. Hijmans

#
Hi Benny

I once had the same problem and couldn't find a solution, so I coded a workaround. Adjusted to your example, this will look like:

library(rgeos)
library(raster)
library(maptools)
im=brick("P1020393.JPG")
extract=(im[[1]]>50)&(im[[2]]>180)&(im[[3]]>220)
poly_dissolve=rasterToPolygons(extract,fun=function(x){x==1},dissolve=T)

len<-length(poly_dissolve at polygons[[1]]@Polygons)

pol<-poly_dissolve
pol at plotOrder<-1:len
pol at data<-data.frame(1:len)

for(i in 1:len){
   pol at polygons[[i]]<-Polygons(list(poly_dissolve at polygons[[1]]@Polygons[[i]]),i)
}

plot(pol,col=rainbow(len))

May not be the most elegant way, but it solved my problem.

HTH,
Rafi
On 25.05.2012, at 19:13, BennyB wrote:

            
--
Rafael W?est
Swiss Federal Research Institute WSL
Z?rcherstrasse 111
8903 Birmensdorf
Switzerland

+41 44 7392126
rafael.wueest at wsl.ch
http://www.wsl.ch/info/mitarbeitende/wueest