TusharsinghBaghel commited on
Commit
055a6c5
·
verified ·
1 Parent(s): 90be64a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +111 -6
app.py CHANGED
@@ -170,6 +170,108 @@ def interpolation_geojson(json_file, chem_symbol, lat_min, lat_max, long_min, lo
170
  except Exception as e:
171
  return {"message": f"An error occurred: {str(e)}"}
172
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173
  def create_heatmap(geojson_file, chem_symbol, lat_min, lat_max, long_min, long_max):
174
  """
175
  Generates a heatmap from geochemical data within specified latitudinal and longitudinal limits.
@@ -400,7 +502,8 @@ def create_geochem_avg_histogram(geojson_file, chem_symbols, lat_min, lat_max, l
400
  plt.close()
401
 
402
  # Create the HTML content
403
- html_content = f'<img src="data:image/png;base64,{img_base64}" />'
 
404
  else:
405
  html_content = '<p>No data available for the specified chemicals and area.</p>'
406
 
@@ -930,8 +1033,8 @@ def Manager_agent(query, lat_min, lat_max, long_min, long_max):
930
  - If it's related to elevation,Empty the list and add 'elevation_' to the list.
931
  - Do not print anything else.
932
 
933
- - Print "Stat": If the query is related to finding mean, mode, variance, etc., of data.
934
- In the next line, print a Python list with one element: the chemical formula(not name) in lowercase mentioned in the query.
935
 
936
  - Print "Contour": If the query is related to creating contour maps.
937
  In the next line, print a Python list with one element: the chemical formula(not name) in lowercase mentioned in the query.
@@ -1083,11 +1186,13 @@ def Manager_agent(query, lat_min, lat_max, long_min, long_max):
1083
  else:
1084
  retval= interpolation_geojson(stream_sed_file, column, lat_min, lat_max, long_min, long_max, 4)
1085
 
1086
-
1087
- elif task == "Stat":
1088
  par_list = eval(lines[1])
 
1089
  column = par_list[0]
1090
- retval= plot_high_value_points(stream_sed_file, column, lat_min, lat_max, long_min, long_max)
 
 
1091
 
1092
  elif task == "Exploration":
1093
  retval=plot_excavation_sites(exploration_file, lat_min, lat_max, long_min, long_max)
 
170
  except Exception as e:
171
  return {"message": f"An error occurred: {str(e)}"}
172
 
173
+
174
+ def plot_high_value_points(geojson_file, chem_symbol, lat_min, lat_max, long_min, long_max, threshold):
175
+ """
176
+ Plots points on a map for geochemical data that have values higher than a specified threshold.
177
+ Also calculates and includes statistics (mean, mode, median, std deviation, variance) of the chemical values.
178
+
179
+ Parameters:
180
+ - geojson_file (str): The file path to the GeoJSON file containing geochemical data.
181
+ - chem_symbol (str): The chemical symbol to visualize.
182
+ - lat_min (float): The minimum latitude value.
183
+ - lat_max (float): The maximum latitude value.
184
+ - long_min (float): The minimum longitude value.
185
+ - long_max (float): The maximum longitude value.
186
+ - threshold (float): The threshold value for chemical concentration in ppm.
187
+
188
+ Returns:
189
+ - dict: A dictionary containing the message and HTML content of the generated map.
190
+ {
191
+ "message": str,
192
+ "html": str (optional)
193
+ }
194
+ """
195
+ # Define lat/long limits
196
+ lat_limits = (lat_min, lat_max)
197
+ long_limits = (long_min, long_max)
198
+
199
+ try:
200
+ # Load GeoJSON data
201
+ gdf = gpd.read_file(geojson_file)
202
+
203
+ # Check if the chemical symbol exists in the data
204
+ if chem_symbol not in gdf.columns:
205
+ return {"message": f"Error: Chemical symbol '{chem_symbol}' not found in the data."}
206
+
207
+ # Filter data for the specified lat/long limits
208
+ gdf = gdf[(gdf.geometry.y >= lat_limits[0]) & (gdf.geometry.y <= lat_limits[1]) &
209
+ (gdf.geometry.x >= long_limits[0]) & (gdf.geometry.x <= long_limits[1])]
210
+
211
+ if gdf.empty:
212
+ return {"message": "Error: No data available within the specified lat/long limits."}
213
+
214
+ # Calculate statistics of the chemical values
215
+ chem_values = gdf[chem_symbol].values
216
+ mean_value = np.mean(chem_values)
217
+ mode_value = stats.mode(chem_values) # Properly access the mode value
218
+ median_value = np.median(chem_values)
219
+ std_deviation = np.std(chem_values)
220
+ variance = np.var(chem_values)
221
+
222
+ # Filter points with values higher than the threshold
223
+ high_value_points = gdf[gdf[chem_symbol] > threshold]
224
+
225
+ # Base map centered on the midpoint of the given lat/long limits
226
+ lat_center = (lat_limits[0] + lat_limits[1]) / 2
227
+ long_center = (long_limits[0] + long_limits[1]) / 2
228
+ m = folium.Map(location=[lat_center, long_center], zoom_start=10)
229
+
230
+ # Add markers for high value points with tooltips
231
+ for idx, row in high_value_points.iterrows():
232
+ folium.Marker(
233
+ location=[row.geometry.y, row.geometry.x],
234
+ tooltip=f'{chem_symbol}: {row[chem_symbol]:.2f} ppm'
235
+ ).add_to(m)
236
+
237
+ # Add layer control to toggle markers
238
+ folium.LayerControl().add_to(m)
239
+ m.add_child(MeasureControl())
240
+
241
+ north_arrow_svg = """
242
+ <div style="position: fixed;
243
+ bottom: 30px; left: 10px; width: 40px; height: 40px;
244
+ z-index: 1000; pointer-events: none;">
245
+ <div>North</div>
246
+ <svg version="1.1" id="icons" xmlns="http://www.w3.org/2000/svg" x="0" y="0" viewBox="0 0 128 128" style="enable-background:new 0 0 128 128" xml:space="preserve">
247
+ <style>.st0,.st1{display:none;fill:#191919}.st1,.st3{fill-rule:evenodd;clip-rule:evenodd}.st3,.st4{display:inline;fill:#191919}</style>
248
+ <g id="row1">
249
+ <path id="nav:2_3_" d="M64 1 17.9 127 64 99.8l46.1 27.2L64 1zm0 20.4 32.6 89.2L64 91.3V21.4z" style="fill:#191919"/>
250
+ </g>
251
+ </svg>
252
+ </div>
253
+ """
254
+ m.get_root().html.add_child(folium.Element(north_arrow_svg))
255
+ # Generate the HTML content
256
+ html_content = m.get_root().render()
257
+
258
+ # Construct the message with statistics
259
+ message = f"Plotted points with '{chem_symbol}' values higher than {threshold:.2f} ppm\n"
260
+ message += f"Mean: {mean_value:.2f} ppm\n"
261
+ message += f"Mode: {mode_value} ppm\n"
262
+ message += f"Median: {median_value:.2f} ppm\n"
263
+ message += f"Standard Deviation: {std_deviation:.2f} ppm\n"
264
+ message += f"Variance: {variance:.2f} ppm"
265
+
266
+ return {
267
+ "message": message,
268
+ "html": html_content
269
+ }
270
+
271
+ except Exception as e:
272
+ return {"message": f"An error occurred: {str(e)}"}
273
+
274
+
275
  def create_heatmap(geojson_file, chem_symbol, lat_min, lat_max, long_min, long_max):
276
  """
277
  Generates a heatmap from geochemical data within specified latitudinal and longitudinal limits.
 
502
  plt.close()
503
 
504
  # Create the HTML content
505
+ html_content = f'<img src="data:image/png;base64,{img_base64}" style="width: 400px; height: 500px;" />'
506
+
507
  else:
508
  html_content = '<p>No data available for the specified chemicals and area.</p>'
509
 
 
1033
  - If it's related to elevation,Empty the list and add 'elevation_' to the list.
1034
  - Do not print anything else.
1035
 
1036
+ - Print "Threshold": If the query is related to finding all the points higher than a threshold.
1037
+ In the next line, print a Python list with 2 elements: first: the chemical formula(not name) in lowercase mentioned in the query and second: the threshold value.
1038
 
1039
  - Print "Contour": If the query is related to creating contour maps.
1040
  In the next line, print a Python list with one element: the chemical formula(not name) in lowercase mentioned in the query.
 
1186
  else:
1187
  retval= interpolation_geojson(stream_sed_file, column, lat_min, lat_max, long_min, long_max, 4)
1188
 
1189
+ elif task == "Threshold":
 
1190
  par_list = eval(lines[1])
1191
+ #print(par_list)
1192
  column = par_list[0]
1193
+ threshold = float(par_list[1])
1194
+ retval= plot_high_value_points(stream_sed_file, column, lat_min, lat_max, long_min, long_max, threshold)
1195
+
1196
 
1197
  elif task == "Exploration":
1198
  retval=plot_excavation_sites(exploration_file, lat_min, lat_max, long_min, long_max)