-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathosm_explorer.py
More file actions
237 lines (200 loc) · 8.66 KB
/
osm_explorer.py
File metadata and controls
237 lines (200 loc) · 8.66 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
### SOURCES ###
'''
OSMNX:
https://github.com/gboeing/osmnx
https://osmnx.readthedocs.io/en/stable/user-reference.html
https://pygis.io/docs/d_access_osm.html
OSM:
https://wiki.openstreetmap.org/wiki/OSMPythonTools
https://racum.blog/articles/osm-python/0
https://wiki.openstreetmap.org/wiki/Map_features
https://wiki.openstreetmap.org/wiki/Top-level_tag
TKinter:
https://pythonguides.com/python-tkinter-search-box/
https://tkdocs.com/tutorial/index.html
TKinterMapView:
https://github.com/HezekiahMD/TkinterMapView2
'''
### DEPENDENCIES ###
'''
!pip install osmnx
!pip install tkintermapview
'''
import tkinter as tk
from tkinter import messagebox
import osmnx as ox
import tkintermapview as tkmv
import geopandas as gpd
# Selection of Top-Level Tags: https://wiki.openstreetmap.org/wiki/Top-level_tag
options = {
"Airport": "aeroway",
"Amenity": "amenity",
"Barrier": "barrier",
"Building": "building",
"Club": "club",
"Craft": "craft",
"Education": "education",
"Emergency": "emergency",
"Geological": "geological",
"Healthcare": "healthcare",
"Roads": "highway",
"Military": "military",
"Natural": "natural",
"Railway": "railway",
"Shop": "shop",
"Tourism": "tourism",
"Waterway": "waterway"
}
default_x, default_y = 38.6270, -90.1994
default_zoom = 10
class MainApplication(tk.Frame):
def __init__(self, parent, *args, **kwargs):
super().__init__(parent, *args, **kwargs)
self.parent = parent
self.pack(fill=tk.BOTH, expand=True)
self.search_var = tk.StringVar()
self.option_var = tk.StringVar(value="boundary")
self.user_tag_var = tk.StringVar()
self.poly_toggle_var = tk.BooleanVar(value=True)
self.point_toggle_var = tk.BooleanVar(value=True)
self.line_toggle_var = tk.BooleanVar(value=True)
self.geocode = None
self.setup_layout()
def setup_layout(self):
'''Create and arrange all widgets in window'''
# top search bar
self.top_frame = tk.Frame(self, height=50, bg="#EAEAEA", borderwidth=1, relief=tk.SOLID)
self.top_frame.pack(side=tk.TOP, fill=tk.X)
# side option bar
self.sidebar_frame = tk.Frame(self, width=200, bg="#F5F5F5", borderwidth=1, relief=tk.SOLID)
self.sidebar_frame.pack(side=tk.LEFT, fill=tk.Y)
# map frame
self.map_frame = tk.Frame(self, bg="lightgray")
self.map_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
# search/option buttons and entry boxes
tk.Label(self.top_frame, text="Enter Place Name:", bg="#EAEAEA", font=("Arial", 10)).pack(side=tk.LEFT, padx=10, pady=10)
search_entry = tk.Entry(self.top_frame, textvariable=self.search_var, width=50, font=("Arial", 10))
search_entry.pack(side=tk.LEFT, padx=5, pady=10, ipady=4)
search_button = tk.Button(self.top_frame, text="Search", command=self.search_osm, font=("Arial", 10, "bold"))
search_button.pack(side=tk.LEFT, padx=10, pady=10)
tk.Label(self.sidebar_frame, text="Features", bg="#F5F5F5", font=("Arial", 12, "bold")).pack(pady=10, padx=10, anchor="w")
# New toggle switch for points
self.point_toggle = tk.Checkbutton(
self.top_frame,
variable=self.point_toggle_var,
onvalue=True,
offvalue=False,
text='Points',
width=10,
)
self.point_toggle.pack(side=tk.RIGHT, padx=10, pady=10)
self.line_toggle = tk.Checkbutton(
self.top_frame,
variable=self.line_toggle_var,
onvalue=True,
offvalue=False,
text='Lines',
width=10,
)
self.line_toggle.pack(side=tk.RIGHT, padx=10, pady=10)
self.poly_toggle = tk.Checkbutton(
self.top_frame,
variable=self.poly_toggle_var,
onvalue=True,
offvalue=False,
text='Polygons',
width=10,
)
self.poly_toggle.pack(side=tk.RIGHT, padx=10, pady=10)
# build radio buttons
for text, value in options.items():
tk.Radiobutton(
self.sidebar_frame, text=text, variable=self.option_var,
value=value, bg="#F5F5F5", font=("Arial", 10)
).pack(anchor="w", padx=20, pady=2)
tk.Radiobutton(
self.sidebar_frame, text="Custom Tag", variable=self.option_var,
value="custom", bg="#F5F5F5", font=("Arial", 10), command=self.custom_radiobutton_callback
).pack(anchor="w", padx=20, pady=2)
self.custom_entry = tk.Entry(self.sidebar_frame, textvariable=self.user_tag_var, font=("Arial", 10), state="disabled")
self.custom_entry.pack(anchor="w", padx=40, pady=2, ipady=4)
update_button = tk.Button(self.sidebar_frame, text="Update View", command=self.update_data, font=("Arial", 10, "bold"), bg="#DDDDDD")
update_button.pack(pady=20, padx=10, fill=tk.X)
# create map widget
self.map_widget = tkmv.TkinterMapView(self.map_frame, corner_radius=0)
self.map_widget.pack(fill=tk.BOTH, expand=True)
self.map_widget.set_position(default_x, default_y) #default position
self.map_widget.set_zoom(default_zoom)
def custom_radiobutton_callback(self):
# Enable or disable the custom entry field based on the selected radio button
if self.option_var.get() == "custom":
self.custom_entry.config(state="normal")
else:
self.custom_entry.config(state="disabled")
def search_osm(self):
# Fetches search results from OSM API
search_input = self.search_var.get().strip()
if not search_input:
messagebox.showerror("Error", "Please enter a place name.")
return
try:
self.parent.title(f"Searching for {search_input}...")
geocode_gdf = ox.geocode_to_gdf(search_input)
self.geocode = geocode_gdf
self.update_data()
self.parent.title(f"OpenStreetMap Search - {search_input}")
except Exception as e:
messagebox.showerror("Error", f"Could not find data for '{search_input}'.\nDetail: {e}")
self.parent.title("OpenStreetMap Search")
def update_data(self):
geocode_gdf = self.geocode
if geocode_gdf.empty:
self.search_osm()
else:
tag_key = self.option_var.get()
if tag_key == 'custom':
tag_key = self.user_tag_var.get().strip()
if tag_key == 'boundary':
self.draw_data(geocode_gdf)
else:
tags = {tag_key: True}
features_gdf = ox.features_from_polygon(geocode_gdf.dissolve().iloc[0].geometry, tags)
self.draw_data(features_gdf)
def draw_data(self, gdf): #add param for tags
# clear map
self.map_widget.delete_all_polygon()
self.map_widget.delete_all_marker()
self.map_widget.delete_all_path()
self.layers.clear()
gdf = gdf[gdf.geometry.notna()].explode(index_parts=False)
if gdf.empty:
messagebox.showinfo("No Data", f"No features found.")
return
try:
self.draw_polygons(gdf[gdf.geometry.geom_type == 'Polygon'])
self.draw_points(gdf[gdf.geometry.geom_type == 'Point'])
self.draw_lines(gdf[gdf.geometry.geom_type == 'LineString'])
self.map_widget.fit_bounding_box((gdf.total_bounds[3], gdf.total_bounds[0]), (gdf.total_bounds[1], gdf.total_bounds[2]))
except Exception as e:
messagebox.showerror("Plotting Error", f"An error occurred while plotting the data.\n\nDetail: {e}")
def draw_polygons(self, poly_gdf):
if not poly_gdf.empty and self.poly_toggle_var.get():
for row in poly_gdf.iterrows():
coords = [(y, x) for x, y in row.geometry.exterior.coords]
self.map_widget.set_polygon(coords, outline_color="#2c3e50", border_width=2)
def draw_points(self, point_gdf):
if not point_gdf.empty and self.point_toggle_var.get():
for row in point_gdf.iterrows():
x, y = row.geometry.x , row.geometry.y
self.map_widget.set_marker(y,x,command = 'marker_click')
def draw_lines(self, line_gdf):
if not line_gdf.empty and self.line_toggle_var.get():
for row in line_gdf.iterrows():
coords = [(y, x) for x, y in row.geometry.coords]
self.map_widget.set_path(coords)
if __name__ == "__main__":
root = tk.Tk()
root.title("OpenStreetMap Search")
root.geometry("1200x800")
app = MainApplication(root)
root.mainloop()