Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,6 @@ from flask import Flask, render_template, request
|
|
| 2 |
import folium
|
| 3 |
from folium.plugins import HeatMapWithTime, FeatureGroupSubGroup, HeatMap
|
| 4 |
import pandas as pd
|
| 5 |
-
import os
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
|
|
@@ -10,33 +9,36 @@ app = Flask(__name__)
|
|
| 10 |
df = pd.read_csv('final_crop_historic_data_pkjk.csv')
|
| 11 |
df.columns = ['State', 'District', 'Crop_Year', 'Season', 'Crop', 'Area', 'Production', 'Latitude', 'Longitude']
|
| 12 |
|
| 13 |
-
|
| 14 |
@app.route('/')
|
| 15 |
def home():
|
| 16 |
-
return render_template('index.html', map_html=
|
| 17 |
-
|
| 18 |
|
| 19 |
-
@app.route('/
|
| 20 |
def production_analysis():
|
| 21 |
crop_options = df['Crop'].unique().tolist()
|
| 22 |
selected_crop = request.form.get('crop_type') if request.method == 'POST' else None
|
| 23 |
|
| 24 |
if not selected_crop:
|
| 25 |
-
return render_template('index.html', map_html=
|
| 26 |
crop_options=crop_options, selected_crop=None)
|
| 27 |
|
| 28 |
crop_data = df[df['Crop'] == selected_crop]
|
| 29 |
|
| 30 |
if crop_data.empty:
|
| 31 |
-
return render_template('index.html', map_html=
|
| 32 |
crop_options=crop_options, selected_crop=selected_crop)
|
| 33 |
|
| 34 |
time_index = crop_data['Crop_Year'].unique()
|
| 35 |
-
heatmap_data = [
|
| 36 |
-
[
|
|
|
|
|
|
|
| 37 |
for year in time_index
|
| 38 |
]
|
| 39 |
|
|
|
|
|
|
|
|
|
|
| 40 |
m = folium.Map(location=[20.5937, 78.9629], zoom_start=5)
|
| 41 |
heatmap = HeatMapWithTime(
|
| 42 |
heatmap_data,
|
|
@@ -47,17 +49,16 @@ def production_analysis():
|
|
| 47 |
heatmap.add_to(m)
|
| 48 |
|
| 49 |
map_html = m._repr_html_()
|
| 50 |
-
return render_template('index.html', map_html=map_html, selected_map=
|
| 51 |
crop_options=crop_options, selected_crop=selected_crop)
|
| 52 |
|
| 53 |
-
|
| 54 |
@app.route('/heatmap_analysis')
|
| 55 |
def heatmap_analysis():
|
| 56 |
-
global df
|
| 57 |
m = folium.Map(location=[20.5937, 78.9629], zoom_start=5)
|
| 58 |
-
fg = folium.FeatureGroup(name=
|
| 59 |
m.add_child(fg)
|
| 60 |
-
df_sampled = df.sample(frac=0.005, random_state=42)
|
| 61 |
for crop in df_sampled['Crop'].unique():
|
| 62 |
subgroup = FeatureGroupSubGroup(fg, crop)
|
| 63 |
m.add_child(subgroup)
|
|
@@ -69,23 +70,14 @@ def heatmap_analysis():
|
|
| 69 |
folium.LayerControl(collapsed=False).add_to(m)
|
| 70 |
|
| 71 |
map_html = m._repr_html_()
|
| 72 |
-
return render_template('index.html', map_html=map_html, selected_map=
|
| 73 |
-
|
| 74 |
|
| 75 |
@app.route('/season_analysis')
|
| 76 |
def season_analysis():
|
| 77 |
-
global df
|
| 78 |
-
|
| 79 |
-
# Initialize the map centered over India with an appropriate zoom level
|
| 80 |
m = folium.Map(location=[20.5937, 78.9629], zoom_start=5)
|
| 81 |
-
|
| 82 |
-
# Sample a fraction of the dataframe for performance
|
| 83 |
df_sampled = df.sample(frac=0.005, random_state=42)
|
| 84 |
-
|
| 85 |
-
# Create a dictionary to store top 5 crops for each location
|
| 86 |
top_crops = {}
|
| 87 |
-
|
| 88 |
-
# Collect the top crops for each unique location (Latitude, Longitude)
|
| 89 |
for _, row in df_sampled.iterrows():
|
| 90 |
lat_lon = (row['Latitude'], row['Longitude'])
|
| 91 |
crop = row['Crop']
|
|
@@ -98,19 +90,16 @@ def season_analysis():
|
|
| 98 |
top_crops[lat_lon]['Crops'][crop] = 0
|
| 99 |
top_crops[lat_lon]['Crops'][crop] += production
|
| 100 |
|
| 101 |
-
# Limit to top 5 crops for each location
|
| 102 |
for location, data in top_crops.items():
|
| 103 |
top_crops[location]['Crops'] = sorted(data['Crops'].items(), key=lambda x: x[1], reverse=True)[:5]
|
| 104 |
|
| 105 |
-
# Add scatter points for each unique location with a different color for each season
|
| 106 |
season_colors = {
|
| 107 |
'Kharif': 'orange',
|
| 108 |
'Rabi': 'green',
|
| 109 |
'Winter': 'blue',
|
| 110 |
-
'Autumn':'pink',
|
| 111 |
-
'
|
| 112 |
-
'
|
| 113 |
-
'Whole Year':'Red'
|
| 114 |
}
|
| 115 |
|
| 116 |
for (latitude, longitude), data in top_crops.items():
|
|
@@ -118,35 +107,29 @@ def season_analysis():
|
|
| 118 |
top_crop_list = data['Crops']
|
| 119 |
area = data['Area']
|
| 120 |
|
| 121 |
-
|
| 122 |
-
top_crops_str = "<br>".join([f"{crop[0]}: {crop[1]}" for crop in top_crop_list])
|
| 123 |
|
| 124 |
-
# Add a scatter point to the map for each location
|
| 125 |
folium.CircleMarker(
|
| 126 |
location=[latitude, longitude],
|
| 127 |
-
radius=7,
|
| 128 |
-
color=season_colors.get(season, 'gray'),
|
| 129 |
fill=True,
|
| 130 |
fill_color=season_colors.get(season, 'gray'),
|
| 131 |
fill_opacity=0.7,
|
| 132 |
-
tooltip=(f
|
| 133 |
-
f
|
| 134 |
-
f
|
| 135 |
-
f
|
| 136 |
-
f
|
| 137 |
).add_to(m)
|
| 138 |
|
| 139 |
-
# Convert the map to HTML format for rendering
|
| 140 |
map_html = m._repr_html_()
|
| 141 |
-
|
| 142 |
-
# Render the map in the template
|
| 143 |
-
return render_template('index.html', map_html=map_html, selected_map="Season Analysis")
|
| 144 |
-
|
| 145 |
|
| 146 |
@app.route('/crop_analysis')
|
| 147 |
def crop_analysis():
|
| 148 |
-
global df
|
| 149 |
-
df_sampled = df.sample(frac=0.005, random_state=42)
|
| 150 |
m = folium.Map(location=[20.5937, 78.9629], zoom_start=5)
|
| 151 |
|
| 152 |
for district in df_sampled['District'].unique():
|
|
@@ -156,31 +139,24 @@ def crop_analysis():
|
|
| 156 |
|
| 157 |
folium.Marker(
|
| 158 |
location=[lat, lon],
|
| 159 |
-
popup=f
|
| 160 |
icon=folium.Icon(icon='arrow-up', color='green')
|
| 161 |
).add_to(m)
|
| 162 |
|
| 163 |
map_html = m._repr_html_()
|
| 164 |
-
return render_template('index.html', map_html=map_html, selected_map=
|
| 165 |
-
|
| 166 |
|
| 167 |
@app.route('/combined_analysis')
|
| 168 |
def combined_analysis():
|
| 169 |
-
global df
|
| 170 |
-
|
| 171 |
-
# Sample a fraction of the dataframe for performance
|
| 172 |
df_sampled = df.sample(frac=0.005, random_state=42)
|
| 173 |
-
|
| 174 |
-
# Create the map centered over India with an appropriate zoom level
|
| 175 |
m = folium.Map(location=[20.5937, 78.9629], zoom_start=5)
|
| 176 |
|
| 177 |
-
# Prepare heatmap data for area
|
| 178 |
area_heat_data = [
|
| 179 |
[row['Latitude'], row['Longitude'], row['Area']]
|
| 180 |
for _, row in df_sampled.iterrows()
|
| 181 |
]
|
| 182 |
|
| 183 |
-
# Add the heatmap for area (blue to red: low to high)
|
| 184 |
HeatMap(
|
| 185 |
data=area_heat_data,
|
| 186 |
min_opacity=0.3,
|
|
@@ -190,13 +166,11 @@ def combined_analysis():
|
|
| 190 |
gradient={0.0: 'blue', 0.5: 'lightblue', 1.0: 'red'}
|
| 191 |
).add_to(m)
|
| 192 |
|
| 193 |
-
# Prepare heatmap data for production
|
| 194 |
production_heat_data = [
|
| 195 |
[row['Latitude'], row['Longitude'], row['Production']]
|
| 196 |
for _, row in df_sampled.iterrows()
|
| 197 |
]
|
| 198 |
|
| 199 |
-
# Add the heatmap for production (green to red: low to high production)
|
| 200 |
HeatMap(
|
| 201 |
data=production_heat_data,
|
| 202 |
min_opacity=0.3,
|
|
@@ -206,36 +180,31 @@ def combined_analysis():
|
|
| 206 |
gradient={0.0: 'green', 0.5: 'yellow', 1.0: 'red'}
|
| 207 |
).add_to(m)
|
| 208 |
|
| 209 |
-
# Scatter plot for different seasons with distinct colors
|
| 210 |
season_colors = {
|
| 211 |
'Kharif': 'purple',
|
| 212 |
'Rabi': 'orange',
|
| 213 |
-
'
|
| 214 |
-
'
|
| 215 |
-
'
|
| 216 |
-
'Whole Year':'Red'
|
| 217 |
}
|
| 218 |
|
| 219 |
for _, row in df_sampled.iterrows():
|
| 220 |
season = row['Season']
|
| 221 |
-
color = season_colors.get(season, 'gray')
|
| 222 |
folium.CircleMarker(
|
| 223 |
-
location=[row['Latitude'], row['Longitude']
|
| 224 |
radius=5,
|
| 225 |
color=color,
|
| 226 |
fill=True,
|
| 227 |
fill_opacity=0.7,
|
| 228 |
-
tooltip=(f
|
| 229 |
-
f
|
| 230 |
-
f
|
| 231 |
-
f
|
| 232 |
).add_to(m)
|
| 233 |
|
| 234 |
-
# Convert the map to HTML format
|
| 235 |
map_html = m._repr_html_()
|
| 236 |
-
|
| 237 |
-
# Render the map in the template
|
| 238 |
-
return render_template('index.html', map_html=map_html, selected_map="Combined Area & Production Heatmaps")
|
| 239 |
|
| 240 |
if __name__ == '__main__':
|
| 241 |
-
app.run(port=7860,host='0.0.0.0')
|
|
|
|
| 2 |
import folium
|
| 3 |
from folium.plugins import HeatMapWithTime, FeatureGroupSubGroup, HeatMap
|
| 4 |
import pandas as pd
|
|
|
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
|
|
|
| 9 |
df = pd.read_csv('final_crop_historic_data_pkjk.csv')
|
| 10 |
df.columns = ['State', 'District', 'Crop_Year', 'Season', 'Crop', 'Area', 'Production', 'Latitude', 'Longitude']
|
| 11 |
|
|
|
|
| 12 |
@app.route('/')
|
| 13 |
def home():
|
| 14 |
+
return render_template('index.html', map_html='', selected_map='Home')
|
|
|
|
| 15 |
|
| 16 |
+
@app.route('/production_analysis', methods=['GET', 'POST'])
|
| 17 |
def production_analysis():
|
| 18 |
crop_options = df['Crop'].unique().tolist()
|
| 19 |
selected_crop = request.form.get('crop_type') if request.method == 'POST' else None
|
| 20 |
|
| 21 |
if not selected_crop:
|
| 22 |
+
return render_template('index.html', map_html='', selected_map='Production Analysis',
|
| 23 |
crop_options=crop_options, selected_crop=None)
|
| 24 |
|
| 25 |
crop_data = df[df['Crop'] == selected_crop]
|
| 26 |
|
| 27 |
if crop_data.empty:
|
| 28 |
+
return render_template('index.html', map_html='', selected_map='No Data Available',
|
| 29 |
crop_options=crop_options, selected_crop=selected_crop)
|
| 30 |
|
| 31 |
time_index = crop_data['Crop_Year'].unique()
|
| 32 |
+
heatmap_data = [[
|
| 33 |
+
[row['Latitude'], row['Longitude']]
|
| 34 |
+
for _, row in crop_data[crop_data['Crop_Year'] == year].dropna().iterrows()
|
| 35 |
+
]
|
| 36 |
for year in time_index
|
| 37 |
]
|
| 38 |
|
| 39 |
+
for year, data in zip(time_index, heatmap_data):
|
| 40 |
+
print(f'Year: {year}, Data: {data}')
|
| 41 |
+
|
| 42 |
m = folium.Map(location=[20.5937, 78.9629], zoom_start=5)
|
| 43 |
heatmap = HeatMapWithTime(
|
| 44 |
heatmap_data,
|
|
|
|
| 49 |
heatmap.add_to(m)
|
| 50 |
|
| 51 |
map_html = m._repr_html_()
|
| 52 |
+
return render_template('index.html', map_html=map_html, selected_map='Production Heatmap Analysis',
|
| 53 |
crop_options=crop_options, selected_crop=selected_crop)
|
| 54 |
|
|
|
|
| 55 |
@app.route('/heatmap_analysis')
|
| 56 |
def heatmap_analysis():
|
| 57 |
+
global df
|
| 58 |
m = folium.Map(location=[20.5937, 78.9629], zoom_start=5)
|
| 59 |
+
fg = folium.FeatureGroup(name='Crops')
|
| 60 |
m.add_child(fg)
|
| 61 |
+
df_sampled = df.sample(frac=0.005, random_state=42)
|
| 62 |
for crop in df_sampled['Crop'].unique():
|
| 63 |
subgroup = FeatureGroupSubGroup(fg, crop)
|
| 64 |
m.add_child(subgroup)
|
|
|
|
| 70 |
folium.LayerControl(collapsed=False).add_to(m)
|
| 71 |
|
| 72 |
map_html = m._repr_html_()
|
| 73 |
+
return render_template('index.html', map_html=map_html, selected_map='Crop Heatmap Analysis')
|
|
|
|
| 74 |
|
| 75 |
@app.route('/season_analysis')
|
| 76 |
def season_analysis():
|
| 77 |
+
global df
|
|
|
|
|
|
|
| 78 |
m = folium.Map(location=[20.5937, 78.9629], zoom_start=5)
|
|
|
|
|
|
|
| 79 |
df_sampled = df.sample(frac=0.005, random_state=42)
|
|
|
|
|
|
|
| 80 |
top_crops = {}
|
|
|
|
|
|
|
| 81 |
for _, row in df_sampled.iterrows():
|
| 82 |
lat_lon = (row['Latitude'], row['Longitude'])
|
| 83 |
crop = row['Crop']
|
|
|
|
| 90 |
top_crops[lat_lon]['Crops'][crop] = 0
|
| 91 |
top_crops[lat_lon]['Crops'][crop] += production
|
| 92 |
|
|
|
|
| 93 |
for location, data in top_crops.items():
|
| 94 |
top_crops[location]['Crops'] = sorted(data['Crops'].items(), key=lambda x: x[1], reverse=True)[:5]
|
| 95 |
|
|
|
|
| 96 |
season_colors = {
|
| 97 |
'Kharif': 'orange',
|
| 98 |
'Rabi': 'green',
|
| 99 |
'Winter': 'blue',
|
| 100 |
+
'Autumn': 'pink',
|
| 101 |
+
'Summer': 'yellow',
|
| 102 |
+
'Whole Year': 'red'
|
|
|
|
| 103 |
}
|
| 104 |
|
| 105 |
for (latitude, longitude), data in top_crops.items():
|
|
|
|
| 107 |
top_crop_list = data['Crops']
|
| 108 |
area = data['Area']
|
| 109 |
|
| 110 |
+
top_crops_str = '<br>'.join([f'{crop[0]}: {crop[1]}' for crop in top_crop_list])
|
|
|
|
| 111 |
|
|
|
|
| 112 |
folium.CircleMarker(
|
| 113 |
location=[latitude, longitude],
|
| 114 |
+
radius=7,
|
| 115 |
+
color=season_colors.get(season, 'gray'),
|
| 116 |
fill=True,
|
| 117 |
fill_color=season_colors.get(season, 'gray'),
|
| 118 |
fill_opacity=0.7,
|
| 119 |
+
tooltip=(f'Latitude: {latitude}<br>'
|
| 120 |
+
f'Longitude: {longitude}<br>'
|
| 121 |
+
f'Season: {season}<br>'
|
| 122 |
+
f'Area: {area}<br>'
|
| 123 |
+
f'Top 5 Crops:<br>{top_crops_str}')
|
| 124 |
).add_to(m)
|
| 125 |
|
|
|
|
| 126 |
map_html = m._repr_html_()
|
| 127 |
+
return render_template('index.html', map_html=map_html, selected_map='Season Analysis')
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
@app.route('/crop_analysis')
|
| 130 |
def crop_analysis():
|
| 131 |
+
global df
|
| 132 |
+
df_sampled = df.sample(frac=0.005, random_state=42)
|
| 133 |
m = folium.Map(location=[20.5937, 78.9629], zoom_start=5)
|
| 134 |
|
| 135 |
for district in df_sampled['District'].unique():
|
|
|
|
| 139 |
|
| 140 |
folium.Marker(
|
| 141 |
location=[lat, lon],
|
| 142 |
+
popup=f'<b>District:</b> {district}<br><b>Top 5 Crops:</b> {', '.join(top_crops)}',
|
| 143 |
icon=folium.Icon(icon='arrow-up', color='green')
|
| 144 |
).add_to(m)
|
| 145 |
|
| 146 |
map_html = m._repr_html_()
|
| 147 |
+
return render_template('index.html', map_html=map_html, selected_map='District Crop Analysis')
|
|
|
|
| 148 |
|
| 149 |
@app.route('/combined_analysis')
|
| 150 |
def combined_analysis():
|
| 151 |
+
global df
|
|
|
|
|
|
|
| 152 |
df_sampled = df.sample(frac=0.005, random_state=42)
|
|
|
|
|
|
|
| 153 |
m = folium.Map(location=[20.5937, 78.9629], zoom_start=5)
|
| 154 |
|
|
|
|
| 155 |
area_heat_data = [
|
| 156 |
[row['Latitude'], row['Longitude'], row['Area']]
|
| 157 |
for _, row in df_sampled.iterrows()
|
| 158 |
]
|
| 159 |
|
|
|
|
| 160 |
HeatMap(
|
| 161 |
data=area_heat_data,
|
| 162 |
min_opacity=0.3,
|
|
|
|
| 166 |
gradient={0.0: 'blue', 0.5: 'lightblue', 1.0: 'red'}
|
| 167 |
).add_to(m)
|
| 168 |
|
|
|
|
| 169 |
production_heat_data = [
|
| 170 |
[row['Latitude'], row['Longitude'], row['Production']]
|
| 171 |
for _, row in df_sampled.iterrows()
|
| 172 |
]
|
| 173 |
|
|
|
|
| 174 |
HeatMap(
|
| 175 |
data=production_heat_data,
|
| 176 |
min_opacity=0.3,
|
|
|
|
| 180 |
gradient={0.0: 'green', 0.5: 'yellow', 1.0: 'red'}
|
| 181 |
).add_to(m)
|
| 182 |
|
|
|
|
| 183 |
season_colors = {
|
| 184 |
'Kharif': 'purple',
|
| 185 |
'Rabi': 'orange',
|
| 186 |
+
'Winter': 'Yellow',
|
| 187 |
+
'Summer': 'Green',
|
| 188 |
+
'Whole Year': 'Red'
|
|
|
|
| 189 |
}
|
| 190 |
|
| 191 |
for _, row in df_sampled.iterrows():
|
| 192 |
season = row['Season']
|
| 193 |
+
color = season_colors.get(season, 'gray')
|
| 194 |
folium.CircleMarker(
|
| 195 |
+
location=[row['Latitude'], row['Longitude'],
|
| 196 |
radius=5,
|
| 197 |
color=color,
|
| 198 |
fill=True,
|
| 199 |
fill_opacity=0.7,
|
| 200 |
+
tooltip=(f'District: {row['District']}<br>'
|
| 201 |
+
f'Season: {row['Season']}<br>'
|
| 202 |
+
f'Area: {row['Area']}<br>'
|
| 203 |
+
f'Production: {row['Production']}')
|
| 204 |
).add_to(m)
|
| 205 |
|
|
|
|
| 206 |
map_html = m._repr_html_()
|
| 207 |
+
return render_template('index.html', map_html=map_html, selected_map='Combined Area & Production Heatmaps')
|
|
|
|
|
|
|
| 208 |
|
| 209 |
if __name__ == '__main__':
|
| 210 |
+
app.run(port=7860, host='0.0.0.0')
|