[R-sig-Geo] Convert raster to polygon

Rolf Turner r.turner at auckland.ac.nz
Thu Oct 13 22:23:05 CEST 2011


On 14/10/11 09:01, Arnaud Mosnier wrote:
> Dear all,
>
> In searching a solution from the internet, I found someone who had the same
> problem as me but ... who had no answers.
> I hope I will have better chance!  :-)
>
> Here is her explanation:
> http://r-sig-geo.2731867.n2.nabble.com/Converting-raster-to-polygons-with-just-one-polygon-for-cells-with-common-value-td6065464.html
>
> In the same way, I want to convert a raster with several values to
> SpatialPolygons.
> As in the previous message, I used the function "rasterToPolygons" (package
> raster) ... but it is really slow and memory hungry.
> Moreover, I want to have polygons that include contiguous raster cells with
> the same value, not one polygon by raster cell.
> The function unionSpatialPolygons can do that.
>
> Is there another (more efficient) way to do this in R ?

If you can convert your "raster" to a spatstat object of class "owin",
(and type "mask" of course) then the spatstat function "as.polygonal" will
convert it to an owin object of type "polygonal".  Which you will then
want, I presume, to convert to an object of class "SpatialPolygons".

The latter can be done easily via:

require(maptools)
Z <- as(Y,"SpatialPolygons")

where Y is the output of as.polygonal().

I don't know about the raster --> owin conversion though.  Others may
be able to tell you.

Note that you will need to have "gpclib" enabled to use as.polygonal()
and hence will need to be operating under the terms allowed by the
gpclib license.

HTH.

     cheers,

         Rolf Turner



More information about the R-sig-Geo mailing list