-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathndvi_visualization.py
More file actions
304 lines (252 loc) · 10 KB
/
Copy pathndvi_visualization.py
File metadata and controls
304 lines (252 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
"""NDVI Visualization Module
This module provides visualization functions for NDVI statistics including:
- Bar charts for class distribution
- Pie charts for vegetation coverage
- Comparison charts for baseline vs updated NDVI
- Summary statistics displays
"""
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import plotly.graph_objects as go
import plotly.express as px
from plotly.subplots import make_subplots
import pandas as pd
import numpy as np
from typing import Dict, List, Tuple
import io
import base64
# Color schemes for visualizations
NDVI_CLASS_COLORS = [
'#a50026', # Absent Vegetation
'#ed5e3d', # Bare Soil
'#f9f7ae', # Low Vegetation
'#f4ff78', # Light Vegetation
'#9ed569', # Moderate Vegetation
'#229b51', # Strong Vegetation
'#006837' # Dense Vegetation
]
class NDVIVisualizer:
"""Class for creating NDVI data visualizations"""
def __init__(self, color_palette='Normal'):
"""
Initialize visualizer with color palette
Args:
color_palette: Color palette name for accessibility
"""
self.color_palette = color_palette
self.colors = self._get_color_scheme(color_palette)
def _get_color_scheme(self, palette_name):
"""Get appropriate color scheme based on accessibility needs"""
if palette_name == 'Deuteranopia':
return ['#95a600', '#92ed3e', '#affac5', '#78ffb0', '#69d6c6', '#22459c', '#000e69']
elif palette_name == 'Protanopia':
return ['#95a600', '#92ed3e', '#affac5', '#78ffb0', '#69d6c6', '#22459c', '#000e69']
elif palette_name == 'Tritanopia':
return ['#ed4700', '#ed8a00', '#e1fabe', '#99ff94', '#87bede', '#2e40cf', '#0600bc']
elif palette_name == 'Achromatopsia':
return ['#004f3d', '#338796', '#66a4f5', '#3683ff', '#3d50ca', '#421c7f', '#290058']
else:
return NDVI_CLASS_COLORS
def create_class_distribution_chart(self, class_stats: Dict, title: str = "NDVI Class Distribution"):
"""
Create bar chart showing NDVI class distribution
Args:
class_stats: Dictionary with class distribution data
title: Chart title
Returns:
Plotly figure object
"""
classes = list(class_stats.keys())
percentages = [stats['percentage'] for stats in class_stats.values()]
areas_ha = [stats['area_hectares'] for stats in class_stats.values()]
fig = go.Figure()
fig.add_trace(go.Bar(
x=classes,
y=percentages,
text=[f"{p:.1f}%<br>{a:.2f} ha" for p, a in zip(percentages, areas_ha)],
textposition='outside',
marker_color=self.colors,
hovertemplate='<b>%{x}</b><br>Percentage: %{y:.2f}%<br>Area: %{customdata:.2f} ha<extra></extra>',
customdata=areas_ha
))
fig.update_layout(
title=title,
xaxis_title="NDVI Class",
yaxis_title="Percentage of Total Area (%)",
xaxis_tickangle=-45,
height=500,
showlegend=False
)
return fig
def create_vegetation_pie_chart(self, vegetation_stats: Dict):
"""
Create pie chart for vegetation vs non-vegetation coverage
Args:
vegetation_stats: Dictionary with vegetation coverage data
Returns:
Plotly figure object
"""
labels = ['Vegetation', 'Non-Vegetation']
values = [
vegetation_stats['vegetation_percentage'],
vegetation_stats['non_vegetation_percentage']
]
colors = ['#4CAF50', '#FF9800']
fig = go.Figure(data=[go.Pie(
labels=labels,
values=values,
hole=.3,
marker_colors=colors,
textinfo='label+percent',
hovertemplate='<b>%{label}</b><br>%{percent}<br>Area: %{customdata:.2f} m²<extra></extra>',
customdata=[vegetation_stats['vegetation_area_sq_m'], vegetation_stats['non_vegetation_area_sq_m']]
)])
fig.update_layout(
title="Vegetation Coverage Distribution",
height=400
)
return fig
def create_comparison_chart(self, initial_stats: Dict, updated_stats: Dict):
"""
Create side-by-side comparison of initial and updated NDVI
Args:
initial_stats: Initial NDVI class distribution
updated_stats: Updated NDVI class distribution
Returns:
Plotly figure object
"""
classes = list(initial_stats.keys())
initial_pct = [stats['percentage'] for stats in initial_stats.values()]
updated_pct = [stats['percentage'] for stats in updated_stats.values()]
fig = go.Figure()
fig.add_trace(go.Bar(
name='Initial',
x=classes,
y=initial_pct,
marker_color='lightblue'
))
fig.add_trace(go.Bar(
name='Updated',
x=classes,
y=updated_pct,
marker_color='lightgreen'
))
fig.update_layout(
title="NDVI Class Distribution Comparison",
xaxis_title="NDVI Class",
yaxis_title="Percentage of Total Area (%)",
xaxis_tickangle=-45,
barmode='group',
height=500,
legend=dict(x=0.8, y=1.0)
)
return fig
def create_change_detection_chart(self, change_stats: Dict):
"""
Create chart showing areas of improvement, degradation, and stability
Args:
change_stats: Dictionary with change detection data
Returns:
Plotly figure object
"""
categories = list(change_stats['change_categories'].keys())
percentages = [stats['percentage'] for stats in change_stats['change_categories'].values()]
areas = [stats['area_sq_m'] / 10000 for stats in change_stats['change_categories'].values()] # Convert to hectares
colors_map = {'improvement': '#4CAF50', 'stable': '#FFC107', 'degradation': '#F44336'}
colors = [colors_map[cat] for cat in categories]
fig = go.Figure()
fig.add_trace(go.Bar(
x=[cat.capitalize() for cat in categories],
y=percentages,
text=[f"{p:.1f}%<br>{a:.2f} ha" for p, a in zip(percentages, areas)],
textposition='outside',
marker_color=colors,
hovertemplate='<b>%{x}</b><br>Percentage: %{y:.2f}%<br>Area: %{customdata:.2f} ha<extra></extra>',
customdata=areas
))
fig.update_layout(
title="NDVI Change Detection Analysis",
xaxis_title="Change Category",
yaxis_title="Percentage of Total Area (%)",
height=450,
showlegend=False
)
return fig
def create_statistics_table(self, summary_stats: Dict):
"""
Create formatted table for summary statistics
Args:
summary_stats: Dictionary with summary statistics
Returns:
Plotly table figure
"""
stats_df = pd.DataFrame([
['Minimum', f"{summary_stats['min']:.4f}"],
['Maximum', f"{summary_stats['max']:.4f}"],
['Mean', f"{summary_stats['mean']:.4f}"],
['Median', f"{summary_stats['median']:.4f}"],
['Std Dev', f"{summary_stats['std_dev']:.4f}"],
['25th Percentile', f"{summary_stats['percentile_25']:.4f}"],
['75th Percentile', f"{summary_stats['percentile_75']:.4f}"]
], columns=['Statistic', 'Value'])
fig = go.Figure(data=[go.Table(
header=dict(
values=['<b>Statistic</b>', '<b>Value</b>'],
fill_color='paleturquoise',
align='left',
font=dict(size=12, color='black')
),
cells=dict(
values=[stats_df['Statistic'], stats_df['Value']],
fill_color='lavender',
align='left',
font=dict(size=11)
)
)])
fig.update_layout(
title="NDVI Summary Statistics",
height=350
)
return fig
def create_matplotlib_class_chart(class_stats: Dict, colors: List = None):
"""
Create matplotlib bar chart for class distribution (for PDF export)
Args:
class_stats: Dictionary with class distribution data
colors: Optional list of colors
Returns:
Matplotlib figure object
"""
if colors is None:
colors = NDVI_CLASS_COLORS
classes = list(class_stats.keys())
percentages = [stats['percentage'] for stats in class_stats.values()]
fig, ax = plt.subplots(figsize=(12, 6))
bars = ax.bar(classes, percentages, color=colors, edgecolor='black', linewidth=0.5)
ax.set_xlabel('NDVI Class', fontsize=12, fontweight='bold')
ax.set_ylabel('Percentage of Total Area (%)', fontsize=12, fontweight='bold')
ax.set_title('NDVI Class Distribution', fontsize=14, fontweight='bold')
ax.set_xticklabels(classes, rotation=45, ha='right')
# Add percentage labels on bars
for bar, pct in zip(bars, percentages):
height = bar.get_height()
ax.text(bar.get_x() + bar.get_width()/2., height,
f'{pct:.1f}%',
ha='center', va='bottom', fontsize=9)
plt.tight_layout()
return fig
def fig_to_base64(fig):
"""
Convert matplotlib figure to base64 string for embedding
Args:
fig: Matplotlib figure object
Returns:
Base64 encoded string
"""
buf = io.BytesIO()
fig.savefig(buf, format='png', dpi=150, bbox_inches='tight')
buf.seek(0)
img_base64 = base64.b64encode(buf.read()).decode('utf-8')
buf.close()
plt.close(fig)
return img_base64