I am working on a script that automatically generates heatmaps from point data. I am aware of the heatmap/KDE tool already existing in PyQGIS (I am working in QGIS 3.10). What I want to do after computing the heatmap is to reclassify the data into five different classes like so:
Instead of this:
To do so, a reclassify is required. This is doable with the reclassify tool in QGIS, however, you must insert the reclass values yourself. This is not desirable when creating a 'one size fits all' script, as one would need to generate the min and max values and intervals for each class over and over again. In ArcPy, the slice tool can 'slice' the heatmap into different sections according to for example the 'natural breaks' standard.
Is there a good way to do so in PyQGIS?
Note: The points where the heatmap is created with include values, which should be taken into account when creating the heatmap.
ps. I got it to work with quantiles with the help of numpy. The result looks like this:
import numpy as np from osgeo import gdal, gdal_array dataset = gdal.Open("path/to/rasterlayer.sdat") array = dataset.ReadAsArray() output = np.zeros_like(array).astype(np.uint8) percentile_80 = np.percentile(array, 80) percentile_60 = np.percentile(array, 60) percentile_40 = np.percentile(array, 40) percentile_20 = np.percentile(array, 20) percentile_0 = np.percentile(array, 0) output = np.where((array > percentile_0), 1, output) output = np.where((array > percentile_20), 2, output) output = np.where((array > percentile_40), 3, output) output = np.where((array > percentile_60), 4, output) output = np.where((array > percentile_80), 5, output) outname = ("path/to/rasterlayer_percentiles.tif") gdal_array.SaveArray(output, outname, "gtiff", prototype=dataset)
Would there be a similar way to do this, but then with natural breaks?