Kelbec commited on
Commit
b79ff85
1 Parent(s): acf68bc

fixed feature selection bug

Browse files
Files changed (1) hide show
  1. pages/01_dcascade.py +53 -49
pages/01_dcascade.py CHANGED
@@ -13,10 +13,15 @@ from solara.components.file_drop import FileInfo
13
  from dcascade_py import dcascade_py
14
  import xarray as xr
15
  from shapely.geometry import LineString
 
 
16
 
 
 
 
17
  zoom = solara.reactive(6)
18
  center = solara.reactive((20, 0))
19
- map_loaded = solara.reactive(False)
20
  variables = solara.reactive([])
21
  sel_var = solara.reactive("")
22
  ds = solara.reactive(None)
@@ -49,6 +54,7 @@ map_name = solara.reactive(list(maps)[0])
49
 
50
  def on_variable_change(variable):
51
  print("feature",feature_properties.value)
 
52
  if feature_properties.value is None:
53
  solara.Warning(f"Select a feature on the map first",
54
  text=True,
@@ -58,16 +64,16 @@ def on_variable_change(variable):
58
  else:
59
  chart_options.set({
60
  "line": {
61
- "title": {"text": variable+" "+str(feature_properties.value['FromN']-1)},
62
  "tooltip": {},
63
- "legend": {"data": [variable+" "+str(feature_properties.value['FromN']-1)]},
64
  "xAxis": {"name": "Time step","nameLocation": "middle","nameGap": 30,"data":list(range(1, 21))}, # {"type": "category"},
65
- "yAxis": {"name": variable+" "+str(feature_properties.value['FromN']-1),"nameLocation": "middle","nameGap": 60},
66
  "emphasis": {"itemStyle": {"shadowBlur":10}},
67
  "series": [{
68
  "type": "line",
69
  "universalTransition": True,
70
- "data": ds.value[variable].values[:,0].tolist()
71
  }]
72
  }
73
  })
@@ -80,57 +86,46 @@ def on_feature_click(feature, **kwargs):
80
  feature_properties.set(properties)
81
  sel_feature.set(feature)
82
 
83
- print("GD",geo_data.value.geo_dataframe)
84
- for f in geo_data.value.geo_dataframe['FromN']:
85
- if f == properties['FromN']:
86
- print("FOUND f",f)
87
- # geo_data.value.geo_dataframe.loc[geo_data.value.geo_dataframe['FromN'] == f, 'color'] = 'red'
88
- else:
89
- geo_data.value.geo_dataframe.loc[geo_data.value.geo_dataframe['FromN'] == f, 'color'] = '#3366cc'
90
-
91
- # Highlight the clicked feature
92
- # feature.style = {'color': 'red', 'fillColor': 'red', 'opacity':0.5, 'weight':3.9, 'dashArray':'2', 'fillOpacity':0.5}
93
-
94
-
95
- filename = os.getcwd()+'/public/deposito.nc'
96
- ds.set(xr.open_dataset(filename, decode_times=False))
97
- variables.set(list(ds.value.keys()))
98
-
99
- # ds_dec = xr.decode_cf(ds,decode_timedelta=False)
100
- chart_options.set({
101
- "line": {
102
- "title": {"text": sel_var.value+" "+str(properties['FromN']-1)},
103
- "tooltip": {},
104
- "legend": {"data": [sel_var.value+" "+str(properties['FromN']-1)]},
105
- "xAxis": {"name": "Time step","nameLocation": "middle","nameGap": 30,"data":list(range(1, 21))}, # {"type": "category"},
106
- "yAxis": {"name": sel_var.value+" "+str(properties['FromN']-1),"nameLocation": "middle","nameGap": 60},
107
- "emphasis": {"itemStyle": {"shadowBlur":10}},
108
- "series": [{
109
- "type": "line",
110
- "universalTransition": True,
111
- "data": ds.value[sel_var.value].values[:,properties['FromN']-1].tolist()
112
- }]
113
- }
114
- })
115
 
116
 
117
  @solara.component
118
  def MapComponent():
119
  # Isolation is required to prevent the map from overlapping navigation (when screen width < 960px)
120
- print("RENDERING MAP COMPONENT")
121
  with solara.Column(style={"isolation": "isolate"}):
122
 
123
  sel_geo_data = None
124
 
125
- if not map_loaded.value:
126
- sel_df = gpd.read_file(os.getcwd()+'/public/deposito.shp')
127
- geo_df = sel_df.to_crs(4326) #32634
 
128
  geo_data.set(GeoData(geo_dataframe=geo_df,
129
  hover_style={'fillColor': 'red' , 'fillOpacity': 5.2}, name="deposito"))
130
  geo_data.value.on_click(on_feature_click)
131
  center.set((geo_df.total_bounds[1], geo_df.total_bounds[0]))
132
  geo_data.value.style = {'color': 'black', 'fillColor': '#3366cc', 'opacity':0.5, 'weight':3.9, 'dashArray':'2', 'fillOpacity':0.1}
133
- print(type(sel_feature.value))
134
 
135
  m = Map(center=center.value, zoom = zoom.value, basemap= basemaps.Esri.WorldTopoMap)
136
  map_type = maps[map_name.value]
@@ -140,7 +135,6 @@ def MapComponent():
140
  m.add(geo_data.value)
141
  m.add(LayersControl())
142
  if sel_feature.value is not None:
143
- # print("SEL FEATURE",sel_feature.value)
144
  # Extracting the geometry
145
  geometry = LineString(sel_feature.value['geometry']['coordinates'])
146
 
@@ -180,7 +174,7 @@ def MapComponent():
180
  geo_data.value
181
  ]
