calculate_mean_value_in_neighbouring_cells
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
calculate_mean_value_in_neighbouring_cells [2019/02/25 17:33] – stritiha | calculate_mean_value_in_neighbouring_cells [2023/04/21 16:04] (current) – irladmin | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ==== Mean of neighboring | + | ==== Mean of neighbouring |
This script calculates the mean of a continuous variable (e.g. % forest cover) in a specified window around each cell. It runs before the first iteration (i.e. to prepare the input data). | This script calculates the mean of a continuous variable (e.g. % forest cover) in a specified window around each cell. It runs before the first iteration (i.e. to prepare the input data). | ||
Line 9: | Line 9: | ||
Input: | Input: | ||
- | * continuous raster (1 band) [Forest_cover] | + | * continuous raster (1 band) |
- | * window size (distance) | + | * window size (distance) |
Output: | Output: | ||
- | * hard evidence on a continuous node [Forest_cover_neighb] | + | * hard evidence on a continuous node |
+ | |||
+ | ### Calculates the mean of a continuous variable (e.g. % forest cover) | ||
+ | ### in a specified window around each cell. | ||
+ | ### Input raster: continuous raster (1 band) [Forest_cover] | ||
+ | ### Sets hard evidence on continuous node [Forest_cover_neighb] | ||
+ | # import packages | ||
+ | import numpy | ||
+ | import math | ||
+ | from node_utils import * | ||
+ | # set names for labelling the script while running | ||
+ | SCRIPT_NAME = " | ||
+ | CELLS_PRINTING = 3 | ||
+ | # SET FUNCTION PARAMETERS | ||
+ | # neighbourhood distance - size of the window [same units as raster] | ||
+ | d = 500 | ||
+ | # name of input node | ||
+ | input_name = " | ||
+ | # name of output node | ||
+ | output_name = " | ||
+ | # DEFINE THE FUNCTION | ||
+ | def process(dataset, | ||
+ | # only use it at the beginning | ||
+ | if (iteration != -1): | ||
+ | return [] | ||
+ | # get pixel size | ||
+ | pixelsize = dataset.GetGeoTransform()[1] | ||
+ | # number of pixels to take into account for the neighborhood | ||
+ | npix = int(d/ | ||
+ | print(SCRIPT_NAME, | ||
+ | input_data = getNodeByName(nodes_data, | ||
+ | if (not input_data): | ||
+ | print(SCRIPT_NAME, | ||
+ | return [] | ||
+ | # create a list with only the prob of the state of interest (road) | ||
+ | # create empty list | ||
+ | data_list = [] | ||
+ | |||
+ | for cell in range(dataset.RasterXSize * dataset.RasterYSize ): | ||
+ | |||
+ | # get value of state | ||
+ | value = getValue(input_data, | ||
+ | # append the value for this cell to the list | ||
+ | data_list.append(value) | ||
+ | |||
+ | # check if the values are correct | ||
+ | print(" | ||
+ | |||
+ | # create 2D array from the vector (easier for edge problems) | ||
+ | data_array = numpy.array(data_list) | ||
+ | # replace no data values with nan | ||
+ | data_array[data_array==PY_NODATA_VALUE]=numpy.nan | ||
+ | wide = data_array.reshape(dataset.RasterYSize, | ||
+ | |||
+ | # create empty neighborhood array | ||
+ | neighb = numpy.empty((dataset.RasterYSize, | ||
+ | |||
+ | # loop through cells and calculate mean of neighbouring cells | ||
+ | for i in range(wide.shape[0]): | ||
+ | for j in range (wide.shape[1]): | ||
+ | if i >= npix and j >= npix: | ||
+ | neighblist = wide[i-npix : i+npix+1, j-npix : j+npix+1] | ||
+ | elif i >= npix: | ||
+ | neighblist = wide[i-npix : i+npix+1, | ||
+ | elif j >= npix: | ||
+ | neighblist = wide[: i+npix+1, j-npix : j+npix+1] | ||
+ | else: | ||
+ | neighblist = wide[: i+npix+1, : j+npix+1] | ||
+ | |||
+ | average = neighblist[~numpy.isnan(neighblist)].mean() | ||
+ | |||
+ | neighb[i,j] = average | ||
+ | # check maximum and minimum | ||
+ | print(" | ||
+ | print(" | ||
+ | # write the values into the node_data format | ||
+ | # into a continuous node format | ||
+ | |||
+ | # translate to 1D list | ||
+ | neighb_list = neighb.flatten().tolist() | ||
+ | print(" | ||
+ | |||
+ | new_nodes_data = [{' | ||
+ | |||
+ | for i in range(CELLS_PRINTING): | ||
+ | print(" | ||
+ | return new_nodes_data |
calculate_mean_value_in_neighbouring_cells.1551112401.txt.gz · Last modified: 2023/04/21 15:30 (external edit)