QGIS Density Analysis Plugin

This plugin adds additional density heatmap algorithms to QGIS including geohash, H3, styled heatmap, and polygon density maps. It adds a vector density heatmap explorer to examine hotspot areas of greatest concentrations. It wraps the QGIS Heatmap (Kernel Density Estimation) algorithm into a new version that automatically styles the layer and allows the user to specify the cell size in various units of measure and not just the units of the layer's CRS. It provides an algorithm to create a raster density map of polygons. It has processing algorithms to create a gradient style, random style, and raster pseudocolor style so that they can be used in QGIS models. Another tool allows a copied style or a .qml file to be pasted onto all selected layers. Once installed, the plugin is located under Plugins->Density analysis in the QGIS menu, on the toolbar, and can be found in the Processing Toolbox under Density analysis. This shows plugin from the main menu with Geohash density algorithms expanded.

Density Analysis

This shows the expanded H3 density algorithms.

Density Analysis

Note that several algorithms in this plugin use H3 (Hexagonal hierarchical geospatial indexing system). This is an incredibly fast algorithm for generating hexagon density maps, but requires installation of the H3 python library. The H3 package can be installed by running the OSGeo4W shell as system administrator and running 'pip install h3' or whatever method you use to install python packages. If H3 is not installed, the rest of the algorithms will still work, but you will not be able to run the H3 algorithms. In one test using the QGIS Create grid processing algorithm, followed by Count points in a polygon algorithm took 63.18 seconds to process spatially indexed point data. To do the same thing with H3 only took 3.79 seconds.

Many of the default parameters in the algorithms can be set from the settings dialog found in Plugins->Density analysis->Settings. This allows the user to customize these settings one time.

Random style Styled density map

Given point features, this will create a rectangle, diamond, or hexagon grid histogram of points that occur in each polygon grid cell. This algorithm uses the QGIS Count points in polygon algorithm which is fairly time intensive even though it has been masterfully implemented in core QGIS and significantly beats the speed implemented in commercial software. To optimize the speed make sure your input data is spatially indexed; otherwise, this algorithm will be painfully slow. The advantage to this algorithm is that it gives the most control over the size of the polygon grid cells. If speed is more important then use Styled geohash density map or Styled H3 density map algorithm. Both of these use geohash indexing to count points in each geohash grid cell and are very fast. The former creates a square or rectangular grid and H3 creates a hexagon grid. For H3 support, the H3 library needs to be installed in QGIS. The disadvantage of these geohash density maps is that they have fixed resolutions and you cannot choose anything in between, but this is also what makes them fast.

Here is an example of crime in Chicago. Each point on the left is a criminal event. On the right is a hexagon heatmap counting the number of events in each cell and displaying it as a heatmap. The darker the red, the more crime is in that area.

Chicago crime

This shows the parameters used in the algorithm.

Density Algorithm

These are the basic input parameters:

These are the Advanced Parameters.

Geohash density algorithms

There are four geohash density algorithm variations. Two are automatically styled and two of them work with multiple vector layers.

Styled geohash density map Styled geohash density map

This algorithm iterates through every point indexing them using a geohash with a count of the number of times each geohash has been seen. The bounds of each geohash cell is then created as a polygon. Depending on the resolution these polygons are either a square or rectangle. Here is an example.

Geohash Density Map

This shows the algorithm's dialog.

Geohash Density Map Algorithm

These are the input parameters:

Resolution
Level
Approximate
Dimensions
1 ≤ 5,000km X 5,000km
2 ≤ 1,250km X 625km
3 ≤ 156km X 156km
4 ≤ 39.1km X 19.5km
5 ≤ 4.89km X 4.89km
6 ≤ 1.22km X 0.61km
7 ≤ 153m X 153m
8 ≤ 38.2m X 19.1m
9 ≤ 4.77m X 4.77m
10 ≤ 1.19m X 0.596m
11 ≤ 149mm X 149mm
12 ≤ 37.2mm X 18.6mm

These are the Advanced Parameters.

The output geohash table contains a unique identifier ID, geoshash string GEOHASH and the count of number of points or weighted count of the number points within the geohash cell NUMPOINTS.

Geohash attributes

Geohash density grid Geohash density grid

This is the same as Styled geohash density map, but without the styling. The algorithm iterates through every point of the input layer indexing them using a geohash with a count of the number of times each geohash has been seen and saved to the NUMPOINTS attribute. The bounds of each geohash cell is then created as a polygon. Depending on the resolution these polygons are either a square or rectangle.

Geohash Density Grid Algorithm

Styled geohash multi-layer density map Styled geohash multi-layer density map

This is the same as the Styled geohash density map algorithm with the exception that it supports multiple input point vector layers which contribute to the output density map. If a Weight field is used, then all selected layers must contain the same weight attribute field. To select the input layers, click on the "..." button on the right and it will give a dialog with all of the point vector layers that are available to be included in the output density map.

Multi-layer geohash

Geohash multi-layer density grid Geohash multi-layer density grid

This is the same as Styled multi-layer geohash density map, but without the styling. The algorithm iterates through every selected point vector layer and every point within the layer, indexing them using a geohash with a count of the number of times each geohash has been seen. The bounds of each geohash cell is then created as a polygon. Depending on the resolution these polygons are either a square or rectangle.

H3 density algorithms

There are four H3 density algorithm variations. Two are automatically styled and two of them work with multiple vector layers.

