Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
# Coords Buffer

Fetches TMA geojson data from lfv echarts, creates files and add a option \
to create a buffer around a TMA.
Fetches geojson data based on LFV echarts and returns new buffer coordiantes based on user input distance (nautical miles).

Add a buffer (in nautical miles) around a TMA.
## Usage

## Dependencies
```sh
uv run coord-buffer -h
usage: coord-buffer [-h] [-l] [--msid MSID] [-f INPUT_FILE] [-b BUFFER]

Geopandas
Creates a specified buffer around user specified area.

Shapely
options:
-h, --help show this help message and exit
-l, --list Prints list of available geometries and their
id.
--msid MSID Get coords for the selected geometries.
-f INPUT_FILE, --input_file INPUT_FILE
Path to a GeoJSON file with coordinates
-b BUFFER, --buffer BUFFER
Buffer size in NM (default: 0)
```
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dependencies = [
"ruff>=0.11.7",
"shapely==2.0.3",
"six==1.16.0",
"tabulate>=0.9.0",
"tenacity>=9.1.2",
"tzdata==2024.1",
"urllib3==2.2.3",
Expand All @@ -39,4 +40,4 @@ dev = [
[project.scripts]
coord-buffer = "coord_buffer.cli:main"
[tool.uv]
package = true
package = true
94 changes: 31 additions & 63 deletions src/coord_buffer/cli.py
Original file line number Diff line number Diff line change
@@ -1,94 +1,62 @@
import argparse
import os
import sys
from io import BytesIO

import geopandas as gpd

from coord_buffer.config import DB_PARAMS, OUTPUT_FOLDER
from coord_buffer.coords import to_dms_coords, to_wgs84
from coord_buffer.fetcher import fetch_tmas
from coord_buffer.processor import (
from coord_buffer.utils import (
buffer_polygon,
create_geojson_files,
insert_tmas_to_db,
is_airac_current,
list_coords_from_db,
logger,
read_coords,
read_coords_from_db,
to_dms_coords,
)
from coord_buffer.utils import logger


def parse_args():
parser = argparse.ArgumentParser(description="Fetch and process TMA data")
parser = argparse.ArgumentParser(
description="Creates a specified buffer around user specified area."
)
parser.add_argument(
"-l",
"--list",
action="store_true",
help="Prints list of available geometries and their id.",
)
parser.add_argument(
"input_file",
nargs="?",
"--msid",
default=None,
help="Path to a GeoJSON file with coordinates",
help="Get coords for the selected geometries.",
)
parser.add_argument(
"--buffer", type=float, default=0, help="Buffer size in NM (default: 0)"
"-f",
"--input_file",
default=None,
help="Path to a GeoJSON file with coordinates",
)
parser.add_argument(
"--check-airac",
type=str,
help="Check if the provided AIRAC date (YYYY-MM-DD) is the latest in the database",
"-b", "--buffer", type=float, default=0, help="Buffer size in NM (default: 0)"
)
return parser.parse_args()


def main():
args = parse_args()

if args.check_airac:
try:
is_current = is_airac_current(DB_PARAMS, args.check_airac)
if is_current:
logger.info(
f"AIRAC date {args.check_airac} is current or newer than the latest in the database"
)
else:
logger.warning(
f"AIRAC date {args.check_airac} is older than the latest in the database"
)
sys.exit(0 if is_current else 1)
except Exception as e:
logger.error(f"Error checking AIRAC date: {e}")
sys.exit(1)

if not os.path.exists(OUTPUT_FOLDER):
logger.info(f"Creating {OUTPUT_FOLDER} folder")
os.makedirs(OUTPUT_FOLDER)

# If no input is provided, fetch TMAs & save to DB
if args.input_file is None:
logger.info("No input file provided, fetching TMAs")
try:
tmas = fetch_tmas()
gdf = gpd.read_file(BytesIO(tmas))
gdf = to_wgs84(gdf)
create_geojson_files(gdf, OUTPUT_FOLDER)
insert_tmas_to_db(gdf, DB_PARAMS)
logger.info(f"TMAs saved to {OUTPUT_FOLDER} & inserted into database")
except Exception as e:
logger.info(f"Error fetching or processing TMA data: {e}")
sys.exit(1)
return

# Check if coord file exists
if not os.path.isfile(args.input_file):
logger.info(f"Input file does not exist: {args.input_file}")
sys.exit(1)
logger.info(f"Processing input file: {args.input_file}")
try:
coords = read_coords(args.input_file)
if args.list:
list_coords_from_db()
return
elif args.msid:
logger.info(f"Processing msid: {args.msid}")
coords = read_coords_from_db(args.msid)
elif args.input_file:
logger.info(f"Processing input file: {args.input_file}")
coords = read_coords(args.input_file)
buffered_gdf = buffer_polygon(coords, args.buffer)
coords_df = buffered_gdf.get_coordinates()
for _, row in coords_df.iterrows():
print(to_dms_coords([row["y"], row["x"]]))
except Exception as e:
logger.error(f"Error processing file: {e}")
sys.exit(1)
return


if __name__ == "__main__":
Expand Down
2 changes: 0 additions & 2 deletions src/coord_buffer/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
BUFFER_MULTIPLIER = 1852
DEFAULT_EPSG = 4326
METRIC_EPSG = 3006
TMA_URL = os.getenv("TMA_URL", "https://daim.lfv.se/geoserver/wfs")
OUTPUT_FOLDER = os.getenv("OUTPUT_FOLDER", "POLYGONES")
DB_PARAMS = {
"dbname": os.getenv("POSTGRES_DB"),
"user": os.getenv("POSTGRES_USER"),
Expand Down
30 changes: 0 additions & 30 deletions src/coord_buffer/coords.py

This file was deleted.

44 changes: 0 additions & 44 deletions src/coord_buffer/db.py

This file was deleted.

25 changes: 0 additions & 25 deletions src/coord_buffer/fetcher.py

This file was deleted.

Loading