|
7 | 7 | import geopandas as gpd |
8 | 8 | import pandas as pd |
9 | 9 | import numpy as np |
10 | | - |
11 | 10 | from utils import get_csv_from_zip |
12 | | -# Load database configuration from config.json |
13 | | -with open("../config.json", "r") as file: |
| 11 | + |
| 12 | +script_dir = os.path.dirname(os.path.abspath(__file__)) |
| 13 | +output_file= os.path.join(script_dir, ".", "trajectories_mf1.json") |
| 14 | +config_path = os.path.join(script_dir, "..", "config.json") |
| 15 | +with open(config_path, "r") as file: |
14 | 16 | config = json.load(file) |
15 | 17 |
|
16 | | -# Construct the database URL for SQLAlchemy |
| 18 | + |
17 | 19 | database_url = ( |
18 | 20 | f"postgresql://{config['DB_USER']}:{config['DB_PASS']}@" |
19 | 21 | f"{config['DB_HOST']}:{config['DB_PORT']}/{config['DB_NAME']}" |
20 | 22 | ) |
21 | 23 |
|
22 | 24 | # Create the SQLAlchemy engine |
23 | 25 | engine = create_engine(database_url) |
24 | | -data_csv_path = get_csv_from_zip("aisdk-2024-08-07.zip") |
| 26 | +zip_path = os.path.join(script_dir, "aisdk-2024-08-07.zip") |
| 27 | +data_csv_path = get_csv_from_zip(zip_path,script_dir) |
25 | 28 | data_csv_path=os.path.abspath(data_csv_path) |
26 | 29 | print(data_csv_path) |
27 | 30 | #update this based on whether you're running your db from docker or local/ this is a docker version, otherwise comment it out |
|
33 | 36 |
|
34 | 37 | # Data collection and 9.3 Cleaning Static Attribute +9.4 Voyage related Attribbutes: |
35 | 38 | try: |
36 | | - # Read the SQL file CleaningStaticAttributes.sql |
37 | | - with open("init.sql", "r") as sql_file: |
| 39 | + init_sql= os.path.join(script_dir, ".", "init.sql") |
| 40 | + with open(init_sql, "r") as sql_file: |
38 | 41 | sql_query = sql_file.read() |
39 | 42 |
|
40 | | - # Create a text object with parameters |
41 | 43 | stmt = text(sql_query).bindparams(data_csv_path=data_csv_path) |
42 | 44 |
|
43 | | - # Execute with parameter |
44 | 45 | with engine.connect() as conn: |
45 | | - # Start a transaction |
| 46 | + |
46 | 47 | with conn.begin(): |
47 | | - # Execute the COPY command with parameter |
48 | 48 | conn.execute(stmt) |
49 | 49 | print(f"Successfully loaded data from {data_csv_path}") |
50 | 50 |
|
51 | 51 | except Exception as e: |
52 | 52 | print(f"Error loading data: {e}") |
53 | 53 | raise |
54 | 54 |
|
55 | | - |
56 | | -# After your existing data loading code, add this: |
57 | | - |
58 | | -def export_json_from_view(engine, output_file="trajectories_mf1.json"): |
| 55 | +def export_json_from_view(engine, output_file=output_file): |
59 | 56 | query = "SELECT json_data FROM ships_json ORDER BY mmsi;" |
60 | 57 |
|
61 | 58 | try: |
62 | | - # Fetch all JSON data from the view |
63 | | - # for df in pd.read_sql(query, engine,chunksize=686): |
64 | | - |
| 59 | + |
65 | 60 | df = pd.read_sql(query, engine) |
66 | 61 |
|
67 | 62 | if df.empty: |
68 | 63 | print("No data found in ships_json view") |
69 | 64 | return [] |
70 | 65 |
|
71 | | - # Extract the json_data column as a list of dicts |
| 66 | + |
72 | 67 | json_ready = df['json_data'].tolist() |
73 | | - print('777777777777777777777777777777777777777777777') |
74 | | - # Ensure output directory exists |
75 | | - # os.makedirs(os.path.dirname(output_file), exist_ok=True) |
76 | | - |
77 | | - # Write to JSON file |
| 68 | + |
78 | 69 | with open(output_file, "w") as f: |
79 | 70 | json.dump(json_ready, f, indent=2, default=str) |
80 | 71 |
|
81 | | - print(f"Successfully exported {len(json_ready)} vessels to {output_file}") |
| 72 | + print(f"Successfully exported {len(json_ready)} ais vessels to {output_file}") |
82 | 73 | return json_ready |
83 | 74 |
|
84 | 75 | except Exception as e: |
85 | 76 | print(f"Error exporting JSON: {e}") |
86 | 77 | raise |
87 | 78 |
|
88 | | -# Call the function after your data loading |
| 79 | + |
89 | 80 | if __name__ == "__main__": |
90 | 81 |
|
91 | 82 |
|
92 | | - # Export to JSON |
93 | | - export_json_from_view(engine, "trajectories_mf1.json") |
| 83 | + #to JSON |
| 84 | + export_json_from_view(engine, output_file=output_file) |
94 | 85 |
|
95 | 86 |
|
96 | 87 | # DROP VIEW IF EXISTS ships_json CASCADE; |
|
0 commit comments