These algorithms use the H3 (Hexagonal hierarchical geospatial indexing system) library for fast density map generation. They iterate through every point using H3 indexing with a count of the number of times each H3 index has been seen. Each H3 cell is then created as a polygon. The polygons are in a hexagon shape.

To create H3 density maps you will need to install the H3 Library (https://h3geo.org/). The H3 package can be installed by running the OSGeo4W shell as system administrator and running 'pip install h3' or whatever method you use to install python packages. The H3 algorithms will give a warning message if H3 has not been installed.

H3 density map Styled H3 density map

This algorithm generates a styled H3 density map from a point vector layer.

Here is an example.

H3 Density Map

This shows the algorithm dialog.

H3 Density Map Algorithm

The parameters are as follows:

Resolution
Level
Average Hexagon
Edge Length
0 1107.71 km
1 418.68 km
2 158.24 km
3 59.81 km
4 22.61 km
5 8.54 km
6 3.23 km
7 1.22 km
8 461.35 m
9 174.38 m
10 65.91 m
11 24.91 m
12 9.42 m
13 3.56 m
14 1.35 m
15 0.51 m

These are the Advanced Parameters.

H3 density map H3 density grid

This is the same as Styled H3 density map, but without the styling.

H3 Density Grid Algorithm

Styled H3 multi-layer density map Styled H3 multi-layer density map

This is the same as the Styled H3 density map algorithm with the exception that it supports multiple input point vector layers which contribute to the output density map. If a Weight field is used, then all selected layers must contain the same weight attribute field. To select the input layers, click on the "..." button on the right and it will give a dialog with all of the point vector layers that are available to be included in the output density map.

Styled H3 multi-layer density map Algorithm

H3 multi-layer density grid H3 multi-layer density grid

This is the same as Styled multi-layer H3 density map, but without the styling. The algorithm iterates through every selected vector layer and every point within the layer, indexing them using a H3 geohash index with a count of the number of times each index has been seen. The bounds of each geohash index cell is then created as a polygon.

H3 grid H3 grid

This will create a grid of H3 polygons based on the extent of a layer, canvas, or user drawn extent. H3 Resolution is a value between 0 and 15 specifying the resolution of the H3 grid.

Density explorer tool Density map analysis tool

With this tool you can quickly look at the top scoring values. Select the original point layer and the density heatmap polygon layer generated by the above algorithms. ID should be set to a unique identifier, and Score should be set to the histogram count attribute. If the selected heatmap vector layer has the attributes id and NUMPOINTS, these will be automatically selected.

Heatmap density analysis

Once the parameters have been set, click on Display Density Values and the top scores will be listed. If you click on any of entries only that grid cell will be display. A drop down set of actions selects what happens when clicking on one or more of the score entries.

You can then examine the features within the grid cell. Here is an example view.

Example

Styled heatmap Styled heatmap (Kernel density estimation)

This algorithm is a wrapper for the native QGIS Heatmap (Kernel Density Estimation) algorithm, but adds automatic styling and simplifies specifying the pixel/grid size of the output image. The user specifies the measurement unit such as kilometers, meters, etc. rather than having to know the units used for the CRS. The algorithm creates a density heatmap raster image. The output image size will be based on the Cell/pixel dimension in measurement units parameter and bounding box of the input vector layer. If either dimension of the output image exceeds Maximum width of height dimensions of output image, then an error will be generated and the user will need to either increase Cell/pixel dimension in measurement units or Maximum width or height dimensions of output image.

Styled KDE Heatmap

The following are the main parameters.

These are Advanced Parameters.

Styled Polygon density map Styled polygon density map

Like the Styled Heatmap, this algorithm uses a raster image to accumulate the summation of rasterized polygon layers and then automatically style the results. Here is an example of the result of summing a cluster of polygons.

Styled polygon density map

The parameters in dialog box are as follows:

Styled polygon density map

These are the Advanced Parameters.

Polygon density map Polygon density map

This is the same as the Styled density map, but without the styling. It uses a raster image to accumulate the summation of rasterized polygon layers. Here is an example of the result of summing a cluster of polygons.

Polygon density map

The parameters in dialog box are as follows:

Polygon density map

Apply style to selected layers Apply style to selected layers

QGIS lacks a function to paste a style to more than one layer so this tool was developed to fix that lack in capability. If you have a .qml style or have a style copied on the clipboard you can apply it to all the selected layers.

Apply style to selected layers

When pasting a graduated style the symbol class values are preserved unless Automatically reclassify graduated layers is checked. When checked, each layer's minimum and maximum are evaluated along with the graduated mode to reclassify the values.

Applying graduated and random categorized styles

The purpose of these two algorithms, is to set random and graduated styles using an algorithm. This makes it possible to set a layer's style in the model builder.

Apply a pseudocolor raster style Apply a pseudocolor raster style

Pseudocolor raster style dialog

This achieves some of the functionality you get from right-mouse clicking on a single band image and selecting properties and selecting the Symbology tab and choosing Singleband pseudocolor for the *Render type. For more information on the parameters visit the QGIS documentation. These are the settings.

Settings Settings

The settings are found in the QGIS menu under Plugins->Density Analysis->Settings. These settings allow the user to set default values for some of the most common parameters in the algorithms. This setting is persistent from one launch of QGIS to the next. If the user is consistently using a certain color ramp or a certain cell density, these default parameters can be set and save time later on when then algorithms are run. Here are the default settings:

Settings dialog box