How to create a raster map in GRASS GIS with vertical / horizontal gradient

Sometimes it is necessary to test a certain raster operation with a set of different raster values. For example, in a recent task I wanted to test the GRASS add-on r.fuzzy.system to estimate the habitat suitability for a certain fish species based on two parameters, flow velocity and water depth. Consequently, r.fuzzy.system requires one raster map indicating the flow velocity and one containing information about water depth (in addition to the fuzzy rules and fuzzy set definition files). As I wanted to get a map of suitabilities for any combination of flow velocity and depth I decided to create two artificial maps for those two parameters, one map with a horizontal gradient of flow velocity ranging from 0 to 1.5 m/s and one map with a vertical gradient of water depth ranging from 0 to 2 m. In GRASS GIS this can easily be done with the raster calculator and the row() and col() command. As I am mainly working with Python scripts in GRASS here the pythonic way to create a vertically and a horizontally graded raster map for the desired ranges of values:

# First we need to get the region settings and total number of rows and columns
region = grass.read_command("g.region",
flags="p")
regiondict = dict(item.split(":") for item in region.replace(" ", "").splitlines())

# Here we define the new minimum values and maximum values (desired range of gradient)
NewMin = 1
NewMax = 20

# And now we can calculate the graded rasters
# for gradient of rows
grass.mapcalc("$newmap = (((row() - $OldMin) * ($NewMax - $NewMin)) / ($OldMax - $OldMin)) + $NewMin",
newmap="newmap",
NewMin=NewMin,
NewMax=NewMax,
OldMin=1,
OldMax=regiondict["rows"],
overwrite=True)

#for gradient of cols
grass.mapcalc("$newmap = (((col() - $OldMin) * ($NewMax - $NewMin)) / ($OldMax - $OldMin)) + $NewMin",
newmap="newmap",
NewMin=NewMin,
NewMax=NewMax,
OldMin=1,
OldMax=regiondict["cols"],
overwrite=True)

And that’s how it might look like:

Vertically graded raster

Vertically graded raster

Advertisements

2 thoughts on “How to create a raster map in GRASS GIS with vertical / horizontal gradient

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s