Skip to content

Commit

Permalink
Support band names lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
banesullivan committed Jan 28, 2024
1 parent 4c04307 commit a535b6f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
4 changes: 4 additions & 0 deletions localtileserver/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ def info(self):
def metadata(self):
return get_meta_data(self.reader)

@property
def band_names(self):
return [desc[0] for desc in self.metadata["band_descriptions"]]

@property
def min_zoom(self):
return self.info.minzoom
Expand Down
15 changes: 14 additions & 1 deletion localtileserver/tiler/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ def get_source_bounds(tile_source: Reader, projection: str = "EPSG:4326", decima


def _handle_band_indexes(tile_source: Reader, indexes: Optional[List[int]] = None):
band_names = [desc[0] for desc in tile_source.info().band_descriptions]

def _index_lookup(index_or_name: str):
try:
return int(index_or_name)
except ValueError:
pass
try:
return band_names.index(index_or_name) + 1
except ValueError:
pass
raise ValueError(f"Could not find band {index_or_name}")

if not indexes:
RGB_INTERPRETATIONS = [ColorInterp.red, ColorInterp.green, ColorInterp.blue]
RGB_DESCRIPTORS = ["red", "green", "blue"]
Expand All @@ -84,7 +97,7 @@ def _handle_band_indexes(tile_source: Reader, indexes: Optional[List[int]] = Non
if isinstance(indexes, int):
indexes = [indexes]
if isinstance(indexes, list):
indexes = [int(i) for i in indexes]
indexes = [_index_lookup(ind) for ind in indexes]
return indexes


Expand Down
5 changes: 5 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ def test_multiband(bahamas):
nodata=0,
).format(z=8, x=72, y=110)
assert get_content(url) # just make sure it doesn't fail
# Check that band names are handled
url = bahamas.get_tile_url(
indexes=bahamas.band_names,
).format(z=8, x=72, y=110)
assert get_content(url) # just make sure it doesn't fail


def test_multiband_vmin_vmax(bahamas):
Expand Down

0 comments on commit a535b6f

Please sign in to comment.