Find largest patch in raster map

From GRASS-Wiki
Jump to navigation Jump to 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 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! 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 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 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 landclass96_clumps_ids

# report clumps ordered by area size landclass96_clumps_ids option=area units=kilometers sort=asc

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