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.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

distancerasters-0.4.2.tar.gz (340.4 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

distancerasters-0.4.2-cp314-cp314-win_amd64.whl (185.9 kB view details)

Uploaded CPython 3.14Windows x86-64

distancerasters-0.4.2-cp314-cp314-macosx_11_0_arm64.whl (300.1 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

distancerasters-0.4.2-cp313-cp313-win_amd64.whl (185.8 kB view details)

Uploaded CPython 3.13Windows x86-64

distancerasters-0.4.2-cp313-cp313-macosx_11_0_arm64.whl (299.2 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

distancerasters-0.4.2-cp312-cp312-win_amd64.whl (185.9 kB view details)

Uploaded CPython 3.12Windows x86-64

distancerasters-0.4.2-cp312-cp312-manylinux_2_34_x86_64.whl (352.4 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ x86-64

distancerasters-0.4.2-cp312-cp312-macosx_11_0_arm64.whl (299.0 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

distancerasters-0.4.2-cp311-cp311-win_amd64.whl (187.6 kB view details)

Uploaded CPython 3.11Windows x86-64

distancerasters-0.4.2-cp311-cp311-macosx_11_0_arm64.whl (302.5 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

distancerasters-0.4.2-cp310-cp310-win_amd64.whl (187.6 kB view details)

Uploaded CPython 3.10Windows x86-64

File details

Details for the file distancerasters-0.4.2.tar.gz.

File metadata

  • Download URL: distancerasters-0.4.2.tar.gz
  • Upload date:
  • Size: 340.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for distancerasters-0.4.2.tar.gz
Algorithm Hash digest
SHA256 1c336ba04cde2cd36ef40a26bc0b361a5ff5a3303e80fb4c8245e5d98dc4f36a
MD5 b0f83113386684cb85916a26463df3b9
BLAKE2b-256 bdf03f6e5e9f4c66b3d5715805c4a81f65ea90c2665eed7bca69df5c719a6ca4

See more details on using hashes here.

File details

Details for the file distancerasters-0.4.2-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for distancerasters-0.4.2-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 d0aeb0ee3ab9c2c06fc6e424de4b49f5d80caab2ac3dee9f08e6b43f1a622ea9
MD5 ec397dbf851f3f8363891e0b94ccd8f5
BLAKE2b-256 f0d1711d06a9a4bb03f2506145b0e41467e76a5db1521ab88bfc005c25d42606

See more details on using hashes here.

File details

Details for the file distancerasters-0.4.2-cp314-cp314-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for distancerasters-0.4.2-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ad8fe78f4479510671a8f7c24b16bad060512f73bb346d929c94237fe396a21f
MD5 a1d89df9a55ac42c389bc10e67780030
BLAKE2b-256 db422e77c2a463fafd34983a3ff439ea51f198f20b75e1f65fb43442ddad2fe4

See more details on using hashes here.

File details

Details for the file distancerasters-0.4.2-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for distancerasters-0.4.2-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 e13d6f9fd0cc5059bead657871cd2f0be84b5faf7d5de48aecc9a05098f899e6
MD5 6abf967fe9a325847d9c607655e975f9
BLAKE2b-256 a0902e7c0df5360546c23d87ddb2518a51f0d156062f6555ea3986642e8d03d9

See more details on using hashes here.

File details

Details for the file distancerasters-0.4.2-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for distancerasters-0.4.2-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 69f317ed6d0c544422e5822fc80d89d9e3ff19be2bca7ccc990b5a5c858f423a
MD5 eaf1d523a5eaa2498a173ccb01ab0247
BLAKE2b-256 74310ea427fc685da6d613ad84d72412f80a196daf5c0357c3d716c1e54a4365

See more details on using hashes here.

File details

Details for the file distancerasters-0.4.2-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for distancerasters-0.4.2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 2ce5aa6f5b038f323c31bfa2448833804cad03db896739901a8f23dd21f98158
MD5 fd659eb676b3dcef55c9dac7428dca03
BLAKE2b-256 509185f77346e8148cc7f6e02c16762c77554fd167eff3313afaac9a274304e8

See more details on using hashes here.

File details

Details for the file distancerasters-0.4.2-cp312-cp312-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for distancerasters-0.4.2-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 2598d85097be55f05bef638ef6200603ebcd7a0269cffa23958939cf9e17df12
MD5 6ab53757cad95355056842a12688d12b
BLAKE2b-256 49b5a00aa82fb19da6eddd1eb74d77e70ca7e66d5adc41592470d98c6a1d332a

See more details on using hashes here.

File details

Details for the file distancerasters-0.4.2-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for distancerasters-0.4.2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c94b7bda1d96438e120ffb0d6be360894e1a127cae51f2b972f2b104ea02363b
MD5 4049a03daf89cc5a4a804b11c3b92d63
BLAKE2b-256 0b5963aa80d53006fc7d818c7318c6175d9424c5fe550ab5a2683a37d25d7a07

See more details on using hashes here.

File details

Details for the file distancerasters-0.4.2-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for distancerasters-0.4.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 973521ab2b035f96ff39ce4d3822d1ee1f8a618956013d8491cefe9b9fea48ce
MD5 9ac999b15be2cd084a4925d918882c8e
BLAKE2b-256 4f62e160b464594a6d0905aa8e30dead7ca0f23605185b1793d7178a903a3f14

See more details on using hashes here.

File details

Details for the file distancerasters-0.4.2-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for distancerasters-0.4.2-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0cb05f1ba122bbb17cea6bc8865b3b7e05fb65fcdd9cab9dd3f6555a1b9ff736
MD5 7232a017cc961eabfaf1ab59416932d4
BLAKE2b-256 9eaf3b715a7277f52aaa7c822658ba47f286820b631769c4157b9c345ecc436f

See more details on using hashes here.

File details

Details for the file distancerasters-0.4.2-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for distancerasters-0.4.2-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 7fb8938a5fca48204ca6c5d6c8021d1febb8fd04de03d4a7b0d721cc1905967b
MD5 a578b89a9ece181396f3e3bff16c5034
BLAKE2b-256 1cb10b0752fb7667a9626da8bb9b52c06fcbfa244ced740cc8da1c3ac5920493

See more details on using hashes here.

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