Use Grafana to visualize geospatial data stored in TimescaleDB

To complete this tutorial, you need a cursory knowledge of the Structured Query Language (SQL). The tutorial walks you through each SQL command, but it is helpful if you’ve seen SQL before.

Once your installation of TimescaleDB and Grafana are complete, ingest the data found in the NYC Taxi Cab tutorial and configure Grafana to connect to that database. Be sure to follow the full tutorial if you’re interested in background on how to use TimescaleDB.

tip

Be sure to pay close attention to the geospatial query portion of the tutorial and complete those steps.

The NYC Taxi Cab data also contains the location of each ride pickup. In the tutorial, we examined rides that originated near Times Square. Let’s build on that query and visualize rides whose distance traveled was greater than five miles in Manhattan.

We can do this in Grafana using the ‘Worldmap Panel’. Start by creating a new panel, selecting ‘New Visualization’, and selecting the ‘Worldmap Panel’.

Once again, you can edit the query directly. In the Query screen, be sure to select your NYC Taxicab Data as the data source. In the ‘Format as’ dropdown, select ‘Table’. Click on ‘Edit SQL’ and enter the following query in the text window:

In the second and third lines of the SELECT statement, we are using the pickup_longitude and pickup_latitude fields in the database and mapping them to variables longitude and , respectively.

In the WHERE clause, we are applying a geospatial boundary to look for trips within 2000m of Times Square.

Finally, in the GROUP BY clause, we supply the trip_distance and location variables so that Grafana can plot data properly.

warning

This query may take a while, depending on the speed of your Internet connection. This is why we’re using the LIMIT statement for demonstration purposes.

Now let’s configure our Worldmap visualization. Select the ‘Visualization’ tab in the far left of the Grafana user interface. You’ll see options for ‘Map Visual Options’, ‘Map Data Options’, and more.

First, make sure the ‘Map Data Options’ are set to ‘table’ and ‘current’. Then in the ‘Field Mappings’ section. Set the ‘Table Query Format’ to be ‘Table’. We can map the ‘Latitude Field’ to our latitude variable, the ‘Longitude Field’ to our variable, and the ‘Metric’ field to our value variable.

In the ‘Threshold Options’ set the ‘Thresholds’ to ‘2,5,10’. This auto configures a set of colors. Any plot whose value is below 2 is a color, any value between 2 and 5 is another color, any value between 5 and 10 is a third color, and any value over 10 is a fourth color.

Your configuration should look like this:

At this point, data should be flowing into our Worldmap visualization, like so:

Visualizing time series data in PostgreSQL using the Grafana Worldmap

You should be able to edit the time filter at the top of your visualization to see trip pickup data for different timeframes.