Skip to main content

distance-rasters

Generate distance raster using arbitrary sets of spatial features

build badge Coverage Status Downloads

Distance-rasters was designed to support the generation of rasters in which each pixel is the distance to a discrete set of user defined locations. Common uses include calculating the distances to roads, water features, or cities. Distance-rasters is flexible and can support a wide range of use cases and input data, and provides the tools necessary for rasterizing vector features.

The most notable existing tool for calculating raster distance or proximity is GDAL's gdal_proximity.py which while powerful, is not flexible enough for many use cases and has practical limitations when integrating distance calculations into broader workflows. Distance-rasters offers a simple, flexible, and Pythonic alternative.

See the Example section below for a simple example of using distance-rasters. Distance-rasters is extremely fast for small areas at moderate resolution, and has performed well when tested at global scales with moderate-coarse resolution (1km).

Example

The below example calculates the distance to a rough approximation of a section of the Niger river in Mali. See the examples folder for the code and data.

import fiona
import distancerasters as dr

# load vector data (crs = epsg:4326)
shp = fiona.open("examples/linestrings.geojson", "r")

# resolution (in units matching projection) at which vector data will be rasterized
pixel_size = 0.01

# rasterize vector data and output to geotiff
rv_array, affine = dr.rasterize(shp, pixel_size=pixel_size, bounds=shp.bounds, output="examples/linestrings_rasterized_binary.tif")

"""
# you can manually export rasterized vector data if needed

dr.export_raster(rv_array, affine, "linestrings_rasterized_binary.tif")


# you can also load an existing raster directly to use for distance calculations

import rasterio

with rasterio.open("examples/linestrings_rasterized_binary.tif") as src:
    affine = src.transform
    rv_array = src.read(1)

"""

# function to define which cells from rasterized input to calculate distance to
#   - this is the default function, and does not need to be explicity passed to class
#   - this would be modified if using a non-binary rasterization
def raster_conditional(rarray):
    return (rarray == 1)

# generate distance array and output to geotiff
my_dr = dr.DistanceRaster(rv_array, affine=affine,
                          output_path="examples/linestrings_distance_raster.tif",
                          conditional=raster_conditional)

# dist_array = my_dr.dist_array

# Output:
#
# Tree build time: 0.0330 seconds
# Building distance array...
# Total run time: 2.88 seconds

The resulting distance raster indicates the distance to the line segment. In the image below, areas close to the line segment (in dark blue) are yellow and get darker the further away you go.

Stylized Example Result

An additional example is included in examples/adv_rasterization_example.py to demonstrate how multiple sets of vector data can be combined to create a distance raster. This example also illustrates how the area for which distances are calculated can be adjusted based on a user's needs.

Using GeoPandas

You can also use GeoPandas GeoDataFrames directly with distance-rasters:

import geopandas as gpd
import distancerasters as dr

# load vector data as GeoDataFrame
gdf = gpd.read_file("examples/linestrings.geojson")

# rasterize the GeoDataFrame
rv_array, affine = dr.rasterize(gdf, pixel_size=0.01, bounds=gdf.total_bounds)

# generate distance raster
my_dr = dr.DistanceRaster(rv_array, affine=affine)

Note: GeoPandas is not installed by default. Install it separately with pip install geopandas.

Usage Tips & Information

  • Distance-rasters primarily uses an implementation of the Haversine formula to calculate distance in kilometers
  • For the best results, convert your data to EPSG 4326 (i.e., "WGS84") before using distance-rasters. Performing this conversion prior to using distance-rasters allows the package to remain lightweight and return results fast and accurately.
  • If you do not want to convert your data to EPSG 4326, or are using non-geographic data already in a raster/array format (i.e., an abritrary grid) you can pass the data to the DistanceRaster class without an affine or output_path argument in order to use a simple Euclidean/index based distance calculation.

Installation

Using pip

The latest version of distance-rasters is available on PyPi, and can be installed with Pip:

pip install distancerasters

If you'd like to install the latest development (alpha) release, there may be a newer version on TestPyPi:

pip install -i https://test.pypi.org/simple/ distancerasters

From source

To install this package from source, first clone this repository, then use pip to install:

git clone git@github.com:sgoodm/python-distance-rasters.git
cd python-distance-rasters
pip install .

