Skip to content

Commit 9447655

Browse files
committed
FIX: Use already computed bands stored in tmp for Planet products
1 parent 6369337 commit 9447655

6 files changed

Lines changed: 80 additions & 118 deletions

File tree

CHANGES.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Release History
22

3+
## 0.20.2 (2023-06-20)
4+
5+
### Bug Fixes
6+
7+
- FIX: Use already computed bands stored in `tmp` for Planet products
8+
39
## 0.20.1 (2023-06-20)
410

511
### Bug Fixes

eoreader/__meta__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"""
1818
**EOReader** library
1919
"""
20-
__version__ = "0.20.1"
20+
__version__ = "0.20.2.dev0"
2121
__title__ = "eoreader"
2222
__description__ = (
2323
"Remote-sensing opensource python library reading optical and SAR constellations, "

eoreader/products/optical/pla_product.py

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -467,41 +467,6 @@ def _get_stack_path(self, as_list: bool = False) -> Union[str, list]:
467467

468468
return stack_path
469469

470-
def get_band_paths(
471-
self, band_list: list, pixel_size: float = None, **kwargs
472-
) -> dict:
473-
"""
474-
Return the paths of required bands.
475-
476-
.. code-block:: python
477-
478-
>>> from eoreader.reader import Reader
479-
>>> from eoreader.bands import *
480-
>>> path = r"SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2"
481-
>>> prod = Reader().open(path)
482-
>>> prod.get_band_paths([GREEN, RED])
483-
{
484-
<SpectralBandNames.GREEN: 'GREEN'>:
485-
'SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2/SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2_FRE_B3.tif',
486-
<SpectralBandNames.RED: 'RED'>:
487-
'SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2/SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2_FRE_B4.tif'
488-
}
489-
490-
Args:
491-
band_list (list): List of the wanted bands
492-
pixel_size (float): Band pixel size
493-
kwargs: Other arguments used to load bands
494-
495-
Returns:
496-
dict: Dictionary containing the path of each queried band
497-
"""
498-
band_paths = {}
499-
path = self._get_stack_path(as_list=False)
500-
for band in band_list:
501-
band_paths[band] = path
502-
503-
return band_paths
504-
505470
def _to_reflectance(
506471
self,
507472
band_arr: xr.DataArray,

eoreader/products/optical/planet_product.py

Lines changed: 73 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import geopandas as gpd
3030
import numpy as np
31+
import rasterio
3132
import xarray as xr
3233
from cloudpathlib import CloudPath
3334
from lxml import etree
@@ -200,7 +201,9 @@ def _post_init(self, **kwargs) -> None:
200201
(setting sensor type, band names and so on)
201202
"""
202203
# Manage Raw unit
203-
band_name = files.get_filename(self.get_default_band_path()).upper().split("_")
204+
band_name = (
205+
files.get_filename(self._get_stack_path(as_list=False)).upper().split("_")
206+
)
204207
if "SR" in band_name:
205208
self._raw_units = RawUnits.REFL
206209
elif "DN" in band_name:
@@ -389,6 +392,48 @@ def footprint(self) -> gpd.GeoDataFrame:
389392

390393
return gpd.GeoDataFrame(geometry=footprint.geometry, crs=footprint.crs)
391394

395+
def get_band_paths(
396+
self, band_list: list, pixel_size: float = None, **kwargs
397+
) -> dict:
398+
"""
399+
Return the paths of required bands.
400+
401+
.. code-block:: python
402+
403+
>>> from eoreader.reader import Reader
404+
>>> from eoreader.bands import *
405+
>>> path = r"SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2"
406+
>>> prod = Reader().open(path)
407+
>>> prod.get_band_paths([GREEN, RED])
408+
{
409+
<SpectralBandNames.GREEN: 'GREEN'>:
410+
'SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2/SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2_FRE_B3.tif',
411+
<SpectralBandNames.RED: 'RED'>:
412+
'SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2/SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2_FRE_B4.tif'
413+
}
414+
415+
Args:
416+
band_list (list): List of the wanted bands
417+
pixel_size (float): Band pixel size
418+
kwargs: Other arguments used to load bands
419+
420+
Returns:
421+
dict: Dictionary containing the path of each queried band
422+
"""
423+
band_paths = {}
424+
path = self._get_stack_path(as_list=False)
425+
for band in band_list:
426+
# Get clean band path
427+
clean_band = self._get_clean_band_path(
428+
band, pixel_size=pixel_size, **kwargs
429+
)
430+
if clean_band.is_file():
431+
band_paths[band] = clean_band
432+
else:
433+
band_paths[band] = path
434+
435+
return band_paths
436+
392437
def _read_band(
393438
self,
394439
path: Union[CloudPath, Path],
@@ -413,15 +458,32 @@ def _read_band(
413458
xr.DataArray: Band xarray
414459
415460
"""
416-
# Read band
417-
band_arr = utils.read(
418-
path,
419-
pixel_size=pixel_size,
420-
size=size,
421-
resampling=Resampling.bilinear,
422-
indexes=[self.bands[band].id],
423-
**kwargs,
424-
)
461+
with rasterio.open(str(path)) as dst:
462+
# Manage the case if we open a simple band (EOReader processed bands)
463+
if dst.count == 1:
464+
# Read band
465+
band_arr = utils.read(
466+
path,
467+
pixel_size=pixel_size,
468+
size=size,
469+
resampling=Resampling.bilinear,
470+
**kwargs,
471+
)
472+
473+
# Manage the case if we open a stack (native DIMAP bands)
474+
else:
475+
band_arr = utils.read(
476+
path,
477+
pixel_size=pixel_size,
478+
size=size,
479+
resampling=Resampling.bilinear,
480+
indexes=[self.bands[band].id],
481+
**kwargs,
482+
)
483+
484+
# Pop useless long name
485+
if "long_name" in band_arr.attrs:
486+
band_arr.attrs.pop("long_name")
425487

426488
# To float32
427489
if band_arr.dtype != np.float32:
@@ -526,7 +588,7 @@ def _load_bands(
526588
return {}
527589

528590
# Get band paths
529-
band_paths = self.get_band_paths(bands, **kwargs)
591+
band_paths = self.get_band_paths(bands, pixel_size, **kwargs)
530592

531593
# Open bands and get array (resampled if needed)
532594
band_arrays = self._open_bands(

eoreader/products/optical/re_product.py

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -261,42 +261,6 @@ def _get_stack_path(self, as_list: bool = False) -> Union[str, list]:
261261

262262
return stack_path
263263

264-
def get_band_paths(
265-
self, band_list: list, pixel_size: float = None, **kwargs
266-
) -> dict:
267-
"""
268-
Return the paths of required bands.
269-
270-
.. code-block:: python
271-
272-
>>> from eoreader.reader import Reader
273-
>>> from eoreader.bands import *
274-
>>> path = r"SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2"
275-
>>> prod = Reader().open(path)
276-
>>> prod.get_band_paths([GREEN, RED])
277-
{
278-
<SpectralBandNames.GREEN: 'GREEN'>:
279-
'SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2/SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2_FRE_B3.tif',
280-
<SpectralBandNames.RED: 'RED'>:
281-
'SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2/SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2_FRE_B4.tif'
282-
}
283-
284-
Args:
285-
band_list (list): List of the wanted bands
286-
pixel_size (float): Band pixel size
287-
kwargs: Other arguments used to load bands
288-
289-
Returns:
290-
dict: Dictionary containing the path of each queried band
291-
"""
292-
band_paths = {}
293-
path = self._get_stack_path(as_list=False)
294-
295-
for band in band_list:
296-
band_paths[band] = path
297-
298-
return band_paths
299-
300264
def _dn_to_toa_rad(self, dn_arr: xr.DataArray, band: BandNames) -> xr.DataArray:
301265
"""
302266
Compute DN to TOA radiance

eoreader/products/optical/sky_product.py

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -375,41 +375,6 @@ def _get_stack_path(self, as_list: bool = False) -> Union[str, list]:
375375

376376
return stack_path
377377

378-
def get_band_paths(
379-
self, band_list: list, pixel_size: float = None, **kwargs
380-
) -> dict:
381-
"""
382-
Return the paths of required bands.
383-
384-
.. code-block:: python
385-
386-
>>> from eoreader.reader import Reader
387-
>>> from eoreader.bands import *
388-
>>> path = r"SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2"
389-
>>> prod = Reader().open(path)
390-
>>> prod.get_band_paths([GREEN, RED])
391-
{
392-
<SpectralBandNames.GREEN: 'GREEN'>:
393-
'SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2/SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2_FRE_B3.tif',
394-
<SpectralBandNames.RED: 'RED'>:
395-
'SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2/SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2_FRE_B4.tif'
396-
}
397-
398-
Args:
399-
band_list (list): List of the wanted bands
400-
pixel_size (float): Band pixel size
401-
kwargs: Other arguments used to load bands
402-
403-
Returns:
404-
dict: Dictionary containing the path of each queried band
405-
"""
406-
band_paths = {}
407-
path = self._get_stack_path(as_list=False)
408-
for band in band_list:
409-
band_paths[band] = path
410-
411-
return band_paths
412-
413378
def _to_reflectance(
414379
self,
415380
band_arr: xr.DataArray,

0 commit comments

Comments
 (0)