lyimo commited on
Commit
85a2beb
·
verified ·
1 Parent(s): e3d52c2

Update part2_visualization.py

Browse files
Files changed (1) hide show
  1. part2_visualization.py +125 -41
part2_visualization.py CHANGED
@@ -4,10 +4,23 @@ import plotly.graph_objects as go
4
  from plotly.subplots import make_subplots
5
  import folium
6
  import numpy as np
 
 
 
7
 
8
  class VisualizationHandler:
9
  def __init__(self, optimal_conditions):
10
  self.optimal_conditions = optimal_conditions
 
 
 
 
 
 
 
 
 
 
11
 
12
  def create_interactive_plots(self, df):
13
  """Create enhanced interactive Plotly visualizations"""
@@ -150,57 +163,97 @@ class VisualizationHandler:
150
 
151
  return fig
152
 
153
- def create_map(self, lat, lon, score):
154
- """Create an interactive map with growing suitability overlay"""
155
- m = folium.Map(location=[lat, lon], zoom_start=10)
156
-
157
- # Add location marker
158
- folium.Marker(
159
- [lat, lon],
160
- popup='Analysis Location',
161
- icon=folium.Icon(color='red', icon='info-sign')
162
- ).add_to(m)
163
-
164
- # Color based on score
165
- if score >= 0.8:
166
- color = 'green'
167
- elif score >= 0.6:
168
- color = 'yellow'
169
- elif score >= 0.4:
170
- color = 'orange'
171
- else:
172
- color = 'red'
173
-
174
- # Add analysis zones
175
- folium.Circle(
176
- radius=5000,
177
- location=[lat, lon],
178
- popup=f'Growing Suitability Score: {score:.2f}',
179
- color=color,
180
- fill=True,
181
- fillOpacity=0.4
182
- ).add_to(m)
183
-
184
- # Add smaller analysis zones
185
- for radius in [2000, 3500]:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
186
  folium.Circle(
187
- radius=radius,
188
  location=[lat, lon],
 
189
  color=color,
190
- fill=False,
191
- weight=1
192
  ).add_to(m)
193
-
194
- return m._repr_html_()
195
 
196
- def create_gauge_chart(self, score):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
197
  """Create an enhanced gauge chart for the overall score"""
