-1

Can AIS marine traffic live vessel data be integrated into QGIS?

I understand this could be possible using an OpenCPN plugin and Grass.

1

Sorry for this late answer but want to share this if you still need it. I used Vesselfinder, fairly the same to MarineTraffic, and I think I accomplished what you are trying to achieve.

I used a python script (with psycopg2 extension) that received the information via an API from Vesselfinder. The script saved the data in a PostgreSQL database (with PostGIS extension) and in QGIS a PostgreSQL/PostGIS database is accessible via the database manager. Every time the script runs (i used a Loop Bat. file, to let it run every 5 min) the data is updated en immediately visible in QGIS.

this is the python code to get data and store it in a db.

    # Import libraries
    import requests
    import pprint
    import json
    import sys
    import psycopg2
    import datetime

    # Make database connection
    conn = psycopg2.connect("dbname=YOURDBNAME user=YOURUSERNAME password=YOURPASSWORD")

    # Get cursor
    cur = conn.cursor()

    # Define headers
    headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
           'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
           'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
           'Accept-Encoding': 'none',
           'Accept-Language': 'en-US,en;q=0.8',
           'Connection': 'keep-alive'}

    # Build URL
    url = 'YOUR VESSELFINDER/MARINETRAFFIC API URL

    # Execute HTTP request
    print('Get vessel positions from ' + str(url))
    response = requests.get(url, headers=headers)

    # Get result and store in database (point in 4326, convert date 2019-02-01 06:29:43 UTC)
    # Update scheepstrack
    if response.status_code == 200 :
        print ('Request OK')
        if len(response.json()) > 0 :
            i = 0
            while i < len(response.json()):
                YOURCOLLUMNAME = str(response.json()[i]['AIS']['NAME'])
                 YOURCOLLUMNAME = int(response.json()[i]['AIS']['MMSI'])
                 YOURCOLLUMNAME = float(response.json()[i]['AIS']['LATITUDE'])
                 YOURCOLLUMNAME = float(response.json()[i]['AIS']['LONGITUDE'])
                 YOURCOLLUMNAME = datetime.datetime.strptime(str(response.json()[i]['AIS']['TIMESTAMP'][:-4]), '%Y-%m-%d %H:%M:%S')

# Insert new position
            insert_stmt = 'insert into YOURDBNAME ( YOURCOLLUMNAME, YOURCOLLUMNAME, "YOURCOLLUMNAME", "YOURCOLLUMNAME" ) values ( %s, %s, ST_SetSRID(ST_MakePoint(%s, %s),4326), %s )' 
            cur.execute ( insert_stmt, ( YOURCOLLUMNAME, YOURCOLLUMNAME, YOURCOLLUMNAME, YOURCOLLUMNAME, YOURCOLLUMNAME ) )

This will create a real-time feed of you vessel(s) Hope this is still to some use to you or somebody else that reads this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.