Contribute

New issues are always welcome, and if you'd like to make a change, fork the repo and submit a pull request.

Testing and Coverage

We use Pytest and Coveralls to run unit tests and track code coverage of tests. If you submit code, please make sure it passes existing tests and adds relevant testing coverage for new features.

You can run tests and coverage checks locally, or you can fork the repository and utilize GitHub actions and Coveralls. To use GitHub actions and Coveralls, you'll need to add your forked repo to your own Coverall accounts and add you Coveralls token to your repository as a GitHub Secret (see below).

To run tests and coverage checks locally, you can use the following commands:

pip install pytest coverage
coverage run -m pytest ./
coverage html

GitHub Secrets

There are three GitHub Secrets required to enable all of our GitHub Actions:

  1. COVERALLS_REPO_TOKEN - this is the API token for Coveralls, used for publishing code coverage reports
  2. TEST_PYPI_API_TOKEN - this is the API token for TestPyPi, needed for publishing alpha releases
  3. PYPI_API_TOKEN - this is the API token for PyPi, needed for publishing releases

Note: contributors do not need PyPi tokens; if you create a new release in a forked repo it will trigger a GitHub action that will attempt to publish to PyPi and fail.

Release files for distancerasters 0.4.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for distancerasters 0.4.2
File Size Uploaded
distancerasters-0.4.2.tar.gz 340.4 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for distancerasters 0.4.2
File
distancerasters-0.4.2-cp314-cp314-win_amd64.whl CPython 3.14 CPython 3.14 Windows x86-64 Details
distancerasters-0.4.2-cp314-cp314-macosx_11_0_arm64.whl CPython 3.14 CPython 3.14 macOS 11.0+ ARM64 Details
distancerasters-0.4.2-cp313-cp313-win_amd64.whl CPython 3.13 CPython 3.13 Windows x86-64 Details
distancerasters-0.4.2-cp313-cp313-macosx_11_0_arm64.whl CPython 3.13 CPython 3.13 macOS 11.0+ ARM64 Details
distancerasters-0.4.2-cp312-cp312-win_amd64.whl CPython 3.12 CPython 3.12 Windows x86-64 Details
distancerasters-0.4.2-cp312-cp312-manylinux_2_34_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.34+ x86-64 Details
distancerasters-0.4.2-cp312-cp312-macosx_11_0_arm64.whl CPython 3.12 CPython 3.12 macOS 11.0+ ARM64 Details
distancerasters-0.4.2-cp311-cp311-win_amd64.whl CPython 3.11 CPython 3.11 Windows x86-64 Details
distancerasters-0.4.2-cp311-cp311-macosx_11_0_arm64.whl CPython 3.11 CPython 3.11 macOS 11.0+ ARM64 Details
distancerasters-0.4.2-cp310-cp310-win_amd64.whl CPython 3.10 CPython 3.10 Windows x86-64 Details

Total release size: 2.8 MB

Release files / distancerasters-0.4.2.tar.gz

Download URL distancerasters-0.4.2.tar.gz
Size 340.4 kB
Tags Source
SHA-256 checksum
How to use checksums
1c336ba04cde2cd36ef40a26bc0b361a5ff5a3303e80fb4c8245e5d98dc4f36a
BLAKE2b-256 checksum
How to use checksums
bdf03f6e5e9f4c66b3d5715805c4a81f65ea90c2665eed7bca69df5c719a6ca4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.14

Release files / distancerasters-0.4.2-cp314-cp314-win_amd64.whl

Download URL distancerasters-0.4.2-cp314-cp314-win_amd64.whl
Size 185.9 kB
Tags CPython 3.14 Windows x86-64
SHA-256 checksum
How to use checksums
d0aeb0ee3ab9c2c06fc6e424de4b49f5d80caab2ac3dee9f08e6b43f1a622ea9
BLAKE2b-256 checksum
How to use checksums
f0d1711d06a9a4bb03f2506145b0e41467e76a5db1521ab88bfc005c25d42606
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.14

Release files / distancerasters-0.4.2-cp314-cp314-macosx_11_0_arm64.whl

