MarineCadastre.gov also offer a Track Builder script that creates lines out of the broadcast points. (It can also join additional information from the vessel and voyage layers.) We could reproduce the line creation step using tools such as Processing’s Point to path but this post will show how to create PostGIS trajectories instead.
First, we have to import the points into PostGIS using either DB Manager or Processing’s Import into PostGIS tool:
Then we can create the trajectories. I’ve opted to create a materialized view:
The first part of the query creates a temporary table called ptm (short for PointM). This step adds time stamp information to each point. The second part of the query then aggregates these PointMs into trajectories of type LineStringM.
CREATE MATERIALIZED VIEW ais.trajectory AS WITH ptm AS ( SELECT b.mmsi, st_makepointm( st_x(b.geom), st_y(b.geom), date_part('epoch', b.basedatetime) ) AS pt, b.basedatetime t FROM ais.broadcast b ORDER BY mmsi, basedatetime ) SELECT row_number() OVER () AS id, st_makeline(ptm.pt) AS st_makeline, ptm.mmsi, min(ptm.t) AS min_t, max(ptm.t) AS max_t FROM ptm GROUP BY ptm.mmsi WITH DATA;
The trajectory start and end times (min_t and max_t) are optional but they can help speed up future queries.
One of the advantages of creating trajectory lines is that they render many times faster than the original points.
Of course, we end up with some artifacts at the border of the dataset extent. (Files are split by UTM zone.) Trajectories connect the last known position before the vessel left the observed area with the position of reentry. This results, for example, in vertical lines which you can see in the bottom left corner of the above screenshot.
We can also perform trajectory analysis, such as trajectory generalization:
This is a first proof of concept. It would be great to have a script that automatically fetches the datasets for a specified time frame and list of UTM zones and loads them into PostGIS for further processing. In addition, it would be great to also make use of the information in the vessel and voyage tables, thus splitting up trajectories into individual voyages.
This post is part of a series. Read more about movement data in GIS.