Rename geospatial_analyzer.py to geospatial_visualization.py
Browse files- geospatial_analyzer.py +0 -47
- geospatial_visualization.py +54 -0
geospatial_analyzer.py
DELETED
|
@@ -1,47 +0,0 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import plotly.express as px
|
| 4 |
-
|
| 5 |
-
class GeospatialAnalyzer:
|
| 6 |
-
def analyze_geospatial_data(self, df):
|
| 7 |
-
lat_columns = [col for col in df.columns if 'lat' in col.lower()]
|
| 8 |
-
lon_columns = [col for col in df.columns if 'lon' in col.lower()]
|
| 9 |
-
|
| 10 |
-
if len(lat_columns) > 0 and len(lon_columns) > 0:
|
| 11 |
-
lat_column = st.selectbox("Select latitude column", lat_columns)
|
| 12 |
-
lon_column = st.selectbox("Select longitude column", lon_columns)
|
| 13 |
-
|
| 14 |
-
map_type = st.selectbox("Select map type", ["Scatter Mapbox", "Density Mapbox"])
|
| 15 |
-
|
| 16 |
-
if map_type == "Scatter Mapbox":
|
| 17 |
-
self.create_scatter_mapbox(df, lat_column, lon_column)
|
| 18 |
-
elif map_type == "Density Mapbox":
|
| 19 |
-
self.create_density_mapbox(df, lat_column, lon_column)
|
| 20 |
-
else:
|
| 21 |
-
st.write("No latitude and longitude columns found in the dataset.")
|
| 22 |
-
|
| 23 |
-
def create_scatter_mapbox(self, df, lat_column, lon_column):
|
| 24 |
-
st.subheader("Scatter Mapbox")
|
| 25 |
-
|
| 26 |
-
color_column = st.selectbox("Select color column (optional)", ["None"] + df.columns.tolist())
|
| 27 |
-
size_column = st.selectbox("Select size column (optional)", ["None"] + df.columns.tolist())
|
| 28 |
-
|
| 29 |
-
fig = px.scatter_mapbox(df,
|
| 30 |
-
lat=lat_column,
|
| 31 |
-
lon=lon_column,
|
| 32 |
-
color=None if color_column == "None" else color_column,
|
| 33 |
-
size=None if size_column == "None" else size_column,
|
| 34 |
-
zoom=3)
|
| 35 |
-
fig.update_layout(mapbox_style="open-street-map")
|
| 36 |
-
st.plotly_chart(fig)
|
| 37 |
-
|
| 38 |
-
def create_density_mapbox(self, df, lat_column, lon_column):
|
| 39 |
-
st.subheader("Density Mapbox")
|
| 40 |
-
|
| 41 |
-
fig = px.density_mapbox(df,
|
| 42 |
-
lat=lat_column,
|
| 43 |
-
lon=lon_column,
|
| 44 |
-
zoom=3,
|
| 45 |
-
radius=10)
|
| 46 |
-
fig.update_layout(mapbox_style="open-street-map")
|
| 47 |
-
st.plotly_chart(fig)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
geospatial_visualization.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import geopandas as gpd
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
|
| 5 |
+
class GeospatialVisualizer:
|
| 6 |
+
def __init__(self):
|
| 7 |
+
self.world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
|
| 8 |
+
|
| 9 |
+
def visualize(self, data, location_column, value_column):
|
| 10 |
+
# Merge data with world map
|
| 11 |
+
merged = self.world.merge(data, how='left', left_on=['name'], right_on=[location_column])
|
| 12 |
+
|
| 13 |
+
# Create the plot
|
| 14 |
+
fig, ax = plt.subplots(figsize=(15, 10))
|
| 15 |
+
merged.plot(column=value_column, ax=ax, legend=True,
|
| 16 |
+
legend_kwds={'label': value_column, 'orientation': 'horizontal'},
|
| 17 |
+
missing_kwds={'color': 'lightgrey'})
|
| 18 |
+
|
| 19 |
+
# Customize the plot
|
| 20 |
+
ax.set_title(f'{value_column} by Country')
|
| 21 |
+
ax.axis('off')
|
| 22 |
+
|
| 23 |
+
return fig
|
| 24 |
+
|
| 25 |
+
def create_choropleth(self, data, location_column, value_column):
|
| 26 |
+
# Merge data with world map
|
| 27 |
+
merged = self.world.merge(data, how='left', left_on=['name'], right_on=[location_column])
|
| 28 |
+
|
| 29 |
+
# Create the choropleth map
|
| 30 |
+
fig, ax = plt.subplots(figsize=(15, 10))
|
| 31 |
+
merged.plot(column=value_column, ax=ax, legend=True,
|
| 32 |
+
legend_kwds={'label': value_column, 'orientation': 'horizontal'},
|
| 33 |
+
cmap='YlOrRd', missing_kwds={'color': 'lightgrey'})
|
| 34 |
+
|
| 35 |
+
# Customize the plot
|
| 36 |
+
ax.set_title(f'Choropleth Map: {value_column} by Country')
|
| 37 |
+
ax.axis('off')
|
| 38 |
+
|
| 39 |
+
return fig
|
| 40 |
+
|
| 41 |
+
def create_bubble_map(self, data, lat_column, lon_column, size_column):
|
| 42 |
+
# Create a GeoDataFrame from the data
|
| 43 |
+
gdf = gpd.GeoDataFrame(data, geometry=gpd.points_from_xy(data[lon_column], data[lat_column]))
|
| 44 |
+
|
| 45 |
+
# Create the bubble map
|
| 46 |
+
fig, ax = plt.subplots(figsize=(15, 10))
|
| 47 |
+
self.world.plot(ax=ax, color='lightgrey')
|
| 48 |
+
gdf.plot(ax=ax, markersize=data[size_column]/data[size_column].max()*100, alpha=0.5)
|
| 49 |
+
|
| 50 |
+
# Customize the plot
|
| 51 |
+
ax.set_title(f'Bubble Map: {size_column} by Location')
|
| 52 |
+
ax.axis('off')
|
| 53 |
+
|
| 54 |
+
return fig
|