Skip to content

Commit

Permalink
added flag to return absolute file paths
Browse files Browse the repository at this point in the history
  • Loading branch information
ungarj committed Sep 18, 2017
1 parent ceb79ef commit d8aa304
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
26 changes: 19 additions & 7 deletions s2reader/s2reader.py
Expand Up @@ -353,20 +353,32 @@ def nodata_mask(self):
polys = list(self._get_mask(mask_type="MSK_NODATA"))
return MultiPolygon([poly["geometry"] for poly in polys]).buffer(0)

def band_path(self, band_id, for_gdal=False):
def band_path(self, band_id, for_gdal=False, absolute=False):
"""Return paths of given band's jp2 files for all granules."""
band_id = str(band_id).zfill(2)
if not isinstance(band_id, str) or band_id not in BAND_IDS:
raise ValueError("band ID not valid: %s" % band_id)
if self.dataset.is_zip and for_gdal:
zip_prefix = "/vsizip/"
granule_basepath = zip_prefix + os.path.dirname(
self.dataset.product_metadata_path
)
if absolute:
granule_basepath = zip_prefix + os.path.dirname(os.path.join(
self.dataset.path,
self.dataset.product_metadata_path
))
else:
granule_basepath = zip_prefix + os.path.dirname(
self.dataset.product_metadata_path
)
else:
granule_basepath = os.path.dirname(
self.dataset.product_metadata_path
)
if absolute:
granule_basepath = os.path.dirname(os.path.join(
self.dataset.path,
self.dataset.product_metadata_path
))
else:
granule_basepath = os.path.dirname(
self.dataset.product_metadata_path
)
product_org = self.dataset._product_metadata.iter(
"Product_Organisation").next()
granule_item = [
Expand Down
17 changes: 13 additions & 4 deletions tests/test_s2reader.py
Expand Up @@ -111,10 +111,19 @@ def _test_attributes(test_data, safe_path):
if not granule.nodata_mask.is_empty:
assert granule.nodata_mask.intersects(granule.footprint)
assert isinstance(granule.band_path(2), str)
assert isinstance(granule.band_path("02", for_gdal=True), str)
for bid in BAND_IDS:
band_path = granule.band_path(bid, for_gdal=False)
abs_path = granule.band_path(bid, absolute=True)
assert os.path.isabs(abs_path)
rel_path = granule.band_path(bid, absolute=False)
abs_gdal_path = granule.band_path(
bid, absolute=True, for_gdal=True
)
rel_gdal_path = granule.band_path(
bid, absolute=False, for_gdal=True
)
if safe.is_zip:
assert band_path in safe._zipfile.namelist()
assert abs_gdal_path.startswith("/vsizip/")
assert rel_gdal_path.startswith("/vsizip/")
assert rel_path in safe._zipfile.namelist()
else:
assert os.path.isfile(band_path)
assert os.path.isfile(rel_path)

0 comments on commit d8aa304

Please sign in to comment.