198
  fig = go.Figure(go.Indicator(
199
  mode="gauge+number+delta",
200
  value=score,
201
  domain={'x': [0, 1], 'y': [0, 1]},
202
  title={
203
- 'text': "Growing Conditions Score",
204
  'font': {'size': 24}
205
  },
206
  delta={
@@ -235,4 +288,35 @@ class VisualizationHandler:
235
  font={'color': "darkblue", 'family': "Arial"}
236
  )
237
 
238
- return fig
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  from plotly.subplots import make_subplots
5
  import folium
6
  import numpy as np
7
+ import ee
8
+ import geemap
9
+ from branca.colormap import LinearColormap
10
 
11
  class VisualizationHandler:
12
  def __init__(self, optimal_conditions):
13
  self.optimal_conditions = optimal_conditions
14
+ self.ndvi_colors = [
15
+ '#d73027', # Very low vegetation
16
+ '#f46d43', # Low vegetation
17
+ '#fdae61', # Sparse vegetation
18
+ '#fee08b', # Moderate vegetation
19
+ '#d9ef8b', # Good vegetation
20
+ '#a6d96a', # High vegetation
21
+ '#66bd63', # Very high vegetation
22
+ '#1a9850' # Dense vegetation
23
+ ]
24
 
25
  def create_interactive_plots(self, df):
26
  """Create enhanced interactive Plotly visualizations"""
 
163
 
164
  return fig
165
 
166
+ def create_enhanced_map(self, lat, lon, weather_score, ndvi_data=None):
167
+ """Create an interactive map with both weather and NDVI data"""
168
+ try:
169
+ # Create base map
170
+ m = folium.Map(location=[lat, lon], zoom_start=15)
171
+
172
+ # If NDVI data is available, add it to the map
173
+ if ndvi_data and 'image' in ndvi_data:
174
+ try:
175
+ # Get NDVI visualization parameters
176
+ vis_params = {
177
+ 'min': -0.2,
178
+ 'max': 0.8,
179
+ 'palette': self.ndvi_colors
180
+ }
181
+
182
+ # Add NDVI layer
183
+ map_id_dict = ndvi_data['image'].getMapId(vis_params)
184
+ folium.TileLayer(
185
+ tiles=map_id_dict['tile_fetcher'].url_format,
186
+ attr='NDVI Data',
187
+ overlay=True,
188
+ name='NDVI Layer'
189
+ ).add_to(m)
190
+
191
+ # Add NDVI legend
192
+ colormap = LinearColormap(
193
+ colors=self.ndvi_colors,
194
+ vmin=-0.2,
195
+ vmax=0.8,
196
+ caption='NDVI Values'
197
+ )
198
+ colormap.add_to(m)
199
+
200
+ except Exception as e:
201
+ print(f"Error adding NDVI layer: {e}")
202
+
203
+ # Calculate combined score if NDVI data is available
204
+ if ndvi_data and 'stats' in ndvi_data:
205
+ ndvi_score = (ndvi_data['stats'].get('NDVI_mean', 0) + 1) / 2
206
+ combined_score = (weather_score * 0.6 + ndvi_score * 0.4)
207
+ else:
208
+ combined_score = weather_score
209
+
210
+ # Color based on combined score
211
+ if combined_score >= 0.8:
212
+ color = 'green'
213
+ elif combined_score >= 0.6:
214
+ color = 'yellow'
215
+ elif combined_score >= 0.4:
216
+ color = 'orange'
217
+ else:
218
+ color = 'red'
219
+
220
+ # Add analysis circles
221
  folium.Circle(
222
+ radius=2000,
223
  location=[lat, lon],
224
+ popup=f'Combined Score: {combined_score:.2f}<br>Weather Score: {weather_score:.2f}',
225
  color=color,
226
+ fill=True,
227
+ fillOpacity=0.4
228
  ).add_to(m)
 
 
229
 
230
+ # Add smaller analysis zones
231
+ for radius in [1000, 1500]:
232
+ folium.Circle(
233
+ radius=radius,
234
+ location=[lat, lon],
235
+ color=color,
236
+ fill=False,
237
+ weight=1
238
+ ).add_to(m)
239
+
240
+ # Add layer control
241
+ folium.LayerControl().add_to(m)
242
+
243
+ return m._repr_html_()
244
+
245
+ except Exception as e:
246
+ print(f"Error creating enhanced map: {e}")
247
+ return None
248
+
249
+ def create_gauge_chart(self, score, title="Growing Conditions Score"):
250
  """Create an enhanced gauge chart for the overall score"""
251
  fig = go.Figure(go.Indicator(
252
  mode="gauge+number+delta",
253
  value=score,
254
  domain={'x': [0, 1], 'y': [0, 1]},
255
  title={
256
+ 'text': title,
257
  'font': {'size': 24}
258
  },
259
  delta={
 
288
  font={'color': "darkblue", 'family': "Arial"}
289
  )
290
 
291
+ return fig
292
+
293
+ def create_ndvi_report(self, ndvi_data):
294
+ """Create a detailed NDVI analysis report"""
295
+ if not ndvi_data or 'stats' not in ndvi_data:
296
+ return None
297
+
298
+ stats = ndvi_data['stats']
299
+ mean_ndvi = stats.get('NDVI_mean', 0)
300
+
301
+ # Create analysis text
302
+ if mean_ndvi < 0:
303
+ vegetation_status = "Very low vegetation - likely bare soil or water"
304
+ elif mean_ndvi < 0.2:
305
+ vegetation_status = "Low vegetation - sparse cover or stressed vegetation"
306
+ elif mean_ndvi < 0.4:
307
+ vegetation_status = "Moderate vegetation - typical for agricultural areas"
308
+ elif mean_ndvi < 0.6:
309
+ vegetation_status = "High vegetation - healthy crops or natural vegetation"
310
+ else:
311
+ vegetation_status = "Very high vegetation - dense, healthy vegetation"
312
+
313
+ report = {
314
+ 'mean_ndvi': mean_ndvi,
315
+ 'std_dev': stats.get('NDVI_stdDev', 0),
316
+ 'min_ndvi': stats.get('NDVI_min', 0),
317
+ 'max_ndvi': stats.get('NDVI_max', 0),
318
+ 'vegetation_status': vegetation_status,
319
+ 'optimal_range': self.optimal_conditions['ndvi']
320
+ }
321
+
322
+ return report