Elevation Sparklines

Elevation sparklines of walks on BeyondTracks
Elevation sparklines of walks on BeyondTracks

Sparklines are a lightweight chart and are well suited at showing at a glance what the elevation profile of a bushwalk is like.

The graphic above shows the elevation sparklines of all warks on BeyondTracks, if you look long enough you’ll see most walks are either:

We use vue-trend to draw the sparklines, it’s easy to use.

On the data side we have PostGIS LINESTRING geometries with a z coordinate for elevation (more on that in later post), and use this simple function to generate a PostgreSQL ARRAY of elevation values for a fixed number of points, exactly what we need to pass to vue-trend.

-- create an elevation profile as an array based on a fixed number of points
CREATE OR REPLACE FUNCTION elevation_profile(geom geometry, num_points integer) RETURNS integer[] AS $$
DECLARE
    profile integer[] DEFAULT '{}';
BEGIN
    FOR i in 0..(num_points - 1) LOOP
        profile := array_append(profile, round(ST_Z(ST_LineInterpolatePoint(geom, i::double precision / (num_points - 1))))::integer);
    END LOOP;

    RETURN profile;
END;
$$ LANGUAGE plpgsql;