antiquesordo commited on
Commit
dfe27ab
·
verified ·
1 Parent(s): de27c05

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +166 -0
app.py ADDED
@@ -0,0 +1,166 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import folium
2
+ from folium.plugins import MeasureControl, FloatImage
3
+ from folium.features import DivIcon
4
+ import os
5
+ from selenium import webdriver
6
+ from selenium.webdriver.chrome.service import Service
7
+ from webdriver_manager.chrome import ChromeDriverManager
8
+ from bs4 import BeautifulSoup
9
+ import simplekml
10
+ import gradio as gr
11
+
12
+ # Function to create the map
13
+ def create_permaculture_map(coordinates, output_dir="output"):
14
+ # Create the map centered on the provided coordinates
15
+ map = folium.Map(location=coordinates, zoom_start=18, control_scale=True)
16
+
17
+ # Add a scale bar to the map
18
+ map.add_child(folium.plugins.ScaleControl(position="bottomleft"))
19
+
20
+ # Add a north arrow (custom HTML)
21
+ north_arrow_html = """
22
+ <div style="position: fixed; top: 10px; right: 10px; z-index: 1000; font-size: 24px; font-weight: bold; color: black;">↑ N</div>
23
+ """
24
+ map.get_root().html.add_child(folium.Element(north_arrow_html))
25
+
26
+ # Add coordinates display
27
+ coordinates_html = f"""
28
+ <div style="position: fixed; bottom: 50px; left: 10px; z-index: 1000; background: white; padding: 5px; border: 1px solid black;">
29
+ <strong>Coordinates:</strong> {coordinates}
30
+ </div>
31
+ """
32
+ map.get_root().html.add_child(folium.Element(coordinates_html))
33
+
34
+ # Add map items (buildings, vegetation, paths, water management, energy)
35
+ folium.Marker(
36
+ location=[coordinates[0] + 0.0001, coordinates[1] + 0.0001],
37
+ icon=folium.Icon(color="red", icon="home"),
38
+ popup="Building",
39
+ ).add_to(map)
40
+
41
+ folium.Marker(
42
+ location=[coordinates[0] - 0.0001, coordinates[1] - 0.0001],
43
+ icon=folium.Icon(color="green", icon="tree"),
44
+ popup="Vegetation",
45
+ ).add_to(map)
46
+
47
+ folium.PolyLine(
48
+ locations=[
49
+ [coordinates[0] - 0.0002, coordinates[1] - 0.0002],
50
+ [coordinates[0] + 0.0002, coordinates[1] + 0.0002],
51
+ ],
52
+ color="brown",
53
+ weight=2,
54
+ popup="Path",
55
+ ).add_to(map)
56
+
57
+ folium.Circle(
58
+ location=[coordinates[0] + 0.0003, coordinates[1] - 0.0003],
59
+ radius=10,
60
+ color="blue",
61
+ fill=True,
62
+ popup="Water Management",
63
+ ).add_to(map)
64
+
65
+ folium.Marker(
66
+ location=[coordinates[0] - 0.0003, coordinates[1] + 0.0003],
67
+ icon=folium.Icon(color="orange", icon="bolt"),
68
+ popup="Energy",
69
+ ).add_to(map)
70
+
71
+ # Add a legend
72
+ legend_html = """
73
+ <div style="position: fixed; bottom: 10px; left: 10px; z-index: 1000; background: white; padding: 10px; border: 1px solid black;">
74
+ <h4>Legend</h4>
75
+ <p><i class="fa fa-home" style="color:red"></i> Buildings</p>
76
+ <p><i class="fa fa-tree" style="color:green"></i> Vegetation</p>
77
+ <p><i class="fa fa-road" style="color:brown"></i> Paths</p>
78
+ <p><i class="fa fa-tint" style="color:blue"></i> Water Management</p>
79
+ <p><i class="fa fa-bolt" style="color:orange"></i> Energy</p>
80
+ </div>
81
+ """
82
+ map.get_root().html.add_child(folium.Element(legend_html))
83
+
84
+ # Save the map as an HTML file
85
+ if not os.path.exists(output_dir):
86
+ os.makedirs(output_dir)
87
+ map_path = os.path.join(output_dir, "permaculture_map.html")
88
+ map.save(map_path)
89
+
90
+ # Export the map as PNG, SVG, and KML
91
+ export_map(map_path, coordinates, output_dir)
92
+
93
+ return map_path
94
+
95
+ # Function to export the map as PNG, SVG, and KML
96
+ def export_map(map_path, coordinates, output_dir):
97
+ # Export as PNG using Selenium
98
+ export_png(map_path, output_dir)
99
+
100
+ # Export as SVG using BeautifulSoup
101
+ export_svg(map_path, output_dir)
102
+
103
+ # Export as KML using SimpleKML
104
+ export_kml(coordinates, output_dir)
105
+
106
+ # Function to export the map as PNG
107
+ def export_png(map_path, output_dir):
108
+ options = webdriver.ChromeOptions()
109
+ options.add_argument("--headless") # Run in headless mode
110
+ options.add_argument("--disable-gpu")
111
+ options.add_argument("--window-size=1920x1080")
112
+
113
+ driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
114
+ driver.get(f"file://{os.path.abspath(map_path)}")
115
+ driver.implicitly_wait(10) # Wait for the map to load
116
+
117
+ png_path = os.path.join(output_dir, "permaculture_map.png")
118
+ driver.save_screenshot(png_path)
119
+ driver.quit()
120
+ print(f"Map exported as PNG: {png_path}")
121
+
122
+ # Function to export the map as SVG
123
+ def export_svg(map_path, output_dir):
124
+ with open(map_path, "r", encoding="utf-8") as file:
125
+ soup = BeautifulSoup(file, "html.parser")
126
+
127
+ svg_path = os.path.join(output_dir, "permaculture_map.svg")
128
+ with open(svg_path, "w", encoding="utf-8") as file:
129
+ file.write(soup.prettify())
130
+ print(f"Map exported as SVG: {svg_path}")
131
+
132
+ # Function to export the map as KML
133
+ def export_kml(coordinates, output_dir):
134
+ kml = simplekml.Kml()
135
+
136
+ # Add map items to KML
137
+ kml.newpoint(name="Building", coords=[(coordinates[1] + 0.0001, coordinates[0] + 0.0001)])
138
+ kml.newpoint(name="Vegetation", coords=[(coordinates[1] - 0.0001, coordinates[0] - 0.0001)])
139
+ kml.newlinestring(name="Path", coords=[(coordinates[1] - 0.0002, coordinates[0] - 0.0002), (coordinates[1] + 0.0002, coordinates[0] + 0.0002)])
140
+ kml.newpoint(name="Water Management", coords=[(coordinates[1] - 0.0003, coordinates[0] + 0.0003)])
141
+ kml.newpoint(name="Energy", coords=[(coordinates[1] + 0.0003, coordinates[0] - 0.0003)])
142
+
143
+ kml_path = os.path.join(output_dir, "permaculture_map.kml")
144
+ kml.save(kml_path)
145
+ print(f"Map exported as KML: {kml_path}")
146
+
147
+ # Gradio interface
148
+ def gradio_interface(latitude, longitude):
149
+ coordinates = [latitude, longitude]
150
+ map_path = create_permaculture_map(coordinates)
151
+ return map_path
152
+
153
+ # Gradio app
154
+ iface = gr.Interface(
155
+ fn=gradio_interface,
156
+ inputs=[
157
+ gr.Number(label="Latitude", value=45.5236),
158
+ gr.Number(label="Longitude", value=-122.6750),
159
+ ],
160
+ outputs=gr.HTML(label="Permaculture Map"),
161
+ title="Permaculture Base Map Designer",
162
+ description="Design a permaculture base map with buildings, vegetation, paths, water management, and energy. Enter coordinates to generate the map.",
163
+ )
164
+
165
+ # Run the app
166
+ iface.launch()