3232
3333
3434class STACRasterBandProperties (BaseModel ):
35- nodata : NodataVal = None
35+ nodata : Optional [ NodataVal ] = None
3636 data_type : Optional [str ] = None
3737 scale : float = 1.0
3838 offset : float = 0.0
3939
4040 @staticmethod
4141 def from_asset (
4242 asset : pystac .Asset ,
43- nodataval : NodataVal = None ,
43+ nodataval : Optional [ NodataVal ] = None ,
4444 ) -> STACRasterBandProperties :
45- if asset .extra_fields .get ("raster:offset" ) is not None :
45+ if asset .extra_fields .get ("raster:offset" , {}) :
4646 properties = dict (
4747 offset = asset .extra_fields .get ("raster:offset" ),
4848 scale = asset .extra_fields .get ("raster:scale" ),
@@ -87,16 +87,22 @@ def asset_to_np_array(
8787 )
8888
8989 logger .debug ("reading asset %s and indexes %s ..." , asset , indexes )
90- data = read_raster (
90+ array = read_raster (
9191 inp = path ,
9292 indexes = indexes ,
9393 grid = grid ,
9494 resampling = resampling .name ,
9595 dst_nodata = band_properties .nodata ,
96- ).data
96+ ).masked_array ()
9797
9898 if apply_offset and band_properties .offset :
99- data_type = band_properties .data_type or data .dtype
99+ logger .debug (
100+ "apply offset %s and scale %s to asset %s" ,
101+ band_properties .offset ,
102+ band_properties .scale ,
103+ asset ,
104+ )
105+ data_type = band_properties .data_type or array .dtype
100106
101107 # determine value range for the target data_type
102108 clip_min , clip_max = dtype_ranges [str (data_type )]
@@ -105,18 +111,17 @@ def asset_to_np_array(
105111 if clip_min == band_properties .nodata :
106112 clip_min += 1
107113
108- data [: ] = (
114+ array [ ~ array . mask ] = (
109115 (
110- ((data * band_properties .scale ) + band_properties .offset )
116+ ((array [ ~ array . mask ] * band_properties .scale ) + band_properties .offset )
111117 / band_properties .scale
112118 )
113119 .round ()
114120 .clip (clip_min , clip_max )
115121 .astype (data_type , copy = False )
116122 .data
117123 )
118-
119- return data
124+ return array
120125
121126
122127def get_assets (
0 commit comments