182
  )
183
- map_loaded.set(True)
184
  solara.Select(label="Variable", value=sel_var, values=variables.value, on_value=on_variable_change)
185
  FigureEcharts(option=chart_options.value["line"])
186
 
@@ -268,23 +262,33 @@ def Page():
268
  if (river_filename == "" or q_filename == ""):
269
  set_no_file_selected(True)
270
  else:
 
 
271
  set_no_file_selected(False)
272
  sel_form = formulas.index(str(formula).replace("'",""))+1
273
  sel_part = partitionings.index(str(partitioning).replace("'",""))+1
274
  sel_timescale = int(str(timescale).replace("'",""))
275
 
276
  set_map_loaded(False)
 
277
  river = river_filename
278
  q = q_filename
279
- out = os.getcwd()+"/public/deposito"
280
- out_shp = out+".nc" #".shp"
 
 
 
 
 
281
  # out_prj = out+".prj"
282
  # shutil.copyfile(river,out_prj)
283
- dcascade_py(river, q, sed_range=sed_range.value, class_size=class_size.value, deposit=deposit.value, timescale=sel_timescale, formula=sel_form, partitioning=sel_part, out=out_shp, version=False, verbose=False, debug=False, credits=False)
284
  set_map_loaded(True)
285
- filename = os.getcwd()+'/public/deposito.nc'
286
- ds.set(xr.open_dataset(filename, decode_times=False))
287
  variables.set(list(ds.value.keys()))
 
 
288
 
289
  with solara.Div() as main:
290
 
 
13
  from dcascade_py import dcascade_py
14
  import xarray as xr
15
  from shapely.geometry import LineString
16
+ import time
17
+ import pandas as pd
18
 
19
+
20
+ file_nc = solara.reactive("")
21
+ file_shp = solara.reactive("")
22
  zoom = solara.reactive(6)
23
  center = solara.reactive((20, 0))
24
+ global_map_loaded = solara.reactive(False)
25
  variables = solara.reactive([])
26
  sel_var = solara.reactive("")
27
  ds = solara.reactive(None)
 
54
 
55
  def on_variable_change(variable):
56
  print("feature",feature_properties.value)
57
+ sel_var.set(variable)
58
  if feature_properties.value is None:
