Skip to content

How to Add a New Tool

This page focuses on the practical contributor workflow for adding a new tool.

For runtime internals (CLI/GUI call modes, argument composition, multiprocessing), see: - Tool Runtime Model

1) Create the tool script

Add a new script in its module's folder, beratools/modules/<module>/ (you can start from beratools/core/tool_template.py).

Typical API pattern:

def my_tool(..., processes=0, call_mode=CallMode.CLI, log_level="INFO"):
    ...

Add __main__ entrypoint:

if __name__ == "__main__":
    from beratools.utility.tool_args import compose_tool_kwargs
    kwargs = compose_tool_kwargs("my_tool")
    my_tool(**kwargs)

2) Register in GUI metadata

Add your tool definition to the tools list of its module's file, beratools/modules/<module>/tools.json (mapping, pc2products, raster2products, ...).

At minimum, define:

  • name, info, tool_api, cli_name, entry, tech_link, and optionally tool_type and icon
  • parameter list (variable, label, description, type, subtype, default, output, optional)

tool_api must match the module name and the string passed to compose_tool_kwargs, or the tool stops with Tool API '<name>' not found in any module's tools.json. cli_name is the bt subcommand and entry is the function to run, as package.module:function, for example beratools.modules.mapping.my_tool:my_tool.

Each file is checked against beratools/gui/assets/module.schema.json when the frame starts. A file with an unknown or misspelled key, a missing field, a name, tool_api or cli_name that another module already uses, or a depends_on or field_of that names no variable of the tool is skipped with a warning that names the problem, and its tools don't appear. tests/test_registry.py fails on the same problems. Add the tool's cli_name to CLI_NAMES in its module's tests/test_<module>_tools_json.py.

Work in progress

A tool that isn't ready to release gets "status": "wip" in its tools.json entry (or at the top of the file, for a whole module). It is then left out of the GUI, the bt CLI and pipelines unless the environment variable BERATOOLS_WIP is 1, and with it set the GUI shows it in italics. To keep its code out of the packages too, add its files to both exclude lists in pyproject.toml and to $wipPaths in packaging/build.ps1, and keep its doc page out of the module's docs/files; tests/test_wip.py checks these. To release it, delete its "status": "wip" and undo those steps.

Field parameters

A text parameter that names a field of a vector input can show that layer's fields in a drop-down. Add field_of with the variable of the vector input; add "field_check": "year" to also check that the field holds years:

{
  "variable": "year_field",
  "field_of": "in_line",
  "field_check": "year",
  "type": "text",
  "subtype": "string",
  ...
}

The list refreshes when the input file or layer changes, and a name can still be typed. A field that isn't in the layer, or a year field with no years between 1900 and the current year, blocks Run; a year field with some empty or invalid values shows a warning. The command line still takes the field as text.

Taking part in pipelines

Every registered python tool can be a step in a GUI pipeline. To let its inputs be linked to an earlier step's output, tag the parameters with a kind:

  • an input parameter gets "accepts": "<kind>";
  • an output parameter gets "produces": {"<kind>": [patterns]}. An empty list means the output value itself (an output file such as lines.gpkg|centerline, or a folder of point clouds); otherwise the pipeline uses the first glob pattern that matches files in the output folder, newest file first.
{"variable": "in_chm", "accepts": "chm", ...}
{"variable": "out_line", "produces": {"line": []}, ...}
{"variable": "out_folder", "produces": {"dtm": ["Merged_*_DTM.tif", "*_DTM.tif"]}, ...}

The kinds in use are line, footprint, chm, las, dtm, dsm, microform, water and dtw; add a new one when a tool writes something new. tests/test_cross_module.py checks that every accepted kind has a producer. For the pipeline to stop when your tool fails, exit with a non-zero code (sys.exit(1) or an uncaught exception), not a plain exit().

Adding the CLI subcommand

The cli_name and entry fields of the tool's tools.json entry make the subcommand: bt <cli_name> imports the entry function and calls it. No code change is needed in beratools/cli/entry.py.

3) Keep tools thin; move heavy logic to an algorithm file

  • Put high-level tool wiring in beratools/modules/<module>/my_tool.py
  • Put algorithm-heavy logic in beratools/modules/<module>/algo_my_tool.py
  • Import only frame code (beratools/core, beratools/utility) and your own module, not another module

This keeps code easier to test and maintain.

4) Test from CLI first

Run help and a sample execution before validating in GUI:

python beratools/modules/<module>/my_tool.py --help

Then test via GUI once CLI behavior is stable.

