|
3 | 3 | import requests |
4 | 4 | from lxml import etree |
5 | 5 | from flask import Blueprint, jsonify, request |
| 6 | +from shapely.geometry import shape, Point, Polygon |
| 7 | +import json |
6 | 8 | from libs.weather_data_lock import weather_data, data_lock |
7 | 9 | from dataclasses import dataclass |
8 | 10 | from typing import List |
@@ -102,25 +104,37 @@ class WindPoint: |
102 | 104 |
|
103 | 105 | @weather_blueprint.route('/sigmets') |
104 | 106 | def _get_sigmets(): |
| 107 | + artcc_id = request.args.get('artcc') |
105 | 108 | 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) |
124 | 138 |
|
125 | 139 | @weather_blueprint.route('/datis/airport/<airport>') |
126 | 140 | def _get_datis(airport): |
|
0 commit comments