Skip to content

Mapping API Reference

Tools API

Copyright (C) 2025 Applied Geospatial Research Group.

This script is licensed under the GNU General Public License v3.0. See https://gnu.org/licenses/gpl-3.0 for full license details.

Author: Richard Zeng

Description

This script is part of the BERA Tools. Webpage: https://github.com/BERATool/beratools

The purpose of this script is to provide main interface for line grouping tool.

Copyright (C) 2025 Applied Geospatial Research Group.

This script is licensed under the GNU General Public License v3.0. See https://gnu.org/licenses/gpl-3.0 for full license details.

Author: Richard Zeng

Description

This script is part of the BERA Tools. Webpage: https://github.com/BERATool/beratools

The purpose of this script is the public interface for vertex optimization.

Copyright (C) 2025 Applied Geospatial Research Group.

This script is licensed under the GNU General Public License v3.0. See https://gnu.org/licenses/gpl-3.0 for full license details.

Author: Richard Zeng

Description

This script is part of the BERA Tools. Webpage: https://github.com/BERATool/beratools

The purpose of this script is to provide main interface for centerline tool.

centerline

centerline(in_line, in_raster, line_radius, proc_segments, out_line, merge_by_original_line=False, centerline_method=value, guided_strategy=value, simplify_centerline=False, simplify_diameter=10.0, lcp_simplify_enabled=False, lcp_simplify_diameter=10.0, lcp_smooth_enabled=False, lcp_smooth_iterations=1, chm_mode=value, chm_buffer_width=5.0, chm_buffer_multiplier=0.5, astar_corridor_line_bias_weight=0.1, astar_corridor_distance_penalty_weight=0.2, corridor_simplify_polygon=False, corridor_simplify_length=0.5, corridor_smooth_polygon=False, corridor_polygon_smooth_iterations=1, use_angle_grouping=True, processes=0, call_mode=CLI, log_level='INFO')

Run centerline extraction.

guided_strategy modes: - MAIN_ROUTE: unguided main-route extraction. - PAIRWISE: endpoint-guided search over endpoint node pairs. - VIRTUAL_NODES: endpoint-guided search using virtual source/destination graph nodes. - DIRECT_INSERT: endpoint-guided search by inserting endpoints directly into the Voronoi graph.

Copyright (C) 2025 Applied Geospatial Research Group.

This script is licensed under the GNU General Public License v3.0. See https://gnu.org/licenses/gpl-3.0 for full license details.

Author: Richard Zeng

Description

This script is part of the BERA Tools. Webpage: https://github.com/BERATool/beratools

The purpose of this script is to provide main interface for canopy footprint tool.

canopy_footprint_abs

canopy_footprint_abs(in_line, in_chm, corridor_thresh=3.0, max_ln_width=32.0, exp_shk_cell=0, out_footprint=None, footprint_mode='absolute', tree_radius=1.5, max_line_dist=1.5, canopy_avoidance=0.0, exponent=1.0, canopy_thresh_percentage=50, simplify_footprint_polygon=True, footprint_simplify_length=0.5, smooth_footprint_polygon=False, footprint_polygon_smooth_iterations=1, merge_by_original_line=False, processes=0, call_mode=CLI, log_level='INFO')

Run canopy footprint in absolute or adaptive mode.

line_footprint_relative

line_footprint_relative(in_line, in_chm, max_ln_width, exp_shk_cell, out_footprint, out_centerline, off_ln_dist, canopy_percentile, canopy_thresh_percentage, tree_radius, max_line_dist, canopy_avoidance, exponent, processes, call_mode, log_level, merge_by_original_line=False)

Create canopy footprint polygons using per-line relative canopy thresholds.

Runs in two steps. First, main_canopy_threshold_relative samples the CHM on both sides of each input line and derives an individual canopy threshold height from the surrounding forest. Second, main_line_footprint_relative builds a canopy and cost raster per line and traces a least cost corridor to produce the footprint polygons and, optionally, centerlines. Results are written to out_footprint and out_centerline; if the threshold step fails, an error is printed and nothing is written.

Numeric arguments may be passed as strings (as they are from the GUI) and are converted before use.

Parameters:

Name Type Description Default
in_line

Input line file.

required
in_chm

CHM raster used to calculate the surrounding forest percentile and to build the canopy and cost rasters.

required
max_ln_width

Maximum processing width for input lines. A large value increases processing time; a small value may clip the footprint.

