BuildingRegularizer

BuildingRegularizer#

class rapidtools.processing.BuildingRegularizer(prompt: str | list[str] = 'building', batch_size: int = 4, load_in_4bit: bool = True, min_overlap_ratio: float = 0.15, max_gap_bridge_ft: float | None = None, confidence_threshold: float = 0.5, mask_threshold: float = 0.5, retry_dropped: bool = True, retry_confidence_threshold: float = 0.3, retry_mask_threshold: float = 0.3, snap_tolerance_ft: float | None = None, cancel_event: Event | None = None, regularize_outlines: bool = True, neck_width_m: float = 1.0, min_area_m2: float = 4.0, max_mask_side: int | None = 1024, model: SAM3Inference | None = None)[source]#

Bases: object

Pipeline component to regularize and refine preliminary building polygons.

This regularizer takes assets that have preliminary building geometries and associated cropped imagery, segments the crops with SAM 3, and returns one clean footprint per building:

  1. The masks of every crop are traced into polygons; a mask whose pieces are separate, or joined only by a strip narrower than neck_width_m, becomes several candidates.

  2. Crops overlap (a building appears in its own crop and in those of its neighbors), so the candidates are fused across crops: two are the same building only if they agree where both crops could see, and two instances from the same crop are never merged. The outline is the one most crops agree on.

  3. Fused buildings are kept if they match a preliminary footprint (IoU of at least min_overlap_ratio, or covering at least half of it, or lying at least half inside it). A preliminary footprint that covered two buildings therefore yields both.

  4. Preliminary footprints without a match get a second, low-threshold pass, which rescues many buildings in shadow.

  5. The footprints get straight, square walls (see FootprintRegularizer) and are made mutually non-overlapping.

Every image attached to an input asset must carry a 'native_georef' (preferred) or 'wgs84_bounds' property, as written by AerialImageryExtractor; images without either are ignored.

Parameters:
  • prompt (str | list[str], optional) – The text prompt(s) used for SAM 3 segmentation (e.g., 'building roof'). Lists are joined with '. '. Defaults to 'building'.

  • batch_size (int, optional) – Batch size for SAM 3 inference. Defaults to 4.

  • load_in_4bit (bool, optional) – Whether to load the SAM 3 model in 4-bit precision to save memory. Defaults to True.

  • min_overlap_ratio (float, optional) – Minimum Intersection over Union (IoU) between a building and a preliminary footprint for the building to be kept (it is also kept when it covers at least half of the footprint or lies at least half inside it). Defaults to 0.15.

  • max_gap_bridge_ft (float | None, optional) – Deprecated and ignored. Gaps inside a mask are no longer bridged: bridging is what merged neighboring buildings.

  • confidence_threshold (float, optional) – Primary confidence threshold for SAM 3 mask generation. Defaults to 0.5.

  • mask_threshold (float, optional) – Primary threshold for SAM 3 mask binarization. Defaults to 0.5.

  • retry_dropped (bool, optional) – Whether to run a second pass with lower thresholds on dropped assets. Defaults to True.

  • retry_confidence_threshold (float, optional) – Lower confidence threshold for the retry pass. Defaults to 0.3.

  • retry_mask_threshold (float, optional) – Lower mask threshold for the retry pass. Defaults to 0.3.

  • snap_tolerance_ft (float | None, optional) – Deprecated and ignored.

  • cancel_event (Event | None, optional) – Cooperative cancellation flag shared with the internal segmenter. When set, OperationCanceled is raised between SAM 3 batches or between pipeline stages. Defaults to None.

  • regularize_outlines (bool, optional) – Give the footprints straight, square walls. With False the fused mask outlines are returned (still non-overlapping). Defaults to True.

  • neck_width_m (float, optional) – Masks are cut where they narrow below this width in meters. Defaults to 1.0.

  • min_area_m2 (float, optional) – Footprints smaller than this are dropped. Defaults to 4.

  • max_mask_side (int | None, optional) – Longest side, in pixels, at which SAM 3 masks are kept (see SAM3ImageSegmenter). Defaults to 1024, which keeps the crops of centimeter-resolution drone orthomosaics from exhausting memory.

  • model (SAM3Inference | None, optional) – An already loaded SAM 3, for example the one of the SAM3OrthoFeatureExtractor that found the preliminary footprints, to use instead of loading a second copy (load_in_4bit is then ignored). Defaults to None.

Example

>>> from rapidtools.processing import (
...     AerialImageryExtractor, BuildingRegularizer, Pipeline
... )
>>> pipeline = Pipeline([
...     AerialImageryExtractor('ortho.tif', 'crops', buffer_asset='30%'),
...     BuildingRegularizer(prompt=['building', 'roof'], batch_size=8),
... ])
>>> refined = pipeline.run(prelim_buildings)
>>> refined[0].attributes['asset_type']
'building_footprint'

Methods

process

Execute the full building regularization pipeline.

process(input_assets: PhysicalAssetCollection) → PhysicalAssetCollection[source]#

Execute the full building regularization pipeline.

This pipeline consists of:

  1. Segmenting the image crops with SAM 3 and tracing the masks.

  2. Fusing the crops’ views of every building and matching the buildings to the preliminary footprints.

  3. (Optional) Retrying dropped assets with lower thresholds.

  4. Giving the footprints straight walls and removing overlaps.

  5. Removing degenerate geometries.

Parameters:

input_assets (PhysicalAssetCollection) – Collection of preliminary building assets.

Returns:

PhysicalAssetCollection – Collection of refined, non-overlapping building assets. The output assets carry new IDs ('bldg_merged_<hash>') and the attributes 'asset_type': 'building_footprint', 'confidence' (mean SAM 3 score, None when the model reported none), 'views' (number of crops that detected the building) and 'source_ids' (IDs of the matched preliminary assets). Returns an empty collection when input_assets is empty.

Raises:

OperationCanceled – If cancel_event is set during processing.

Example

>>> refined = BuildingRegularizer(retry_dropped=False).process(prelim)
>>> len(refined) <= len(prelim) + 5
True