Skip to content

Commit

Permalink
Merge pull request #58 from MAAP-Project/addDataInPackage
Browse files Browse the repository at this point in the history
add csv and gif in package
  • Loading branch information
emmalu authored May 12, 2023
2 parents eadf9d6 + f622bb3 commit d14e0b5
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 17 deletions.
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand Down
40 changes: 25 additions & 15 deletions stac_ipyleaflet/core.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Main module."""
import os
import csv
from io import BytesIO
import re
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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]
Expand All @@ -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':
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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):
Expand All @@ -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)
Expand Down Expand Up @@ -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)

Expand All @@ -342,4 +352,4 @@ def gen_popup_icon(self, msg):
warning_msg.value=f"<b>{msg}</b>"
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);
self.add_layer(popup_warning);
File renamed without changes.
File renamed without changes
9 changes: 7 additions & 2 deletions write_biomass_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()

Expand Down

0 comments on commit d14e0b5

Please sign in to comment.