Skip to content

Commit 4b71917

Browse files
authored
Merge pull request #14 from vFlightDataSystems/j/filter-sigmets
Filter SIGMETs to within 150nm of given ARTCC
2 parents 92ab4d1 + 2aa22e8 commit 4b71917

3 files changed

Lines changed: 36 additions & 20 deletions

File tree

blueprints/weather_bp.py

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import requests
44
from lxml import etree
55
from flask import Blueprint, jsonify, request
6+
from shapely.geometry import shape, Point, Polygon
7+
import json
68
from libs.weather_data_lock import weather_data, data_lock
79
from dataclasses import dataclass
810
from typing import List
@@ -102,25 +104,37 @@ class WindPoint:
102104

103105
@weather_blueprint.route('/sigmets')
104106
def _get_sigmets():
107+
artcc_id = request.args.get('artcc')
105108
response = requests.get(
106-
'https://aviationweather.gov/api/data/airsigmet?format=xml')
107-
sigmet_list = []
108-
tree = etree.fromstring(response.content)
109-
for entry in tree.iter('AIRSIGMET'):
110-
try:
111-
sigmet_entry = {
112-
'text': entry.find('raw_text').text,
113-
'hazard': dict(entry.find('hazard').attrib),
114-
'area': [[p.find('longitude').text, p.find('latitude').text] for p in entry.find('area').iter('point')],
115-
'altitude': dict(entry.find('altitude').attrib),
116-
'airsigmet_type': entry.find('airsigmet_type').text,
117-
}
118-
sigmet_list.append(sigmet_entry)
119-
except Exception as e:
120-
pass
121-
# logging.Logger(str(e))
122-
return jsonify(sigmet_list)
123-
109+
'https://aviationweather.gov/api/data/airsigmet?format=json').json()
110+
111+
if artcc_id:
112+
# Load ARTCC boundary polygon
113+
with open('resources/ArtccBoundaries.geojson') as f:
114+
geojson = json.load(f)
115+
artcc_poly = None
116+
for feature in geojson['features']:
117+
if feature['properties']['id'].upper() == artcc_id.upper():
118+
artcc_poly = shape(feature['geometry'])
119+
break
120+
if not artcc_poly:
121+
return jsonify([]) # or handle error
122+
123+
filtered_sigmets = []
124+
for sigmet in response:
125+
coords = sigmet.get('coords', [])
126+
for coord in coords:
127+
pt = Point(coord['lon'], coord['lat'])
128+
if artcc_poly.contains(pt):
129+
filtered_sigmets.append(sigmet)
130+
break
131+
# Check distance to polygon boundary
132+
if artcc_poly.exterior.distance(pt) * 60 <= 150: # degrees to nm approx
133+
filtered_sigmets.append(sigmet)
134+
break
135+
return jsonify(filtered_sigmets)
136+
137+
return jsonify(response)
124138

125139
@weather_blueprint.route('/datis/airport/<airport>')
126140
def _get_datis(airport):

libs/helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import re
2-
2+
from math import radians, sin, cos, sqrt, atan2
33

44
def matches_airway_format(s: str) -> bool:
55
return bool(re.match(r'^[A-Z]{1,2}\d+$', s))

requirements.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ Jinja2==3.1.6
1515
lxml==5.3.2
1616
MarkupSafe==3.0.2
1717
numpy==2.3.2
18+
pymongo==4.11.3
19+
python-dotenv==1.1.1
1820
packaging==25.0
1921
pygrib==2.1.6
20-
pymongo==4.11.3
2122
pyproj==3.7.2
2223
requests==2.32.3
24+
shapely==2.1.1
2325
urllib3==2.3.0
2426
Werkzeug==3.1.3

0 commit comments

Comments
 (0)