From f622bb3922145210440cfbf8e352ac77916b8953 Mon Sep 17 00:00:00 2001 From: vincentsarago Date: Fri, 12 May 2023 17:04:40 +0200 Subject: [PATCH] add csv and gif in package --- MANIFEST.in | 2 + stac_ipyleaflet/core.py | 40 +++++++++++------- .../data/biomass-layers.csv | 0 .../data/loading.gif | Bin write_biomass_layers.py | 9 +++- 5 files changed, 34 insertions(+), 17 deletions(-) rename biomass-layers.csv => stac_ipyleaflet/data/biomass-layers.csv (100%) rename loading.gif => stac_ipyleaflet/data/loading.gif (100%) diff --git a/MANIFEST.in b/MANIFEST.in index 292d6dd..d45689c 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -2,6 +2,8 @@ include CONTRIBUTING.rst include HISTORY.rst include LICENSE include README.rst +include stac_ipyleaflet/data/* +include stac_ipyleaflet/stac_discovery/catalogs/* recursive-include tests * recursive-exclude * __pycache__ diff --git a/stac_ipyleaflet/core.py b/stac_ipyleaflet/core.py index 5acc959..54d32eb 100644 --- a/stac_ipyleaflet/core.py +++ b/stac_ipyleaflet/core.py @@ -1,4 +1,5 @@ """Main module.""" +import os import csv from io import BytesIO import re @@ -29,8 +30,8 @@ class StacIpyleaflet(Map): titiler_endpoint: str = "https://titiler.maap-project.org" titiler_stac_endpoint: str = "https://titiler-stac.maap-project.org" histogram_layer: Popup - warning_layer: Popup = None - loading_widget_layer: Popup = None + warning_layer: Popup = None + loading_widget_layer: Popup = None bbox_centroid: list = [] def __init__(self, **kwargs): @@ -62,13 +63,18 @@ def __init__(self, **kwargs): self.add_control(draw_control) self.draw_control = draw_control - f=open("./loading.gif", "rb") - gif_widget=ipywidgets.Image( - value=f.read(), - format='png', - width=200, - height=200, + gif_file = os.path.join( + os.path.dirname(__package__), + "data", + "loading.gif", ) + with open(gif_file, "rb") as f: + gif_widget=ipywidgets.Image( + value=f.read(), + format='png', + width=200, + height=200, + ) loading_widget=ipywidgets.VBox() loading_widget.children=[gif_widget] @@ -83,7 +89,7 @@ def layers_button_clicked(self, b): layers_widget.layout.display = 'block' elif layers_widget.layout.display == 'block': layers_widget.layout.display = 'none' - + def stac_widget_display(self, b): stac_widget = self.stac_widget if stac_widget.layout.display == 'none': @@ -148,7 +154,11 @@ def add_toolbar(self): self.add(WidgetControl(widget=stac_widget, position="topright")) def add_biomass_layers(self): - biomass_file = 'biomass-layers.csv' + biomass_file = os.path.join( + os.path.dirname(__package__), + "data", + "biomass-layers.csv", + ) with open(biomass_file, newline='') as f: csv_reader = csv.reader(f) next(csv_reader, None) # skip the headers @@ -213,7 +223,7 @@ def _part_read(src_path: str, *args, **kwargs) -> ImageData: with Reader(src_path) as src: # src.part((minx, miny, maxx, maxy), **kwargs) return src.part(bounds, *args, **kwargs) - # mosaic_reader will use multithreading to distribute the image fetching + # mosaic_reader will use multithreading to distribute the image fetching # and then merge all arrays together # Vincent: This will not work if the image do not have the same resolution (because we won't be able to overlay them). # If you know the resolution you want to use you can use width=.., height=.. instead of max_size=512 (it will ensure you create the same array size for all the images. @@ -229,7 +239,7 @@ def _part_read(src_path: str, *args, **kwargs) -> ImageData: # hist = {} # for ii, b in enumerate(img.count): # h_counts, h_keys = numpy.histogram(data[b].compressed()) - # hist[f"b{ii + 1}"] = [h_counts.tolist(), h_keys.tolist()] + # hist[f"b{ii + 1}"] = [h_counts.tolist(), h_keys.tolist()] return xr.DataArray(data) def update_selected_data(self): @@ -255,7 +265,7 @@ def update_selected_data(self): for idx, layer in enumerate(visible_layers): layer_url = layer.url ds = None - title = layer.name.replace('_', ' ').upper() + title = layer.name.replace('_', ' ').upper() match = re.search('url=(.+.tif)', layer_url) if match and match.group(1): s3_url = match.group(1) @@ -319,7 +329,7 @@ def create_histograms(self, b): except Exception as err: self.remove_layer(self.loading_widget_layer) self.gen_popup_icon(f"Error: {err}") - return + return axes.set_title(dataset.attrs['title']) display(fig) @@ -342,4 +352,4 @@ def gen_popup_icon(self, msg): warning_msg.value=f"{msg}" popup_warning = Popup(location=self.bbox_centroid or self.center, draggable=True, child=warning_msg) self.warning_layer=popup_warning - self.add_layer(popup_warning); \ No newline at end of file + self.add_layer(popup_warning); diff --git a/biomass-layers.csv b/stac_ipyleaflet/data/biomass-layers.csv similarity index 100% rename from biomass-layers.csv rename to stac_ipyleaflet/data/biomass-layers.csv diff --git a/loading.gif b/stac_ipyleaflet/data/loading.gif similarity index 100% rename from loading.gif rename to stac_ipyleaflet/data/loading.gif diff --git a/write_biomass_layers.py b/write_biomass_layers.py index d86ac97..86b93e7 100644 --- a/write_biomass_layers.py +++ b/write_biomass_layers.py @@ -7,7 +7,12 @@ def write_biomass_layers(): data_dir = 'biomass-dashboard-datasets/datasets/' files = os.listdir(data_dir) - with open('biomass-layers.csv', 'w', newline='') as csv_file: + biomass_file = os.path.join( + os.path.dirname(__package__), + "data", + "biomass-layers.csv", + ) + with open(biomass_file, 'w', newline='') as csv_file: fieldnames = ['Layer Name', 'Tiles URL'] writer = csv.DictWriter(csv_file, fieldnames=fieldnames) writer.writeheader() @@ -20,7 +25,7 @@ def write_biomass_layers(): tile_url = tile_url.replace('{titiler_server_url}', 'https://titiler.maap-project.org') file_obj.close() writer.writerow({ - fieldnames[0]: data['id'].capitalize().replace('_', ' '), + fieldnames[0]: data['id'].capitalize().replace('_', ' '), fieldnames[1]: tile_url}) csv_file.close()