-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamlit_app.py
200 lines (163 loc) · 7.5 KB
/
streamlit_app.py
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
"""
# NOTAM Mapper
Generate interactive maps from notice to airman (NOTAM) documents
"""
import streamlit as st # Web app
import folium # Display map
import pandas as pd # Data manipulation
import re # Text searching via regular expressions
from streamlit_folium import st_folium # Integrate maps with web apps
import simplekml # .kml file generator
from polycircles import polycircles # .kml file circle generator
st.set_page_config(
page_title="Automatic mapper"
)
st.title("NOTAM Mapper")
st.write("Built with ☕ by [Johnathan Fernandes](johnathanfernandes.github.io/)")
rawtext = st.text_area("Paste NOTAM below:", height=400)
kml = simplekml.Kml() # Initialize kml
while len(rawtext) != 0: # User input check
data = rawtext.replace("\n", " ") # Join lines
data = data.replace(" ", " ") # Remove multiple spaces
# Define text search terms
circle_text = re.compile(
r" E\)(.*?)(AREA CIRCLE WITH RADIUS) ([\d]*[\.][\d]*|[\d]*) (M|NM|KM) (CENTERED ON) (([0-9]*[\.][0-9]+)|([0-9]*))(N |S )(([0-9]*[\.][0-9]+)|([0-9]*))(W|E)"
)
poly_text = re.compile(
r" E\)(.*?)(AREA BOUNDED BY LINES JOINING:((( [0-9]*[\.][0-9]+)|( [0-9]*))(N|S)(( [0-9]*[\.][0-9]+)|( [0-9]*))(W|E))*)"
)
# Search using regular expressions
circles = pd.DataFrame(re.findall(circle_text, data)) # Circles
polygons = pd.DataFrame(re.findall(poly_text, data)) # Polygons
if len(circles) + len(polygons) == 0:
st.write(
"No events found. Exiting. Please raise an [issue on the github page](https://github.com/johnathanfernandes/NOTAM_mapper/issues) with your NOTAM"
)
quit()
def process_circles(circle):
# Convert latitude from standard NOTAM notation to map form
lat_deg = float(circle[5][0:2])
lat_min = float(circle[5][2:4])
lat_sec = float(circle[5][4:])
lat = lat_deg + (lat_min / 60) + (lat_sec / 3600)
# Convert longitude
long_deg = float(circle[9][0:3])
long_min = float(circle[9][3:5])
long_sec = float(circle[9][5 : len(circle[9])])
longi = long_deg + (long_min / 60) + (long_sec / 3600)
# Convert NM and KM to M if present
if circle[3] == "NM":
rad = float(circle[2]) * 1852.001
elif circle[3] == "KM":
rad = float(circle[2]) * 1000
else:
rad = float(circle[2])
# Define event name
name = str(re.sub("(.*?)E\)", "", circle[0]) + " ".join(circle[1:]))
return [name, lat, longi, rad]
def process_polygons(polygon):
# Keep only coordinate text
coords_list = polygon[1][30:].split()
coordinate_pairs = []
# Iterate through coordinates and split into pairs
for i in range(0, len(coords_list), 2):
# Convert latitude
lat_deg = float(coords_list[i][0:2])
lat_min = float(coords_list[i][2:4])
lat_sec = float(coords_list[i][4:-1])
lat = lat_deg + (lat_min / 60) + (lat_sec / 3600)
# Convert longitude
long_deg = float(coords_list[i + 1][0:3])
long_min = float(coords_list[i + 1][3:5])
long_sec = float(coords_list[i + 1][5:-1])
longi = long_deg + (long_min / 60) + (long_sec / 3600)
coordinate_pairs.append([lat, longi])
name = str(re.sub("(.*?)E\)", "", polygon[0]) + polygon[1])
return [name, coordinate_pairs]
if len(circles) != 0:
circles.drop(circles.columns[[6, 7, 10, 11]], axis=1, inplace=True)
circle_list = circles.apply(process_circles, axis=1, result_type="expand")
circle_list.columns = ["Event name", "Latitude", "Longitude", "Radius (m)"]
if len(polygons) != 0:
polygons = polygons.iloc[:, 0:2]
polygon_list = polygons.apply(process_polygons, axis=1, result_type="expand")
polygon_list.columns = ["Event name", "Locations"]
# Define mapping function
def create_map():
try:
AIO_map = folium.Map(
location=[
circle_list["Latitude"].median(),
circle_list["Longitude"].median(),
]
) # Try to center around median of circles
except (ValueError, NameError):
AIO_map = folium.Map(
location=[
polygon_list["Locations"][0][1][
0
], # If no circles exist, center around first polygon
polygon_list["Locations"][0][1][1],
]
)
if len(circles) != 0:
for idx, circle in circle_list.iterrows():
folium.Circle(
location=(circle["Latitude"], circle["Longitude"]),
radius=circle["Radius (m)"],
popup=str(circle["Event name"]),
tooltip=str(circle["Event name"]),
fill=True,
).add_to(AIO_map)
folium.Marker(
location=(circle["Latitude"], circle["Longitude"]),
icon=folium.features.DivIcon(
icon_size=(150, 36),
icon_anchor=(0, 0),
html='<div style="font-size: 12pt; background: rgba(0, 0, 0, .2)">%s</div>'
% circle["Event name"],
),
).add_to(AIO_map)
polycircle = polycircles.Polycircle(
latitude=circle["Latitude"],
longitude=circle["Longitude"],
radius=circle["Radius (m)"],
number_of_vertices=round(circle["Radius (m)"] / 2) + 1,
)
pol = kml.newpolygon(
name=circle["Event name"], outerboundaryis=polycircle.to_kml()
)
pol.style.polystyle.color = simplekml.Color.changealphaint(
200, simplekml.Color.green
)
# kml.save(f"Circle {circle["Event name"]} kml")
if len(polygons) != 0:
for idx, polygon in polygon_list.iterrows():
folium.Polygon(
locations=(polygon["Locations"]),
popup=str(polygon["Event name"]),
tooltip=str(polygon["Event name"]),
fill=True,
).add_to(AIO_map)
folium.Marker(
location=(polygon["Locations"][0]),
icon=folium.features.DivIcon(
icon_size=(150, 36),
icon_anchor=(0, 0),
html='<div style="font-size: 12pt; background: rgba(0, 0, 0, .2)">%s</div>'
% polygon["Event name"],
),
).add_to(AIO_map)
pol = kml.newpolygon(name=polygon["Event name"])
pol.outerboundaryis = polygon["Locations"]
pol.style.linestyle.color = simplekml.Color.green
pol.style.linestyle.width = 5
pol.style.polystyle.color = simplekml.Color.changealphaint(
200, simplekml.Color.green
)
return AIO_map
AIO_map = create_map()
st_image = st_folium(AIO_map, width=725)
file = kml.save("Circle.kml")
f = open("Circle.kml")
st.download_button("Download kml file", f, ".kml")