Building on the previous section, where we used AIS data to create AISdb databases, users can export AIS data from these databases into CSV format. In this section, we provide examples of exporting data from SQLite or PostgreSQL databases into CSV files. While we demonstrate these operations using internal data, you can apply the same techniques to your databases.
Export CSV from SQLite Database
In the first example, we connected to a SQLite database, queried data in a specific time range and area of interest, and then exported the queried data to a CSV file:
import csvimport aisdbimport nest_asynciofrom aisdb import DBConn, DBQuery, DomainFromPointsfrom aisdb.database.dbconn import SQLiteDBConnfrom datetime import datetimenest_asyncio.apply()dbpath ='YOUR_DATABASE.db'# Path to your databaseend_time = datetime.strptime("2018-01-02 00:00:00", '%Y-%m-%d %H:%M:%S')start_time = datetime.strptime("2018-01-01 00:00:00", '%Y-%m-%d %H:%M:%S')domain =DomainFromPoints(points=[(-63.6, 44.6)], radial_distances=[50000])# Connect to SQLite databasedbconn =SQLiteDBConn(dbpath=dbpath)withSQLiteDBConn(dbpath=dbpath)as dbconn: qry =DBQuery( dbconn=dbconn, start=start_time, end=end_time, xmin=domain.boundary['xmin'], xmax=domain.boundary['xmax'], ymin=domain.boundary['ymin'], ymax=domain.boundary['ymax'], callback=aisdb.database.sqlfcn_callbacks.in_time_bbox_validmmsi, ) tracks = aisdb.track_gen.TrackGen(qry.gen_qry(), decimate=False)# Define the headers for the CSV fileheaders = ['mmsi','time','lon','lat','cog','sog','utc_second','heading','rot','maneuver']# Open the CSV file for writingcsv_filename ='output_sqlite.csv'withopen(csv_filename, mode='w', newline='')as file: writer = csv.DictWriter(file, fieldnames=headers) writer.writeheader()# Write the header oncefor track in tracks:for i inrange(len(track['time'])): row ={'rot': track['rot'],'mmsi': track['mmsi'],'lon': track['lon'][i],'lat': track['lat'][i],'cog': track['cog'][i],'sog': track['sog'][i],'time': track['time'][i],'heading': track['heading'],'maneuver': track['maneuver'],'utc_second': track['utc_second'][i],} writer.writerow(row)# Write the row to the CSV fileprint(f"All tracks have been combined and written to {csv_filename}")
Now we can check the data in the exported CSV file:
Similar to exporting data from a SQLite database to a CSV file, the only difference this time is that you'll need to connect to your PostgreSQL database and query the data you want to export to CSV. We showed a full example as follows:
import csvimport aisdbimport nest_asynciofrom datetime import datetimefrom aisdb.database.dbconn import PostgresDBConnfrom aisdb import DBConn, DBQuery, DomainFromPointsnest_asyncio.apply()dbconn =PostgresDBConn( host='localhost', # PostgreSQL address port=5432, # PostgreSQL port user='your_username', # PostgreSQL username password='your_password', # PostgreSQL password dbname='database_name'# Database name)qry =DBQuery( dbconn=dbconn, start=datetime(2023, 1, 1), end=datetime(2023, 1, 3), xmin=domain.boundary['xmin'], xmax=domain.boundary['xmax'], ymin=domain.boundary['ymin'], ymax=domain.boundary['ymax'], callback=aisdb.database.sqlfcn_callbacks.in_time_bbox_validmmsi)tracks = aisdb.track_gen.TrackGen(qry.gen_qry(), decimate=False)# Define the headers for the CSV fileheaders = ['mmsi','time','lon','lat','cog','sog','utc_second','heading','rot','maneuver']# Open the CSV file for writingcsv_filename ='output_postgresql.csv'withopen(csv_filename, mode='w', newline='')as file: writer = csv.DictWriter(file, fieldnames=headers) writer.writeheader()# Write the header oncefor track in tracks:for i inrange(len(track['time'])): row ={'rot': track['rot'],'mmsi': track['mmsi'],'lon': track['lon'][i],'lat': track['lat'][i],'cog': track['cog'][i],'sog': track['sog'][i],'time': track['time'][i],'heading': track['heading'],'maneuver': track['maneuver'],'utc_second': track['utc_second'][i],} writer.writerow(row)# Write the row to the CSV fileprint(f"All tracks have been combined and written to {csv_filename}")