File size: 7,265 Bytes
8ba7382 e5db78b 8ba7382 e5db78b 8ba7382 e5db78b 8ba7382 e5db78b 8ba7382 e5db78b 8ba7382 e5db78b 8ba7382 e5db78b 8ba7382 e5db78b 8ba7382 e5db78b 8ba7382 e5db78b 8ba7382 e5db78b 8ba7382 e5db78b 8ba7382 e5db78b 8ba7382 e5db78b 8ba7382 e5db78b 8ba7382 e5db78b 8ba7382 e5db78b 8ba7382 e5db78b 8ba7382 e5db78b 8ba7382 e5db78b 8ba7382 e5db78b 8ba7382 e5db78b 8ba7382 e5db78b 8ba7382 e5db78b 8ba7382 e5db78b 8ba7382 e5db78b 8ba7382 e5db78b |
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 |
from flask import Flask, render_template, request
import folium
from folium.plugins import HeatMapWithTime, FeatureGroupSubGroup, HeatMap
import pandas as pd
app = Flask(__name__)
# Load the dataset
df = pd.read_csv('final_crop_historic_data_pkjk.csv')
df.columns = ['State', 'District', 'Crop_Year', 'Season', 'Crop', 'Area', 'Production', 'Latitude', 'Longitude']
@app.route('/')
def home():
return render_template('index.html', map_html='', selected_map='Home')
@app.route('/production_analysis', methods=['GET', 'POST'])
def production_analysis():
crop_options = df['Crop'].unique().tolist()
selected_crop = request.form.get('crop_type') if request.method == 'POST' else None
if not selected_crop:
return render_template('index.html', map_html='', selected_map='Production Analysis',
crop_options=crop_options, selected_crop=None)
crop_data = df[df['Crop'] == selected_crop]
if crop_data.empty:
return render_template('index.html', map_html='', selected_map='No Data Available',
crop_options=crop_options, selected_crop=selected_crop)
time_index = crop_data['Crop_Year'].unique()
heatmap_data = [[
[row['Latitude'], row['Longitude']]
for _, row in crop_data[crop_data['Crop_Year'] == year].dropna().iterrows()
]
for year in time_index
]
for year, data in zip(time_index, heatmap_data):
print(f'Year: {year}, Data: {data}')
m = folium.Map(location=[20.5937, 78.9629], zoom_start=5)
heatmap = HeatMapWithTime(
heatmap_data,
index=[str(year) for year in time_index],
auto_play=True,
max_opacity=0.6
)
heatmap.add_to(m)
map_html = m._repr_html_()
return render_template('index.html', map_html=map_html, selected_map='Production Heatmap Analysis',
crop_options=crop_options, selected_crop=selected_crop)
@app.route('/heatmap_analysis')
def heatmap_analysis():
global df
m = folium.Map(location=[20.5937, 78.9629], zoom_start=5)
fg = folium.FeatureGroup(name='Crops')
m.add_child(fg)
df_sampled = df.sample(frac=0.005, random_state=42)
for crop in df_sampled['Crop'].unique():
subgroup = FeatureGroupSubGroup(fg, crop)
m.add_child(subgroup)
crop_data = df_sampled[df_sampled['Crop'] == crop]
heatmap_data = [[row['Latitude'], row['Longitude']] for _, row in crop_data.iterrows()]
HeatMap(heatmap_data).add_to(subgroup)
folium.LayerControl(collapsed=False).add_to(m)
map_html = m._repr_html_()
return render_template('index.html', map_html=map_html, selected_map='Crop Heatmap Analysis')
@app.route('/season_analysis')
def season_analysis():
global df
m = folium.Map(location=[20.5937, 78.9629], zoom_start=5)
df_sampled = df.sample(frac=0.005, random_state=42)
top_crops = {}
for _, row in df_sampled.iterrows():
lat_lon = (row['Latitude'], row['Longitude'])
crop = row['Crop']
production = row['Production']
if lat_lon not in top_crops:
top_crops[lat_lon] = {'Season': row['Season'], 'Crops': {}, 'Area': row['Area']}
if crop not in top_crops[lat_lon]['Crops']:
top_crops[lat_lon]['Crops'][crop] = 0
top_crops[lat_lon]['Crops'][crop] += production
for location, data in top_crops.items():
top_crops[location]['Crops'] = sorted(data['Crops'].items(), key=lambda x: x[1], reverse=True)[:5]
season_colors = {
'Kharif': 'orange',
'Rabi': 'green',
'Winter': 'blue',
'Autumn': 'pink',
'Summer': 'yellow',
'Whole Year': 'red'
}
for (latitude, longitude), data in top_crops.items():
season = data['Season']
top_crop_list = data['Crops']
area = data['Area']
top_crops_str = '<br>'.join([f'{crop[0]}: {crop[1]}' for crop in top_crop_list])
folium.CircleMarker(
location=[latitude, longitude],
radius=7,
color=season_colors.get(season, 'gray'),
fill=True,
fill_color=season_colors.get(season, 'gray'),
fill_opacity=0.7,
tooltip=(f'Latitude: {latitude}<br>'
f'Longitude: {longitude}<br>'
f'Season: {season}<br>'
f'Area: {area}<br>'
f'Top 5 Crops:<br>{top_crops_str}')
).add_to(m)
map_html = m._repr_html_()
return render_template('index.html', map_html=map_html, selected_map='Season Analysis')
@app.route('/crop_analysis')
def crop_analysis():
global df
df_sampled = df.sample(frac=0.005, random_state=42)
m = folium.Map(location=[20.5937, 78.9629], zoom_start=5)
for district in df_sampled['District'].unique():
district_data = df_sampled[df_sampled['District'] == district]
top_crops = district_data.groupby('Crop')['Production'].sum().nlargest(5).index.tolist()
lat, lon = district_data.iloc[0]['Latitude'], district_data.iloc[0]['Longitude']
folium.Marker(
location=[lat, lon],
popup=f'<b>District:</b> {district}<br><b>Top 5 Crops:</b> {', '.join(top_crops)}',
icon=folium.Icon(icon='arrow-up', color='green')
).add_to(m)
map_html = m._repr_html_()
return render_template('index.html', map_html=map_html, selected_map='District Crop Analysis')
@app.route('/combined_analysis')
def combined_analysis():
global df
df_sampled = df.sample(frac=0.005, random_state=42)
m = folium.Map(location=[20.5937, 78.9629], zoom_start=5)
area_heat_data = [
[row['Latitude'], row['Longitude'], row['Area']]
for _, row in df_sampled.iterrows()
]
HeatMap(
data=area_heat_data,
min_opacity=0.3,
max_opacity=0.8,
radius=15,
blur=10,
gradient={0.0: 'blue', 0.5: 'lightblue', 1.0: 'red'}
).add_to(m)
production_heat_data = [
[row['Latitude'], row['Longitude'], row['Production']]
for _, row in df_sampled.iterrows()
]
HeatMap(
data=production_heat_data,
min_opacity=0.3,
max_opacity=0.8,
radius=15,
blur=10,
gradient={0.0: 'green', 0.5: 'yellow', 1.0: 'red'}
).add_to(m)
season_colors = {
'Kharif': 'purple',
'Rabi': 'orange',
'Winter': 'Yellow',
'Summer': 'Green',
'Whole Year': 'Red'
}
for _, row in df_sampled.iterrows():
season = row['Season']
color = season_colors.get(season, 'gray')
folium.CircleMarker(
location=[row['Latitude'], row['Longitude'],
radius=5,
color=color,
fill=True,
fill_opacity=0.7,
tooltip=(f'District: {row['District']}<br>'
f'Season: {row['Season']}<br>'
f'Area: {row['Area']}<br>'
f'Production: {row['Production']}')
).add_to(m)
map_html = m._repr_html_()
return render_template('index.html', map_html=map_html, selected_map='Combined Area & Production Heatmaps')
if __name__ == '__main__':
app.run(port=7860, host='0.0.0.0') |