|
17 | 17 |
|
18 | 18 | import geopandas as gpd
|
19 | 19 | import numpy as np
|
| 20 | +from pyproj import CRS |
| 21 | +from pyproj.exceptions import CRSError |
20 | 22 |
|
21 | 23 | import openeo.udf
|
22 | 24 | import openeo_processes
|
@@ -506,6 +508,15 @@ def extract_arg(args: ProcessArgs, name: str, process_id="n/a"):
|
506 | 508 | # TODO: eliminate this function, use `ProcessArgs.get_required()` directly
|
507 | 509 | return _as_process_args(args, process_id=process_id).get_required(name=name)
|
508 | 510 |
|
| 511 | +def _collection_crs(collection_id, env) -> str: |
| 512 | + metadata = None |
| 513 | + try: |
| 514 | + metadata = env.backend_implementation.catalog.get_collection_metadata(collection_id) |
| 515 | + except CollectionNotFoundException: |
| 516 | + return None |
| 517 | + crs = metadata.get('cube:dimensions', {}).get('x', {}).get('reference_system', None) |
| 518 | + return crs |
| 519 | + |
509 | 520 |
|
510 | 521 | def _align_extent(extent,collection_id,env,target_resolution=None):
|
511 | 522 | metadata = None
|
@@ -593,18 +604,37 @@ def _extract_load_parameters(env: EvalEnv, source_id: tuple) -> LoadParameters:
|
593 | 604 | if "weak_spatial_extent" in constraint:
|
594 | 605 | extent = constraint["weak_spatial_extent"]
|
595 | 606 | if extent is not None:
|
| 607 | + collection_crs = _collection_crs(collection_id[1][0], env) |
| 608 | + crs = constraint.get("resample", {}).get("target_crs", collection_crs) |
| 609 | + |
596 | 610 | if "pixel_buffer" in constraint:
|
597 |
| - buffer = constraint["pixel_buffer"]["buffer_size"] |
598 |
| - extent = { |
599 |
| - "west": extent["west"] - buffer[0], |
600 |
| - "east": extent["east"] + buffer[0], |
601 |
| - "south": extent["south"] - buffer[1], |
602 |
| - "north": extent["north"] + buffer[1], |
603 |
| - "crs": extent["crs"] |
604 |
| - } |
605 | 611 |
|
| 612 | + buffer = constraint["pixel_buffer"]["buffer_size"] |
606 | 613 |
|
607 |
| - if "resample" not in constraint or not constraint["resample"].get("target_crs",None): |
| 614 | + if crs is not None: |
| 615 | + bbox = BoundingBox.from_dict(extent, default_crs=4326) |
| 616 | + extent = bbox.reproject(crs).as_dict() |
| 617 | + |
| 618 | + extent = { |
| 619 | + "west": extent["west"] - buffer[0], |
| 620 | + "east": extent["east"] + buffer[0], |
| 621 | + "south": extent["south"] - buffer[1], |
| 622 | + "north": extent["north"] + buffer[1], |
| 623 | + "crs": extent["crs"] |
| 624 | + } |
| 625 | + else: |
| 626 | + _log.warning("Not applying buffer to extent because the target CRS is not known.") |
| 627 | + |
| 628 | + no_resampling = "resample" not in constraint or crs == collection_crs |
| 629 | + if (not no_resampling) and collection_crs is not None and ("42001" in str(collection_crs)): |
| 630 | + #resampling auto utm to utm is still considered no resampling |
| 631 | + try: |
| 632 | + no_resampling = "UTM zone" in CRS.from_user_input(crs).to_wkt() |
| 633 | + except CRSError as e: |
| 634 | + pass |
| 635 | + |
| 636 | + |
| 637 | + if no_resampling: |
608 | 638 | # Ensure that the extent that the user provided is aligned with the collection's native grid.
|
609 | 639 | target_resolution = constraint.get("resample",{}).get("resolution",None)
|
610 | 640 | extent = _align_extent(extent, collection_id[1][0], env,target_resolution)
|
|
0 commit comments