import folium
from folium.plugins import MeasureControl, FloatImage
from folium.features import DivIcon
import os
from bs4 import BeautifulSoup
import simplekml
import gradio as gr
# Function to create the map
def create_permaculture_map(coordinates, output_dir="output"):
# Create the map centered on the provided coordinates
map = folium.Map(location=coordinates, zoom_start=18, control_scale=True)
# Add a north arrow (custom HTML)
north_arrow_html = """
↑ N
"""
map.get_root().html.add_child(folium.Element(north_arrow_html))
# Add coordinates display
coordinates_html = f"""
Coordinates: {coordinates}
"""
map.get_root().html.add_child(folium.Element(coordinates_html))
# Add map items (buildings, vegetation, paths, water management, energy)
folium.Marker(
location=[coordinates[0] + 0.0001, coordinates[1] + 0.0001],
icon=folium.Icon(color="red", icon="home"),
popup="Building",
).add_to(map)
folium.Marker(
location=[coordinates[0] - 0.0001, coordinates[1] - 0.0001],
icon=folium.Icon(color="green", icon="tree"),
popup="Vegetation",
).add_to(map)
folium.PolyLine(
locations=[
[coordinates[0] - 0.0002, coordinates[1] - 0.0002],
[coordinates[0] + 0.0002, coordinates[1] + 0.0002],
],
color="brown",
weight=2,
popup="Path",
).add_to(map)
folium.Circle(
location=[coordinates[0] + 0.0003, coordinates[1] - 0.0003],
radius=10,
color="blue",
fill=True,
popup="Water Management",
).add_to(map)
folium.Marker(
location=[coordinates[0] - 0.0003, coordinates[1] + 0.0003],
icon=folium.Icon(color="orange", icon="bolt"),
popup="Energy",
).add_to(map)
# Add a legend
legend_html = """
Legend
Buildings
Vegetation
Paths
Water Management
Energy
"""
map.get_root().html.add_child(folium.Element(legend_html))
# Save the map as an HTML file
if not os.path.exists(output_dir):
os.makedirs(output_dir)
map_path = os.path.join(output_dir, "permaculture_map.html")
map.save(map_path)
# Export the map as SVG and KML
export_map(map_path, coordinates, output_dir)
return map_path
# Function to export the map as SVG and KML
def export_map(map_path, coordinates, output_dir):
# Export as SVG using BeautifulSoup
export_svg(map_path, output_dir)
# Export as KML using SimpleKML
export_kml(coordinates, output_dir)
# Function to export the map as SVG
def export_svg(map_path, output_dir):
with open(map_path, "r", encoding="utf-8") as file:
soup = BeautifulSoup(file, "html.parser")
svg_path = os.path.join(output_dir, "permaculture_map.svg")
with open(svg_path, "w", encoding="utf-8") as file:
file.write(soup.prettify())
print(f"Map exported as SVG: {svg_path}")
# Function to export the map as KML
def export_kml(coordinates, output_dir):
kml = simplekml.Kml()
# Add map items to KML
kml.newpoint(name="Building", coords=[(coordinates[1] + 0.0001, coordinates[0] + 0.0001)])
kml.newpoint(name="Vegetation", coords=[(coordinates[1] - 0.0001, coordinates[0] - 0.0001)])
kml.newlinestring(name="Path", coords=[(coordinates[1] - 0.0002, coordinates[0] - 0.0002), (coordinates[1] + 0.0002, coordinates[0] + 0.0002)])
kml.newpoint(name="Water Management", coords=[(coordinates[1] - 0.0003, coordinates[0] + 0.0003)])
kml.newpoint(name="Energy", coords=[(coordinates[1] + 0.0003, coordinates[0] - 0.0003)])
kml_path = os.path.join(output_dir, "permaculture_map.kml")
kml.save(kml_path)
print(f"Map exported as KML: {kml_path}")
# Gradio interface
def gradio_interface(latitude, longitude):
coordinates = [latitude, longitude]
map_path = create_permaculture_map(coordinates)
return map_path
# Gradio app
iface = gr.Interface(
fn=gradio_interface,
inputs=[
gr.Number(label="Latitude", value=45.5236),
gr.Number(label="Longitude", value=-122.6750),
],
outputs=gr.HTML(label="Permaculture Map"),
title="Permaculture Base Map Designer",
description="Design a permaculture base map with buildings, vegetation, paths, water management, and energy. Enter coordinates to generate the map.",
)
# Run the app
iface.launch()