Spaces:
Runtime error
Runtime error
File size: 6,119 Bytes
dfe27ab |
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 |
import folium
from folium.plugins import MeasureControl, FloatImage
from folium.features import DivIcon
import os
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
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 scale bar to the map
map.add_child(folium.plugins.ScaleControl(position="bottomleft"))
# Add a north arrow (custom HTML)
north_arrow_html = """
<div style="position: fixed; top: 10px; right: 10px; z-index: 1000; font-size: 24px; font-weight: bold; color: black;">↑ N</div>
"""
map.get_root().html.add_child(folium.Element(north_arrow_html))
# Add coordinates display
coordinates_html = f"""
<div style="position: fixed; bottom: 50px; left: 10px; z-index: 1000; background: white; padding: 5px; border: 1px solid black;">
<strong>Coordinates:</strong> {coordinates}
</div>
"""
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 = """
<div style="position: fixed; bottom: 10px; left: 10px; z-index: 1000; background: white; padding: 10px; border: 1px solid black;">
<h4>Legend</h4>
<p><i class="fa fa-home" style="color:red"></i> Buildings</p>
<p><i class="fa fa-tree" style="color:green"></i> Vegetation</p>
<p><i class="fa fa-road" style="color:brown"></i> Paths</p>
<p><i class="fa fa-tint" style="color:blue"></i> Water Management</p>
<p><i class="fa fa-bolt" style="color:orange"></i> Energy</p>
</div>
"""
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 PNG, SVG, and KML
export_map(map_path, coordinates, output_dir)
return map_path
# Function to export the map as PNG, SVG, and KML
def export_map(map_path, coordinates, output_dir):
# Export as PNG using Selenium
export_png(map_path, 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 PNG
def export_png(map_path, output_dir):
options = webdriver.ChromeOptions()
options.add_argument("--headless") # Run in headless mode
options.add_argument("--disable-gpu")
options.add_argument("--window-size=1920x1080")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
driver.get(f"file://{os.path.abspath(map_path)}")
driver.implicitly_wait(10) # Wait for the map to load
png_path = os.path.join(output_dir, "permaculture_map.png")
driver.save_screenshot(png_path)
driver.quit()
print(f"Map exported as PNG: {png_path}")
# 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() |