Skip to content

How the PC2Products Tools Run

How all the tools behave

These points apply to the eight LiDAR tools. Surface Water Mapping, Depth-to-Water from Surface Water and Seismic Line Disturbance Analysis work on rasters instead (see their own sections), but they install their R packages the same way:

  • Input: every .las / .laz file in in_las_folder is read as a lidR LAScatalog. If the folder is missing or holds no LAS/LAZ files, the tool stops. This opens every file to read its header, which for thousands of files on a slow or network drive can take minutes, so the tool first prints Reading the headers of 2393 LiDAR file(s)..., and then the time it took when it was 10 s or more.
  • Noise removal: points classified as low noise (class 7) are dropped with filter = "-drop_class 7".
  • Cell size: the DTM and CHM/DSM tools don't take a cell size. They calculate it from the point density, as described in Raster Cell size from LiDAR data. The hummock/hollow tool does the same when cell_size is 0.
  • Output folder: if out_folder doesn't exist, it's created. Existing outputs are overwritten.
  • Resuming a run: a tool stops at the first tile that fails, with that tile's error (for example std::bad_alloc when it runs out of memory). The tools with a tiling option also take skip_existing: set it to true and run the tool again with the same parameters to leave out the tiles whose outputs are complete, and process only the rest. Each output is written as <name>.partial.<ext> and renamed when complete, so a tile the run was writing when it stopped has no output under its name and is made again; the next run removes the .partial files. (A GeoTIFF cut short opens and reads without error, its unwritten rows empty, so it couldn't be told from a complete one.) Outputs of earlier versions are also checked: a LAS file must hold all the points its header counts, and a GeoTIFF's last row must be readable. In the 500 m blocks that have outputs, a resumed run uses the tile sizes of the earlier run, from the plan it saved next to its outputs (tiles_<suffix>.csv, for example tiles_csfgnd_upto30cm_cloth150cm_hard.csv), so it makes the same tiles there even when the free memory has changed; the blocks without outputs are tiled again for the memory free now, so a run that stopped on a tile too large for the memory gets smaller tiles there. An earlier run without a plan had one tile size, which is read from its outputs' corner names. If the remaining tiles don't fit, the tool warns, and you need to free memory or start again in an empty folder. The tool prints how many tiles it skips. The DTM, CHM/DSM and hummock/hollow tools merge the skipped tiles with the new ones. The output names don't record every parameter (the DTM's cell size, for example), so don't use skip_existing after changing one. Before skipping, the tool prints Checking which outputs of an earlier run are complete..., as it opens each finished output to check it.
  • Tiles left out: a tile from which nothing can be interpolated doesn't stop the run. It is left out, with no output, and the run ends with a warning naming its files, for example Warning from 1 tile(s): Left out tile_bad.las: its heights can't be normalized from its 0 ground point(s) of 5 (they don't span an area). This covers a tile whose ground points (DTM, normalization) or points (CHM/DSM) are fewer than 3 or lie on a line, and a tile the algorithm finds nothing to interpolate in, such as a file of a few points all at Z = 0, where pit-free finds no first return above its lowest height threshold. The tiles around it cover the area, if there are any; a rerun with skip_existing tries such a tile again, as it has no output.
  • Tiling: the ground classification, normalization, DTM, CHM/DSM and hummock/hollow tools take a tiling option that sets how the data is split for processing:
  • auto (the default): process each input file as one tile when every file fits the workers' share of the free memory and there are at least as many files as workers. Otherwise the tool plans both ways, by file and as for tiling, and tiles the data only when that is estimated to take less than 1/1.25 of the time by file, as processing by file keeps the file names. A rerun with skip_existing of a run that processed by file stays by file, so the finished outputs aren't made again under tile names.
  • by_file: process each input file as one tile, which keeps the tile names. When a file is too large for the free memory (for example a single large file), the tool warns and splits the data as for tiling instead, so it doesn't stop at its memory limit; the outputs are then named by tile corner.
  • tiling: always split the data into square tiles sized for the free memory and the data's density. Use it for a single large file. The point counts of every file are read from its .lax spatial index, and the data is split into 500 m blocks, aligned at 0, 0. Each block is tiled with the largest of 500, 250, 125 and 100 m whose densest tile, with its buffer, fits, so sparse areas get large tiles, which read less buffer, and dense areas small ones. The number of workers is the one with the least estimated time, buffers included: more workers have less memory each, so smaller tiles. The tool processes the tiles of each size in turn and prints, for example, Processing 162 tile(s) of 125 m.... Without the indexes (a read-only input folder), one size is used for all the data, from the files' average density. How the LiDAR tools tile a point cloud shows this with diagrams.

Rounds by size: one dense file or tile no longer sets the workers for all the data. Whether by file or tiled, the files or tiles too large for the run's workers are processed after the others, in rounds with fewer workers, each with a larger share of the memory: for example Processing 2390 file(s) with 25 worker(s), 2 thread(s) each... and then Processing 1 file(s) with 1 worker(s), 60 thread(s) each.... A round's workers are rounded down to levels each 80% of the one above, so there are only a few rounds. Every round reads the whole catalog, so tiles' buffers still hold the points of every file and the output is wall-to-wall, the same as in one round. A resumed run (skip_existing) plans its workers again for the files or tiles left once it knows which are done, and prints, for example, Using up to 9 worker(s) for the 228 file(s) left.; rounds that would start the same number of workers are one round.

A file processed by file is counted with its buffer: from the density grid when the tool reads it, or else taking the buffer to be as dense as the file.

60% of the free memory is shared between the workers. Each worker is allowed 1.6 GB fixed (R with lidR, sf and terra loaded, and the tile beyond its points), 0.21 GB for each thread after the first, and a number of bytes per point that depends on the tool, measured as the peak committed memory (which the tool's memory limit counts) on 150 m and 250 m windows of the same data (13.9 and 35.7 million points, every point a first return, 50% of them ground):

Tool Measured Allowed
CHM/DSM, pit-free 796 850
CHM/DSM, TIN 102 per point plus ~410 per raster cell 120 per point plus 450 per cell, from the data's average density
DTM, TIN; normalization, TIN; hummock/hollow 231; 352; 309 from the sampled ground share, at most 670
DTM, IDW 76 160
Ground classification; normalization, IDW 224; 226 260
DTM and normalization, kriging not measured 260

The tools that triangulate the ground points need more with more ground: an earlier TIN DTM of a 560 m window with 20.1 million points, 91% of them ground, peaked at 13.3 GB, about 125 bytes per point read and 540 per ground point. A tile's ground points aren't known before reading it, so before planning these tools sample the ground share (classes 2 and 9) of up to 20 files spread over the data, from five 20 m squares in each read through the .lax index (seconds, even over a network), and size the workers for the largest file share with a 25% margin: 1.25 × (125 + 540 × share) bytes per point, or 1.25 × 665 × share for the TIN DTM, which reads only the ground points; never more than 670, the figure for data that is all ground, which is also used when no ground can be sampled. The tool prints, for example, Ground points are up to 38 percent of the points sampled from 7 file(s), so the memory is sized at 411 bytes per point (670 if every point were ground). TIN normalization of files of about 20 million points with their buffers had peaked at about 6 GB per worker, about 250 bytes per point, but was sized at 670, so half the workers it had room for sat unused. The IDW DTM reads only the ground points, so its figure is doubled. Before processing, a .lax spatial index is built next to every input file that has none, which reads each such file once (several files at a time, one per R process, at most 8). With it, reading a tile's buffer from a neighbouring file, or a tile of a large file, reads only that area instead of the whole file. Memory use is unchanged. If the input folder is read-only, the tool warns and reads the files without an index, which is slower. Build LiDAR Spatial Index builds the indexes ahead of time. Tiles processed by file are named after their input file. Other tiles are named by their lower-left corner, for example 526200_6231600_csfgnd_upto30cm_cloth100cm_soft.las. - Progress: R runs non-interactively, so lidR reports each tile as a line (Chunk 3 of 7 (42.9%): state ✓), which the GUI shows on its progress bar as Tile 3 of 7. With tiles of several sizes, the tool prints R_TILES:<done>:<total> before each size, so the GUI counts the tiles of all sizes together. lidR also plots a map of the tiles at the start of each run; R running non-interactively, the map goes to a null device, as the screen device opened a window that kept repainting while R was busy. To see lidR's map of the tiles in an R Graphics window, set the environment variable BERATOOLS_R_GRAPHICS=1 before starting the GUI or the tool; R then runs interactively, as lidR draws the map only then. In that mode lidR shows a text progress bar instead of the tile lines, which the GUI follows too, unless the R package progress is installed: lidR then uses that package's bar, which the GUI can't read, and only the map shows the progress. - Tiled output and mosaic: the raster tools write one raster per tile. When there is more than one tile, they also merge this run's tiles into a single Merged_*.tif: gdalbuildvrt lists the tiles in a VRT, and gdal_translate (through the R package sf) writes the merge from it in one pass as a tiled, DEFLATE-compressed GeoTIFF (BigTIFF when needed). Where tiles overlap, the first tile's cells are kept. The merged hummock/hollow raster (Merged_HH_*) is Int16, with NA as -32768; the other merged rasters keep the tiles' float type. terra::mosaic, used before, wrote the merge twice on one core: 144 tiles of 2000 x 2000 cells took 155-237 s that way, against 3-5 s now. - Spatial indexes: a .lax file that isn't a LAStools index (cut short when a run stopped while writing it, say) is built again. If that fails, that file's points are taken to be spread evenly over it, and the other files still size the tiles from their indexes. - Warnings: a warning is printed once per message after each run, with the number of tiles that gave it. Messages that differ only in their numbers share one line with each number's range, for example Warning from 9 tile(s): Interpolation of 416 to 347838 points failed .... - Parallel processing: the tools run their tiles on R workers through future: mirai daemons (future.mirai) when that package is installed, which the tools that start workers install when it's missing, and otherwise R sessions (future::plan(multisession)). lidR hands the tiles out one at a time and, after each, checks every running tile; with multisession on Windows each check waits ~0.2 s, so with many workers most of them sat idle (46 workers on files of about a minute kept only ~12-16 busy). mirai's check doesn't wait, so all the workers are kept busy. If future.mirai can't be installed (no internet, say) the tool warns and uses multisession. A processes value of 0 or less (the default) means "use every CPU core". - The tools with a tiling option start as many workers as processes and the CPU cores allow (r_max_workers), reduced to the most whose tiles still fit in their share of the free memory (see Tiling), never more workers than tiles, and one worker per 20 million points at most, as starting a worker takes seconds. The workers share the allowed count as lidR threads, floor(allowed / workers) each, for lidR's multi-threaded steps (rasterizing, interpolating, normalizing): memory often allows fewer workers than cores, which otherwise sat idle. A worker starts with OMP_NUM_THREADS set to its threads, as R's BLAS (Intel MKL) commits about 0.2 GB per OpenMP thread when sf and terra load, one per core by default. The tool prints how many workers it uses, the threads of each, and how many points each can hold. The TIN and kriging DTM tools first halve the allowed count when the cell size is under 1 map unit. New lidR tools start their workers through apply_tiling (or start_workers). - LiDAR data info and Raster Cell size read only the files' headers (and one file) and start no workers. The surface water and depth-to-water tools size their workers by the memory of a tile (r_processes_by_memory). - R packages: the environment provides only R and rpy2. Each tool checks for the R packages it needs and installs any missing ones from the CRAN mirror in Canada into the library of the R it runs on (<R_HOME>/library, inside the conda or pixi environment). The first run of each tool can therefore take a while. If a package still can't be installed, the tool stops with R packages could not be installed into <library>: <names>.

How R is located

r_interface.py looks for R before it imports rpy2, and checks these places in order:

  1. An existing R_HOME environment variable
  2. Rscript or R on the PATH (R_HOME is set from the output of <exe> RHOME)
  3. R installed in the conda or pixi environment: Lib/R (Windows) or lib/R (Linux), next to the Python executable or under sys.prefix
  4. A standard Windows install under C:\Program Files\R\R-*

It then adds the environment's Library\bin, Library\mingw-w64\bin, Library\usr\bin, Scripts and R_HOME\bin folders to the DLL search path and PATH. If it can't find R, it prints sys.executable, sys.prefix, R_HOME and the which R / which Rscript results to help with debugging, then raises an error with installation instructions.

How the GUI runs a tool

The GUI starts the tool module in a new process and passes the parameters as JSON with -i/--input. The module checks them against its tools.json entry in BTData.validate_tool_params: it confirms that input folders exist, checks list values against the allowed options, and converts numbers and booleans to the right type. Output parameters aren't checked, because they don't exist yet.

Each module's compose_tool_kwargs("<tool_api>") call must use the tool's tool_api from its module's tools.json, or the tool stops with Tool API '<name>' not found in any module's tools.json.