Spatial Kernel Density Esimation
Create Spatial Kernel Density / Heatmap raster from point based vector data, à la QGIS / ArcGIS.
Creates a kernel density (heatmap) raster from vector point data using kernel density estimation. The density is calculated based on the number of points in a location, with larger numbers of clustered points resulting in larger values, and points can be optionally weighted. Kernel Density / Heatmaps allow easy for identification of hotspots and clustering of points. This implementation provides an equivalent to QGIS' Heatmap and ArcGIS/ArcMap/ArcPro's Kernel Density spatial analyst function. Note that any distance calculations are planar, therefore care should be taken when using points over large areas that are in a geographic coordinate system.
The implementation of kernel density uses the Quartic kernel for it's estimates, with the methodology implemented following QGIS and as described in ArcGIS' documentation explaining how Kernel Density works. There are many alternative kernel density estimate (KDE) functions available in popular python libraries that may offer better performance, for example scipy, scikit-learn, KDEpy etc., though these alternatives may not implement the Quartic kernel (with optional weights), as is typically found in GIS software. Additionally, performance with this package will be reduced compared to the native C++ implementaion in QGIS.
Installation
The package can be installed from pip:
pip install spatial-kde
Usage
After installation, the skde tool is available from the command line with the following usage:
Usage: skde [OPTIONS] VECTOR OUTPUT
Create a Spatial Kernel Density / Heatmap raster from an input vector.
The input vector file must be readable by GeoPandas and contain Point type
geometry (for non-point geometries the centroid will be used for the KDE).
Arguments:
VECTOR Path to input vector file [required]
OUTPUT Output path for created raster [required]
Options:
--radius FLOAT Radius/Bandwith for the KDE. Same units as
the CRS of `vector`. [default: 1]
--output-pixel-size FLOAT Output pixel size (resolution). Same units
as the CRS of `vector`. [default: 1]
--output-driver TEXT Output driver (file format) used by rasterio
(Default = GeoTiff). [default: GTiff]
--weight-field TEXT Optional field in `vector` containing
weights of each point.
--scaled / --no-scaled Set to True to scale the KDE values, leave
false to use raw values. [default: no-
scaled]
Alternatively, the spatial_kernel_density function can be used in python:
from typing import Optional
import geopandas as gpd
from spatial_kde import spatial_kernel_density
spatial_kernel_density(
points: gpd.GeoDataFrame = gdf,
radius: float = 1.0,
output_path: str = "/output/path.tif",
output_pixel_size: float = 1.0,
output_driver: str = "GTiff",
weight_col: Optional[str] = None,
scaled: bool = False,
)
"""Calculate Kernel Density / heatmap from ``points``
.. note:: Distance calculations are planar so care should be taken with data
that is in geographic coordinate systems
Parameters
----------
points : gpd.GeoDataFrame
Input GeoDataFrame of points to generate a KDE from
radius : float
Radius of KDE, same units as the coordinate reference system of ``points``
Sometimes referred to as search radius or bandwidth
output_path : str
Path to write output raster to
output_pixel_size : float
Output cell/pixel size of the created array. Same units as the coordinate
reference system of ``points``
output_driver : str
Output format (driver) used to create image. See also
https://rasterio.readthedocs.io/en/latest/api/rasterio.drivers.html
weight_col : Optional[str], optional
A column in ``points`` to weight the kernel density by, any points that
are NaN in this field will not contribute to the KDE.
If None, the all points will have uniform weight of 1.
scaled : bool
If True will output mathematically scaled values, else will output raw
values.
"""
Development
Prequisites:
The Makefile includes helpful commands setting a development environment, get started by installing the package into a new environment and setting up pre-commit by running make install. Run make help to see additional available commands (e.g. linting, testing and so on).
- Pytest is used for testing the application (see
/tests). - Code is linted using flake8
- Code formatting is validated using Black
- pre-commit is used to run these checks locally before files are pushed to git
- The Github Actions pipeline runs these checks and tests
TODO
- Github actions pipeline runs on a matrix of python versions
- Documentation (e.g. mkdocs, read-the-docs w/ sphinx or similar)
- Tooling for managing versioning/releasing (e.g. bump2version)
- Makefile commands for releasing to (test) pypi
- Support geodesic distance calculation
- Performance improvements
Metadata
Release files for spatial-kde 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| spatial-kde-0.1.0.tar.gz | 8.5 kB | Without provenance | Details for spatial-kde-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| spatial_kde-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for spatial_kde-0.1.0-py3-none-any.whl |
Total release size: 18.6 kB
Release files / spatial-kde-0.1.0.tar.gz
| Download URL | spatial-kde-0.1.0.tar.gz |
|---|---|
| Size | 8.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
322f0be0b4eb69c8df0497657f4731938b832d435ddecfbeaec76742ff5f61ae
|
|
BLAKE2b-256 checksum How to use checksums |
de1f5b35c2f2d2340a68a627d5959e4fb0febd8272e19900484dc10e620da520
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.12 CPython/3.9.10 Linux/5.15.15-76051515-generic
|
Release files / spatial_kde-0.1.0-py3-none-any.whl
| Download URL | spatial_kde-0.1.0-py3-none-any.whl |
|---|---|
| Size | 10.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e46ff3f6e47044b5917e14594ebe940c60867a9f5c1e2687c2a1239702236448
|
|
BLAKE2b-256 checksum How to use checksums |
40219e2676b1a610a4a9597e60624888e83a2029bd8db8d1fed45d4e3ba059a4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.12 CPython/3.9.10 Linux/5.15.15-76051515-generic
|
Security for spatial-kde 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.