antiquesordo commited on
Commit
cd03462
·
verified ·
1 Parent(s): 992d98d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -124
app.py CHANGED
@@ -1,141 +1,109 @@
1
  import folium
2
- from folium.plugins import MeasureControl, FloatImage
3
- from folium.features import DivIcon
4
  import os
5
  from bs4 import BeautifulSoup
6
  import simplekml
7
- import gradio as gr
8
 
9
- # Function to create the map
10
  def create_permaculture_map(coordinates, output_dir="output"):
11
- # Create the map centered on the provided coordinates
12
- map = folium.Map(location=coordinates, zoom_start=18, control_scale=True)
13
-
14
- # Add a north arrow (custom HTML)
15
- north_arrow_html = """
16
- <div style="position: fixed; top: 10px; right: 10px; z-index: 1000; font-size: 24px; font-weight: bold; color: black;">↑ N</div>
 
 
 
 
 
 
17
  """
18
- map.get_root().html.add_child(folium.Element(north_arrow_html))
19
-
20
- # Add coordinates display
21
- coordinates_html = f"""
22
- <div style="position: fixed; bottom: 50px; left: 10px; z-index: 1000; background: white; padding: 5px; border: 1px solid black;">
23
- <strong>Coordinates:</strong> {coordinates}
24
- </div>
25
- """
26
- map.get_root().html.add_child(folium.Element(coordinates_html))
27
-
28
- # Add map items (buildings, vegetation, paths, water management, energy)
29
- folium.Marker(
30
- location=[coordinates[0] + 0.0001, coordinates[1] + 0.0001],
31
- icon=folium.Icon(color="red", icon="home"),
32
- popup="Building",
33
- ).add_to(map)
34
-
35
- folium.Marker(
36
- location=[coordinates[0] - 0.0001, coordinates[1] - 0.0001],
37
- icon=folium.Icon(color="green", icon="tree"),
38
- popup="Vegetation",
39
- ).add_to(map)
40
-
41
  folium.PolyLine(
42
  locations=[
43
  [coordinates[0] - 0.0002, coordinates[1] - 0.0002],
44
- [coordinates[0] + 0.0002, coordinates[1] + 0.0002],
45
  ],
46
  color="brown",
47
  weight=2,
48
- popup="Path",
49
- ).add_to(map)
50
-
51
- folium.Circle(
52
- location=[coordinates[0] + 0.0003, coordinates[1] - 0.0003],
53
- radius=10,
54
- color="blue",
55
- fill=True,
56
- popup="Water Management",
57
- ).add_to(map)
58
-
59
- folium.Marker(
60
- location=[coordinates[0] - 0.0003, coordinates[1] + 0.0003],
61
- icon=folium.Icon(color="orange", icon="bolt"),
62
- popup="Energy",
63
- ).add_to(map)
64
-
65
- # Add a legend
66
- legend_html = """
67
- <div style="position: fixed; bottom: 10px; left: 10px; z-index: 1000; background: white; padding: 10px; border: 1px solid black;">
68
- <h4>Legend</h4>
69
- <p><i class="fa fa-home" style="color:red"></i> Buildings</p>
70
- <p><i class="fa fa-tree" style="color:green"></i> Vegetation</p>
71
- <p><i class="fa fa-road" style="color:brown"></i> Paths</p>
72
- <p><i class="fa fa-tint" style="color:blue"></i> Water Management</p>
73
- <p><i class="fa fa-bolt" style="color:orange"></i> Energy</p>
74
- </div>
75
- """
76
- map.get_root().html.add_child(folium.Element(legend_html))
77
-
78
- # Save the map as an HTML file
79
- if not os.path.exists(output_dir):
80
- os.makedirs(output_dir)
81
- map_path = os.path.join(output_dir, "permaculture_map.html")
82
- map.save(map_path)
83
-
84
- # Export the map as SVG and KML
85
- export_map(map_path, coordinates, output_dir)
86
 
 
 
 
 
87
  return map_path
88
 
89
- # Function to export the map as SVG and KML
90
- def export_map(map_path, coordinates, output_dir):
91
- # Export as SVG using BeautifulSoup
92
- export_svg(map_path, output_dir)
93
-
94
- # Export as KML using SimpleKML
95
- export_kml(coordinates, output_dir)
96
-
97
- # Function to export the map as SVG
98
- def export_svg(map_path, output_dir):
99
- with open(map_path, "r", encoding="utf-8") as file:
100
- soup = BeautifulSoup(file, "html.parser")
101
-
102
- svg_path = os.path.join(output_dir, "permaculture_map.svg")
103
- with open(svg_path, "w", encoding="utf-8") as file:
104
- file.write(soup.prettify())
105
- print(f"Map exported as SVG: {svg_path}")
106
-
107
- # Function to export the map as KML
108
- def export_kml(coordinates, output_dir):
109
- kml = simplekml.Kml()
110
-
111
- # Add map items to KML
112
- kml.newpoint(name="Building", coords=[(coordinates[1] + 0.0001, coordinates[0] + 0.0001)])
113
- kml.newpoint(name="Vegetation", coords=[(coordinates[1] - 0.0001, coordinates[0] - 0.0001)])
114
- kml.newlinestring(name="Path", coords=[(coordinates[1] - 0.0002, coordinates[0] - 0.0002), (coordinates[1] + 0.0002, coordinates[0] + 0.0002)])
115
- kml.newpoint(name="Water Management", coords=[(coordinates[1] - 0.0003, coordinates[0] + 0.0003)])
116
- kml.newpoint(name="Energy", coords=[(coordinates[1] + 0.0003, coordinates[0] - 0.0003)])
117
-
118
- kml_path = os.path.join(output_dir, "permaculture_map.kml")
119
- kml.save(kml_path)
120
- print(f"Map exported as KML: {kml_path}")
121
-
122
  # Gradio interface