required
exp_shk_cell

Cell range used to expand and shrink the corridor before polygons are generated, to remove small artifacts. Use 0 when the cell size is 1 m or larger.

required
out_footprint

Output footprint polygon file.

required
out_centerline

Optional output centerline file.

required
off_ln_dist

Offset distance from input lines. Currently not used.

required
canopy_percentile

Nth percentile of the surrounding forest height.

required
canopy_thresh_percentage

Percentage of the canopy percentile height used as the canopy threshold.

required
tree_radius

Radius of canopy influence in the cost raster. Smooths the canopy so gaps in sparse vegetation are not taken as footprint.

required
max_line_dist

Maximum euclidean distance from canopy, a second smoothing factor that centers the line in the footprint.

required
canopy_avoidance

Weight between tree search radius (near 0) and euclidean distance (near 1) in the cost raster.

required
exponent

Exponent applied to the cost of vegetated areas.

required
processes

Number of parallel processes.

required
call_mode

How the tool was invoked (see CallMode).

required
log_level

Logging level. Currently not used.

required
merge_by_original_line

Merge footprints and centerlines sharing an original line ID (OLnFID) into one feature; otherwise keep one per input line.

False

Copyright (C) 2025 Applied Geospatial Research Group.

This script is licensed under the GNU General Public License v3.0. See https://gnu.org/licenses/gpl-3.0 for full license details.

Author: Richard Zeng, Maverick Fong

Description

This script is part of the BERA Tools. Webpage: https://github.com/BERATool/beratools

This file hosts the ground_footprint tool.

prepare_line_args

prepare_line_args(line_gdf, poly_gdf, n_samples, offset, width_percentile)

Generate arguments for each line in the GeoDataFrame.

Returns:

Name Type Description
line_args

list row : inter_poly : n_samples : offset : width_percentile :

generate_sample_points

generate_sample_points(line, n_samples=10)

Generate evenly spaced points along a line.

Parameters:

Name Type Description Default
line LineString

The line along which to generate points.

required
n_samples int

The number of points to generate (default is 10).

10

Returns:

Name Type Description
list

List of shapely Point objects.

generate_fixed_width_footprint

generate_fixed_width_footprint(line_gdf, max_width=False)

Create a buffer around each line.

In the GeoDataFrame using its 'max_width' attribute and saves the resulting polygons in a new shapefile.

Args: line_gdf: GeoDataFrame containing LineString with 'max_width' attribute. max_width: Use max width or not to produce buffer.

calculate_average_width

calculate_average_width(line, in_poly, offset, n_samples)

Calculate the average width of a polygon perpendicular to the given line.

Core Algorithms

Copyright (C) 2025 Applied Geospatial Research Group.

This script is licensed under the GNU General Public License v3.0. See https://gnu.org/licenses/gpl-3.0 for full license details.

Author: Richard Zeng

Description

This script is part of the BERA Tools. Webpage: https://github.com/BERATool/beratools

This file is intended to be hosting algorithms and utility functions/classes for centerline tool.

CenterlineParams

Bases: float, Enum

Parameters for centerline generation.

These parameters are used to control the behavior of centerline generation and should be adjusted based on the specific requirements of the application.

CenterlineStatus

Bases: IntEnum

Status of centerline generation.

This enum is used to indicate the status of centerline generation. It can be used to track the success or failure of the centerline generation process.

EndPointAnchorResult dataclass

EndPointAnchorResult(anchored: bool, reversed_match: bool, start_dist: float, end_dist: float)

SeedLine

SeedLine(line_gdf, ras_file, proc_segments, line_radius, guided_strategy='main_route', centerline_method=value, lcp_simplify_enabled=False, lcp_simplify_diameter=10.0, lcp_smooth_enabled=False, lcp_smooth_iterations=1, chm_mode=value, chm_buffer_width=5.0, chm_buffer_multiplier=0.5, astar_corridor_line_bias_weight=0.1, astar_corridor_distance_penalty_weight=0.2, corridor_simplify_polygon=False, corridor_simplify_length=0.5, corridor_smooth_polygon=False, corridor_polygon_smooth_iterations=1)

Class to store seed line and least cost path.

centerline_is_valid

centerline_is_valid(centerline, input_line, cid=None)

Check if centerline is valid.

Parameters:

Name Type Description Default
centerline _type_

description

required
input_line LineString

Seed line or least cost path.

required

Returns:

Name Type Description
bool

