#!/usr/bin/env python3 ############################################################################ # # MODULE: r.reclass.area # AUTHOR(S): NRCS # Converted to Python by Glynn Clements # Added rmarea method by Luca Delucchi # PURPOSE: Reclasses a raster map greater or less than user specified area size (in hectares) # SPDX-FileCopyrightText: 1999-2026 GRASS Development Team # SPDX-License-Identifier: GPL-2.0-or-later # ############################################################################# # 04/2026: rewrite with Tools API and new options (Stefan Blumentrath) # 10/2013: added option to use a pre-clumped input map (Eric Goddard) # 8/2012: added fp maps support, cleanup, removed tabs AK # 3/2007: added label support MN # 3/2004: added parser support MN # 11/2001 added mapset support markus # 2/2001 fixes markus # 2000: updated to GRASS 5 # 1998 from NRCS, slightly modified for GRASS 4.2.1 # %module # % description: Reclasses a raster map greater or less than user specified area size (in hectares). # % keyword: raster # % keyword: statistics # % keyword: aggregation # %end # %option G_OPT_R_INPUT # %end # %option G_OPT_R_OUTPUT # %end # %option # % key: value # % type: double # % description: Value option that sets the area size limit (in hectares) (deprecated) # % required: no # % guisection: Area # %end # %option # % key: mode # % type: string # % description: Keep only areas lesser or greater than specified value (deprecated) # % options: lesser,greater # % required: no # % guisection: Area # %end # %option # % key: lower # % type: double # % description: Value for the lower area size limit (in hectares). Pixel clumps with areas below this value are removed. # % required: no # % guisection: Area # %end # %option # % key: upper # % type: double # % description: Value for the upper area size limit (in hectares). Pixel clumps with areas above this value are removed. # % required: no # % guisection: Area # %end # %option # % key: method # % type: string # % description: Method used for area size filtering # % options: reclass,rmarea # % answer: reclass # % guisection: Area # %end # %flag # % key: c # % description: Input map is clumped # %end # %flag # % key: d # % description: Generate clumps including diagonal neighbors # %end # %flag # % key: i # % description: Invert filter (remove clumps larger than lower and smaller than upper, only for method 'reclass') # %end # %flag # % key: m # % description: Apply lower area threshold as "minsize" during clumping # %end # %flag # % key: v # % description: Output vector map, do not convert back to raster (only for method 'rmarea') # %end # %rules # % required: lower, upper, mode # % requires: mode, value # % requires: -m, lower # % excludes: -c, -d, -m # % excludes: -m, -i # %end import atexit import io import sys from functools import partial from math import ceil, inf, prod from typing import Literal import grass.script as gs from grass.tools import Tools TEMP_PREFIX = gs.tempname(12) def cleanup() -> None: """Delete all temporary maps.""" tools = Tools(capture_output=False) tools.g_remove( flags="fb", type="all", pattern=f"{TEMP_PREFIX}_*", superquiet=True, ) def check_projection() -> tuple[float, float]: """Check if current projection is supported. Issues a fatal message if the tool does not reliably work in the current type of projection (XY, latlon) """ tools = Tools(capture_output=True) proj = tools.g_region(flags="p", format="json").json if proj["crs"]["type_code"] == "0": gs.fatal( _( "xy-locations are not supported\n" "Need projected data with grids in meters", ), ) in_proj = gs.parse_key_val(tools.g_proj(flags="p", format="shell").text) if in_proj["unit"].lower() == "degree": gs.fatal(_("Latitude-longitude locations are not supported")) if in_proj["name"].lower() == "xy_location_unprojected": gs.fatal(_("xy-locations are not supported")) return proj["ewres"], proj["nsres"] def get_clumpfile( input_map: str, resolution: tuple[float, float], minsize: float | None = None, *, clump_flags: Literal["d"] | None = None, ) -> str: """Generate a clump-raster map.""" tools = Tools(capture_output=False) clump_map = f"{TEMP_PREFIX}_clump" if minsize: minsize = ceil((minsize * 10000.0) / prod(resolution)) if clump_flags and minsize: msg = _( "Generating a clumped raster file including diagonal neighbors" " with minimum {pixel_count} pixels" ).format(pixel_count=minsize) elif clump_flags: msg = _("Generating a clumped raster file including diagonal neighbors") elif minsize: msg = _( "Generating a clumped raster file with minimum {pixel_count} pixels" ).format(pixel_count=minsize) else: msg = _("Generating a clumped raster file") gs.verbose(msg) tools.r_clump( flags=clump_flags, input=input_map, minsize=minsize, output=clump_map, quiet=True, ) return clump_map def reclass( clump_map: str, output_map: str | None, lower: float | None = None, upper: float | None = None, input_map: str | None = None, *, invert: bool = False, ) -> None: """Perform raster-based filtering.""" tools = Tools(capture_output=True) stats_input = clump_map expected_fields_number = 4 # Each combination of thresholds gets complete messages, so that # translators can reorder the words and numbers freely. if lower and upper: if invert: verbose_message = _( "Generating a reclass map with area size smaller than {lower} hectares" " or larger than {upper} hectares..." ).format(lower=lower, upper=upper) fatal_message = _( "No areas of size smaller than {lower} hectares" " or larger than {upper} hectares" ).format(lower=lower, upper=upper) else: verbose_message = _( "Generating a reclass map with area size larger than or equal to" " {lower} hectares and smaller than or equal to {upper} hectares..." ).format(lower=lower, upper=upper) fatal_message = _( "No areas of size larger than or equal to {lower} hectares" " and smaller than or equal to {upper} hectares" ).format(lower=lower, upper=upper) elif lower: if invert: verbose_message = _( "Generating a reclass map with area size" " smaller than {lower} hectares..." ).format(lower=lower) fatal_message = _("No areas of size smaller than {lower} hectares").format( lower=lower ) else: verbose_message = _( "Generating a reclass map with area size" " larger than or equal to {lower} hectares..." ).format(lower=lower) fatal_message = _( "No areas of size larger than or equal to {lower} hectares" ).format(lower=lower) elif upper: if invert: verbose_message = _( "Generating a reclass map with area size" " larger than {upper} hectares..." ).format(upper=upper) fatal_message = _("No areas of size larger than {upper} hectares").format( upper=upper ) else: verbose_message = _( "Generating a reclass map with area size" " smaller than or equal to {upper} hectares..." ).format(upper=upper) fatal_message = _( "No areas of size smaller than or equal to {upper} hectares" ).format(upper=upper) else: # A zero lower limit and no upper limit filter nothing. verbose_message = _("Generating a reclass map without area size limits...") fatal_message = _("No areas found") gs.verbose(verbose_message) if not lower: lower = -inf if not upper: upper = inf recfile = f"{TEMP_PREFIX}_recl" sflags = "aln" if input_map: # For non-null clumps r.stats should produce 5 columns expected_fields_number = 5 stats_input = (clump_map, input_map) if gs.raster_info(input_map)["datatype"] in {"FCELL", "DCELL"}: sflags += "i" clump_areas = ( tools.r_stats( flags=sflags, input=stats_input, sep=";", quiet=True, ) .text.rstrip() .split("\n") ) rules = "" for line in clump_areas: f = line.rstrip().split(";") if len(f) < expected_fields_number: continue hectares = float(f[-1]) * 0.0001 in_range = lower <= hectares <= upper keep = not in_range if invert else in_range if keep: rules += "{} = {} {}\n".format(*[f[i] for i in [0, 2, -2]]) if rules: tools.r_reclass( input=clump_map, output=recfile, rules=io.StringIO(rules), quiet=True, ) else: gs.fatal(fatal_message) tools.r_mapcalc(expression=f"{output_map} = {recfile}") def rmarea( input_map: str, output_map: str, lower: float | None = None, upper: float | None = None, *, return_vector: bool = False, ) -> None: """Perform vector-based filtering.""" tools = Tools(capture_output=False) if gs.raster_info(input_map)["datatype"] != "CELL": gs.warning( _("Raster map <%s> is not of type 'CELL'. Results may not be as expected."), ) # Setup temporary map names with pattern vectfile = f"{TEMP_PREFIX}_vect" cleanfile = f"{TEMP_PREFIX}_clean" edit_vector = vectfile # Convert input map to vector raster values as category tools.r_to_vect( flags="v", input=input_map, output=vectfile, type="area", quiet=True, ) # Apply lower threshold # transform user input from hectares to meters because currently v.clean # rmarea accepts only meters as threshold if lower: lower *= 10000.0 tools.v_clean( input=vectfile, output=cleanfile, tool="rmarea", threshold=lower, quiet=True, ) edit_vector = cleanfile # Apply upper threshold # contrary to the lower threshold, areas larger than threshold are # not merged with a neighbour, but deleted if upper: tools.v_to_db( map=edit_vector, type="centroid", option="area", columns="area_ha", units="hectares", quiet=True, ) tools.v_edit( map=edit_vector, tool="delete", where=f"area_ha >= {upper}", superquiet=True, ) # Produce final output map (raster / vector) if not return_vector: tools.v_to_rast( input=edit_vector, output=output_map, use="cat", quiet=True, ) else: tools.v_clean( flags="c", input=edit_vector, output=output_map, tool=["rmdangle", "rmbridge"], threshold=-1, quiet=True, ) def main() -> None: """Run area filtering.""" if not gs.find_file(options["input"])["name"]: gs.fatal(_("Raster map <%s> not found") % options["input"]) if flags["i"] and options["method"] == "rmarea": gs.fatal(_("Flag -i is not supported with method='rmarea'")) # Check for unsupported locations resolution = check_projection() # Give deprecation warnings if options["mode"]: gs.warning( _( "Options and are deprecated. " "Use 'lower' and/or 'upper'.", ), ) # backwards compatibility for mixed-up logic in previous version # of r.reclass.area if options["method"] == "rmarea": options["lower" if options["mode"] == "lesser" else "upper"] = options[ "value" ] else: options["lower" if options["mode"] == "greater" else "upper"] = options[ "value" ] # Define filter thresholds lower = float(options["lower"]) if options["lower"] else None upper = float(options["upper"]) if options["upper"] else None # Compute clump map if needed clump_map = ( options["input"] if flags["c"] or options["method"] == "rmarea" else get_clumpfile( options["input"], resolution=resolution, minsize=lower if flags["m"] else None, clump_flags="d" if flags["d"] else None, ) ) # Filter clumped map filter_method = ( partial(reclass, input_map=options["input"], invert=flags["i"]) if options["method"] == "reclass" else partial(rmarea, return_vector=flags["v"]) ) filter_method(clump_map, options["output"], lower=lower, upper=upper) if __name__ == "__main__": options, flags = gs.parser() atexit.register(cleanup) sys.exit(main())