sentinel_2
Sentinel2Search #
Sentinel2Search(
collection: PlanetaryComputerS2Collection | str = L2A,
date_range: DateLike = None,
bbox: BBoxLike | None = None,
intersects: IntersectsLike | None = None,
logger: Logger = LOGGER,
)
Bases: AbstractStacWrapper
Executable wrapper for Sentinel-2 L2A data on Planetary Computer.
Implements a fluent builder pattern to construct STAC queries for optical data.
Execution and result storage are delegated to an underlying StacSearch client
via proxy properties.
Initialize Sentinel2Search.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
collection
|
PlanetaryComputerS2Collection | str
|
Collection used to search for Sentinel 2 products. |
L2A
|
date_range
|
DateLike
|
Temporal filter, native pystac DateLike. |
None
|
bbox
|
BBoxLike | None
|
Spatial bounding box filter. |
None
|
intersects
|
IntersectsLike | None
|
Spatial GeoJSON geometry filter. |
None
|
logger
|
Logger
|
Custom logger instance. |
LOGGER
|
Source code in src/geospatial_tools/stac/planetary_computer/sentinel_2.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | |
search_results
property
#
search_results: list[Item] | None
Proxy property for STAC search results from the underlying client.
downloaded_assets
property
#
downloaded_assets: list[Asset] | None
Proxy property for downloaded assets from the underlying client.
filter_by_cloud_cover #
filter_by_cloud_cover(max_cloud_cover: int) -> Self
Filter by maximum cloud cover percentage.
Invalidates current search results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_cloud_cover
|
int
|
Maximum percentage of cloud cover allowed. |
required |
Returns:
| Type | Description |
|---|---|
Self
|
The instance itself (Self) for fluent chaining. |
Source code in src/geospatial_tools/stac/planetary_computer/sentinel_2.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | |
filter_by_nodata_pixel_percentage #
filter_by_nodata_pixel_percentage(max_no_data_value: int) -> Self
Filter by maximum no-data pixel percentage.
Invalidates current search results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_no_data_value
|
int
|
Maximum percentage of no-data pixels allowed. |
required |
Returns:
| Type | Description |
|---|---|
Self
|
The instance itself (Self) for fluent chaining. |
Source code in src/geospatial_tools/stac/planetary_computer/sentinel_2.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | |
filter_by_mgrs_tile #
filter_by_mgrs_tile(mgrs_tiles: list[str] | str) -> Self
Filter by MGRS tile ID(s).
Invalidates current search results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mgrs_tiles
|
list[str] | str
|
Single MGRS tile ID or list of IDs. |
required |
Returns:
| Type | Description |
|---|---|
Self
|
The instance itself (Self) for fluent chaining. |
Source code in src/geospatial_tools/stac/planetary_computer/sentinel_2.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | |
with_custom_query #
with_custom_query(query_params: dict[str, Any]) -> Self
Merge custom STAC query parameters and invalidate current state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query_params
|
dict[str, Any]
|
Dictionary of custom STAC query parameters. |
required |
Returns:
| Type | Description |
|---|---|
Self
|
The instance itself (Self) for fluent chaining. |
Source code in src/geospatial_tools/stac/core.py
939 940 941 942 943 944 945 946 947 948 949 950 951 | |
search #
search() -> list[Item] | None
Execute the STAC search using the built query and parameters.
Returns:
| Type | Description |
|---|---|
list[Item] | None
|
List of matched pystac Items, or None if no results. |
Source code in src/geospatial_tools/stac/core.py
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 | |
download #
download(bands: list[str], base_directory: str | Path) -> list[Asset] | None
Download assets for the matched search results.
Triggers a search if no results are currently available.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bands
|
list[str]
|
List of asset keys (bands) to download. |
required |
base_directory
|
str | Path
|
Local directory where assets will be saved. |
required |
Returns:
| Type | Description |
|---|---|
list[Asset] | None
|
List of downloaded Asset objects. |
Source code in src/geospatial_tools/stac/core.py
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 | |
BestProductsForFeatures #
BestProductsForFeatures(
sentinel2_tiling_grid: GeoDataFrame,
sentinel2_tiling_grid_column: str,
vector_features: GeoDataFrame,
vector_features_column: str,
collection: PlanetaryComputerS2Collection | str = L2A,
date_ranges: list[str] | None = None,
max_cloud_cover: int = 5,
max_no_data_value: int = 5,
logger: Logger = LOGGER,
)
Class made to facilitate and automate searching for Sentinel 2 products using the Sentinel 2 tiling grid as a reference.
Current limitation is that vector features used must fit, or be completely contained inside a single Sentinel 2 tiling grid.
For larger features, a mosaic of products will be necessary.
This class was conceived first and foremost to be used for numerous smaller vector
features, like polygon grids created from
geospatial_tools.vector.create_vector_grid
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sentinel2_tiling_grid
|
GeoDataFrame
|
GeoDataFrame containing Sentinel 2 tiling grid |
required |
sentinel2_tiling_grid_column
|
str
|
Name of the column in |
required |
vector_features
|
GeoDataFrame
|
GeoDataFrame containing the vector features for which the best Sentinel 2 products will be chosen for. |
required |
vector_features_column
|
str
|
Name of the column in |
required |
date_ranges
|
list[str] | None
|
Date range used to search for Sentinel 2 products. should be created using
|
None
|
max_cloud_cover
|
int
|
Maximum cloud cover used to search for Sentinel 2 products. |
5
|
logger
|
Logger
|
Logger instance |
LOGGER
|
Source code in src/geospatial_tools/stac/planetary_computer/sentinel_2.py
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 | |
create_date_ranges #
create_date_ranges(
start_year: int, end_year: int, start_month: int, end_month: int
) -> list[str] | None
This function create a list of date ranges.
For example, I want to create date ranges for 2020 and 2021, but only for the months from March to May. I therefore expect to have 2 ranges: [2020-03-01 to 2020-05-30, 2021-03-01 to 2021-05-30].
Handles the automatic definition of the last day for the end month, as well as periods that cross over years
For example, I want to create date ranges for 2020 and 2022, but only for the months from November to January. I therefore expect to have 2 ranges: [2020-11-01 to 2021-01-31, 2021-11-01 to 2022-01-31].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_year
|
int
|
Start year for ranges |
required |
end_year
|
int
|
End year for ranges |
required |
start_month
|
int
|
Starting month for each period |
required |
end_month
|
int
|
End month for each period (inclusively) |
required |
Returns:
| Type | Description |
|---|---|
list[str] | None
|
List of date ranges |
Source code in src/geospatial_tools/stac/planetary_computer/sentinel_2.py
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 | |
find_best_complete_products #
find_best_complete_products(
max_cloud_cover: int | None = None, max_no_data_value: int = 5
) -> dict
Finds the best complete products for each Sentinel 2 tiles. This function will filter out all products that have more than 5% of nodata values.
Filtered out tiles will be stored in self.incomplete and tiles for which
the search has found no results will be stored in self.error_list
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_cloud_cover
|
int | None
|
Max percentage of cloud cover allowed used for the search (Default value = None) |
None
|
max_no_data_value
|
int
|
Max percentage of no-data coverage by individual Sentinel 2 product (Default value = 5) |
5
|
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary of product IDs and their corresponding Sentinel 2 tile names. |
Source code in src/geospatial_tools/stac/planetary_computer/sentinel_2.py
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 | |
select_best_products_per_feature #
select_best_products_per_feature() -> GeoDataFrame
Return a GeoDataFrame containing the best products for each Sentinel 2 tile.
Source code in src/geospatial_tools/stac/planetary_computer/sentinel_2.py
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | |
to_file #
to_file(output_dir: str | Path) -> None
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_dir
|
str | Path
|
Output directory used to write to file |
required |
Source code in src/geospatial_tools/stac/planetary_computer/sentinel_2.py
295 296 297 298 299 300 301 302 303 304 305 306 307 | |
sentinel_2_complete_tile_search #
sentinel_2_complete_tile_search(
tile_id: int,
collection: str,
date_ranges: list[str],
max_cloud_cover: int,
max_no_data_value: int = 5,
) -> tuple[int, str, float | None, float | None] | None
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
collection
|
str
|
|
required |
tile_id
|
int
|
|
required |
date_ranges
|
list[str]
|
|
required |
max_cloud_cover
|
int
|
|
required |
max_no_data_value
|
int
|
(Default value = 5) |
5
|
Returns:
Source code in src/geospatial_tools/stac/planetary_computer/sentinel_2.py
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 | |
find_best_product_per_s2_tile #
find_best_product_per_s2_tile(
collection: str,
date_ranges: list[str],
max_cloud_cover: int,
s2_tile_grid_list: list,
max_no_data_value: int = 5,
num_of_workers: int = 4,
)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
collection
|
str
|
|
required |
date_ranges
|
list[str]
|
|
required |
max_cloud_cover
|
int
|
|
required |
s2_tile_grid_list
|
list
|
|
required |
max_no_data_value
|
int
|
(Default value = 5) |
5
|
num_of_workers
|
int
|
(Default value = 4) |
4
|
Returns:
Source code in src/geospatial_tools/stac/planetary_computer/sentinel_2.py
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 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 | |
write_best_product_ids_to_dataframe #
write_best_product_ids_to_dataframe(
spatial_join_results: GeoDataFrame,
tile_dictionary: dict,
best_product_column: str = "best_s2_product_id",
s2_tiles_column: str = "s2_tiles",
logger: Logger = LOGGER,
) -> None
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spatial_join_results
|
GeoDataFrame
|
|
required |
tile_dictionary
|
dict
|
|
required |
best_product_column
|
str
|
|
'best_s2_product_id'
|
s2_tiles_column
|
str
|
|
's2_tiles'
|
logger
|
Logger
|
|
LOGGER
|
Returns:
Source code in src/geospatial_tools/stac/planetary_computer/sentinel_2.py
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 | |
write_results_to_file #
write_results_to_file(
cloud_cover: int,
successful_results: dict,
incomplete_results: list | None = None,
error_results: list | None = None,
output_dir: str | Path = DATA_DIR,
logger: Logger = LOGGER,
) -> dict
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cloud_cover
|
int
|
|
required |
successful_results
|
dict
|
|
required |
incomplete_results
|
list | None
|
|
None
|
error_results
|
list | None
|
|
None
|
output_dir
|
str | Path
|
|
DATA_DIR
|
logger
|
Logger
|
|
LOGGER
|
Returns:
Source code in src/geospatial_tools/stac/planetary_computer/sentinel_2.py
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 | |
download_and_process_sentinel2_asset #
download_and_process_sentinel2_asset(
product_id: str,
product_bands: list[str],
collections: PlanetaryComputerS2Collection | str = L2A,
target_projection: int | str | None = None,
base_directory: str | Path = DATA_DIR,
delete_intermediate_files: bool = False,
logger: Logger = LOGGER,
) -> Asset
This function downloads a Sentinel 2 product based on the product ID provided.
It will download the individual asset bands provided in the bands argument,
merge then all in a single tif and then reproject them to the input CRS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
product_id
|
str
|
ID of the Sentinel 2 product to be downloaded |
required |
product_bands
|
list[str]
|
List of the product bands to be downloaded |
required |
collections
|
PlanetaryComputerS2Collection | str
|
Collections to be downloaded from. Defaults to |
L2A
|
target_projection
|
int | str | None
|
The CRS project for the end product. If |
None
|
stac_client
|
StacSearch client to used. A new one will be created if not provided |
required | |
base_directory
|
str | Path
|
The base directory path where the downloaded files will be stored |
DATA_DIR
|
delete_intermediate_files
|
bool
|
Flag to determine if intermediate files should be deleted. Defaults to False |
False
|
logger
|
Logger
|
Logger instance |
LOGGER
|
Returns:
| Type | Description |
|---|---|
Asset
|
Asset instance |
Source code in src/geospatial_tools/stac/planetary_computer/sentinel_2.py
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 | |