• Blog posts

    Bio

    by David Crowther

    Geoserver_Heatmaps_01.png

    Question:

    Using GeoServer is it possible to create HeatMaps

    Answer:

    Yes, utilising the power of Style Layer Descriptors (SLDs) you can publish individual Point Geometry as HeatMaps by Transforming Vector geometry into Raster Images.

     Geoserver_Heatmaps_02.jpg

    Firstly, in GeoServer you will need to setup a DATA SOURCE connection to your source data and then publish that as a WMS layer. Here I am publishing a Crime Table from my PostGIS database and applying the generic Point STYLE.

    Geoserver_Heatmaps_03.jpg

    In a previous FAQ I explored how to create Staked Points to cluster the Crime Points to show High locations of Crime.

    Geoserver_Heatmaps_04.jpg

    This is a high impact WMS layer, however it would be really great to show these Crime Locations as Hotspots using a Heatmap – something commonly done within desktop GIS.

    After doing a little research I found a link on Stack Exchange with the sample SLD code that would create a Heatmap where individual Points are clustered together – https://docs.geoserver.org/latest/en/user/styling/sld/extensions/rendering-transform.html

    At its core this SLD performs a Render Transformation to aggregate input Vector data to generate a Raster output image. Other Transformations include:

    Geoserver_Heatmaps_05.jpg

    The online resource suggested that I would need to install a WPS extension…. Although I simply copied the SLD provided without needing to install the extension in my GeoServer and it worked for me!... the steps to do this are outlined below.

    In my GeoServer Instance I created a New Style FileLiverpool Crime Heatmap

    Geoserver_Heatmaps_06.jpg

    .. and copied in the sample SLD code from the Stack Exchange link.

    Geoserver_Heatmaps_07.jpg

    Once I then applied the new SLD file to my WMS layer, I was then able to show a Heatmap for my Crime data…

    Geoserver_Heatmaps_08.jpg

    … this was very exciting as it was the first time I had been able to create a Heatmap WMS from underlying point data.

    Looking at the SLD in more detail at its heart it performs a Transformation to use the underlying Vector dataset (Crimes) to create a Raster Image.

    Geoserver_Heatmaps_09.jpg

    It then has a Rule that uses a Raster Symbolizer to then Colour each pixel that has a value – Yellow, Red and Blue. In addition, the top two Colour Map Entries find pixels that are 0.02 or 0 and applies a white colour with full OPACITY.

    Geoserver_Heatmaps_10.jpg

    NB: Without these Colour Map Entries you will colour all Raster Pixels with a colour which will mask the whole image.

    Geoserver_Heatmaps_11.jpg

    Keeping all Colour Map Entries in and applying the SLD then Transforms the Crime Points into a Raster Heatmap, with the three colours applied and the low/no crime areas opaque.

    Geoserver_Heatmaps_12.jpg

    Geoserver_Heatmaps_13.jpg

    Feeling confident I then started to tinker a little!

    The changes I wanted to make included:

    • Swapping the Red and Yellow Ranges to show areas of highest crime with the Red colour
    • Adding in two extra bands with an Amber and a Green Colour
    • Editing the Legend to apply better textual values to explain the banding

    1 - Swapping the Red and Yellow colours simply involved changing the HEX Colour for the bottom two bands.

    Geoserver_Heatmaps_14.jpg

    Geoserver_Heatmaps_15.jpg

    2 - Adding the Amber and Green Bands was a simple Copy and Paste to create two new bands and then edit each band with the Orange and Green Colour Hex values.

    Geoserver_Heatmaps_16.jpg

    Geoserver_Heatmaps_17.jpg

    3 – I then edited the SLD to rename the Legend Values to replace the words ‘Values’ with High Crime or Low Crime. In addition, I removed the 0.02 band so that the legend looked a little clearer when shown in a Client application e.g. desktop GIS (QGIS) and webGIS (MapThat).

    Geoserver_Heatmaps_18.jpg

    Geoserver_Heatmaps_19.jpg

    Finally, I used this new Crime Heatmaps WMS within my client webGIS – MapThat – to create a high impact Crime Hotspot Map for my Users.

    Geoserver_Heatmaps_20.jpg

    As you zoom in the SLD then recalculates the Heatmap on the fly

    Geoserver_Heatmaps_21.jpg

    Geoserver_Heatmaps_22.jpg

    .. and now shown with the Crime Stacked Points WMS we have two high impact WMS Layers allowing our Users to better interpret Crime Patterns across the whole area.

    Geoserver_Heatmaps_23.jpg