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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -13
app.py CHANGED
@@ -916,9 +916,9 @@ def create_contour_map(geojson_file, chem_symbol, lat_min, lat_max, long_min, lo
916
  return {"message": f"An error occurred: {str(e)}"}
917
 
918
 
919
- def plot_high_value_points(geojson_file, chem_symbol, lat_min, lat_max, long_min, long_max):
920
  """
921
- Plots points on a map for geochemical data that have values higher than the average value of the chemical.
922
  Also calculates and includes statistics (mean, mode, median, std deviation, variance) of the chemical values.
923
 
924
  Parameters:
@@ -928,6 +928,7 @@ def plot_high_value_points(geojson_file, chem_symbol, lat_min, lat_max, long_min
928
  - lat_max (float): The maximum latitude value.
929
  - long_min (float): The minimum longitude value.
930
  - long_max (float): The maximum longitude value.
 
931
 
932
  Returns:
933
  - dict: A dictionary containing the message and HTML content of the generated map.
@@ -936,16 +937,19 @@ def plot_high_value_points(geojson_file, chem_symbol, lat_min, lat_max, long_min
936
  "html": str (optional)
937
  }
938
  """
 
939
  lat_limits = (lat_min, lat_max)
940
  long_limits = (long_min, long_max)
941
 
942
  try:
 
943
  gdf = gpd.read_file(geojson_file)
944
 
945
  # Check if the chemical symbol exists in the data
946
  if chem_symbol not in gdf.columns:
947
  return {"message": f"Error: Chemical symbol '{chem_symbol}' not found in the data."}
948
 
 
949
  gdf = gdf[(gdf.geometry.y >= lat_limits[0]) & (gdf.geometry.y <= lat_limits[1]) &
950
  (gdf.geometry.x >= long_limits[0]) & (gdf.geometry.x <= long_limits[1])]
951
 
@@ -955,14 +959,15 @@ def plot_high_value_points(geojson_file, chem_symbol, lat_min, lat_max, long_min
955
  # Calculate statistics of the chemical values
956
  chem_values = gdf[chem_symbol].values
957
  mean_value = np.mean(chem_values)
958
- mode_value = stats.mode(chem_values) # Mode can have multiple values, taking the first one
959
  median_value = np.median(chem_values)
960
  std_deviation = np.std(chem_values)
961
  variance = np.var(chem_values)
962
 
963
- # Filter points with values higher than the average
964
- high_value_points = gdf[gdf[chem_symbol] > mean_value]
965
 
 
966
  lat_center = (lat_limits[0] + lat_limits[1]) / 2
967
  long_center = (long_limits[0] + long_limits[1]) / 2
968
  m = folium.Map(location=[lat_center, long_center], zoom_start=10)
@@ -971,12 +976,13 @@ def plot_high_value_points(geojson_file, chem_symbol, lat_min, lat_max, long_min
971
  for idx, row in high_value_points.iterrows():
972
  folium.Marker(
973
  location=[row.geometry.y, row.geometry.x],
974
- tooltip=f'{chem_symbol}: {row[chem_symbol]}'
975
  ).add_to(m)
976
 
977
  # Add layer control to toggle markers
978
  folium.LayerControl().add_to(m)
979
  m.add_child(MeasureControl())
 
980
  north_arrow_svg = """
981
  <div style="position: fixed;
982
  bottom: 30px; left: 10px; width: 40px; height: 40px;
@@ -991,14 +997,16 @@ def plot_high_value_points(geojson_file, chem_symbol, lat_min, lat_max, long_min
991
  </div>
992
  """
993
  m.get_root().html.add_child(folium.Element(north_arrow_svg))
 
994
  html_content = m.get_root().render()
995
 
996
- message = f"Plotted points with '{chem_symbol}' values higher than mean ({mean_value:.2f})\n"
997
- message += f"Mean: {mean_value:.2f}\n"
998
- message += f"Mode: {mode_value}\n"
999
- message += f"Median: {median_value:.2f}\n"
1000
- message += f"Standard Deviation: {std_deviation:.2f}\n"
1001
- message += f"Variance: {variance:.2f}"
 
1002
 
1003
  return {
1004
  "message": message,
@@ -1006,7 +1014,8 @@ def plot_high_value_points(geojson_file, chem_symbol, lat_min, lat_max, long_min
1006
  }
1007
 
1008
  except Exception as e:
1009
- return {"message": f"An error occurred while generating the statistics: {str(e)}"}
 
1010
 
1011
 
1012
  def Manager_agent(query, lat_min, lat_max, long_min, long_max):
 
916
  return {"message": f"An error occurred: {str(e)}"}
917
 
918
 
919
+ def plot_high_value_points(geojson_file, chem_symbol, lat_min, lat_max, long_min, long_max, threshold):
920
  """
921
+ Plots points on a map for geochemical data that have values higher than a specified threshold.
922
  Also calculates and includes statistics (mean, mode, median, std deviation, variance) of the chemical values.
923
 
924
  Parameters:
 
928
  - lat_max (float): The maximum latitude value.
929
  - long_min (float): The minimum longitude value.
930
  - long_max (float): The maximum longitude value.
931
+ - threshold (float): The threshold value for chemical concentration in ppm.
932
 
933
  Returns:
934
  - dict: A dictionary containing the message and HTML content of the generated map.
 
937
  "html": str (optional)
938
  }
939
  """
940
+ # Define lat/long limits
941
  lat_limits = (lat_min, lat_max)
942
  long_limits = (long_min, long_max)
943
 
944
  try:
945
+ # Load GeoJSON data
946
  gdf = gpd.read_file(geojson_file)
947
 
948
  # Check if the chemical symbol exists in the data
949
  if chem_symbol not in gdf.columns:
950
  return {"message": f"Error: Chemical symbol '{chem_symbol}' not found in the data."}
951
 
952
+ # Filter data for the specified lat/long limits
953
  gdf = gdf[(gdf.geometry.y >= lat_limits[0]) & (gdf.geometry.y <= lat_limits[1]) &
954
  (gdf.geometry.x >= long_limits[0]) & (gdf.geometry.x <= long_limits[1])]
955
 
 
959
  # Calculate statistics of the chemical values
960
  chem_values = gdf[chem_symbol].values
961
  mean_value = np.mean(chem_values)
962
+ mode_value = stats.mode(chem_values) # Properly access the mode value
963
  median_value = np.median(chem_values)
964
  std_deviation = np.std(chem_values)
965
  variance = np.var(chem_values)
966
 
967
+ # Filter points with values higher than the threshold
968
+ high_value_points = gdf[gdf[chem_symbol] > threshold]
969
 
970
+ # Base map centered on the midpoint of the given lat/long limits
971
  lat_center = (lat_limits[0] + lat_limits[1]) / 2
972
  long_center = (long_limits[0] + long_limits[1]) / 2
973
  m = folium.Map(location=[lat_center, long_center], zoom_start=10)
 
976
  for idx, row in high_value_points.iterrows():
977
  folium.Marker(
978
  location=[row.geometry.y, row.geometry.x],
979
+ tooltip=f'{chem_symbol}: {row[chem_symbol]:.2f} ppm'
980
  ).add_to(m)
981
 
982
  # Add layer control to toggle markers
983
  folium.LayerControl().add_to(m)
984
  m.add_child(MeasureControl())
985
+
986
  north_arrow_svg = """
987
  <div style="position: fixed;
988
  bottom: 30px; left: 10px; width: 40px; height: 40px;
 
997
  </div>
998
  """
999
  m.get_root().html.add_child(folium.Element(north_arrow_svg))
1000
+ # Generate the HTML content
1001
  html_content = m.get_root().render()
1002
 
1003
+ # Construct the message with statistics
1004
+ message = f"Plotted points with '{chem_symbol}' values higher than {threshold:.2f} ppm\n"
1005
+ message += f"Mean: {mean_value:.2f} ppm\n"
1006
+ message += f"Mode: {mode_value} ppm\n"
1007
+ message += f"Median: {median_value:.2f} ppm\n"
1008
+ message += f"Standard Deviation: {std_deviation:.2f} ppm\n"
1009
+ message += f"Variance: {variance:.2f} ppm"
1010
 
1011
  return {
1012
  "message": message,
 
1014
  }
1015
 
1016
  except Exception as e:
1017
+ return {"message": f"An error occurred: {str(e)}"}
1018
+
1019
 
1020
 
1021
  def Manager_agent(query, lat_min, lat_max, long_min, long_max):