Ais traffic
Vessel Traffic Example¶
Using moving pandas with the TransMAP REST API
In [ ]:
Copied!
# Source ref: https://github.com/anitagraser/movingpandas-examples/blob/main/analysis-examples/2-ship-data.ipynb
make_url = lambda uri: "https://oak.cast.uark.edu/{0}".format(uri)
# Source ref: https://github.com/anitagraser/movingpandas-examples/blob/main/analysis-examples/2-ship-data.ipynb
make_url = lambda uri: "https://oak.cast.uark.edu/{0}".format(uri)
In [ ]:
Copied!
%matplotlib inline
%matplotlib inline
In [ ]:
Copied!
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))
from IPython.core.display import display, HTML
display(HTML(""))
In [ ]:
Copied!
import requests
import numpy as np
import pandas as pd
import geopandas
from shapely.geometry import Point, LineString, Polygon
from datetime import datetime, timedelta
import matplotlib.pyplot as plt
import movingpandas as mpd
import warnings
warnings.simplefilter("ignore")
import requests
import numpy as np
import pandas as pd
import geopandas
from shapely.geometry import Point, LineString, Polygon
from datetime import datetime, timedelta
import matplotlib.pyplot as plt
import movingpandas as mpd
import warnings
warnings.simplefilter("ignore")
In [ ]:
Copied!
mpd.__version__
def make_date(year: int, month: int, day: int, hour: int = 0, minute: int = 0, second: int = 0):
return datetime(year, month, day, hour, minute, second).strftime("%Y-%m-%dT%H:%M:%S")
mpd.__version__
def make_date(year: int, month: int, day: int, hour: int = 0, minute: int = 0, second: int = 0):
return datetime(year, month, day, hour, minute, second).strftime("%Y-%m-%dT%H:%M:%S")
In [ ]:
Copied!
ais_data = requests.get(make_url('ais/vessels'), params={
"limit": 50000,
"start_date": make_date(2020, 5, 29),
"end_date": make_date(2020, 5, 30)
})
ais_data.raise_for_status()
ais_data = requests.get(make_url('ais/vessels'), params={
"limit": 50000,
"start_date": make_date(2020, 5, 29),
"end_date": make_date(2020, 5, 30)
})
ais_data.raise_for_status()
In [ ]:
Copied!
df = pd.DataFrame(ais_data.json())
df
df = pd.DataFrame(ais_data.json())
df
In [ ]:
Copied!
df2 = pd.DataFrame(df.location.tolist(), index= df.index)
gpdf = geopandas.GeoDataFrame(df, geometry=geopandas.points_from_xy(df2[0], df2[1]))
df2 = pd.DataFrame(df.location.tolist(), index= df.index)
gpdf = geopandas.GeoDataFrame(df, geometry=geopandas.points_from_xy(df2[0], df2[1]))
In [ ]:
Copied!
gpdf.set_crs('epsg:4326')
gpdf.set_crs('epsg:4326')
In [ ]:
Copied!
gpdf.plot()
gpdf.plot()
In [ ]:
Copied!
gpdf['t'] = pd.to_datetime(gpdf['BaseDateTime'], format='%Y-%m-%dT%H:%M:%S')
gpdf['t'] = pd.to_datetime(gpdf['BaseDateTime'], format='%Y-%m-%dT%H:%M:%S')
In [ ]:
Copied!
gpdf = gpdf.set_index('t')
gpdf = gpdf.set_index('t')
In [ ]:
Copied!
gpdf['SOG'].hist(bins=10, figsize=(15, 3))
gpdf['SOG'].hist(bins=10, figsize=(15, 3))
In [ ]:
Copied!
print("Original size: {} rows".format(len(gpdf)))
gpdf = gpdf[gpdf.SOG>0]
print("Reduced to {} rows after removing 0 speed records".format(len(gpdf)))
gpdf['SOG'].hist(bins=10, figsize=(15,3))
print("Original size: {} rows".format(len(gpdf)))
gpdf = gpdf[gpdf.SOG>0]
print("Reduced to {} rows after removing 0 speed records".format(len(gpdf)))
gpdf['SOG'].hist(bins=10, figsize=(15,3))
In [ ]:
Copied!
gpdf['VesselType'] = gpdf['VesselType'].astype(float)
gpdf['VesselType'].value_counts().plot(kind='bar', figsize=(15,3))
gpdf['VesselType'] = gpdf['VesselType'].astype(float)
gpdf['VesselType'].value_counts().plot(kind='bar', figsize=(15,3))
In [ ]:
Copied!
%%time
MIN_LENGTH = 0.02 # degrees - May need to reproject to use meters
traj_collection = mpd.TrajectoryCollection(gpdf, 'MMSI', min_length=MIN_LENGTH)
print("Finished creating {} trajectories".format(len(traj_collection)))
%%time
MIN_LENGTH = 0.02 # degrees - May need to reproject to use meters
traj_collection = mpd.TrajectoryCollection(gpdf, 'MMSI', min_length=MIN_LENGTH)
print("Finished creating {} trajectories".format(len(traj_collection)))
In [ ]:
Copied!
traj_collection = mpd.MinTimeDeltaGeneralizer(traj_collection).generalize(tolerance=timedelta(minutes=1))
# this is looking for a specific 't' index
traj_collection = mpd.MinTimeDeltaGeneralizer(traj_collection).generalize(tolerance=timedelta(minutes=1))
# this is looking for a specific 't' index
In [ ]:
Copied!
shiptype_to_color = {'Passenger': 'blue', 'HSC': 'green', 'Tanker': 'red', 'Cargo': 'orange'}
# Should be changed to map to these codes: https://coast.noaa.gov/data/marinecadastre/ais/VesselTypeCodes2018.pdf
traj_collection.plot(column='VesselType', column_to_color=shiptype_to_color, linewidth=1, capstyle='round')
shiptype_to_color = {'Passenger': 'blue', 'HSC': 'green', 'Tanker': 'red', 'Cargo': 'orange'}
# Should be changed to map to these codes: https://coast.noaa.gov/data/marinecadastre/ais/VesselTypeCodes2018.pdf
traj_collection.plot(column='VesselType', column_to_color=shiptype_to_color, linewidth=1, capstyle='round')
In [ ]:
Copied!
passenger = traj_collection
passenger.hvplot(title='Passenger ferries', line_width=2)
passenger = traj_collection
passenger.hvplot(title='Passenger ferries', line_width=2)
In [ ]:
Copied!
In [ ]:
Copied!
Last update:
2022-09-26