Skip to content

Commit

Permalink
Add exclude_heliports
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjaminPelletier committed May 17, 2024
1 parent 2a5e6b7 commit 9d2c6bb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 3 additions & 1 deletion aerodata/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from gevent import monkey
from requests import HTTPError

monkey.patch_all()

import flask
from loguru import logger
from requests import HTTPError

from aerodata.fetch import get_features
from aerodata.query import AerodromeQueryParams, select_features
Expand All @@ -13,6 +14,7 @@

@webapp.route("/aerodromes")
def get_aerodromes():
logger.debug(f"Staring aerodromes handler for {flask.request.query_string.decode()}")
try:
query_params = AerodromeQueryParams.from_dict(flask.request.args)
except ValueError as e:
Expand Down
18 changes: 17 additions & 1 deletion aerodata/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class AerodromeQueryParams(object):
page_size: int
exclude_runways: bool
exclude_helipads: bool
exclude_heliports: bool
exclude_aerodromes: bool
latA: float
latB: float
Expand All @@ -27,6 +28,7 @@ def from_dict(params: dict) -> AerodromeQueryParams:
raise ValueError("Invalid page_size")
kwargs["exclude_runways"] = params.get("exclude_runways", "false").lower() == "true"
kwargs["exclude_helipads"] = params.get("exclude_helipads", "false").lower() == "true"
kwargs["exclude_heliports"] = params.get("exclude_heliports", "false").lower() == "true"
kwargs["exclude_aerodromes"] = params.get("exclude_aerodromes", "false").lower() == "true"

bounding_box = params.get("bounding_box", None)
Expand Down Expand Up @@ -71,7 +73,7 @@ def from_dict(params: dict) -> AerodromeQueryParams:
return AerodromeQueryParams(**kwargs)


def select_features(all_features: dict, query: AerodromeQueryParams) -> dict:
def select_features(all_features: list[dict], query: AerodromeQueryParams) -> dict:
"""Filter the provided features and return a FeatureCollection with selected features.
Args:
Expand Down Expand Up @@ -119,6 +121,20 @@ def select_features(all_features: dict, query: AerodromeQueryParams) -> dict:

features.append(feature)

if query.exclude_heliports:
to_remove = []
for feature in features:
if feature["properties"]["aerodrome_element_type"] == "Aerodrome":
surfaces = [
f for f in all_features
if f is not feature
and f["properties"].get("aerodrome_identifier", "") == feature["properties"]["aerodrome_identifier"]
]
if all(s["properties"]["aerodrome_element_type"] == "Helipad" for s in surfaces):
to_remove.append(feature)
for feature in to_remove:
features.remove(feature)

if query.page_token:
skip = int(query.page_token)
if skip >= len(features):
Expand Down

0 comments on commit 9d2c6bb

Please sign in to comment.