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
|
#!/usr/bin/env python3
# Use the proper idiom in the main module ...
# NOTE: See https://docs.python.org/3.12/library/multiprocessing.html#the-spawn-and-forkserver-start-methods
if __name__ == "__main__":
# Import standard modules ...
import json
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; run \"pip install --user git+https://github.com/Guymer/fmc.git\"") from None
try:
import pyguymer3
import pyguymer3.geo
import pyguymer3.image
except:
raise Exception("\"pyguymer3\" is not installed; run \"pip install --user PyGuymer3\"") from None
# **************************************************************************
# Create short-hand for the colour map and define the nautical mile ...
cmap = matplotlib.colormaps["turbo"]
nm = 1852.0 # [m/NM]
# **************************************************************************
# Create a list of ISO 3166-1 Alpha-3 codes of British Overseas Territories
# (see "ISO 3166-1.json" in my "misc" repository for the complete list) ...
a3s = [
"AIA", # Anguilla
"BMU", # Bermuda
"CYM", # Cayman Islands (the)
"FLK", # Falkland Islands (the)
"GBR", # United Kingdom of Great Britain and Northern Ireland (the)
"GIB", # Gibraltar
"IOT", # British Indian Ocean Territory (the)
"MSR", # Montserrat
"PCN", # Pitcairn
"SGS", # South Georgia and the South Sandwich Islands
"SHN", # Saint Helena, Ascension and Tristan da Cunha
"TCA", # Turks and Caicos Islands (the)
"VGB", # Virgin Islands (British)
]
# Load airport list ...
with open(f"{fmc.__path__[0]}/db.json", "rt", encoding = "utf-8") as fObj:
airports = json.load(fObj)
# Create list of British airfields (manually adding LCRA as it is marked as
# being part of Cyprus in the OurAirports Data GitHub repository) ...
airfields = [] # [°], [°]
for airport in airports:
if "lon" not in airport or "lat" not in airport:
continue
if "ICAO" in airport:
if airport["ICAO"] == "LCRA":
pnt = shapely.geometry.Point(airport["lon"], airport["lat"]) # [°], [°]
airfields.append(pnt) # [°], [°]
continue
if "ISO 3166-1 Alpha-3" in airport:
if airport["ISO 3166-1 Alpha-3"] in a3s:
pnt = shapely.geometry.Point(airport["lon"], airport["lat"]) # [°], [°]
airfields.append(pnt) # [°], [°]
continue
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,
add_coastlines = False,
add_gridlines = False,
debug = False,
nIter = 100,
onlyValid = False,
repair = False,
)
# Configure axis ...
pyguymer3.geo.add_map_background(
ax,
debug = False,
resolution = "large8192px",
)
# Add country outlines ...
for record in cartopy.io.shapereader.Reader(
cartopy.io.shapereader.natural_earth(
category = "cultural",
name = "admin_0_countries",
resolution = "10m",
)
).records():
ax.add_geometries(
pyguymer3.geo.extract_polys(
record.geometry,
onlyValid = False,
repair = False,
),
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 = 0.1,
fillSpace = "EuclideanSpace",
keepInteriors = True,
nAng = 361,
nIter = 100,
simp = 0.01,
),
onlyValid = False,
repair = False,
)
geoms = shapely.ops.unary_union(geoms).simplify(1.0e-10)
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,
onlyValid = False,
repair = False,
):
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 = 0.1,
fillSpace = "EuclideanSpace",
keepInteriors = True,
nAng = 361,
nIter = 100,
simp = 0.01,
)
# NOTE: "geoms" is now the [Multi]Polygon buffer of "geoms".
# Add to plot ...
ax.add_geometries(
pyguymer3.geo.extract_polys(
geoms,
onlyValid = False,
repair = False,
),
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.optimise_image(
"find_airfields.png",
debug = False,
strip = True,
timeout = 60.0,
)
|