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

Uploaded CPython 3.14Windows x86-64

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

Uploaded CPython 3.14macOS 11.0+ ARM64

distancerasters-0.4.0-cp313-cp313-win_amd64.whl (179.9 kB view details)

Uploaded CPython 3.13Windows x86-64

distancerasters-0.4.0-cp313-cp313-macosx_11_0_arm64.whl (296.3 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.12Windows x86-64

distancerasters-0.4.0-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.0-cp312-cp312-macosx_11_0_arm64.whl (296.3 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

distancerasters-0.4.0-cp311-cp311-win_amd64.whl (181.6 kB view details)

Uploaded CPython 3.11Windows x86-64

distancerasters-0.4.0-cp311-cp311-macosx_11_0_arm64.whl (298.4 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

distancerasters-0.4.0-cp310-cp310-win_amd64.whl (181.6 kB view details)

Uploaded CPython 3.10Windows x86-64

File details

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

File metadata

  • Download URL: distancerasters-0.4.0.tar.gz
  • Upload date:
  • Size: 349.1 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.0.tar.gz
Algorithm Hash digest
SHA256 b702e740f623fa226cdaa9d41f9ba870050b2f8a9c8b8e364d8b1ad6ca351088
MD5 18a6ebb3d40aa6e8c6914fdd429fcd7e
BLAKE2b-256 5cae1918c8ea267c8d010c049017345908bdbc7a92b614c4ad6da08aedb19ae8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for distancerasters-0.4.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 c15310205d27e24344d480bea4e2a885cbb8b7512dea308bd854a715c7429170
MD5 65314bedbb89ee66241218b513e7264e
BLAKE2b-256 dbfcf29856b0239f6c1ba265ca77e2b62e4311dfcdc2d132b29fe831ebab64ab

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for distancerasters-0.4.0-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b30ba99b75542f7661e7db123ce0ae88097d9bf187ec55b1acb936a13dc78731
MD5 722fc5272181a02f569d6a1934405df2
BLAKE2b-256 df415b3adc2c85a1ae050aaf7e77ccb29b66d4894cecc2a01365aa45b9e9cca3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for distancerasters-0.4.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 7f72aa89393430417ae305efe90489eea321547a1a09f4f13a7547f71441e772
MD5 a6938698caf8c86352ac3d1973c16a43
BLAKE2b-256 4c47a7cbfc058f2f9d0da4c09bc44d55a859a6947eac3a5e74d8897f22bf0705

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for distancerasters-0.4.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3c17a17af35c61fae5b88d02baad235e41669163f14aaa78690d6aae3e69d927
MD5 ec2a70f0ededd95be8f85fc458627f70
BLAKE2b-256 2f6d889edaa0bcffd158587529d6ea985dfa8962d06eda546f070327e12377ac

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for distancerasters-0.4.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 824a720b5524b0d8a9045e9c8eabbc86f4ec79afdfba1cdfd6de3430b428a750
MD5 dc8b95ffd534b2b586c812274d52759f
BLAKE2b-256 9f3d409bbb82646153fdcd49ce37b198b0836e95ccc628801bbab21e147c7793

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for distancerasters-0.4.0-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 88562a6c7f09c0ed99c6798939b896429121b8b94a7342ee49e664f343925a4f
MD5 b56df7ffdf849a796b87f835a8846c15
BLAKE2b-256 375f16ad6a235832e5efa3af2df7ae848c704b5b605a7305973760087bda3d3c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for distancerasters-0.4.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f6cb9a32194b1997dc0bfd65b26dd5aac53d9ea9bbe1b9a56b33d1eb090764cb
MD5 28a55c84a20fb855ae791b6f647872ab
BLAKE2b-256 743ab16ef05eb6186e12eae402ee7c319d3fcf4ad2225aef8fd9c39800d536a2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for distancerasters-0.4.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 302539a8350e95ec55e32b4b8ddeeb6c226853930dcd7fcb15c892eaccd2af3d
MD5 e0d19d35817b264ec9ccc3634180fed7
BLAKE2b-256 8d03f19bb0e45d6bf688fc16a82d815791994acf905f988e13a4f7bff2929370

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for distancerasters-0.4.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 13807b718145e2acff548f16ba6b4fbfeedf4b089112ac504402d2993bda1cfd
MD5 d2d28bd130570e42766cc41765124f9d
BLAKE2b-256 3d81930ae7b51aef7ec19f4bd3bd72914dc0e8804791ed60a090d7f495555463

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for distancerasters-0.4.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 2b746b24800e9e7a6b164f84eac166ca2734a4a25b3d7e647bbb182a58ff3464
MD5 214035f6536600e925c2f2ba8b6bcfef
BLAKE2b-256 4ba6aa2085bc5b7c11887da410b355a99440457c70e5245d1194546ef62e219d

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