Skip to content

r.extract

Extracts specified categories of an integer input map.

r.extract [-cs] input=name output=name cats=range [--overwrite] [--verbose] [--quiet] [--qq] [--ui]

Example:

r.extract input=name output=name cats=range

grass.script.run_command("r.extract", input, output, cats, flags=None, overwrite=False, verbose=False, quiet=False, superquiet=False)

Example:

gs.run_command("r.extract", input="name", output="name", cats="range")

Parameters

input=name [required]
    Name of input raster map
output=name [required]
    Name for output raster map
cats=range [required]
    Category values
    Example: 1,3,7-9,13
-c
    Clip to minimum extent
-s
    Output reclassified map instead of true map
--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
    Name of input raster map
    Used as: input, raster, name
output : str, required
    Name for output raster map
    Used as: output, raster, name
cats : str, required
    Category values
    Example: 1,3,7-9,13
    Used as: input, cats, range
flags : str, optional
    Allowed values: c, s
    c
        Clip to minimum extent
    s
        Output reclassified map instead of true map
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

This module extracts selected categories from an integer raster map into a new raster, similarly to v.extract for vectors. The categories and color table are preserved.

The category values can be specified as:

  • cat1,cat2,...
  • cat1-cat3
  • cat1-
  • -cat1
  • and their combination: -cat1,cat2,cat3-cat6

The extent of the new raster can be changed with flag -c to fit the extent of the extracted data. When using flag -s output raster is a reclassified input raster (see r.reclass for details).

NOTES

This implementation supports only integer raster maps (type CELL).

EXAMPLES

The following examples are using the full North Carolina sample dataset. We will extract certain zipcodes. First print zipcodes raster info:

g.region raster=zipcodes -p
r.info zipcodes -rg
...
rows=1350
cols=1500
min=27511
max=27610
...

Now we extract 2 categories and automatically adjust the raster extent to fit the extracted data.

r.extract input=zipcodes output=selected_zipcodes cats=27605,27601 -c
r.info selected_zipcodes -rg
...
rows=404
cols=377
min=27601
max=27605
...

SEE ALSO

r.reclass (used in this implementation)
v.extract (for extracting vector data)

AUTHOR

Anna Petrasova, NCSU GeoForAll Lab

SOURCE CODE

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