59
  solara.Warning(f"Select a feature on the map first",
60
  text=True,
 
64
  else:
65
  chart_options.set({
66
  "line": {
67
+ "title": {"text": sel_var.value+" "+str(feature_properties.value['FromN']-1)},
68
  "tooltip": {},
69
+ "legend": {"data": [sel_var.value+" "+str(feature_properties.value['FromN']-1)]},
70
  "xAxis": {"name": "Time step","nameLocation": "middle","nameGap": 30,"data":list(range(1, 21))}, # {"type": "category"},
71
+ "yAxis": {"name": sel_var.value+" "+str(feature_properties.value['FromN']-1),"nameLocation": "middle","nameGap": 60},
72
  "emphasis": {"itemStyle": {"shadowBlur":10}},
73
  "series": [{
74
  "type": "line",
75
  "universalTransition": True,
76
+ "data": ds.value[sel_var.value].values[:,feature_properties.value['FromN']-1].tolist()
77
  }]
78
  }
79
  })
 
86
  feature_properties.set(properties)
87
  sel_feature.set(feature)
88
 
89
+ if(file_nc.value):
90
+ ds.set(xr.open_dataset(file_nc.value, decode_times=False))
91
+ variables.set(list(ds.value.keys()))
92
+
93
+ # ds_dec = xr.decode_cf(ds,decode_timedelta=False)
94
+ chart_options.set({
95
+ "line": {
96
+ "title": {"text": sel_var.value+" "+str(properties['FromN']-1)},
97
+ "tooltip": {},
98
+ "legend": {"data": [sel_var.value+" "+str(properties['FromN']-1)]},
99
+ "xAxis": {"name": "Time step","nameLocation": "middle","nameGap": 30,"data":list(range(1, 21))}, # {"type": "category"},
100
+ "yAxis": {"name": sel_var.value+" "+str(properties['FromN']-1),"nameLocation": "middle","nameGap": 60},
101
+ "emphasis": {"itemStyle": {"shadowBlur":10}},
102
+ "series": [{
103
+ "type": "line",
104
+ "universalTransition": True,
105
+ "data": ds.value[sel_var.value].values[:,properties['FromN']-1].tolist()
106
+ }]
107
+ }
108
+ })
109
+ ds.value.close()
 
 
 
 
 
 
 
 
 
 
 
110
 
111
 
112
  @solara.component
113
  def MapComponent():
114
  # Isolation is required to prevent the map from overlapping navigation (when screen width < 960px)
 
115
  with solara.Column(style={"isolation": "isolate"}):
116
 
117
  sel_geo_data = None
118
 
119
+ if not global_map_loaded.value:
120
+ #sel_df = xr.open_dataset(file_nc.value) # os.getcwd()+'/public/deposito.shp'
121
+ sel_df = gpd.read_file(file_shp.value)
122
+ geo_df = sel_df.to_crs(4326) # 32634
123
  geo_data.set(GeoData(geo_dataframe=geo_df,
124
  hover_style={'fillColor': 'red' , 'fillOpacity': 5.2}, name="deposito"))
125
  geo_data.value.on_click(on_feature_click)
126
  center.set((geo_df.total_bounds[1], geo_df.total_bounds[0]))
127
  geo_data.value.style = {'color': 'black', 'fillColor': '#3366cc', 'opacity':0.5, 'weight':3.9, 'dashArray':'2', 'fillOpacity':0.1}
128
+ # sel_df.close()
129
 
130
  m = Map(center=center.value, zoom = zoom.value, basemap= basemaps.Esri.WorldTopoMap)
131
  map_type = maps[map_name.value]
 
135
  m.add(geo_data.value)
136
  m.add(LayersControl())
137
  if sel_feature.value is not None:
 
138
  # Extracting the geometry
139
  geometry = LineString(sel_feature.value['geometry']['coordinates'])
140
 
 
174
  geo_data.value
175
  ]
176
  )
177
+ global_map_loaded.set(True)
178
  solara.Select(label="Variable", value=sel_var, values=variables.value, on_value=on_variable_change)
179
  FigureEcharts(option=chart_options.value["line"])
180
 
 
262
  if (river_filename == "" or q_filename == ""):
263
  set_no_file_selected(True)
264
  else:
265
+ if ds.value is not None:
266
+ ds.value.close()
267
  set_no_file_selected(False)
268
  sel_form = formulas.index(str(formula).replace("'",""))+1
269
  sel_part = partitionings.index(str(partitioning).replace("'",""))+1
270
  sel_timescale = int(str(timescale).replace("'",""))
271
 
272
  set_map_loaded(False)
273
+ global_map_loaded.set(False)
274
  river = river_filename
275
  q = q_filename
276
+ current_time = str(int(time.time()))
277
+ os.makedirs(os.getcwd()+"/public/data/"+current_time)
278
+ out = f"{os.getcwd()}/public/data/{current_time}/deposito"
279
+ out_nc = out+".nc" #".shp"
280
+ out_shp = out+".shp"
281
+ file_nc.set(out_nc)
282
+ file_shp.set(out_shp)
283
  # out_prj = out+".prj"
284
  # shutil.copyfile(river,out_prj)
285
+ dcascade_py(river, q, sed_range=sed_range.value, class_size=class_size.value, deposit=deposit.value, timescale=sel_timescale, formula=sel_form, partitioning=sel_part, out=out_nc, version=False, verbose=False, debug=False, credits=False)
286
  set_map_loaded(True)
287
+ # filename = os.getcwd()+'/public/deposito.nc'
288
+ ds.set(xr.open_dataset(out_nc, decode_times=False))
289
  variables.set(list(ds.value.keys()))
290
+ sel_var.set(variables.value[0])
291
+ ds.value.close()
292
 
293
  with solara.Div() as main:
294