Skip to content

r.category.trim

Export categories and corresponding colors as QGIS color file or csv file. Non-existing categories and their color definitions will be removed.

r.category.trim [-n] input=name [output=name] [csv=name] [qgis=name] [--overwrite] [--verbose] [--quiet] [--qq] [--ui]

Example:

r.category.trim input=name

grass.script.run_command("r.category.trim", input, output=None, csv=None, qgis=None, flags=None, overwrite=False, verbose=False, quiet=False, superquiet=False)

Example:

gs.run_command("r.category.trim", input="name")

Parameters

input=name [required]
    input map
output=name
    output map
csv=name
    Attribute table (csv format)
qgis=name
    QGIS color file (txt format)
-n
    Recode layer to get consecutive category values
--overwrite
    Allow output files to overwrite existing files
--help
    Print usage summary
--verbose
    Verbose module output
--quiet
    Quiet module output
--qq
    Very quiet module output
--ui
    Force launching GUI dialog

input : str, required
    input map
    Used as: input, raster, name
output : str, optional
    output map
    Used as: output, raster, name
csv : str, optional
    Attribute table (csv format)
    Used as: output, file, name
qgis : str, optional
    QGIS color file (txt format)
    Used as: output, file, name
flags : str, optional
    Allowed values: n
    n
        Recode layer to get consecutive category values
overwrite: bool, optional
    Allow output files to overwrite existing files
    Default: False
verbose: bool, optional
    Verbose module output
    Default: False
quiet: bool, optional
    Quiet module output
    Default: False
superquiet: bool, optional
    Very quiet module output
    Default: False

DESCRIPTION

Trim redundant category labels and colors

In GRASS you can 'cut out' a subset of a larger raster layer by setting the extent to something smaller than the extent of the original map using g.region and/or by setting a mask using r.mask, after which you simply run r.mapcalc "newmap = oldmap".

You may have noticed that when creating the new map, all category labels and colour table of the original map are copied over to the new map, even for category values that are not in the new map. If you are working with categorical maps, this may not be what you want. See here for a more detailed discussion.

With this addon you can trim the category and colour tables so it only contains category labels and colour definitions for the values present in the new map. You can do this on the input map, or do this on a copy of the map.

Recode to consecutive category values

If you prefer the map to have consecutive values (i.e., without gaps), there is the option to change the category values to a consecutive series by setting the n-flag. For example, if the map has the following categories values and labels (after the redundant category labels have been removed):

2 label2
5 label5
9 label9

Then the new recoded layer will have the category values and labels:

1 label2
2 label5
3 label9

Export QGIS color map file

The addon let's you export the categories, category labels and colours as QGIS colour map file. This is just a simple text file with the raster categories and corresponding colour definitions and category labels. QGIS can use this to set the colour and labels for a raster layer. See this blogpost for more details how to use the colour map file in QGIS. Alternatively, you can also export the categories and category labels as a normal comma separated values (CSV) file, which can be easily viewed in a spreadsheet program such as Libre/Open Office Calc, Microsoft Excel, or Google Docs and can be easily shared together with an exported raster file for users that use other GIS programs.

NOTE

The file is only useful for categorical maps. Therefore only integer maps are accepted as input. To export color maps for continuous raster layers (or as an alternative to this plugin), have a look at the r.colors.out_sld plugin. To get QGIS color files, you need a two-step approach: (1) create a sld file using the r.colors.out_sld plugin, and (2) in QGIS, use the SLD4raster plugin to convert this to a qml file.

When you use the option to recode the map, you need to set an output map as well. It uses the r.recode function, with the 'a' flag set, i.e., the region is aligned to the input raster map.

Examples

See here for examples

See also

r.category, r.colors, r.recode r.colors.out_sld

AUTHOR

Paulo van Breugel, paulo at ecodiv.earth

SOURCE CODE

Available at: r.category.trim source code (history)
Latest change: Thursday Mar 20 21:36:57 2025 in commit 7286ecf