antiquesordo commited on
Commit
2b13645
ยท
verified ยท
1 Parent(s): dfc7b26

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -81
app.py CHANGED
@@ -1,109 +1,95 @@
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()
 
 
1
  import gradio as gr
2
+ import folium
3
  import os
4
  from bs4 import BeautifulSoup
5
  import simplekml
6
 
7
+ # Function to create permaculture map
8
+ def create_permaculture_map(lat, lon):
9
+ # Validate coordinates
10
+ if not (-90 <= lat <= 90) or not (-180 <= lon <= 180):
11
+ raise gr.Error("Invalid coordinates! Latitude: -90 to 90, Longitude: -180 to 180")
12
+
13
  # Create base map
14
+ m = folium.Map(location=[lat, lon], zoom_start=16, control_scale=True)
15
+
16
+ # Add permaculture elements
17
+ elements = [
18
+ {"name": "Building", "location": [lat+0.0001, lon+0.0001], "color": "red", "icon": "home"},
19
+ {"name": "Vegetation", "location": [lat-0.0001, lon-0.0001], "color": "green", "icon": "tree"},
20
+ {"name": "Water", "location": [lat+0.0002, lon-0.0002], "color": "blue", "icon": "tint"},
21
+ {"name": "Energy", "location": [lat-0.0002, lon+0.0002], "color": "orange", "icon": "bolt"}
22
+ ]
23
+
24
+ for element in elements:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  folium.Marker(
26
+ element["location"],
27
+ popup=element["name"],
28
+ icon=folium.Icon(color=element["color"], icon=element["icon"])
 
29
  ).add_to(m)
30
+
31
  # Add paths
32
  folium.PolyLine(
33
+ locations=[[lat-0.0003, lon-0.0003], [lat+0.0003, lon+0.0003]],
 
 
 
34
  color="brown",
35
+ weight=2
 
36
  ).add_to(m)
37
+
38
  # Save map
39
+ os.makedirs("output", exist_ok=True)
40
+ map_path = os.path.join("output", "map.html")
41
  m.save(map_path)
 
 
 
 
 
 
 
 
 
42
 
43
+ # Return HTML content
44
  with open(map_path, "r") as f:
45
+ return f.read()
 
 
46
 
47
+ # Gradio interface
48
  with gr.Blocks() as app:
49
+ gr.Markdown("# ๐ŸŒฟ Permaculture Base Map Creator")
50
 
51
  with gr.Row():
52
  with gr.Column():
53
+ gr.Markdown("## ๐Ÿ“ Location Selection")
 
 
 
54
  with gr.Row():
55
+ lat = gr.Number(label="Latitude", value=45.5236)
56
+ lon = gr.Number(label="Longitude", value=-122.6750)
57
+ gr.Markdown("### OR")
58
+ gr.Markdown("Search on map below:")
59
+ map_html = gr.HTML(value="<div id='map' style='height: 300px'></div>")
60
+ btn = gr.Button("Generate Map", variant="primary")
 
 
 
61
 
 
62
  with gr.Column():
63
+ gr.Markdown("## ๐Ÿ—บ๏ธ Your Permaculture Design")
64
+ output_map = gr.HTML()
 
 
 
 
 
 
65
 
66
+ # Interactive map script
67
+ app.load(
68
+ None,
69
+ None,
70
+ None,
71
+ _js="""
72
+ () => {
73
+ const map = L.map('map').setView([45.5236, -122.6750], 13);
74
+ L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png').addTo(map);
75
+
76
+ let marker;
77
+ map.on('click', (e) => {
78
+ if(marker) map.removeLayer(marker);
79
+ marker = L.marker(e.latlng).addTo(map);
80
+ document.querySelector('#component-1 input').value = e.latlng.lat;
81
+ document.querySelector('#component-2 input').value = e.latlng.lng;
82
+ });
83
+ }
84
+ """
85
+ )
86
+
87
+ btn.click(
88
+ fn=create_permaculture_map,
89
+ inputs=[lat, lon],
90
+ outputs=output_map,
91
+ _js="(lat, lon) => `<iframe srcdoc='${create_permaculture_map(lat, lon)}' width='100%' height='500'></iframe>`"
92
+ )
93
 
 
94
  if __name__ == "__main__":
95
  app.launch()