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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
#!/usr/bin/env python3
# Use the proper idiom in the main module ...
# NOTE: See https://docs.python.org/3.11/library/multiprocessing.html#the-spawn-and-forkserver-start-methods
if __name__ == "__main__":
# Import standard modules ...
import os
# Import special modules ...
try:
import cartopy
cartopy.config.update(
{
"cache_dir" : os.path.expanduser("~/.local/share/cartopy_cache"),
}
)
except:
raise Exception("\"cartopy\" is not installed; run \"pip install --user Cartopy\"") from None
try:
import matplotlib
matplotlib.rcParams.update(
{
"backend" : "Agg", # NOTE: See https://matplotlib.org/stable/gallery/user_interfaces/canvasagg.html
"figure.dpi" : 300,
"figure.figsize" : (9.6, 7.2), # NOTE: See https://github.com/Guymer/misc/blob/main/README.md#matplotlib-figure-sizes
"font.size" : 8,
}
)
import matplotlib.pyplot
except:
raise Exception("\"matplotlib\" is not installed; run \"pip install --user matplotlib\"") from None
try:
import shapely
import shapely.geometry
import shapely.ops
except:
raise Exception("\"shapely\" is not installed; run \"pip install --user Shapely\"") from None
# Import my modules ...
try:
import fmc
except:
raise Exception("\"fmc\" is not installed; you need to have the Python module from https://github.com/Guymer/fmc located somewhere in your $PYTHONPATH") from None
try:
import pyguymer3
import pyguymer3.geo
import pyguymer3.image
except:
raise Exception("\"pyguymer3\" is not installed; you need to have the Python module from https://github.com/Guymer/PyGuymer3 located somewhere in your $PYTHONPATH") from None
# **************************************************************************
# Create short-hand for the colour map and define the nautical mile, number
# of angles and resolution ...
cmap = matplotlib.pyplot.get_cmap("jet")
nm = 1852.0 # [m/NM]
fill = 0.1 # [°]
nang = 361 # [#]
res = "10m"
simp = 0.01 # [°]
tol = 1.0e-10 # [°]
# Find file containing all the country shapes ...
sfile = cartopy.io.shapereader.natural_earth(
category = "cultural",
name = "admin_0_countries",
resolution = res,
)
# Create a list of tuples of territories ...
# NOTE: The first entry is the territory name and the second is the list of
# Natural Earth country names that make up the territory. The BAT has
# been ignored as it has restrictions on its use.
territories = [
("United Kingdom" , ["United Kingdom"] ),
("Akrotiri & Dhekelia" , ["Akrotiri", "Dhekelia"] ),
("Anguilla" , ["Anguilla"] ),
("Bermuda" , ["Bermuda"] ),
("Cayman Islands" , ["Cayman Is."] ),
("Falkland Islands" , ["Falkland Is."] ),
("Gibraltar" , ["Gibraltar"] ),
("South Georgia & the South Sandwich Islands", ["S. Geo. and the Is."] ),
("British Indian Ocean Territory" , ["Br. Indian Ocean Ter."]),
("Montserrat" , ["Montserrat"] ),
("Pitcairn Islands" , ["Pitcairn Is."] ),
("Saint Helena, Ascension & Tristan da Cunha", ["Saint Helena"] ),
("Turks and Caicos Islands" , ["Turks and Caicos Is."] ),
("British Virgin Islands" , ["British Virgin Is."] ),
]
# Create list of British polygons ...
polys = []
# Loop over records ...
for record in cartopy.io.shapereader.Reader(sfile).records():
# Create short-hand ...
neName = pyguymer3.geo.getRecordAttribute(record, "NAME")
# Skip this record if it is not for a territory in the list ...
skip = True
for territory, countries in territories:
if neName in countries:
skip = False
break
if skip:
continue
# Loop over Polygons and add them to the list ...
for poly in pyguymer3.geo.extract_polys(record.geometry):
# HACK: The Natural Earth polygons are sometimes so coarse that they
# do not cover the runway (e.g. Diego Garcia) so they are
# buffered by 5km.
polys += pyguymer3.geo.extract_polys(
pyguymer3.geo.buffer(
poly,
5000.0,
debug = False,
fill = fill,
nang = nang,
simp = simp,
tol = tol,
)
)
print(f"There are {len(polys):d} British polygons.")
# Create list of British airfields ...
# HACK: Manually add HLE as it is missing from the OpenFlights GitHub
# repository.
airfields = [] # [°], [°]
airfields.append(shapely.geometry.Point(-5.645833, -15.959167)) # [°], [°]
# Loop over airports ...
for airport in fmc.load_airport_list():
# Create point and check if it is within any of the polygons ...
airfield = shapely.geometry.Point(airport["Longitude"], airport["Latitude"])# [°], [°]
for poly in polys:
if poly.contains(airfield):
airfields.append(airfield) # [°], [°]
break
print(f"There are {len(airfields):d} British airfields.")
# Create figure ...
fg = matplotlib.pyplot.figure(figsize = (12.8, 7.2))
# Create axis ...
ax = pyguymer3.geo.add_axis(fg)
# Configure axis ...
pyguymer3.geo.add_map_background(ax, resolution = "large8192px")
# Add country outlines ...
for record in cartopy.io.shapereader.Reader(sfile).records():
ax.add_geometries(
pyguymer3.geo.extract_polys(record.geometry),
cartopy.crs.PlateCarree(),
edgecolor = "black",
facecolor = "none",
linewidth = 0.5,
)
# Initialize distance, labels and lines ...
dist2 = 0.0 # [NM]
labels = []
lines = []
# Loop over distances ...
for i in range(8):
# Set distance ...
dist1 = 500.0 # [NM]
dist2 += dist1 # [NM]
# Decide what needs doing ...
if i == 0:
print(f"Calculating for {dist2:6,.0f} NM (using {len(airfields):,d} airfields) ...")
# Buffer airfields ...
# NOTE: "airfields" is the Point-list to be buffered.
geoms = []
for airfield in airfields:
geoms += pyguymer3.geo.extract_polys(
pyguymer3.geo.buffer(
airfield,
dist1 * nm,
debug = False,
fill = fill,
nang = nang,
simp = simp,
tol = tol,
)
)
geoms = shapely.ops.unary_union(geoms).simplify(tol)
pyguymer3.geo.check(geoms)
# NOTE: "geoms" is now the [Multi]Polygon buffer of "airfields".
else:
# Find out how complicated the [Multi]Polygon is ...
npoints = 0 # [#]
npolys = 0 # [#]
for poly in pyguymer3.geo.extract_polys(geoms):
npoints += len(poly.exterior.coords) # [#]
npolys += 1 # [#]
for interior in poly.interiors:
npoints += len(interior.coords) # [#]
print(f"Calculating for {dist2:6,.0f} NM (using {npoints:,d} points in {npolys:,d} Polygons) ...")
# Buffer area ...
# NOTE: "geoms" is the [Multi]Polygon to be buffered.
geoms = pyguymer3.geo.buffer(
geoms,
dist1 * nm,
debug = False,
fill = fill,
nang = nang,
simp = simp,
tol = tol,
)
# NOTE: "geoms" is now the [Multi]Polygon buffer of "geoms".
# Add to plot ...
ax.add_geometries(
pyguymer3.geo.extract_polys(geoms),
cartopy.crs.PlateCarree(),
edgecolor = cmap(float(i) / 7.0),
facecolor = "none",
linewidth = 1.0,
)
labels.append(f"{dist2:6,.0f} NM")
lines.append(matplotlib.lines.Line2D([], [], color = cmap(float(i) / 7.0)))
print("Plotting ...")
# Configure axis ...
ax.legend(
lines,
labels,
loc = "upper center",
ncol = 4,
)
# Configure figure ...
fg.tight_layout()
# Save figure ...
fg.savefig("find_airfields.png")
matplotlib.pyplot.close(fg)
# Optimize PNG ...
pyguymer3.image.optimize_image(
"find_airfields.png",
strip = True,
)
|