Robert, In my example I used aggregate as that is what you asked about. In your example you use rasterize. The "fun" argument in rasterize has different role. It is to compute a value if there are multiple spatial objects (polygons in your example) that cover the same raster cell. For example you could use "mean". You do not have overlapping polygons. You use "sample(x, 1)". In the example, "x" is 2 for the large polygon, and sample(2,1) returns either 1 or 2 (see ?sample for a description of this infelicity). This seems meaningless. If you have overlapping polygons you could use fun=function(x, ...) x[ sample.int(length(x), 1)] If the problem is that you have multiple classes for a single polygon a similar approach could be used (with a look-up table), but it might be able to rasterize an ID first and then use calc to replace the ID with a randomly selected land cover type.
Robert On Fri, Nov 2, 2012 at 4:23 PM, Robert Pazur <[email protected]> wrote: > Dear Robert, > > thanks a lot for your answer. It helped me much. > I used your suggested random sampling approach also for the rasterization > of shapefile dataset. My results, however, seems strange (according to the > patches proportion) compare to for example the standard max assignment > method. Im posting the example below. Is my code for rp.random wrong? > > Thanks in advance, > Robert > > used shapefile for download > http://www.geography.sav.sk/personal/pazur/data/area.zip > ### > library(raster) > library(rgdal) > #read the polygon > map <- readOGR(dsn="FOLDERFORSHAPEFILE", "0_00") > r <- raster(ncol=300, nrow=300) > #raster extent same as map extent > extent(r) <- extent(map) > #categoric to numeric > code_00_num <- as.numeric(as.factor(map$code_00)) > #rasterization with random sampling > rp.random <- rasterize(map, r, 'code_00_num' ,fun=function(x, ...) > sample(x, 1)) > #rasterization by maximum area > rp.maximum <- rasterize(map, r, 'code_00_num' ,fun=max) > par(mfrow=c(2,1)) > plot(rp.random) > plot(rp.maximum) > ##### > > ------------------------------------------------------- > Robert > Pazur<http://www.sav.sk/index.php?lang=sk&charset=&doc=user-org-user&user_no=6225> > > > Institute of Geography > Slovak Academy Of Sciences > > Mobile : +421 948 001 705 > Skype : ruegdeg > > > 2012/10/12 Robert J. Hijmans <[email protected]> > > > Any suggestion? >> >> Yes, to provide a more complete description of the method and the context >> in which it should be useful. If you cannot do that, you probably should >> not use the method. >> >> Here is aggregation with "some probability function" that, at least for >> the example data, has the property you desire (class frequencies are >> similar). >> >> library(raster) >> # create example data >> r <- raster() >> set.seed(999) >> values(r) <- round(runif(ncell(r))*3) >> >> # aggregate with random sampling >> a <- aggregate(r, fact=10, fun=function(x, ...) sample(x, 1)) >> >> # get frequencies >> fr <- freq(r) >> fa <- freq(a) >> >> # as percentage >> fr[,2] <- round(100 * fr[,2] / sum(fr[,2])) >> fa[,2] <- round(100 * fa[,2] / sum(fa[,2])) >> >> # combine >> f <- cbind(fr, fa[,2]) >> colnames(f)[2:3] <- c('original', 'aggregated') >> f >> >> >> >> Best, Robert >> >> On Thu, Oct 11, 2012 at 3:23 AM, Robert Pazur <[email protected]>wrote: >> >>> Since the description in literature is not very detailed, i can only >>> guess that its based on some probability function. >>> Any suggestion? >>> >>> Robert Pazur >>> >>> >>> 2012/10/10 Robert J. Hijmans <[email protected]> >>> >>>> I am sure there is a way. Can you describe how this is done? Robert >>>> >>>> On Wed, Oct 10, 2012 at 5:51 AM, Robert Pazur <[email protected]>wrote: >>>> >>>>> Dear all, >>>>> >>>>> using land cover fine scale data set with defined categories: >>>>> is there a way ( perhaps in raster, or sp library) how to aggregate >>>>> this >>>>> raster by keeping the overall proportion of each land cover >>>>> classes constant (as much as possible)? >>>>> this aggregation procedure were described as "adjusted" by several >>>>> authors >>>>> : >>>>> >>>>> -Verburg, P. H., Denijs, T., Ritsemavaneck, J., Visser, H., & Dejong, >>>>> K. >>>>> (2004). A method to analyse neighbourhood characteristics of land use >>>>> patterns. Computers, Environment and Urban Systems, 28(6), 667â690. >>>>> doi:10.1016/j.compenvurbsys.2003.07.001 *page 8* >>>>> >>>>> -Schmit, C., Rounsevell, M., & Lajeunesse, I. (2006). The limitations >>>>> of >>>>> spatial land use data in environmental analysis. Environmental Science >>>>> & >>>>> Policy, 9(2), 174â188. doi:10.1016/j.envsci.2005.11.006 *page 5* >>>>> >>>>> -Dendoncker, N., Schmit, C., & Rounsevell, M. (2008). Exploring spatial >>>>> data uncertainties in landâuse change scenarios. International Journal >>>>> of >>>>> Geographical Information Science, 22(9), 1013â1030. >>>>> doi:10.1080/13658810701812836 *page 3* >>>>> * >>>>> >>>>> * >>>>> Thanks in advance, >>>>> Robert. >>>>> >>>>> >>>>> ------------------------------------------------------- >>>>> Robert Pazur< >>>>> http://www.sav.sk/index.php?lang=sk&charset=&doc=user-org-user&user_no=6225 >>>>> > >>>>> >>>>> >>>>> Institute of Geography >>>>> Slovak Academy Of Sciences >>>>> >>>>> Mobile : +421 948 001 705 >>>>> Skype : ruegdeg >>>>> >>>>> [[alternative HTML version deleted]] >>>>> >>>>> >>>>> _______________________________________________ >>>>> R-sig-Geo mailing list >>>>> [email protected] >>>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo >>>>> >>>>> >>>> >>> >> > [[alternative HTML version deleted]]
_______________________________________________ R-sig-Geo mailing list [email protected] https://stat.ethz.ch/mailman/listinfo/r-sig-geo