123
- def gradio_interface(latitude, longitude):
124
- coordinates = [latitude, longitude]
125
- map_path = create_permaculture_map(coordinates)
126
- return map_path
127
-
128
- # Gradio app
129
- iface = gr.Interface(
130
- fn=gradio_interface,
131
- inputs=[
132
- gr.Number(label="Latitude", value=45.5236),
133
- gr.Number(label="Longitude", value=-122.6750),
134
- ],
135
- outputs=gr.HTML(label="Permaculture Map"),
136
- title="Permaculture Base Map Designer",
137
- description="Design a permaculture base map with buildings, vegetation, paths, water management, and energy. Enter coordinates to generate the map.",
138
- )
139
-
140
- # Run the app
141
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import folium
2
+ import gradio as gr
 
3
  import os
4
  from bs4 import BeautifulSoup
5
  import simplekml
 
6
 
7
+ # Function to create the permaculture map
8
  def create_permaculture_map(coordinates, output_dir="output"):
9
+ # Create base map
10
+ m = folium.Map(
11
+ location=coordinates,
12
+ zoom_start=18,
13
+ control_scale=True,
14
+ tiles="cartodbpositron"
15
+ )
16
+
17
+ # Add north arrow
18
+ north_arrow = """
19
+ <div style="position: fixed; top: 10px; right: 10px; z-index: 1000;
20
+ font-size: 24px; font-weight: bold; color: black;">↑ N</div>
21
  """
22
+ m.get_root().html.add_child(folium.Element(north_arrow))
23
+
24
+ # Add scale
25
+ folium.plugins.MousePosition().add_to(m)
26
+
27
+ # Add map elements
28
+ elements = {
29
+ "Building": {"loc": [0.0001, 0.0001], "color": "red", "icon": "home"},
30
+ "Vegetation": {"loc": [-0.0001, -0.0001], "color": "green", "icon": "tree"},
31
+ "Water Management": {"loc": [0.0003, -0.0003], "color": "blue", "icon": "tint"},
32
+ "Energy": {"loc": [-0.0003, 0.0003], "color": "orange", "icon": "bolt"}
33
+ }
34
+
35
+ for name, props in elements.items():
36
+ folium.Marker(
37
+ location=[coordinates[0] + props["loc"][0],
38
+ coordinates[1] + props["loc"][1]],
39
+ icon=folium.Icon(color=props["color"], icon=props["icon"]),
40
+ popup=name
41
+ ).add_to(m)
42
+
43
+ # Add paths
 
44
  folium.PolyLine(
45
  locations=[
46
  [coordinates[0] - 0.0002, coordinates[1] - 0.0002],
47
+ [coordinates[0] + 0.0002, coordinates[1] + 0.0002]
48
  ],
49
  color="brown",
50
  weight=2,
51
+ popup="Path"
52
+ ).add_to(m)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
+ # Save map
55
+ os.makedirs(output_dir, exist_ok=True)
56
+ map_path = os.path.join(output_dir, "map.html")
57
+ m.save(map_path)
58
  return map_path
59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  # Gradio interface
61
+ def generate_map(lat, lon):
62
+ # Validate coordinates
63
+ if not (-90 <= lat <= 90) or not (-180 <= lon <= 180):
64
+ raise gr.Error("Invalid coordinates! Lat: -90 to 90, Lon: -180 to 180")
65
+
66
+ map_path = create_permaculture_map([lat, lon])
67
+
68
+ with open(map_path, "r") as f:
69
+ html = f.read()
70
+
71
+ return f'<iframe srcdoc="{html}" width="100%" height="500"></iframe>'
72
+
73
+ # Create Gradio app
74
+ with gr.Blocks() as app:
75
+ gr.Markdown("# 🌱 Permaculture Base Map Designer")
76
+
77
+ with gr.Row():
78
+ with gr.Column():
79
+ # Map location picker
80
+ map_picker = gr.Map(label="Click map to select location", height=300)
81
+
82
+ # Coordinate inputs
83
+ with gr.Row():
84
+ lat_input = gr.Number(label="Latitude", value=45.5236)
85
+ lon_input = gr.Number(label="Longitude", value=-122.6750)
86
+
87
+ gr.Markdown("### Map Options")
88
+ gr.Button("Generate Map").click(
89
+ generate_map,
90
+ inputs=[lat_input, lon_input],
91
+ outputs=gr.HTML(label="Generated Map")
92
+ )
93
+
94
+ # Display generated map
95
+ with gr.Column():
96
+ gr.HTML(label="Generated Map")
97
+
98
+ # Connect map clicks to coordinate inputs
99
+ def update_coords(map_data):
100
+ if map_data and map_data.get("features"):
101
+ lon, lat = map_data["features"][0]["geometry"]["coordinates"]
102
+ return lat, lon
103
+ return None, None
104
+
105
+ map_picker.select(update_coords, outputs=[lat_input, lon_input])
106
+
107
+ # Launch app
108
+ if __name__ == "__main__":
109
+ app.launch()