5) Add docs and tests

  • Add the tool's page to its module's docs site: beratools/modules/<module>/docs/files/user/<page>.md, listed in the nav of beratools/modules/<module>/docs/mkdocs.yml, with a row in the tool table of the site's index.md and the tool's module in its api.md
  • Set the tool's tech_link in tools.json to that page's path, e.g. user/my_tool/
  • Add technical pages for algorithm/runtime internals under the module's docs/files/technical/
  • Add tests for new behavior and edge cases to the module's tests folder, beratools/modules/<module>/tests/, and any sample data they need to its tests/data/

6) Submit PR

  • Follow branching and contribution guidance in Development
  • Request review and ensure CI passes

Adding a PC2Products (R) tool

A PC2Products tool is a thin Python wrapper that validates its inputs and calls an R function through rpy2. The steps above still apply; these are the differences.

Write the R function

Add the processing function to beratools/modules/pc2products/r_scripts_collection.r. Follow the existing functions: read the input folder as a lidR LAScatalog, drop noise with filter = "-drop_class 7", take the most workers from Python as r_common.r_max_workers(processes), and call mosaic_catalog_outputs to merge tiled raster outputs. Take free_mem and tiling arguments (pass r_common.free_memory() and r_common.check_tiling(tiling) from Python) and call apply_tiling(ctg, tiling, max_workers, free_mem, paste0(out_folder, "/{*}_suffix"), bytes_per_point = ...) instead of starting workers yourself. It sets the catalog's tiles and output template, starts as many workers as fit in the free memory, gives them the other allowed processes as lidR threads (start_workers), and has each output written whole (write_outputs_whole); reset the plan with on.exit(reset_plan(), add = TRUE). bytes_per_point is the memory your algorithm needs per point: measure the peak committed memory of one tile at two sizes and see the figures after BYTES_PER_POINT in the script for how the others were measured. A function that starts workers without apply_tiling should start them with start_workers(workers, max_workers). Also take a skip_existing argument and pass it to apply_tiling, then call select_tiles(ctg, skip_existing, function(save) paste0(save, ".tif")), giving the files each tile writes (pass res = cell_size for a raster output), and run the catalog with catalog_run(ctg, function(ctg) rasterize_terrain(ctg, ...)). Tiles can differ in size with the data's density, and the files or tiles too large for the run's workers are processed with fewer, so catalog_run makes one lidR run (a round) per size and worker count, starting each round's workers, and it stops with the error of a tile that fails; with skip_existing, a rerun skips the tiles whose outputs are complete.

my_function <- function(in_las_folder, out_folder, max_workers, free_mem, tiling = "auto",
                        skip_existing = FALSE) {
    ...
}

Create the Python wrapper

Add beratools/modules/pc2products/rpy_my_tool.py. configure_r_environment() must run before r_common is imported, because it sets up R_HOME and the DLL search path that rpy2 needs. rpy_find_cellsize.py is the smallest complete example.

import os

from beratools.utility.tool_args import compose_tool_kwargs
from beratools.modules.pc2products.r_interface import configure_r_environment
configure_r_environment()
import beratools.modules.pc2products.r_common as r_common


def rpy_my_tool(in_las_folder, out_folder, processes=0, tiling="auto", skip_existing=False, **kwargs):
    r_common.check_las_files_existence(in_las_folder)
    r_common.check_las_out_folder_existence(out_folder)
    r_common.check_r_packages_installation(["lidR", "rlas", "future", "terra"])

    r_script = os.path.join(os.path.dirname(os.path.abspath(__file__)), "r_scripts_collection.r")
    r_common.r["source"](r_script)
    r_common.robjects.globalenv["my_function"](
        in_las_folder, out_folder, r_common.r_max_workers(processes), r_common.free_memory(),
        r_common.check_tiling(tiling), bool(skip_existing))


if __name__ == "__main__":
    kwargs = compose_tool_kwargs("rpy_my_tool")
    rpy_my_tool(**kwargs)

check_r_packages_installation installs any missing R packages from CRAN the first time the tool runs, so list every package the R function uses.

Register, document and test it

  • Add the tool to beratools/modules/pc2products/tools.json.
  • Add a page under beratools/modules/pc2products/docs/files/user/, add it to the User Guide section of nav in beratools/modules/pc2products/docs/mkdocs.yml, add the module to that site's api.md, and add a row to the tool tables in its index.md and technical/pc2products_overview.md. Set tech_link to the page's path, e.g. user/my_tool/.
  • tests/test_cli_entry.py checks every tool's bt subcommand against the registry by itself. If the tool's values need converting or checking, add a case to beratools/modules/pc2products/tests/test_cli_pc2products.py, and add a case to beratools/modules/pc2products/tests/test_r_tools.py that runs on the sample tiles in its data/LiDAR/ folder.