diff --git a/.gitattributes b/.gitattributes index a6344aac8c09253b3b630fb776ae94478aa0275b..ab3825f208d06d6a0eb6966f72e5241212ab9255 100644 --- a/.gitattributes +++ b/.gitattributes @@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text *.zip filter=lfs diff=lfs merge=lfs -text *.zst filter=lfs diff=lfs merge=lfs -text *tfevents* filter=lfs diff=lfs merge=lfs -text +final_crop_historic_data_pkjk.csv filter=lfs diff=lfs merge=lfs -text diff --git a/app.py b/app.py new file mode 100644 index 0000000000000000000000000000000000000000..d7c0384ac937c2968a8fbacac2b3ada77c3b12df --- /dev/null +++ b/app.py @@ -0,0 +1,241 @@ +from flask import Flask, render_template, request +import folium +from folium.plugins import HeatMapWithTime, FeatureGroupSubGroup, HeatMap +import pandas as pd +import os + +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('/prodction_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].iterrows()] + for year in time_index + ] + + 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 # Declare df as global + 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) # Use a different variable for sampled df + 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 # Declare df as global + + # Initialize the map centered over India with an appropriate zoom level + m = folium.Map(location=[20.5937, 78.9629], zoom_start=5) + + # Sample a fraction of the dataframe for performance + df_sampled = df.sample(frac=0.005, random_state=42) + + # Create a dictionary to store top 5 crops for each location + top_crops = {} + + # Collect the top crops for each unique location (Latitude, Longitude) + 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 + + # Limit to top 5 crops for each location + for location, data in top_crops.items(): + top_crops[location]['Crops'] = sorted(data['Crops'].items(), key=lambda x: x[1], reverse=True)[:5] + + # Add scatter points for each unique location with a different color for each season + season_colors = { + 'Kharif': 'orange', + 'Rabi': 'green', + 'Winter': 'blue', + 'Autumn':'pink', + 'Rabi':'brown', + 'Summer':'yellow', + 'Whole Year':'Red' + } + + for (latitude, longitude), data in top_crops.items(): + season = data['Season'] + top_crop_list = data['Crops'] + area = data['Area'] + + # Create a string for the top crops + top_crops_str = "
".join([f"{crop[0]}: {crop[1]}" for crop in top_crop_list]) + + # Add a scatter point to the map for each location + folium.CircleMarker( + location=[latitude, longitude], + radius=7, # Fixed radius for scatter points + color=season_colors.get(season, 'gray'), # Use season color or gray if not found + fill=True, + fill_color=season_colors.get(season, 'gray'), + fill_opacity=0.7, + tooltip=(f"Latitude: {latitude}
" + f"Longitude: {longitude}
" + f"Season: {season}
" + f"Area: {area}
" + f"Top 5 Crops:
{top_crops_str}") + ).add_to(m) + + # Convert the map to HTML format for rendering + map_html = m._repr_html_() + + # Render the map in the template + return render_template('index.html', map_html=map_html, selected_map="Season Analysis") + + +@app.route('/crop_analysis') +def crop_analysis(): + global df # Declare df as global + df_sampled = df.sample(frac=0.005, random_state=42) # Use a different variable for sampled df + 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"District: {district}
Top 5 Crops: {', '.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 # Declare df as global + + # Sample a fraction of the dataframe for performance + df_sampled = df.sample(frac=0.005, random_state=42) + + # Create the map centered over India with an appropriate zoom level + m = folium.Map(location=[20.5937, 78.9629], zoom_start=5) + + # Prepare heatmap data for area + area_heat_data = [ + [row['Latitude'], row['Longitude'], row['Area']] + for _, row in df_sampled.iterrows() + ] + + # Add the heatmap for area (blue to red: low to high) + 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) + + # Prepare heatmap data for production + production_heat_data = [ + [row['Latitude'], row['Longitude'], row['Production']] + for _, row in df_sampled.iterrows() + ] + + # Add the heatmap for production (green to red: low to high production) + 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) + + # Scatter plot for different seasons with distinct colors + season_colors = { + 'Kharif': 'purple', + 'Rabi': 'orange', + 'Rabi': 'cyan', + 'Winter':'Yellow', + 'Summer':'Green', + 'Whole Year':'Red' + } + + for _, row in df_sampled.iterrows(): + season = row['Season'] + color = season_colors.get(season, 'gray') # Default to gray if the season is not recognized + folium.CircleMarker( + location=[row['Latitude'], row['Longitude']], + radius=5, + color=color, + fill=True, + fill_opacity=0.7, + tooltip=(f"District: {row['District']}
" + f"Season: {row['Season']}
" + f"Area: {row['Area']}
" + f"Production: {row['Production']}") + ).add_to(m) + + # Convert the map to HTML format + map_html = m._repr_html_() + + # Render the map in the template + return render_template('index.html', map_html=map_html, selected_map="Combined Area & Production Heatmaps") + +if __name__ == '__main__': + app.run(debug=True) diff --git a/final_crop_historic_data_pkjk.csv b/final_crop_historic_data_pkjk.csv new file mode 100644 index 0000000000000000000000000000000000000000..60f9f60340ef0b403c6d5705f6264031362fbd6c --- /dev/null +++ b/final_crop_historic_data_pkjk.csv @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9cf39965597f1b461e1351928200036a07929f434420cc40e84d097f9226fc3a +size 311198769 diff --git a/templates/crop_analysis.html b/templates/crop_analysis.html new file mode 100644 index 0000000000000000000000000000000000000000..c10edbace19e6efb2c6fb8c1e13485f871b9c36d --- /dev/null +++ b/templates/crop_analysis.html @@ -0,0 +1,11 @@ + + + + + + Crop Analysis + + + + + diff --git a/templates/heatmap.html b/templates/heatmap.html new file mode 100644 index 0000000000000000000000000000000000000000..1e867d3354d592dc9202c8059bc625f4a29abfdf --- /dev/null +++ b/templates/heatmap.html @@ -0,0 +1,205 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + \ No newline at end of file diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000000000000000000000000000000000000..524a53c18dfd48f96a233cdc20a60fb3fa140574 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,88 @@ + + + + + + Crop Production Analysis + + + + + + +
+

Crop Production Analysis

+
+
+ + +
+ +
+ +
+ {{ map_html | safe }} +
+
+ + + + + + diff --git a/templates/map.html b/templates/map.html new file mode 100644 index 0000000000000000000000000000000000000000..2c2723cf4e2ddda7e405ff0ae9873a30b3248987 --- /dev/null +++ b/templates/map.html @@ -0,0 +1,44809 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + \ No newline at end of file diff --git a/templates/prediction_analysis.html b/templates/prediction_analysis.html new file mode 100644 index 0000000000000000000000000000000000000000..2e6efef4c061c5cc1e9864901a993373ed59104f --- /dev/null +++ b/templates/prediction_analysis.html @@ -0,0 +1,11 @@ + + + + + + Production Analysis + + + + + diff --git a/templates/production_heatmap.html b/templates/production_heatmap.html new file mode 100644 index 0000000000000000000000000000000000000000..811ce330e2fcdfcc41bed0f92e5a3049b347fa12 --- /dev/null +++ b/templates/production_heatmap.html @@ -0,0 +1,21 @@ + + + + + + Production Heatmap + + +
+
+
+ {% for crop in crops %} + + {{ crop }}
+ {% endfor %} +
+ +
+ + + diff --git a/templates/seasonal_analysis.html b/templates/seasonal_analysis.html new file mode 100644 index 0000000000000000000000000000000000000000..bd99664fe6af2d522a722d0ed94cba1b9baa6669 --- /dev/null +++ b/templates/seasonal_analysis.html @@ -0,0 +1,11 @@ + + + + + + Season Analysis + + + Season Analysis + +