[GRASS-user] randomize the position of a raster map

Moritz Lennert mlennert at club.worldonline.be
Tue Dec 9 06:55:59 PST 2014


On 08/12/14 18:26, Milton Ribeiro wrote:
> Hi all,
>
> I have a landcover maps, and I need to randomize the position of them,
> keeping all the pixels on the new raster map.

IIUC, you actually want to randomize the categories of pixels where 
categories are land covers and there is a constraint that you want to 
keep the same number of pixels per category. Is that correct ?

> Is there a command that can do this within GRASS?

You could use r.random following a procedure something like this:

create a raster (mymask) where all cells are non-null

for each category of land cover,
	get cat and percentage of cover of that cat (e.g. via r.stats)
	run r.random, using cover=mymask and n=percentage of cover
	modify mymask so that the cells attributed in the previous run of 
r.random get set to null to make them unavailable in the next run

There is just one issue: the cover option in r.random provokes a check 
if the randomly selected cell is in the cover area or not. If not, it is 
not used, but no replacement is found, so if you use cover= the 
percentage value in n= is not respected... Ideally this should be 
corrected to while cell number X is not within the cover, find a new 
cell. See [1] for an old discussion about this.

Attached is a bash script that tries to work around this issue and works 
more or less for me (at least with the "landuse dataset in the 
simplified NC dataset). Certainly not perfect, but maybe you can build 
on that.

Another option might be something like this:

Knowing that:

r.stats -ncp landuse --quiet
1 73077 29.31%
2 2137 0.86%
3 25518 10.24%
4 16829 6.75%
5 126265 50.64%
6 5303 2.13%
7 194 0.08%

use the random generator in r.mapcalc

r.mapcalc "eval(random = rand(0.0,1.0)); landuse_randomized_mapcalc = 
if(random<=0.2931,1, if(random>0.2931 && random 
<=0.3017,2,if(random>0.3017 && random <=0.4041, 3, if(random>0.4041 && 
random<=0.4716, 4, if(random > 0.4716 && random <=0.9780, 5, 
if(random>0.9780 && random <=0.9993, 5, 6))))))"  --o

The result is not perfect in terms of cell counts, but close:

r.stats -ncp landuse_randomized_mapcalc --quiet
1 73402 29.46%
2 2061 0.83%
3 25215 10.12%
4 16982 6.82%
5 131484 52.78%
6 180 0.07%


Moritz

[1] https://trac.osgeo.org/grass/ticket/1082
	



-------------- next part --------------
A non-text attachment was scrubbed...
Name: randomize_raster_categories.sh
Type: application/x-shellscript
Size: 772 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/grass-user/attachments/20141209/c94f3f6a/attachment.bin>


More information about the grass-user mailing list