Find largest patch in raster map

From GRASS-Wiki
Jump to: navigation, search

Q: I have a raster with a series of patches (contiguous areas with same value). How do I isolate the largest patch?

A: You can use r.clump to to recategorize the raster map pixels by grouping cells that form physically discrete areas into unique categories. From there, running r.report on it. Note that the module included in GRASS GIS 7 offers a convenient "sort" parameter.

Example: analysing the land use map of the North Carolina sample dataset:

# set computation region to full map:
g.region rast=landclass96 -p

# check categories in map
r.category landclass96
1	developed
2	agriculture
3	herbaceous
4	shrubland
5	forest
6	water
7	sediment

# see current landuse classes (you may use the GUI for this):
d.rast landclass96

# report area sizes for each class but same classes may be disconnected!
r.report landclass96 units=k sort=asc

Now it is important to clump physically connected areas, i.e. assign unique IDs:

# clump contiguous areas to same ID
r.clump input=landclass96 output=landclass96.clump
d.rast landclass96.clump

# report area sizes for each contiguous ID
r.report landclass96.clump units=k sort=asc

So far so nice but we need to transfer to original classes to these clumped areas:

# create vector map from clumped polygons
r.to.vect input=landclass96.clump output=landclass96_clumps type=area column=clump_id

# add new column to hold landuse class numbers:
v.db.addcolumn landclass96_clumps_ids column="lclass96 integer"

# fetch landuse class numbers from original raster map
v.what.rast map=landclass96_clumps_ids rast=landclass96 column=lclass96
## TODO for grass-dev: also transfer the label to the DB

# verify
v.db.select landclass96_clumps_ids
cat|clump_id|label|lclass96
1|1||5
2|61||4
3|377||5
4|2||4
5|43||4
...

# report clumps ordered by area size 
v.report landclass96_clumps_ids option=area units=kilometers sort=asc
...
1774|2177||3|6.19665525
19|2748||1|50.3741205
611|2832||5|70.441569

The largest patch in the map is the forest class 5 with an extent of 70.44 km^2.