|
1 | 1 | import argparse |
2 | | -import os |
3 | | -import sys |
4 | | -from io import BytesIO |
5 | 2 |
|
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 ( |
12 | 4 | buffer_polygon, |
13 | | - create_geojson_files, |
14 | | - insert_tmas_to_db, |
15 | | - is_airac_current, |
| 5 | + list_coords_from_db, |
| 6 | + logger, |
16 | 7 | read_coords, |
| 8 | + read_coords_from_db, |
| 9 | + to_dms_coords, |
17 | 10 | ) |
18 | | -from coord_buffer.utils import logger |
19 | 11 |
|
20 | 12 |
|
21 | 13 | 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 | + ) |
23 | 23 | parser.add_argument( |
24 | | - "input_file", |
25 | | - nargs="?", |
| 24 | + "--msid", |
26 | 25 | default=None, |
27 | | - help="Path to a GeoJSON file with coordinates", |
| 26 | + help="Get coords for the selected geometries.", |
28 | 27 | ) |
29 | 28 | 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", |
31 | 33 | ) |
32 | 34 | 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)" |
36 | 36 | ) |
37 | 37 | return parser.parse_args() |
38 | 38 |
|
39 | 39 |
|
40 | 40 | def main(): |
41 | 41 | args = parse_args() |
42 | 42 |
|
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}") |
83 | 43 | 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) |
85 | 53 | buffered_gdf = buffer_polygon(coords, args.buffer) |
86 | 54 | coords_df = buffered_gdf.get_coordinates() |
87 | 55 | for _, row in coords_df.iterrows(): |
88 | 56 | print(to_dms_coords([row["y"], row["x"]])) |
89 | 57 | except Exception as e: |
90 | 58 | logger.error(f"Error processing file: {e}") |
91 | | - sys.exit(1) |
| 59 | + return |
92 | 60 |
|
93 | 61 |
|
94 | 62 | if __name__ == "__main__": |
|
0 commit comments