Kelbec commited on
Commit
ce4502f
1 Parent(s): 6787797

improved with parameters input and fixes

Browse files
Files changed (2) hide show
  1. pages/00_home.py +2 -0
  2. pages/01_dcascade.py +131 -92
pages/00_home.py CHANGED
@@ -24,6 +24,8 @@ def Page():
24
  - https://github.com/saferplaces/dcascade_py.git
25
  by Stefano Bagli (stefano.bagli@gecosistema.com) - Gecosistema srl, Italy
26
  and Valerio Luzzi (valerio.luzzi@gecosistema.com) - Gecosistema srl, Italy
 
 
27
 
28
 
29
  """
 
24
  - https://github.com/saferplaces/dcascade_py.git
25
  by Stefano Bagli (stefano.bagli@gecosistema.com) - Gecosistema srl, Italy
26
  and Valerio Luzzi (valerio.luzzi@gecosistema.com) - Gecosistema srl, Italy
27
+
28
+ * The model is currently in development phase, hence some functionalities might not work as intended.
29
 
30
 
31
  """
pages/01_dcascade.py CHANGED
@@ -13,7 +13,7 @@ from dcascade import main as dcascade_main
13
  zoom = solara.reactive(2)
14
  center = solara.reactive((20, 0))
15
  config = {'version': 'v1',
16
- 'config': {'visState': {'filters': [{'dataId': ['Countries'],
17
  'id': '58woufh3',
18
  'name': ['ts'],
19
  'type': 'range',
@@ -25,8 +25,8 @@ config = {'version': 'v1',
25
  'speed': 1}],
26
  'layers': [{'id': 'lqz57i8',
27
  'type': 'geojson',
28
- 'config': {'dataId': 'Countries',
29
- 'label': 'Countries',
30
  'color': [18, 147, 154],
31
  'highlightColor': [252, 242, 26, 255],
32
  'columns': {'geojson': '_geojson'},
@@ -80,7 +80,7 @@ config = {'version': 'v1',
80
  'heightScale': 'linear',
81
  'radiusField': None,
82
  'radiusScale': 'linear'}}],
83
- 'interactionConfig': {'tooltip': {'fieldsToShow': {'Countries': [{'name': 'ts',
84
  'format': None},
85
  {'name': 'Discharge', 'format': None}]},
86
  'compareMode': False,
@@ -122,7 +122,7 @@ class Map(leafmap.Map):
122
  # change_basemap(self)
123
  # self.add_shp()
124
  def updateMap(self):
125
- self.add_shp("public/deposito.shp", "Countries")
126
  self.config = config
127
  # self.add_basemap("OpenTopoMap")
128
  # change_basemap(self)
@@ -154,11 +154,19 @@ def Page():
154
  q_filename, set_q_filename = solara.use_state("")
155
  error_select_csv, set_error_select_csv = solara.use_state("")
156
  error_select_shp, set_error_select_shp = solara.use_state("")
 
157
  # removed_data, set_removed = solara.use_state("")
158
  size, set_size = solara.use_state(0)
159
  file_browser, set_file_browser = solara.use_state(cast(Optional[Path], None))
160
  map_loaded, set_map_loaded = solara.use_state(False)
161
 
 
 
 
 
 
 
 
162
 
163
 
164
  def on_file(file: FileInfo):
@@ -181,9 +189,12 @@ def Page():
181
  if file_path.endswith(".shp"):
182
  set_river_filename(file_path)
183
  set_error_select_shp(False)
 
184
  else:
185
  set_error_select_shp(True)
186
-
 
 
187
 
188
  def open_q_file():
189
  if file_browser:
@@ -193,106 +204,134 @@ def Page():
193
  set_error_select_csv(False)
194
  else:
195
  set_error_select_csv(True)
 
 
196
 
197
  def remove_data():
198
  for file in os.listdir(os.getcwd()+"/public"):
199
- # print(file)
200
  os.remove(os.getcwd()+"/public/"+file)
201
 
202
 
203
  def run_dcascade():
204
- set_map_loaded(False)
205
- river = river_filename
206
- q = q_filename
207
- out = os.getcwd()+"/public/deposito"
208
- out_shp = out+".shp"
209
- out_prj = out+".prj"
210
- shutil.copyfile(river,out_prj)
211
- dcascade_main.main(river, q, timescale=20, formula=3, partitioning=4, out=out_shp, version=False, verbose=False, debug=False, credits=False)
212
- set_map_loaded(True)
 
 
 
 
 
 
 
 
 
213
 
214
  with solara.Div() as main:
215
 
216
  with solara.Card("Dcascade"):
217
  if map_loaded:
218
  MapComponent()
219
- solara.Markdown("####**Upload files**")
220
- solara.Markdown("Required \*.shp, \*.shx, \*.dbf and \*.prj files for **river** file and \*.csv for **q** file. (one at the time)")
221
- with solara.ColumnsResponsive(1, large=6):
222
- solara.FileDrop(
223
- label="Drag and drop a file here ",
224
- on_file=on_file,
225
- lazy=True,
226
- )
227
- solara.Button("Upload file",
228
- color="primary",
229
- on_click=upload_file)
230
- if content and not uploaded:
231
- solara.Info(f"File {filename} has total length: {size}\n",
232
- text=True,
233
- dense=True,
234
- icon=True,
235
- )
236
- # solara.Preformatted("\n".join(textwrap.wrap(repr(content))))
237
- # solara.Preformatted("\n".join(textwrap.wrap(repr(filename))))
238
- elif uploaded:
239
- solara.Success(
240
- f"{filename} uploaded",
241
- text=True,
242
- dense=True,
243
- icon=True,
244
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
245
 
246
-
247
- solara.Markdown("####**Select files**")
248
- solara.Markdown("Select **river** and **q** files from file explorer below, the uploaded files can be found in /public folder. To select files click on the explorer the click on *SELECT RIVER FILE* or *SELECT Q FILE* based on the file selected.")
249
- with solara.ColumnsResponsive(3, large=6):
250
- solara.Markdown(f"**River file**: {river_filename}")
251
-
252
- solara.Button("Select river file",
253
- color="primary",
254
- on_click=open_river_file)
255
- with solara.ColumnsResponsive(3, large=6):
256
- solara.Markdown(f"**Q file**: {q_filename}")
257
-
258
- solara.Button("Select Q file",
259
- color="primary",
260
- on_click=open_q_file)
261
- if error_select_csv:
262
- solara.Warning(f"Q file must be csv",
263
- text=True,
264
- dense=True,
265
- icon=True,
266
- )
267
- if error_select_shp:
268
- solara.Warning(f"River file must be shp",
269
- text=True,
270
- dense=True,
271
- icon=True,
272
- )
273
 
274
- solara.FileBrowser(
275
- can_select=False,
276
- on_file_open=set_file_browser,
277
- directory="./public"
278
- )
279
- solara.Markdown("#### Run model")
280
- with solara.ColumnsResponsive(3, large=6):
281
- solara.Markdown("Click on DCASCADE button to run the model and generate the output map")
282
- solara.Button("DCASCADE",
283
- color="primary",
284
- on_click=run_dcascade)
285
- # solara.Button("REMOVE DATA",
286
- # color="secondary",
287
- # on_click=remove_data)
288
- # solara.Info(f"You are in directory: {directory}")
289
- # solara.Info(f"You selected path: {path}")
290
- # solara.Info(f"You opened file: {file}")
291
- # if removed_data:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
292
 
293
- # solara.Success(
294
- # f"Removed data",
295
- # text=True,
296
- # dense=True,
297
- # icon=True,
298
- # )
 
13
  zoom = solara.reactive(2)
14
  center = solara.reactive((20, 0))
15
  config = {'version': 'v1',
16
+ 'config': {'visState': {'filters': [{'dataId': ['Discharge'],
17
  'id': '58woufh3',
18
  'name': ['ts'],
19
  'type': 'range',
 
25
  'speed': 1}],
26
  'layers': [{'id': 'lqz57i8',
27
  'type': 'geojson',
28
+ 'config': {'dataId': 'Discharge',
29
+ 'label': 'Discharge',
30
  'color': [18, 147, 154],
31
  'highlightColor': [252, 242, 26, 255],
32
  'columns': {'geojson': '_geojson'},
 
80
  'heightScale': 'linear',
81
  'radiusField': None,
82
  'radiusScale': 'linear'}}],
83
+ 'interactionConfig': {'tooltip': {'fieldsToShow': {'Discharge': [{'name': 'ts',
84
  'format': None},
85
  {'name': 'Discharge', 'format': None}]},
86
  'compareMode': False,
 
122
  # change_basemap(self)
123
  # self.add_shp()
124
  def updateMap(self):
125
+ self.add_shp("public/deposito.shp", "Discharge")
126
  self.config = config
127
  # self.add_basemap("OpenTopoMap")
128
  # change_basemap(self)
 
154
  q_filename, set_q_filename = solara.use_state("")
155
  error_select_csv, set_error_select_csv = solara.use_state("")
156
  error_select_shp, set_error_select_shp = solara.use_state("")
157
+ no_file_selected, set_no_file_selected = solara.use_state("")
158
  # removed_data, set_removed = solara.use_state("")
159
  size, set_size = solara.use_state(0)
160
  file_browser, set_file_browser = solara.use_state(cast(Optional[Path], None))
161
  map_loaded, set_map_loaded = solara.use_state(False)
162
 
163
+ formulas = ["Parker and Klingeman 1982", "Wilkock and Crowe 2003", "Engelund and Hansen 1967", "Yang formula 1989", "Wong and Parker 2006", "Ackers and White formula 1973"]
164
+ formula = solara.reactive("Engelund and Hansen 1967")
165
+ partitionings = ["Direct", "Bed material fraction (BMF)", "Transport capacity function (TCF)", "Shear stress correction approach"]
166
+ partitioning = solara.reactive("Shear stress correction approach")
167
+ timescale = solara.reactive("20")
168
+ continuous_update = solara.reactive(True)
169
+
170
 
171
 
172
  def on_file(file: FileInfo):
 
189
  if file_path.endswith(".shp"):
190
  set_river_filename(file_path)
191
  set_error_select_shp(False)
192
+ set_no_file_selected(False)
193
  else:
194
  set_error_select_shp(True)
195
+ else:
196
+ set_error_select_shp(True)
197
+
198
 
199
  def open_q_file():
200
  if file_browser:
 
204
  set_error_select_csv(False)
205
  else:
206
  set_error_select_csv(True)
207
+ else:
208
+ set_error_select_csv(True)
209
 
210
  def remove_data():
211
  for file in os.listdir(os.getcwd()+"/public"):
 
212
  os.remove(os.getcwd()+"/public/"+file)
213
 
214
 
215
  def run_dcascade():
216
+
217
+ if (river_filename == "" or q_filename == ""):
218
+ set_no_file_selected(True)
219
+ else:
220
+ set_no_file_selected(False)
221
+ sel_form = formulas.index(str(formula).replace("'",""))+1
222
+ sel_part = partitionings.index(str(partitioning).replace("'",""))+1
223
+ sel_timescale = int(str(timescale).replace("'",""))
224
+
225
+ set_map_loaded(False)
226
+ river = river_filename
227
+ q = q_filename
228
+ out = os.getcwd()+"/public/deposito"
229
+ out_shp = out+".shp"
230
+ out_prj = out+".prj"
231
+ shutil.copyfile(river,out_prj)
232
+ dcascade_main.main(river, q, timescale=sel_timescale, formula=sel_form, partitioning=sel_part, out=out_shp, version=False, verbose=False, debug=False, credits=False)
233
+ set_map_loaded(True)
234
 
235
  with solara.Div() as main:
236
 
237
  with solara.Card("Dcascade"):
238
  if map_loaded:
239
  MapComponent()
240
+ with solara.Card("Upload files"):
241
+ solara.Markdown("Required \*.shp, \*.shx, \*.dbf and \*.prj files for **river** file and \*.csv for **q** file. (one at the time)")
242
+ with solara.ColumnsResponsive(1, large=6):
243
+ solara.FileDrop(
244
+ label="Drag and drop a file here ",
245
+ on_file=on_file,
246
+ lazy=True,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
247
  )
248
+ solara.Button("Upload file",
249
+ color="primary",
250
+ on_click=upload_file)
251
+ if content and not uploaded:
252
+ solara.Info(f"File {filename} has total length: {size}\n",
253
+ text=True,
254
+ dense=True,
255
+ icon=True,
256
+ )
257
+ # solara.Preformatted("\n".join(textwrap.wrap(repr(content))))
258
+ # solara.Preformatted("\n".join(textwrap.wrap(repr(filename))))
259
+ elif uploaded:
260
+ solara.Success(
261
+ f"{filename} uploaded",
262
+ text=True,
263
+ dense=True,
264
+ icon=True,
265
+ )
266
 
267
+
268
+
269
+ with solara.Card("Select parameters"):
270
+ with solara.ColumnsResponsive(3, large=4):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
271
 
272
+ solara.InputText("Timescale (days)", value=timescale, continuous_update=continuous_update.value)
273
+
274
+ solara.Select(label="Formula", value=formula, values=formulas)
275
+ # solara.Markdown(f"**Selected**: {formula.value}")
276
+
277
+ solara.Select(label="Partitioning ", value=partitioning, values=partitionings)
278
+ # solara.Markdown(f"**Selected**: {partitioning.value}")
279
+
280
+ with solara.Card("Select files"):
281
+ solara.Markdown("Select **river** and **q** files from file explorer below, the uploaded files can be found in /public folder. To select files click on the explorer the click on *SELECT RIVER FILE* or *SELECT Q FILE* based on the file selected.")
282
+ with solara.ColumnsResponsive(3, large=6):
283
+ solara.Markdown(f"**River file**: {river_filename}")
284
+
285
+ solara.Button("Select river file",
286
+ color="primary",
287
+ on_click=open_river_file)
288
+ with solara.ColumnsResponsive(3, large=6):
289
+ solara.Markdown(f"**Q file**: {q_filename}")
290
+
291
+ solara.Button("Select Q file",
292
+ color="primary",
293
+ on_click=open_q_file)
294
+ if error_select_csv:
295
+ solara.Warning(f"Q file must be csv",
296
+ text=True,
297
+ dense=True,
298
+ icon=True,
299
+ )
300
+ if error_select_shp:
301
+ solara.Warning(f"River file must be shp",
302
+ text=True,
303
+ dense=True,
304
+ icon=True,
305
+ )
306
+
307
+ solara.FileBrowser(
308
+ can_select=False,
309
+ on_file_open=set_file_browser,
310
+ directory="./public"
311
+ )
312
+ with solara.Card("Run model"):
313
+ with solara.ColumnsResponsive(3, large=6):
314
+ solara.Markdown("Click on DCASCADE button to run the model and generate the output map")
315
+ solara.Button("DCASCADE",
316
+ color="primary",
317
+ on_click=run_dcascade)
318
+ if no_file_selected:
319
+ solara.Warning(f"Select both River and Q files",
320
+ text=True,
321
+ dense=True,
322
+ icon=True,
323
+ )
324
+ # solara.Button("REMOVE DATA",
325
+ # color="secondary",
326
+ # on_click=remove_data)
327
+ # solara.Info(f"You are in directory: {directory}")
328
+ # solara.Info(f"You selected path: {path}")
329
+ # solara.Info(f"You opened file: {file}")
330
+ # if removed_data:
331
 
332
+ # solara.Success(
333
+ # f"Removed data",
334
+ # text=True,
335
+ # dense=True,
336
+ # icon=True,
337
+ # )