True if line is valid

snap_end_to_end

snap_end_to_end(in_line, line_reference, max_snap_dist=None, cid=None)

Snap centerline ends to opposite seedline endpoints.

Prevents

start -> seed_start end -> seed_start

which creates a ring.

find_centerline

find_centerline(poly, input_line, guided_strategy='main_route', endpoint_mode=None, endpoint_candidate_k=8, allow_regeneration=True, cell_size=1.0, corridor_id='UNKNOWN')

Find centerline from polygon and input line.

Parameters:

Name Type Description Default
poly

sh_geom.Polygon

required
input_line LineString

Least cost path or seed line

required

Returns: centerline (sh_geom.LineString): Centerline status (CenterlineStatus): Status of centerline generation

find_corridor_polygon

find_corridor_polygon(corridor_thresh, in_transform, line_gpd, exp_shk_cell=0.0, cid=None)

process_single_centerline

process_single_centerline(row_and_path)

Find centerline.

Args: row_and_path (list of row (gdf and lc_path)): and least cost path first is GeoPandas row, second is input line, (least cost path)

Returns: row: GeoPandas row with centerline

find_centerlines

find_centerlines(poly_gpd, line_seg, processes)

regenerate_centerline

regenerate_centerline(poly, input_line, corridor_id='UNKNOWN', cell_size=1.0, guided_strategy='pairwise', endpoint_mode='strict', endpoint_candidate_k=8)

Regenerates centerline when initial poly is not valid. Split the corridor polygon, extract each half independently, and merge the two centerlines.

Parameters:

Name Type Description Default
input_line LineString

Seed line or least cost path.

required

Returns:

Type Description

sh_geom.MultiLineString

orient_to_split

orient_to_split(line, split_pt, want_end_at_split=True)

dyn_canopy_cost_raster

dyn_canopy_cost_raster(args)

split_line_fc

split_line_fc(line)

split_line_npart

split_line_npart(line)

split_into_segments

split_into_segments(df)

split_into_equal_nth_segments

split_into_equal_nth_segments(df)

generate_line_args

generate_line_args(line_seg, work_in_bufferL, work_in_bufferC, raster, tree_radius, max_line_dist, canopy_avoidance, exponent, work_in_bufferR, canopy_thresh_percentage)

find_corridor_threshold

find_corridor_threshold(raster)

Find the optimal corridor threshold by raster histogram.

Parameters

raster : corridor raster

Returns

corridor_threshold : float

process_single_line_relative

process_single_line_relative(segment)

multiprocessing_footprint_relative

multiprocessing_footprint_relative(line_args, processes)

main_line_footprint_relative

main_line_footprint_relative(in_line: str, in_chm: str, max_ln_width: float, out_footprint: str, out_centerline: str, exp_shk_cell: int, tree_radius: float, max_line_dist: float, canopy_avoidance: float, exponent: float, full_step: bool, canopy_thresh_percentage: int, processes: int, debug_mode: bool = BT_DEBUGGING, merge_by_original_line: bool = False) -> None

Generate dynamic footprints from the input CHM.

Uses the centerlines with forest canopy height and distance to edge. Optionally, smoothed centerlines are generated too.

Parameters:

Name Type Description Default
in_line str

Path like string for input centerlines

required
in_chm str

Path like string input CHM

required
max_ln_width float

Maximum processing width for input lines

required
out_footprint str

Path like string output footprint

required
out_centerline str

(Option) Path like string output centerline

required
exp_shk_cell int

Range as integer used for cell erosion

required
tree_radius float

Radius of trees influence in the cost raster

required
max_line_dist float

Maximum Euclidean distance from canopy

required
canopy_avoidance float

0 < Ratio < 1 of importance between canopy search radius and Euclidean distance

required
exponent float

Affects the cost of vegetated areas in an exponential fashion

required
canopy_thresh_percentage int

Percentage as integer range 1-100

required
merge_by_original_line bool

Merge outputs sharing an OLnFID into one feature, otherwise keep one footprint and centerline per input line (OLnFID, OLnSEG)

False

Returns:

Type Description
None

New saved footprints and/or with smoothed centerlines dataset(s).

OperationCancelledException

Bases: Exception

main_canopy_threshold_relative

main_canopy_threshold_relative(in_line: str, in_chm: str, canopy_percentile: int, canopy_thresh_percentage: int, full_step: bool, processes: int, out_dyn_centerline: str = None) -> str | None

