import os from pathlib import Path from typing import Callable, Optional, Union, cast # import leafmap import textwrap import leafmap.kepler as leafmap import solara from leafmap.toolbar import change_basemap from solara.components.file_drop import FileInfo from dcascade import main as dcascade_main zoom = solara.reactive(2) center = solara.reactive((20, 0)) config = {'version': 'v1', 'config': {'visState': {'filters': [{'dataId': ['Countries'], 'id': '58woufh3', 'name': ['ts'], 'type': 'range', 'value': [0, 1.68], ###time change here 'enlarged': False, 'plotType': 'histogram', 'animationWindow': 'free', 'yAxis': None, 'speed': 1}], 'layers': [{'id': 'lqz57i8', 'type': 'geojson', 'config': {'dataId': 'Countries', 'label': 'Countries', 'color': [18, 147, 154], 'highlightColor': [252, 242, 26, 255], 'columns': {'geojson': '_geojson'}, 'isVisible': True, 'visConfig': {'opacity': 0.8, 'strokeOpacity': 0.3, 'thickness': 1, 'strokeColor': [210, 0, 0], 'colorRange': {'name': 'Global Warming', 'type': 'sequential', 'category': 'Uber', 'colors': ['#5A1846', '#900C3F', '#C70039', '#E3611C', '#F1920E', '#FFC300']}, 'strokeColorRange': {'name': 'Ice And Fire', 'type': 'diverging', 'category': 'Uber', 'colors': ['#0198BD', '#49E3CE', '#E8FEB5', '#FEEDB1', '#FEAD54', '#D50255']}, 'radius': 10, 'sizeRange': [0, 10], 'radiusRange': [0, 50], 'heightRange': [0, 500], 'elevationScale': 5, 'enableElevationZoomFactor': True, 'stroked': True, 'filled': False, 'enable3d': False, 'wireframe': False}, 'hidden': False, 'textLabel': [{'field': None, 'color': [255, 255, 255], 'size': 18, 'offset': [0, 0], 'anchor': 'start', 'alignment': 'center'}]}, 'visualChannels': {'colorField': None, 'colorScale': 'quantile', 'strokeColorField': {'name': 'Discharge', 'type': 'real'}, 'strokeColorScale': 'quantize', 'sizeField': None, 'sizeScale': 'linear', 'heightField': None, 'heightScale': 'linear', 'radiusField': None, 'radiusScale': 'linear'}}], 'interactionConfig': {'tooltip': {'fieldsToShow': {'Countries': [{'name': 'ts', 'format': None}, {'name': 'Discharge', 'format': None}]}, 'compareMode': False, 'compareType': 'absolute', 'enabled': True}, 'brush': {'size': 0.5, 'enabled': False}, 'geocoder': {'enabled': False}, 'coordinate': {'enabled': False}}, 'layerBlending': 'additive', 'splitMaps': [], 'animationConfig': {'currentTime': None, 'speed': 1}}, 'mapState': {'bearing': 0, 'dragRotate': False, 'latitude': 40.24060796334474, 'longitude': 20.565835594966174, 'pitch': 0, 'zoom': 8.776177236597563, 'isSplit': False}, 'mapStyle': {'styleType': 'dark', 'topLayerGroups': {}, 'visibleLayerGroups': {'label': True, 'road': True, 'border': False, 'building': True, 'water': True, 'land': True, '3d building': False}, 'threeDBuildingColor': [9.665468314072013, 17.18305478057247, 31.1442867897876], 'mapStyles': {}}}} class Map(leafmap.Map): def __init__(self, **kwargs): super().__init__(**kwargs) self.updateMap() # Add what you want below # self.add_basemap("OpenTopoMap") # change_basemap(self) # self.add_shp() def updateMap(self): print("ADDING SHP") self.add_shp("public/deposito.shp", "Countries") self.config = config print("UPDATED MAP AND CONFIGS") # self.add_basemap("OpenTopoMap") # change_basemap(self) @solara.component def MapComponent(): m = Map() m.element( # type: ignore zoom=zoom.value, on_zoom=zoom.set, center=center.value, on_center=center.set, scroll_wheel_zoom=True, toolbar_ctrl=False, data_ctrl=False, ) m.config = config @solara.component def Page(): with solara.Column(style={"min-width": "500px"}): # solara components support reactive variables # solara.SliderInt(label="Zoom level", value=zoom, min=1, max=20) # using 3rd party widget library require wiring up the events manually # using zoom.value and zoom.set # solara.Text(f"Zoom: {zoom.value}") # solara.Text(f"Center: {center.value}") content, set_content = solara.use_state(b"") filename, set_filename = solara.use_state("") size, set_size = solara.use_state(0) file_browser, set_file_browser = solara.use_state(cast(Optional[Path], None)) map_loaded, set_map_loaded = solara.use_state(False) if map_loaded: MapComponent() def on_file(file: FileInfo): set_filename(file["name"]) set_size(file["size"]) f = file["file_obj"] set_content(f.read()) def upload_file(): if content: file_path = os.path.join("public",filename) with open(file_path,"wb") as file: file.write(content) def open_file(): print(type(str(file_browser))) if file_browser: file_path = os.path.join("public",file_browser) with open(file_path,"wb") as file: file.write(content) # m.add_shp(str(file_browser), "Countries") def run_dcascade(): river = os.getcwd()+"/public/River_Network.shp" q = os.getcwd()+"/public/Q_Vjosa.csv" print(os.getcwd()) print(os.listdir(os.getcwd()+"/public")) # print(file_river) # print(os.path.exists(Path(file_river))) dcascade_main.main(river, q, timescale=20, formula=3, partitioning=4, out="public/deposito.shp", version=False, verbose=False, debug=False, credits=False) # m = leafmap.Map.element(center=[20, 0], zoom=1) # m.add_shp("public/deposito.shp", "Countries") # m.config = config print("UPDATING MAP") set_map_loaded(True) # m.updateMap() with solara.Div() as main: with solara.Card("Upload files and run dcascade"): solara.FileDrop( label="Drag and drop a file here ", on_file=on_file, lazy=True, ) with solara.ColumnsResponsive(6, large=4): solara.Button("Upload file", color="primary", on_click=upload_file) # solara.Button("Open file", # color="primary", # on_click=open_file) solara.Button("DCASCADE", color="primary", on_click=run_dcascade) if content: solara.Info(f"File {filename} has total length: {size}\n, first 100 bytes:") # solara.Preformatted("\n".join(textwrap.wrap(repr(content)))) solara.Preformatted("\n".join(textwrap.wrap(repr(filename)))) solara.FileBrowser( can_select=False, on_file_open=set_file_browser ) # solara.Info(f"You are in directory: {directory}") # solara.Info(f"You selected path: {path}") # solara.Info(f"You opened file: {file}")