Skip to content

Commit 052e3ae

Browse files
authored
Merge pull request #10 from druwan/refactor
Refactor
2 parents 5b38960 + 921c226 commit 052e3ae

13 files changed

Lines changed: 196 additions & 718 deletions

File tree

README.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
# Coords Buffer
22

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

6-
Add a buffer (in nautical miles) around a TMA.
5+
## Usage
76

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

10-
Geopandas
11+
Creates a specified buffer around user specified area.
1112

12-
Shapely
13+
options:
14+
-h, --help show this help message and exit
15+
-l, --list Prints list of available geometries and their
16+
id.
17+
--msid MSID Get coords for the selected geometries.
18+
-f INPUT_FILE, --input_file INPUT_FILE
19+
Path to a GeoJSON file with coordinates
20+
-b BUFFER, --buffer BUFFER
21+
Buffer size in NM (default: 0)
22+
```

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ dependencies = [
2626
"ruff>=0.11.7",
2727
"shapely==2.0.3",
2828
"six==1.16.0",
29+
"tabulate>=0.9.0",
2930
"tenacity>=9.1.2",
3031
"tzdata==2024.1",
3132
"urllib3==2.2.3",
@@ -39,4 +40,4 @@ dev = [
3940
[project.scripts]
4041
coord-buffer = "coord_buffer.cli:main"
4142
[tool.uv]
42-
package = true
43+
package = true

src/coord_buffer/cli.py

Lines changed: 31 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,62 @@
11
import argparse
2-
import os
3-
import sys
4-
from io import BytesIO
52

6-
import geopandas as gpd
7-
8-
from coord_buffer.config import DB_PARAMS, OUTPUT_FOLDER
9-
from coord_buffer.coords import to_dms_coords, to_wgs84
10-
from coord_buffer.fetcher import fetch_tmas
11-
from coord_buffer.processor import (
3+
from coord_buffer.utils import (
124
buffer_polygon,
13-
create_geojson_files,
14-
insert_tmas_to_db,
15-
is_airac_current,
5+
list_coords_from_db,
6+
logger,
167
read_coords,
8+
read_coords_from_db,
9+
to_dms_coords,
1710
)
18-
from coord_buffer.utils import logger
1911

2012

2113
def parse_args():
22-
parser = argparse.ArgumentParser(description="Fetch and process TMA data")
14+
parser = argparse.ArgumentParser(
15+
description="Creates a specified buffer around user specified area."
16+
)
17+
parser.add_argument(
18+
"-l",
19+
"--list",
20+
action="store_true",
21+
help="Prints list of available geometries and their id.",
22+
)
2323
parser.add_argument(
24-
"input_file",
25-
nargs="?",
24+
"--msid",
2625
default=None,
27-
help="Path to a GeoJSON file with coordinates",
26+
help="Get coords for the selected geometries.",
2827
)
2928
parser.add_argument(
30-
"--buffer", type=float, default=0, help="Buffer size in NM (default: 0)"
29+
"-f",
30+
"--input_file",
31+
default=None,
32+
help="Path to a GeoJSON file with coordinates",
3133
)
3234
parser.add_argument(
33-
"--check-airac",
34-
type=str,
35-
help="Check if the provided AIRAC date (YYYY-MM-DD) is the latest in the database",
35+
"-b", "--buffer", type=float, default=0, help="Buffer size in NM (default: 0)"
3636
)
3737
return parser.parse_args()
3838

3939

4040
def main():
4141
args = parse_args()
4242

43-
if args.check_airac:
44-
try:
45-
is_current = is_airac_current(DB_PARAMS, args.check_airac)
46-
if is_current:
47-
logger.info(
48-
f"AIRAC date {args.check_airac} is current or newer than the latest in the database"
49-
)
50-
else:
51-
logger.warning(
52-
f"AIRAC date {args.check_airac} is older than the latest in the database"
53-
)
54-
sys.exit(0 if is_current else 1)
55-
except Exception as e:
56-
logger.error(f"Error checking AIRAC date: {e}")
57-
sys.exit(1)
58-
59-
if not os.path.exists(OUTPUT_FOLDER):
60-
logger.info(f"Creating {OUTPUT_FOLDER} folder")
61-
os.makedirs(OUTPUT_FOLDER)
62-
63-
# If no input is provided, fetch TMAs & save to DB
64-
if args.input_file is None:
65-
logger.info("No input file provided, fetching TMAs")
66-
try:
67-
tmas = fetch_tmas()
68-
gdf = gpd.read_file(BytesIO(tmas))
69-
gdf = to_wgs84(gdf)
70-
create_geojson_files(gdf, OUTPUT_FOLDER)
71-
insert_tmas_to_db(gdf, DB_PARAMS)
72-
logger.info(f"TMAs saved to {OUTPUT_FOLDER} & inserted into database")
73-
except Exception as e:
74-
logger.info(f"Error fetching or processing TMA data: {e}")
75-
sys.exit(1)
76-
return
77-
78-
# Check if coord file exists
79-
if not os.path.isfile(args.input_file):
80-
logger.info(f"Input file does not exist: {args.input_file}")
81-
sys.exit(1)
82-
logger.info(f"Processing input file: {args.input_file}")
8343
try:
84-
coords = read_coords(args.input_file)
44+
if args.list:
45+
list_coords_from_db()
46+
return
47+
elif args.msid:
48+
logger.info(f"Processing msid: {args.msid}")
49+
coords = read_coords_from_db(args.msid)
50+
elif args.input_file:
51+
logger.info(f"Processing input file: {args.input_file}")
52+
coords = read_coords(args.input_file)
8553
buffered_gdf = buffer_polygon(coords, args.buffer)
8654
coords_df = buffered_gdf.get_coordinates()
8755
for _, row in coords_df.iterrows():
8856
print(to_dms_coords([row["y"], row["x"]]))
8957
except Exception as e:
9058
logger.error(f"Error processing file: {e}")
91-
sys.exit(1)
59+
return
9260

9361

9462
if __name__ == "__main__":

src/coord_buffer/config.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
BUFFER_MULTIPLIER = 1852
99
DEFAULT_EPSG = 4326
1010
METRIC_EPSG = 3006
11-
TMA_URL = os.getenv("TMA_URL", "https://daim.lfv.se/geoserver/wfs")
12-
OUTPUT_FOLDER = os.getenv("OUTPUT_FOLDER", "POLYGONES")
1311
DB_PARAMS = {
1412
"dbname": os.getenv("POSTGRES_DB"),
1513
"user": os.getenv("POSTGRES_USER"),

src/coord_buffer/coords.py

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/coord_buffer/db.py

Lines changed: 0 additions & 44 deletions
This file was deleted.

src/coord_buffer/fetcher.py

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)