data-village / app.py
eaglelandsonce's picture
Update app.py
a6436b7 verified
raw
history blame
3.65 kB
import streamlit as st
import json
import matplotlib.pyplot as plt
# Function to load and parse the JSON data
def load_data(filename):
with open(filename, 'r') as file:
data = json.load(file)
return data
# Color codes updated with hexadecimal values for compatibility
color_codes = {
"residential": "#ADD8E6", # Light Blue
"commercial": "#008000", # Green
"community_facilities": "#FFFF00", # Yellow
"school": "#FFFF00", # Yellow
"healthcare_facility": "#FFFF00", # Yellow
"green_space": "#006400", # Dark Green
"utility_infrastructure": "#006400", # Dark Green
"emergency_services": "#FF0000", # Red
"cultural_facilities": "#800080", # Purple
"recreational_facilities": "#800080", # Purple
"innovation_center": "#008000", # Green
"elderly_care_home": "#FFFF00", # Yellow
"childcare_centers": "#FFFF00", # Yellow
"places_of_worship": "#800080", # Purple
"event_spaces": "#800080", # Purple
"guest_housing": "#FFA500", # Orange
"pet_care_facilities": "#FFA500", # Orange
"public_sanitation_facilities": "#808080", # Grey
"environmental_monitoring_stations": "#006400", # Dark Green
"disaster_preparedness_center": "#808080", # Grey
"outdoor_community_spaces": "#006400" # Dark Green
}
# Function to draw the grid layout with color coding
def draw_grid(data):
# Create a figure and a grid of subplots
fig, ax = plt.subplots(figsize=(12, 12))
# Setting the grid size
nrows, ncols = data['size']['rows'], data['size']['columns']
ax.set_xlim(0, ncols)
ax.set_ylim(0, nrows)
ax.set_xticks(range(ncols+1))
ax.set_yticks(range(nrows+1))
ax.grid(True)
# Plotting each building with its assigned color from the color_codes dictionary
for building in data['buildings']:
# Extracting the building details
coords = building['coords']
b_type = building['type']
size = building['size']
color = color_codes.get(b_type, '#FFFFFF') # Default color is white if not specified
# Plotting the building on the grid with color
ax.add_patch(plt.Rectangle((coords[1], nrows-coords[0]-size), size, size, color=color, edgecolor='black', linewidth=1))
ax.text(coords[1]+0.5*size, nrows-coords[0]-0.5*size, b_type, ha='center', va='center', fontsize=8, color='black')
# Draw roads
for road in data.get('roads', []): # Check for roads in the data, default to empty list if not found
start, end = road['start'], road['end']
# Determine if the road is vertical or horizontal based on start and end coordinates
if start[0] == end[0]: # Vertical road
for y in range(min(start[1], end[1]), max(start[1], end[1]) + 1):
ax.add_patch(plt.Rectangle((start[0], nrows-y-1), 1, 1, color=road['color']))
else: # Horizontal road
for x in range(min(start[0], end[0]), max(start[0], end[0]) + 1):
ax.add_patch(plt.Rectangle((x, nrows-start[1]-1), 1, 1, color=road['color']))
# Setting labels and title
ax.set_xlabel('Columns')
ax.set_ylabel('Rows')
ax.set_title('Village Layout with Color Coding')
return fig
# Streamlit application starts here
def main():
st.title('Green Smart Village Layout with Color Coding')
# Load and display the data with color coding
data = load_data('grid.json') # Make sure this path is correct
# st.json(data)
# Drawing and displaying the grid layout with color coding
fig = draw_grid(data)
st.pyplot(fig)
if __name__ == "__main__":
main()