# Colour GPS track by speed

I have a CSV file with WGS84 trackpoints and visualizing them with QGIS textplugin works fine. The log interval is 1sec and I like to get the current speed to get a visual feedback, how fast the vehicle was between two points (so distance/time). Any idea how I can realize this with a style?

I can imagine a solution using a SpatiaLite database provided each trackpoint has some sequential id number. First import your CSV file into spatialite (plenty of sources available to explain this step) then make it a spatial layer: (I assume the CSV has columns longitude, latitude for the GPS locations, and a column 'id' with the sequential numbering of the trackpoints)

``````SELECT AddGeometryColumn('trackpoints','geometry',4326,'POINT','XY');
UPDATE trackpoints SET geometry=MakePoint(longitude, latitude, 4326);
``````

Now you have a spatial point layer with each point numbered along the track. Add a column to contain the distance from each point to the next one:

``````ALTER TABLE trackpoints ADD COLUMN dist real;
``````

Now to get the distance between each pair of points, make a line segment from a pair and use the GeodesicLenght() function to get distance in meters. Here's how it would be done:

``````UPDATE trackpoints SET dist=(
SELECT GeodesicDistance(MakeLine(trackpoints.geometry, tp1.geometry))
FROM trackpoints AS tp1
WHERE tp1.id = trackpoints.id+1;
``````

Now the dist column should contain for each point the distance to the next. Finally. divide dist by the time interval between points, and you should have the speed...

• Thanks Micha! I just expected that there is an buildin feature as most GPX viewers have. I will try your approach! Commented Oct 16, 2013 at 18:40

The Trajectools plugin for QGIS provides a tool to create trajectories from points:

Using geometry generators, the resulting LinestringM features can be styled according to the local speed:

The GPX Segment Importer plugin will take a set of points, and convert them to a set of line segments with various statistics, including speed. Use the Create Track Segments function from the processing toolbox.

You can then style the line segments with a Graduated Style.

GPS files can be somewhat erratic in terms of speed. I spent some time trying to smooth the raw speed using a moving average. After much trial and error, I used Field Calculator to add a field to the layer using the following formula. Replace the with your layer. The value 3 will average using 3 segments either side of the current segment ie 7 in total. It can be adjusted to average over a smaller or larger range.

``````aggregate(<LayerId>,'mean',expression:="_speed",filter:=abs("_b_index"-attribute(@parent,'_b_index')) <= 3)
``````

[Techically you should sum the distances and divide by the sum of the times, rather than simply averaging speeds, but for my purposes, the above was sufficient. The accurate version can be done using a more complex version of the above formula]