Generate a grid with sequential numbers

From GRASS-Wiki
Jump to navigation Jump to search

Q: How to generate a grid where the cell numbers are sequential?

A: You can use g.region and r.mapcalc for this:

# In a shell get rows and cols as environment variables
 eval `g.region -g`

#(or just note the output of g.region -p and replace either $rows or $cols in the formulas below)

# generate the a vertical grid (see image below)
r.mapcalc "grid_seq = col() * $rows + row() - $rows"

#or generate a horizontal grid
r.mapcalc "grid_seq = row() * $cols + col() - $cols"

#to visualise the category numbers in the grid
d.mon x0
d.rast.num grid_seq

The resulting raster map of a vertical grid looks like this:

Sequentially numbered raster grid