Skip to main content

Generate distance raster using arbitrary sets of spatial features

Project description

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.

Project details


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.1.tar.gz (339.9 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.1-cp314-cp314-win_amd64.whl (179.8 kB view details)

Uploaded CPython 3.14Windows x86-64

distancerasters-0.4.1-cp314-cp314-macosx_11_0_arm64.whl (296.6 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

distancerasters-0.4.1-cp313-cp313-win_amd64.whl (179.8 kB view details)

Uploaded CPython 3.13Windows x86-64

distancerasters-0.4.1-cp313-cp313-macosx_11_0_arm64.whl (296.2 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

distancerasters-0.4.1-cp312-cp312-win_amd64.whl (180.0 kB view details)

Uploaded CPython 3.12Windows x86-64

distancerasters-0.4.1-cp312-cp312-manylinux_2_34_x86_64.whl (347.0 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ x86-64

distancerasters-0.4.1-cp312-cp312-macosx_11_0_arm64.whl (296.2 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

distancerasters-0.4.1-cp311-cp311-win_amd64.whl (181.5 kB view details)

Uploaded CPython 3.11Windows x86-64

distancerasters-0.4.1-cp311-cp311-macosx_11_0_arm64.whl (298.3 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

distancerasters-0.4.1-cp310-cp310-win_amd64.whl (181.5 kB view details)

Uploaded CPython 3.10Windows x86-64

File details

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

File metadata

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

File hashes

Hashes for distancerasters-0.4.1.tar.gz
Algorithm Hash digest
SHA256 17b3b147ece642dfcff346c1724c842e6612ca37a9cd05706bb89fd528f97a21
MD5 ed1ae150b0c01576dd7cf661aad49ec4
BLAKE2b-256 88ee0e61be7d71a24ce43a58aced2b8ad7aa0e7170ab915bb3f2dd4bf6f1313f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for distancerasters-0.4.1-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 5300adedcdc81d69038282c80077e4c7fe4f9017dd4d88ed90d02acf7f9e896b
MD5 1c3f9a3fcdb3071efbcf992044310eda
BLAKE2b-256 428a6c3e8b80c0799660a47fbcf316c5d0cbd9569da3a684135ff4cc94f979d3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for distancerasters-0.4.1-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 39ae09554e735545cd3ffb9de1b7f113fd7ffd108e1591d9e206bff2c3a846dd
MD5 c597ad3708cc9ed66c1008d6e7cef261
BLAKE2b-256 5e9075ac5285f4728c4a6cdf7542a31d7589ad3243f53bbb03c7d9a657c4c259

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for distancerasters-0.4.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 609cdb944791c8e83eace3a5794e1ec30000775dc18fa63850c2183f6c98b664
MD5 f6618367cdeb7b7ed7dc048fd26324b8
BLAKE2b-256 0018ae5336f92de324a498dd7259ba8f51447ea1393da0c0bb4b9295f5255a0b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for distancerasters-0.4.1-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 373129b2a5efc378de4ada82cee768d486e0037e4e811deb6adf3a4733e0b5ec
MD5 3ed55a87e679223a2f9fd7d546334700
BLAKE2b-256 fdcbad40a4c8ccde14f064c764049107efe04f74e1314f0bba18376efeba81f6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for distancerasters-0.4.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 482e1a7a00ee2f4755a200cba0cb82334a0ad84f78fd5beeeb75b5340a25eca3
MD5 4a55dcd1aa36c9d105e9a1783aaf942f
BLAKE2b-256 d1fd7d1475bdcfb503d7fba5b0a5aa62d5fa82708bc8d95003646341c1734e04

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for distancerasters-0.4.1-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 37834672df4ae7e2eaacf3aa1557b09779d9eb97667edc434a3a5b8f59ee4c07
MD5 9619cbd05f3a5ab8a842086286392af5
BLAKE2b-256 b274602c5dc67a0a5ecae2a5e6768f6e7a16a7de772822907c9752859427a961

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for distancerasters-0.4.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3a7a9f4fd28137fde08d0c6e50c3a34c07d5b98772d09ddc55c5f5ec0b6ef375
MD5 6fef8089d81e9f34180e80b57159bffb
BLAKE2b-256 ea478d53404750c152a830bc2ed52b61de2fa62185428c4fbda168926afd78c6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for distancerasters-0.4.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 6b9471e1c4e05b4c50ce4e3c2cfe86d2447745e3459dda269bff64a0c1d29d53
MD5 a0efbbd823868c2f8652fd6f4b27e372
BLAKE2b-256 502f7f28cb05ed3c4a8f075c99545c99a61cf29b92d9ed8150bea2b2721291ea

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for distancerasters-0.4.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 de7dd880dce1f290e0f17e4d43be70cf0e800ee240d23a9b540c83ca714c91c6
MD5 a8a6ca9ca06b1333bdbd361994458d3c
BLAKE2b-256 6b33f4ec432bb85fec38e0ce2ffd2ac878fd554992a096309fb54393b7f78fc9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for distancerasters-0.4.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 d4f11324b94767e44872ad8f9d6c3555f6d8f663b2d08bcc8d058d023a72e9e9
MD5 1ab7b188d5bd9e8af8e62d0b67f19b1e
BLAKE2b-256 942525e606ad13faca70d1d16cd87394c0ccc798948e4b4de9749d0112a3cf4c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page