Download URL distancerasters-0.4.2-cp314-cp314-macosx_11_0_arm64.whl
Size 300.1 kB
Tags CPython 3.14 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
ad8fe78f4479510671a8f7c24b16bad060512f73bb346d929c94237fe396a21f
BLAKE2b-256 checksum
How to use checksums
db422e77c2a463fafd34983a3ff439ea51f198f20b75e1f65fb43442ddad2fe4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.14

Release files / distancerasters-0.4.2-cp313-cp313-win_amd64.whl

Download URL distancerasters-0.4.2-cp313-cp313-win_amd64.whl
Size 185.8 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
e13d6f9fd0cc5059bead657871cd2f0be84b5faf7d5de48aecc9a05098f899e6
BLAKE2b-256 checksum
How to use checksums
a0902e7c0df5360546c23d87ddb2518a51f0d156062f6555ea3986642e8d03d9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.14

Release files / distancerasters-0.4.2-cp313-cp313-macosx_11_0_arm64.whl

Download URL distancerasters-0.4.2-cp313-cp313-macosx_11_0_arm64.whl
Size 299.2 kB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
69f317ed6d0c544422e5822fc80d89d9e3ff19be2bca7ccc990b5a5c858f423a
BLAKE2b-256 checksum
How to use checksums
74310ea427fc685da6d613ad84d72412f80a196daf5c0357c3d716c1e54a4365
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.14

Release files / distancerasters-0.4.2-cp312-cp312-win_amd64.whl

Download URL distancerasters-0.4.2-cp312-cp312-win_amd64.whl
Size 185.9 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
2ce5aa6f5b038f323c31bfa2448833804cad03db896739901a8f23dd21f98158
BLAKE2b-256 checksum
How to use checksums
509185f77346e8148cc7f6e02c16762c77554fd167eff3313afaac9a274304e8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.14

Release files / distancerasters-0.4.2-cp312-cp312-manylinux_2_34_x86_64.whl

Download URL distancerasters-0.4.2-cp312-cp312-manylinux_2_34_x86_64.whl
Size 352.4 kB
Tags CPython 3.12 Linux glibc 2.34+ x86-64
SHA-256 checksum
How to use checksums
2598d85097be55f05bef638ef6200603ebcd7a0269cffa23958939cf9e17df12
BLAKE2b-256 checksum
How to use checksums
49b5a00aa82fb19da6eddd1eb74d77e70ca7e66d5adc41592470d98c6a1d332a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.14

Release files / distancerasters-0.4.2-cp312-cp312-macosx_11_0_arm64.whl

Download URL distancerasters-0.4.2-cp312-cp312-macosx_11_0_arm64.whl
Size 299.0 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
c94b7bda1d96438e120ffb0d6be360894e1a127cae51f2b972f2b104ea02363b
BLAKE2b-256 checksum
How to use checksums
0b5963aa80d53006fc7d818c7318c6175d9424c5fe550ab5a2683a37d25d7a07
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.14

Release files / distancerasters-0.4.2-cp311-cp311-win_amd64.whl

Download URL distancerasters-0.4.2-cp311-cp311-win_amd64.whl
Size 187.6 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
973521ab2b035f96ff39ce4d3822d1ee1f8a618956013d8491cefe9b9fea48ce
BLAKE2b-256 checksum
How to use checksums
4f62e160b464594a6d0905aa8e30dead7ca0f23605185b1793d7178a903a3f14
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.14

Release files / distancerasters-0.4.2-cp311-cp311-macosx_11_0_arm64.whl

Download URL distancerasters-0.4.2-cp311-cp311-macosx_11_0_arm64.whl
Size 302.5 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
0cb05f1ba122bbb17cea6bc8865b3b7e05fb65fcdd9cab9dd3f6555a1b9ff736
BLAKE2b-256 checksum
How to use checksums
9eaf3b715a7277f52aaa7c822658ba47f286820b631769c4157b9c345ecc436f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.14

Release files / distancerasters-0.4.2-cp310-cp310-win_amd64.whl

Download URL distancerasters-0.4.2-cp310-cp310-win_amd64.whl
Size 187.6 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
7fb8938a5fca48204ca6c5d6c8021d1febb8fd04de03d4a7b0d721cc1905967b
BLAKE2b-256 checksum
How to use checksums
1cb10b0752fb7667a9626da8bb9b52c06fcbfa244ced740cc8da1c3ac5920493
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.14
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page