calculate_distance
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
calculate_distance [2019/02/26 11:58] – stritiha | calculate_distance [2023/04/21 15:48] (current) – irladmin | ||
---|---|---|---|
Line 3: | Line 3: | ||
This script calculates the distance from each cell to the nearest cell of a specific type (e.g. a road). | This script calculates the distance from each cell to the nearest cell of a specific type (e.g. a road). | ||
- | The script can be used used in the [[Habitat suitability]] and [[Carbon storage and wood production]] | + | The script can be used used in the [[Carbon storage and wood production]] |
{{ : | {{ : | ||
Line 15: | Line 15: | ||
Output: | Output: | ||
* hard evidence on a continuous node | * hard evidence on a continuous node | ||
+ | |||
+ | ### Calculates the distance to the nearest cell with a specific state (e.g. road) | ||
+ | ### Input: discrete raster of categories (e.g. 1 = road, 0 = no road) | ||
+ | ### Sets evidence on node continuous node (Distance_road) | ||
+ | | ||
+ | # import packages | ||
+ | import os | ||
+ | import sys | ||
+ | import numpy | ||
+ | from osgeo.gdalconst import * | ||
+ | from osgeo import gdal | ||
+ | import math | ||
+ | from node_utils import * | ||
+ | from scipy.spatial import distance | ||
+ | | ||
+ | # set names for labelling the script while running | ||
+ | SCRIPT_NAME = " | ||
+ | CELLS_PRINTING = 10 | ||
+ | | ||
+ | # SET FUNCTION PARAMETERS | ||
+ | # name of input node | ||
+ | input_name = " | ||
+ | # number of the state in the input raster that defines the cells (e.g. roads), the distance to which we are interested in | ||
+ | state_number = 0; | ||
+ | # name of output node | ||
+ | output_name = " | ||
+ | | ||
+ | | ||
+ | # function that finds cells of interest | ||
+ | def isRoad(node, | ||
+ | return (getStateHighestLikelihood(node, | ||
+ | | ||
+ | # function that finds distance to the nearest cell of interest | ||
+ | def findCloserRoadCell(cell, | ||
+ | | ||
+ | if (len(road_list) == 0): | ||
+ | print(" | ||
+ | return None | ||
+ | | ||
+ | return min(distance.cdist([cell], | ||
+ | | ||
+ | # function that writes distance to the output node | ||
+ | def process(dataset, | ||
+ | | ||
+ | # only use it at the beginning | ||
+ | if (iteration != -1): | ||
+ | return [] | ||
+ | | ||
+ | node_road = getNodeByName(nodes_data, | ||
+ | if (not node_road): | ||
+ | print(SCRIPT_NAME, | ||
+ | return [] | ||
+ | | ||
+ | total_cells = dataset.RasterXSize * dataset.RasterYSize ; | ||
+ | | ||
+ | # get pixel size | ||
+ | pixelsize = dataset.GetGeoTransform()[1] | ||
+ | | ||
+ | print(SCRIPT_NAME, | ||
+ | | ||
+ | road_cells = []; | ||
+ | | ||
+ | for cell in range(total_cells ): | ||
+ | | ||
+ | if (isRoad(node_road, | ||
+ | road_cells.append(cell); | ||
+ | |||
+ | print(SCRIPT_NAME, | ||
+ | | ||
+ | distances=[] | ||
+ | | ||
+ | # Transform to 2-dimensional array | ||
+ | road_coords = list(map(lambda x: [x / dataset.RasterXSize, | ||
+ | | ||
+ | # append distances to new array | ||
+ | for cell in range(total_cells ): | ||
+ | | ||
+ | if (isNODATA(node_road, | ||
+ | distances.append(PY_NODATA_VALUE) | ||
+ | elif isRoad(node_road, | ||
+ | distances.append(0) | ||
+ | else: | ||
+ | distances.append(findCloserRoadCell([cell / dataset.RasterXSize, | ||
+ | | ||
+ | | ||
+ | new_nodes_data = [{' | ||
+ | | ||
+ | | ||
+ | for i in range(CELLS_PRINTING): | ||
+ | print(SCRIPT_NAME, | ||
+ | | ||
+ | | ||
+ | if (validResultData(new_nodes_data, | ||
+ | return new_nodes_data | ||
+ | | ||
+ | else: | ||
+ | print ("Some error here: ") |
calculate_distance.1551178724.txt.gz · Last modified: 2023/04/21 15:30 (external edit)