landcover_utils module¶
Landcover Classification Utilities
This module provides utilities for discrete landcover classification workflows, including tile export with background filtering and radiometric normalization for multi-temporal image comparability.
Key Features: - Enhanced tile filtering with configurable feature ratio thresholds - Separate statistics tracking for different skip reasons - LIRRN (Location-Independent Relative Radiometric Normalization) - Maintains full compatibility with base geoai workflow - Optimized for discrete landcover classification tasks
Date: November 2025
export_landcover_tiles(in_raster, out_folder, in_class_data=None, tile_size=256, stride=128, class_value_field='class', buffer_radius=0, max_tiles=None, quiet=False, all_touched=True, create_overview=False, skip_empty_tiles=False, min_feature_ratio=False, metadata_format='PASCAL_VOC')
¶
Export GeoTIFF tiles optimized for landcover classification training.
This function extends the base export_geotiff_tiles with enhanced filtering capabilities specifically designed for discrete landcover classification. It can filter out tiles dominated by background pixels to improve training data quality and reduce dataset size.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_raster
|
str
|
Path to input raster (image to tile) |
required |
out_folder
|
str
|
Output directory for tiles |
required |
in_class_data
|
Optional[Union[str, GeoDataFrame]]
|
Path to vector mask or GeoDataFrame (optional for image-only export) |
None
|
tile_size
|
int
|
Size of output tiles in pixels (default: 256) |
256
|
stride
|
int
|
Stride for sliding window (default: 128) |
128
|
class_value_field
|
str
|
Field name containing class values (default: "class") |
'class'
|
buffer_radius
|
float
|
Buffer radius around features in pixels (default: 0) |
0
|
max_tiles
|
Optional[int]
|
Maximum number of tiles to export (default: None) |
None
|
quiet
|
bool
|
Suppress progress output (default: False) |
False
|
all_touched
|
bool
|
Include pixels touched by geometry (default: True) |
True
|
create_overview
|
bool
|
Create overview image showing tile locations (default: False) |
False
|
skip_empty_tiles
|
bool
|
Skip tiles with no features (default: False) |
False
|
min_feature_ratio
|
Union[bool, float]
|
Minimum ratio of non-background pixels required to keep tile - False: Disable ratio filtering (default) - 0.0-1.0: Minimum ratio threshold (e.g., 0.1 = 10% features required) |
False
|
metadata_format
|
str
|
Annotation format ("PASCAL_VOC" or "YOLO") |
'PASCAL_VOC'
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dictionary containing: - tiles_exported: Number of tiles successfully exported - tiles_skipped_empty: Number of completely empty tiles skipped - tiles_skipped_ratio: Number of tiles filtered by min_feature_ratio - output_dirs: Dictionary with paths to images and labels directories |
Examples:
Original behavior (no filtering)¶
export_landcover_tiles( "input.tif", "output", "mask.shp", skip_empty_tiles=True )
Light filtering (keep tiles with ≥5% features)¶
export_landcover_tiles( "input.tif", "output", "mask.shp", skip_empty_tiles=True, min_feature_ratio=0.05 )
Moderate filtering (keep tiles with ≥15% features)¶
export_landcover_tiles( "input.tif", "output", "mask.shp", skip_empty_tiles=True, min_feature_ratio=0.15 )
Note
This function is designed for discrete landcover classification where class 0 typically represents background/no data. The min_feature_ratio parameter counts non-zero pixels as "features".
Source code in geoai/landcover_utils.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 | |
normalize_radiometric(subject_image, reference_image, output_path=None, method='lirrn', p_n=500, num_quantisation_classes=3, num_sampling_rounds=3, subsample_ratio=0.1, random_state=None)
¶
Normalize subject image radiometry to match a reference image.
Adjusts brightness and contrast of the subject image so that its pixel value distribution matches the reference image. This is essential for multi-temporal analysis where images are acquired under different atmospheric conditions, sensor calibrations, or illumination angles.
Currently supports the LIRRN (Location-Independent Relative Radiometric Normalization) method, which uses multi-Otsu thresholding and linear regression to identify pseudo-invariant features and transform pixel values band-by-band.
Reference: doi:10.3390/s24072272
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
subject_image
|
Union[str, ndarray]
|
Path to the subject GeoTIFF or numpy array with shape (H, W, B). The image to be normalized. |
required |
reference_image
|
Union[str, ndarray]
|
Path to the reference GeoTIFF or numpy array with shape (H, W, B). The target radiometry to match. |
required |
output_path
|
Optional[str]
|
Path to save the normalized image as GeoTIFF. Only applicable when subject_image is a file path (so spatial metadata is available). If None, the array is returned without saving. Default: None. |
None
|
method
|
str
|
Normalization method. Currently only |
'lirrn'
|
p_n
|
int
|
Number of pseudo-invariant feature samples per quantization level. Higher values increase accuracy but slow computation. Default: 500. |
500
|
num_quantisation_classes
|
int
|
Number of brightness strata for stratified sampling. Default: 3. |
3
|
num_sampling_rounds
|
int
|
Number of iterative refinement rounds for sample selection. Default: 3. |
3
|
subsample_ratio
|
float
|
Fraction of candidates retained for regression. Default: 0.1. |
0.1
|
random_state
|
Optional[Union[int, Generator]]
|
Seed or numpy Generator for reproducible results. Default: None (non-deterministic). |
None
|
Returns:
| Type | Description |
|---|---|
Tuple[ndarray, Dict[str, ndarray]]
|
Tuple of (normalized_image, metrics) where:
- normalized_image: numpy array (H, W, B) float64.
- metrics: dict with keys |
Raises:
| Type | Description |
|---|---|
ValueError
|
If method is not |
ValueError
|
If p_n < 1 or num_sampling_rounds < 1. |
ValueError
|
If subject and reference have different band counts. |
ValueError
|
If input arrays are not 3-dimensional. |
ValueError
|
If output_path is set but subject_image is an array. |
FileNotFoundError
|
If file paths do not point to existing files. |
Examples:
Normalize a satellite image using file paths:
1 2 3 4 5 6 7 | |
Normalize using numpy arrays:
1 2 3 4 5 6 | |
Note
The subject and reference images must have the same number of bands but may have different spatial dimensions (height and width).
Source code in geoai/landcover_utils.py
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 | |