dataset.sampling.geometry¶
geometry ¶
Geometric patch-packing utilities for footprint polygons.
This module computes, for a given target polygon and a patch size, the set of patch centre locations such that every resulting patch has a guaranteed minimum fractional overlap with the target. It then packs patches inside that valid-centre region with optional controllable overlap between adjacent patches.
Two algorithms are provided:
-
:func:
generate_valid_center_region— constructs the region in which a patch centre may be placed while preserving a minimum intersection area with the target. Convex targets are supported; non-convex targets will still usually work provided the area-vs-radius function is monotone along rays from the centroid. -
:func:
pack_patches_independent_strips— sweeps rows and columns through the valid-centre region to place as many patch centres as possible, supporting an overlap parameter (0 ≤ overlap < 1) that controls the fractional overlap between adjacent patches.
Patch dimensions are expressed as (height, width) to match TorchGeo's
convention. A scalar is accepted for square patches.
as_patch_size ¶
Normalise size to (height, width).
generate_valid_center_region ¶
generate_valid_center_region(target_polygon: Polygon, patch_size: float | tuple[float, float], overlap_percentage: float, extra_rays_per_edge: int = 3) -> Polygon
Return the polygon of valid patch-centre locations.
For a rectangular patch of size patch_size (height, width), a centre
inside the returned polygon is guaranteed to produce a patch whose
intersection area with target_polygon is at least
overlap_percentage × (patch_width × patch_height).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_polygon
|
Polygon
|
The footprint polygon (typically convex). |
required |
patch_size
|
float | tuple[float, float]
|
Scalar (square) or |
required |
overlap_percentage
|
float
|
Required minimum fractional overlap in |
required |
extra_rays_per_edge
|
int
|
Resolution of the boundary sampling. Higher values give tighter approximations at cost of speed. |
3
|
Returns:
| Type | Description |
|---|---|
Polygon
|
A Shapely :class: |
Polygon
|
target is too small to satisfy the overlap constraint at any centre. |
Source code in src/dataset/sampling/geometry.py
pack_patches_independent_strips ¶
pack_patches_independent_strips(valid_region: Polygon, patch_size: float | tuple[float, float], patch_overlap: float = 0.0, phase_steps: int = 20) -> list[tuple[float, float]]
Pack patch centres inside valid_region using row/column sweeps.
Tries both horizontal and vertical sweeps across a range of phase offsets and returns the configuration with the most centres.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
valid_region
|
Polygon
|
Output of :func: |
required |
patch_size
|
float | tuple[float, float]
|
Scalar (square) or |
required |
patch_overlap
|
float
|
Fractional overlap between adjacent patches, in
|
0.0
|
phase_steps
|
int
|
Number of phase offsets to try per direction. |
20
|
Returns:
| Type | Description |
|---|---|
list[tuple[float, float]]
|
List of |
Source code in src/dataset/sampling/geometry.py
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 | |
pack_patches_grid ¶
pack_patches_grid(valid_region: Polygon, patch_size: float | tuple[float, float], patch_overlap: float = 0.0, phase_steps: int = 10) -> list[tuple[float, float]]
Pack patch centres using a rigid rectangular grid with phase search.
This is the original strategy: a single axis-aligned grid is swept in both x and y phases and the highest-count phase wins. Produces a regular lattice of centres (unlike the row/column sweep, which relaxes column alignment between rows).