How To Access Maritime Data With Python

API Knowledge zone

How To Access Maritime Data With Python

Maritime data analytics can provide valuable insights into the operations of ships, ports, and other components of the global shipping industry. Python is a popular programming language that is well-suited to working with large volumes of data and can be used to access and analyze maritime data from various sources, including APIs. In this blog post, we will take a look at how to use the Maritime API in Python and provide some technical instructions for getting started.

How can we use data analytics for improving decision-making?

Maritime data analytics can help ship operators, port authorities, and other stakeholders in the shipping industry to make better decisions and improve the efficiency of their operations. By using Python to access and analyze data from various sources, including the Maritime API, it is possible to gain a better understanding of factors such as ship movements, cargo flows, and weather patterns. This can help to optimize routes, reduce fuel consumption, and improve safety at sea.

You might like also to read: The power of maritime data analytics

How to connect Maritime API:

To use the Maritime API in Python, you will first need to sign up for an API key. This can be done on the Maritime API website, such as Datalastic. Moreover, you can get an API key in under than 3 clicks! As simple as that. Once you subscribe, straight after the checkout, your API key will be delivered to your inbox.

Once you have your API key, you can use it to access the API in your Python code.

To use the API, you will need to install the requests library in Python. This can be done using the pip package manager, which is included in most Python distributions. Furthermore, once you have installed the requests library, you can use it to make HTTP requests to the API.

For example, to make a request to the API for ship positions, you could use the following code:

import requests
api_key = "YOUR_API_KEY"
# Set the base URL for the API endpoint
base_url = "https://maritimeapi.azure-api.net/positions"
# Set the request headers
headers = {
 "Ocp-Apim-Subscription-Key": api_key
}
# Make the request to the API
response = requests.get(base_url, headers=headers)
# Print the response
print(response.text) 

This code will make a request to the API for vessel data, using your API key to authenticate the request. The response will be returned as a JSON object, which can be accessed and processed in your Python code.

Testing the API to check technical errors:

To test if the API is working, you can make a simple request to the API and check the response. For instance, you could utilize the code from the previous section to make a request for vessel data. Subsequently, you can print the response to the console.

If the API is functioning correctly, you should see a JSON object containing data about the vessel. In this JSON object, you will find information such as the vessel’s name, IMO number, latitude and longitude, and other relevant details.

Examples:

Here is an example of the response you might receive from the API if you make a request for vessel data:

{
 "data": {
 "uuid": "b8625b67-7142-cfd1-7b85-595cebfe4191",
 "name": "MAERSK CHENNAI",
 "mmsi": "566093000",
 "imo": "9525
338",
"eni": null,
"country_iso": "SG",
"type": "Cargo - Hazard A (Major)",
"type_specific": "Container Ship",
"lat": 6.303473,
"lon": 3.201678,
"speed": 0,
"course": 326,
"navigational_status": "Under Way using engine",
"heading": 226,
"destination": "TANGER MED",
"last_position_epoch": 1620484020,
"last_position_UTC": "2021-05-08T14:27:00Z"
},
"meta": {
"duration": 0.00246942,
"endpoint": "/api/v0/vessel",
"success": true
}
}

This response contains data about a specific vessel, including its name, IMO number, latitude and longitude, speed, and other details.

Examples on how to use vessel API in Python:

Once you have received the data from the API, you can use Python to process and analyze it in various ways. For example, you could use the pandas library to load the data into a DataFrame, which provides a convenient way to manipulate and analyze the data.

Here is an example of how you could use pandas to load the data from the API into a DataFrame and then use it to calculate the average speed of the vessel:

import pandas as pd
Load the data from the API into a DataFrame
df = pd.DataFrame(response.json()["data"])
Calculate the average speed of the vessel
average_speed = df["speed"].mean()
Print the result
print(f"Average speed: {average_speed} knots") 

How to use Pandas with vessel API?

Here are some additional examples of how you could use pandas to analyze the data from the vessel API in Python:

Calculate the average course of the vessel:

import pandas as pd
# Load the data from the API into a DataFrame
df = pd.DataFrame(response.json()["data"])
# Calculate the average course of the vessel
average_course = df["course"].mean()
# Print the result
print(f"Average course: {average_course} degrees")
Calculate the total distance traveled by the vessel:
import pandas as pd
# Load the data from the API into a DataFrame
df = pd.DataFrame(response.json()["data"])
# Calculate the total distance traveled by the vessel
total_distance = df["distance"].sum()
# Print the result
print(f"Total distance traveled: {total_distance} nautical miles")
Plot the vessel's location on a map using the latitude and longitude data:
import pandas as pd
import matplotlib.pyplot as plt
# Load the data from the API into a DataFrame
df = pd.DataFrame(response.json()["data"])
# Set the figure size
plt.figure(figsize=(10, 10))
# Plot the vessel's location on a map
plt.scatter(df["lon"], df["lat"], marker="o")
# Add a title and axes labels
plt.title("Vessel Location")
plt.xlabel("Longitude")
plt.ylabel("Latitude")
# Show the plot
plt.show() 

Examples of projects where you can use maritime API and Python:

The Maritime API and Python can be useful in a variety of projects involving maritime data analytics. Some examples of projects where this technology might be useful include:

– Optimizing vessel routes to reduce fuel consumption and emissions

– Analyzing cargo flows to identify trends and forecast demand

– Monitoring vessel movements to improve safety at sea

– Analyzing weather patterns to inform decision-making in the shipping industry

Read Other Maritime Articles 

Flight API

Flight API

Access real-time, historical and future dates flight API to get flight status, location, delay, route data and more. Flight APIs of Aviation Edge are useful data tools for developers with global flight data and uptime rates of 99.9%.   Flight Tracking: Keep tabs...

Flight Tracker API

Flight Tracker API

Get real-time aircraft location and speed data with Aviation Edge Flight Tracker API. The flight tracking data allows you to monitor an aircraft’s real-time location, speed, and status throughout its route. The API uses REST which allows a clear and simple integration...

Flight Schedules API

Flight Schedules API

Flight Schedules API developed by Aviation Edge provides real-time, historical and future airport timetable data. Track departure and arrival schedules of airports worldwide. Alternatively, use an airline filter to track an airline’s flight schedule.   Flight number  ...

Why the Maritime API is a Must-Have?

Why the Maritime API is a Must-Have?

Maritime API, a revolution in the shipping and logistics sector, have transformed the way businesses in this industry operate. They grant access to a wealth of data about ships, their journeys, and intricate details that can be leveraged to streamline operations....

Find Ships In New York Port

Find Ships In New York Port

Finding ships in the New York Port is akin to tapping into the pulse of global commerce. Serving as the eastern gateway to the U.S., the NYC port is a bustling hub of trade. Its terminals show how important it is for worldwide links. In this article, we will identify...

Find 15 Major Maritime Ports in the World with API

Find 15 Major Maritime Ports in the World with API

Find major ports with API: In the world of maritime transportation, ports play a crucial role as key hubs for global trade and commerce. Identifying the major ports is essential for various stakeholders, including shipping companies, logistics providers, and port...