Find the approximate surrounding forest canopy height and forest edge distance from the input CHM.

Parameters:

Name Type Description Default
in_line str

Path like string

required
in_chm str

Path like string

required
canopy_percentile int

Percentile as integer range 1-100

required
canopy_thresh_percentage int

Percentage as integer range 1-100

required

Returns:

Type Description
str | None

Path like string of the saved centerlines with extra attributes

rate_of_change

rate_of_change(in_arg)

multiprocessing_RofC

multiprocessing_RofC(line_seg, worklnbuffer_dfLRing, worklnbuffer_dfRRing, processes)

split_line_fc

split_line_fc(line)

split_into_segments

split_into_segments(df)

multiprocessing_percentile

multiprocessing_percentile(df: GeoDataFrame, can_percentile: int, can_thr_percentage: int, in_chm: str, processes: int, side: str) -> GeoDataFrame | None

cal_percentile_ring

cal_percentile_ring(line_arg)

copyparallel_lineLRC

copyparallel_lineLRC(line_arg)

Common Tool Utilities

Copyright (C) 2025 Applied Geospatial Research Group.

This script is licensed under the GNU General Public License v3.0. See https://gnu.org/licenses/gpl-3.0 for full license details.

Author: Richard Zeng, Maverick Fong

Description

This script is part of the BERA Tools. Webpage: https://github.com/BERATool/beratools

This file is intended to be hosting common spatial classes/functions for BERA Tools

remove_nan_from_array

remove_nan_from_array(matrix)

split_into_equal_Nth_segments

split_into_equal_Nth_segments(df, seg_length)

cut_line_by_length

cut_line_by_length(line, length, merge_threshold=0.5)

Split line into segments of equal length.

Merge the last segment with the second-to-last if its length is smaller than the given threshold.

Parameters:

Name Type Description Default
line

LineString Line to be split by distance along the line.

required
length

float Length of each segment to cut.

required
merge_threshold

float, optional Threshold below which the last segment is merged with the previous one. Default is 0.5.

required

Returns:

Type Description

List of LineString objects A list containing the resulting line segments.

Example

">>> from shapely.geometry import LineString ">>> line = LineString([(0, 0), (10, 0)]) ">>> segments = cut_line_by_length(line, 3, merge_threshold=1) ">>> for segment in segments: ">>> print(f"Segment: {segment}, Length: {segment.length}")

Output: Segment: LINESTRING (0 0, 3 0), Length: 3.0 Segment: LINESTRING (3 0, 6 0), Length: 3.0 Segment: LINESTRING (6 0, 9 0), Length: 3.0 Segment: LINESTRING (9 0, 10 0), Length: 1.0

After merging the last segment with the second-to-last segment:

Output: Segment: LINESTRING (0 0, 3 0), Length: 3.0 Segment: LINESTRING (3 0, 6 0), Length: 3.0 Segment: LINESTRING (6 0, 10 0), Length: 4.0

chk_df_multipart

chk_df_multipart(df: GeoDataFrame, chk_shp_in_string: str) -> tuple[GeoDataFrame, bool]

Check whether the input geopandas.GeoDataFrame contains multipart geometry.

If multipart geometry is found, it is exploded into single geometries. Returns the GeoDataFrame and whether multipart geometry was found.

Parameters:

Name Type Description Default
df GeoDataFrame

Any geopandas.GeoDataFrame like

required
chk_shp_in_string str

String that the input GeoDataFrame geometry type expected to contain, i.e. 'Point', 'Polygon', 'LineString'

required

Returns: Expected geometry type and boolean of multipart geometry

dyn_fs_raster_stdmean

dyn_fs_raster_stdmean(canopy_ndarray, kernel, nodata)

dyn_smooth_cost

dyn_smooth_cost(canopy_ndarray, max_line_dist, sampling)

dyn_np_cost_raster

dyn_np_cost_raster(canopy_ndarray, cc_mean, cc_std, cc_smooth, avoidance, cost_raster_exponent)

dyn_np_cc_map

dyn_np_cc_map(in_chm, canopy_ht_threshold, nodata)

generate_line_args_DFP_NoClip

generate_line_args_DFP_NoClip(line_seg, work_in_bufferL, work_in_bufferC, in_chm_obj, in_chm, tree_radius, max_line_dist, canopy_avoidance, exponent, work_in_bufferR, canopy_thresh_percentage, exp_shk_cell)