@@ -517,6 +517,18 @@ def _collection_crs(collection_id, env) -> str:
517517 crs = metadata .get ('cube:dimensions' , {}).get ('x' , {}).get ('reference_system' , None )
518518 return crs
519519
520+ def _collection_resolution (collection_id , env ) -> str :
521+ try :
522+ metadata = env .backend_implementation .catalog .get_collection_metadata (collection_id )
523+ except CollectionNotFoundException :
524+ return None
525+ x = metadata .get ('cube:dimensions' , {}).get ('x' , {})
526+ y = metadata .get ('cube:dimensions' , {}).get ('y' , {})
527+ if ( "step" in x and "step" in y ):
528+ return [x ['step' ], y ['step' ]]
529+ else :
530+ return None
531+
520532
521533def _align_extent (extent ,collection_id ,env ,target_resolution = None ):
522534 metadata = None
@@ -529,13 +541,14 @@ def _align_extent(extent,collection_id,env,target_resolution=None):
529541 if metadata is None or not metadata .get ("_vito" ,{}).get ("data_source" , {}).get ("realign" , True ):
530542 return extent
531543
532- crs = metadata .get ('cube:dimensions' , {}).get ('x' , {}).get ('reference_system' , None )
544+ crs = _collection_crs (collection_id , env )
545+ collection_resolution = _collection_resolution (collection_id , env )
533546 isUTM = crs == "AUTO:42001" or "Auto42001" in str (crs )
534547
535548 x = metadata .get ('cube:dimensions' , {}).get ('x' , {})
536549 y = metadata .get ('cube:dimensions' , {}).get ('y' , {})
537- if (target_resolution == None and "step" in x and "step" in y ):
538- target_resolution = [ x [ 'step' ], y [ 'step' ]]
550+ if (target_resolution == None and collection_resolution != None ):
551+ target_resolution = collection_resolution
539552 elif target_resolution == None :
540553 return extent
541554
@@ -606,20 +619,21 @@ def _extract_load_parameters(env: EvalEnv, source_id: tuple) -> LoadParameters:
606619 if extent is not None :
607620 collection_crs = _collection_crs (collection_id [1 ][0 ], env )
608621 crs = constraint .get ("resample" , {}).get ("target_crs" , collection_crs ) or collection_crs
622+ target_resolution = constraint .get ("resample" , {}).get ("resolution" , None ) or _collection_resolution (collection_id [1 ][0 ], env )
609623
610624 if "pixel_buffer" in constraint :
611625
612626 buffer = constraint ["pixel_buffer" ]["buffer_size" ]
613627
614- if crs is not None :
628+ if ( crs is not None ) and target_resolution :
615629 bbox = BoundingBox .from_dict (extent , default_crs = 4326 )
616630 extent = bbox .reproject (crs ).as_dict ()
617631
618632 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 ],
633+ "west" : extent ["west" ] - target_resolution [ 0 ] * buffer [0 ],
634+ "east" : extent ["east" ] + target_resolution [ 0 ] * buffer [0 ],
635+ "south" : extent ["south" ] - target_resolution [ 1 ] * buffer [1 ],
636+ "north" : extent ["north" ] + target_resolution [ 1 ] * buffer [1 ],
623637 "crs" : extent ["crs" ]
624638 }
625639 else :
@@ -636,7 +650,7 @@ def _extract_load_parameters(env: EvalEnv, source_id: tuple) -> LoadParameters:
636650
637651 if no_resampling :
638652 # Ensure that the extent that the user provided is aligned with the collection's native grid.
639- target_resolution = constraint . get ( "resample" ,{}). get ( "resolution" , None )
653+
640654 extent = _align_extent (extent , collection_id [1 ][0 ], env ,target_resolution )
641655
642656 global_extent = spatial_extent_union (global_extent , extent ) if global_extent else extent
0 commit comments