Benjamin,

You can do:

x <- rasterize(r, ref, 'lc')

If that is too slow you may try (with the current version of raster):

x <- raster:::.p3r(r, ref, 'lc')

(this is a function under development that is not well tested and will
disappear in the future, to replace rasterize)

Robert

On Mon, Nov 7, 2011 at 8:52 AM, Benjamin Mack <bmack@uni-bonn.de> wrote:

> Hello list,
> I would like to create a new raster with values from a
> SpatialPolygonsDataFrame. It works with the code below, however it is
> really slow. I would appreciate any suggestions which could speed up the
> code!
> Thanks in advance!
> Ben
>
> Code:
> "r" is the raster file
> "ref" is the SpatialPolygonsDataFrame with one column named "lc" with
> integer values.
> to write the values from the polygons to the raster I do the following:
>
>  u.IDs <- unique(ref@data$lc) # unique lc(land cover) values/IDs
>  for (ii in 1:length(u.IDs)) {
>      temp <- ref[ref@data$lc==u.IDs[ii],] # create a new
> SpatialPolygonsDataFrame for a specific lc-ID
>      ra[temp] <- u.IDs[ii] # write the values to the raster  }
>
> ______________________________**_________________
> R-sig-Geo mailing list
> R-sig-Geo@r-project.org
> https://stat.ethz.ch/mailman/**listinfo/r-sig-geo<https://stat.ethz.ch/mailman/listinfo/r-sig-geo>
>

	[[alternative HTML version deleted]]

