Skip to main content

Python package for the gdistance

Project description

gdistance-python

The gdistance package makes use of raster, which has an underlying fast implementation of sparse matrices. The execution flow is borrowed from a R-gdistance package written by Hijmans and van Etten.

This is on a study (and course in) facility accessibility in developing countries. Think roughly of a national marketshed analysis for hospitals and how reachable (travel time) these are for the population. Hospitals could be other facilities (ATMs, schools, lawyers, drug stores, you name it). The workflow that we focus on aims to determine trvale time to the nearest (in time) facility fork any location in a country's territory.

Travel can take place by car, train, boat or on foot. (There could be more modes, but not in our example). All modes of travel are over a network for which we have data, and each network link max maximum speed that we shall use. (Vector data thus ...) Where there is no travel network, we expect that travel is on foot. Terrain characteristics such as land cover and hillslope then determine the maximum walking speed. You will probably immediately understand that such computations will be done in raster mode, but also that you can not really do this on a by-pixel basis: slope for instance depends on in which direction you cross the pixel. Thus, in one direction you could walk downhill and be fast, while in opposite direction you'd be walking uphill and be slower. A richer data structure than a raster is required for this.

This package, for the purpose above, takes an input raster and creates a graph out of it. Each of the NxM raster cells will be a node in the graph, and from any single cell C there will be 4-8-16 links to the neighbour cells. With a link a cost value can be associated to represent, for instance, slope. Links can represent one direction (or bi-directional), if the cost associates with travel does not depend on direction.

Pre-requisite

To use this package, please install Numpy and GDAL.

Installlation

Use pip to install the package.

pip install gdistance

How to use

Import the sub-packages from the main package as follows.
>> from gdistance.raster import *
>> from gdistance.gdistance import *
>> from gdistance.utils import *

The gdistance.raster sub-packages contains raster-based functions for performing raster analysis derived from GDAL. For instance to create and save a raster object, the following lines of codes can be applied.

>> ncols, nrows = 7,6
>> minX, minY = 0, 0
>> xres, yres = 1, 1
>> maxX = minX + (ncols * xres)
>> maxY = minY + (nrows * xres)
>> values = [[2, 2, 1, 1, 5, 5, 5], 
          [2, 2, 8, 8, 5, 2, 1], 
          [7, 1, 1, 8, 2, 2, 2], 
          [8, 7, 8, 8, 8, 8, 5], 
          [8, 8, 1, 1, 5, 3, 9], 
          [8, 1, 1, 2, 5, 3, 9]]

>> raster = Raster(extent=[minX, maxX,minY, maxY], xres=xres, yres=yres, crs=3857, nodatavalue=-9999, pix_values=values)
>> save_raster(raster, "raster.tif")

Transition function generate an adjacency matrix out of a raster object. It takes a raster object, user-defined function and directions (number of neighbors for a cell).

>> def mean(x1, x2):
        return np.divide(2, np.add(x1, x2))

>> gd = Gdistance()
>> trans = gd.transition(raster, function=mean, directions=4)

You can also load a raster object from a file as shown;

>> raster = Raster("processing/friction.tif")
>> trans =  gd.transition(raster, function=mean, directions=4)

Geocorrection function generates a new transition matrix after applying a distance factor.

>> trans = gd.geocorrection(trans)

Accost function calculate the costs of travelling from every cell to target(s). Target coordinates can be imported from a vector file using the coords_from_vector() function.

>> # targets = coords_from_vector("targets.shp")
>> targets = [(5.5, 1.5)]
>> accost = gd.acc_cost(trans, targets)
>> save_raster(min(accost), "accumcost.tif")

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

gdistance-1.0.4.tar.gz (33.6 kB view details)

Uploaded Source

Built Distributions

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

gdistance-1.0.4-cp37-cp37m-win_amd64.whl (598.5 kB view details)

Uploaded CPython 3.7mWindows x86-64

gdistance-1.0.4-cp37-cp37m-win32.whl (280.5 kB view details)

Uploaded CPython 3.7mWindows x86

File details

Details for the file gdistance-1.0.4.tar.gz.

File metadata

  • Download URL: gdistance-1.0.4.tar.gz
  • Upload date:
  • Size: 33.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.4

File hashes

Hashes for gdistance-1.0.4.tar.gz
Algorithm Hash digest
SHA256 9ab00d8820b3d6c98aea8cf664f5c42120cf51e55167138c6a09f9946ea4ab87
MD5 00392f24587852311e4184ffa85d43c5
BLAKE2b-256 4832baa4f0dbfa5f6528df6dc0a2ae444a070ae4499a99b6c7e882771ec4a7a6

See more details on using hashes here.

File details

Details for the file gdistance-1.0.4-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: gdistance-1.0.4-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 598.5 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.4

File hashes

Hashes for gdistance-1.0.4-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 7206b4d70ecf8bb1cee46a1921fea134dcde027f5099b414d4fb326d6f02efda
MD5 f735673e88c70d30d8d09bff8a9418c9
BLAKE2b-256 56b14540ff3648ed52156644c18d24eaf44019a97b25cf91ef9b727658426579

See more details on using hashes here.

File details

Details for the file gdistance-1.0.4-cp37-cp37m-win32.whl.

File metadata

  • Download URL: gdistance-1.0.4-cp37-cp37m-win32.whl
  • Upload date:
  • Size: 280.5 kB
  • Tags: CPython 3.7m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.4

File hashes

Hashes for gdistance-1.0.4-cp37-cp37m-win32.whl
Algorithm Hash digest
SHA256 6b165bc55ea61630a6eb6b71fd0045e627b80765ddf6f7faf5978f1b18daf564
MD5 45954e55ee2508ec42f123cbb8c08701
BLAKE2b-256 92f34e09f5bbf09a377f6d97f3dbbb8a775bd9388c9e3798a2e4612975c5b95c

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