Tree Density Calculator API

Source code: https://bitbucket.org/kul-reseco/localmaxfilter/src.

For issues, bugs, proposals or remarks, visit the issue tracker.

Core

———————————————————————————————————————-
Date : August 2018
Copyright : © 2018 - 2020 by Tinne Cahy (Geo Solutions) and Ann Crabbé (KU Leuven)
Acknowledgements : Translated from Local Maximum Filter [C++ software]
Ghent University, Laboratory of Forest Management and Spatial Information Techniques
Lieven P.C. Verbeke

This file is part of the QGIS Tree Density Calculator plugin and treedensitycalculator python package.

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
License as published by the Free Software Foundation, either version 3 of the License, or any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License (COPYING.txt). If not see www.gnu.org/licenses.
———————————————————————————————————————-
class localmaxfilter.core.local_max_filter.LocalMaxFilter(window_length)[source]

Bases: object

A window is used to slide over an image and look for maximum reflectance values in the center of that window.

execute(image_array, area_of_interest=None, snap=None, geo_transform=None, set_progress: callable = None)[source]

The function will return an image containing the reflectance values of the local max of a sliding window going over the input image. It is possible to include an area of interest

Parameters:
  • image_array (ndarray) – 2D Numpy array image
  • area_of_interest (ndarray) – 2D Numpy array image indicating with 0 and 1 the area of interest
  • snap – defines the snap distance
  • geo_transform – contains the pixel size of the raster
  • set_progress – communicate progress (refer to the progress bar in case of GUI; otherwise print to console)
Returns:

dict: Dictionary containing the reflectance values of the local max together with pixel locations

localmaxfilter.core.local_max_filter.printProgress(value: int)[source]

Replacement for the GUI progress bar

Interfaces

———————————————————————————————————————-
Date : March 2019
Copyright : © 2019 - 2020 by Tinne Cahy (Geo Solutions) and Ann Crabbé (KU Leuven)
Acknowledgements : Translated from Local Maximum Filter [C++ software]
Ghent University, Laboratory of Forest Management and Spatial Information Techniques
Lieven P.C. Verbeke

This file is part of the QGIS Tree Density Calculator plugin and treedensitycalculator python package.

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
License as published by the Free Software Foundation, either version 3 of the License, or any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License (COPYING.txt). If not see www.gnu.org/licenses.
———————————————————————————————————————-
localmaxfilter.interfaces.run_algorithm_local_max_filter(image_path, window, log_function=<built-in function print>, update_progress_bar=None, mask_path=None, output_base_path=None, snap_distance=None, voronoi=None, feedback=None, context=None)[source]

General interface between the GUI/CLI and script.

Parameters:
  • image_path – the absolute path to the raster file
  • window – window size in meters
  • update_progress_bar – function to update the progress bar
  • log_function – function to log
  • mask_path – absolute path to the vector file (optional)
  • output_base_path – base path for output files (optional)
  • snap_distance – snap distance for output points (optional)
  • voronoi – set to True if you want voronoi polygons as output (optional)
  • feedback – necessary for the processing tool
  • context – necessary for the processing tool
Returns:

———————————————————————————————————————-
Date : August 2018
Copyright : © 2018 - 2020 by Tinne Cahy (Geo Solutions) and Ann Crabbé (KU Leuven)

This file is part of the QGIS Tree Density Calculator plugin and treedensitycalculator python package.

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
License as published by the Free Software Foundation, either version 3 of the License, or any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License (COPYING.txt). If not see www.gnu.org/licenses.
———————————————————————————————————————-
localmaxfilter.interfaces.imports.check_path(path)[source]

Check if path exists. Skipp path which are in memory

Parameters:path – the absolute path to the input file
localmaxfilter.interfaces.imports.import_image(image_path, mask_path=None, window_size=None, reflectance=False, feedback=None, context=None)[source]

Get an input image as array from file path. If a vector layer path is given, the raster is clipped, using a 2*window_size buffer. Also return the the spatial reference system and the geo_transform.

Parameters:
  • image_path – the absolute path to the input image
  • mask_path – the absolute path to the polygon file
  • window_size – the size of the sliding window, only required if mask is given
  • reflectance – return reflectance values instead of DN between 0 and 255
  • feedback – necessary for the processing tool
  • context – necessary for the processing tool
Returns:

numpy array [#bands x #rows x #columns], ogr srs and geo_transform

localmaxfilter.interfaces.imports.import_vector_as_image(path, geo_transform, image_size, image_srs, window_size, feedback=None, context=None)[source]

Browse for a vector file and return it as a raster after buffering with window_size.

Parameters:
  • path – the absolute path to the vector file
  • geo_transform – 6 geo transformation coefficients
  • image_size – size of the image
  • image_srs – OSR spatial reference system object
  • window_size – the size of the sliding window
  • feedback – necessary for the processing tool
  • context – necessary for the processing tool
Returns:

numpy array

———————————————————————————————————————-
Date : August 2018
Copyright : © 2018 - 2020 by Tinne Cahy (Geo Solutions) and Ann Crabbé (KU Leuven)

This file is part of the QGIS Tree Density Calculator plugin and treedensitycalculator python package.

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
License as published by the Free Software Foundation, either version 3 of the License, or any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License (COPYING.txt). If not see www.gnu.org/licenses.
———————————————————————————————————————-
localmaxfilter.interfaces.exports.check_path(path)[source]

Check if path exists. Skipp path which are in memory

Parameters:path – the absolute path to the input file
localmaxfilter.interfaces.exports.write_mask_layer(output_path, mask_path, trees_path, log=<built-in function print>)[source]
Copy the input mask to a new shapefile, and add an extra attributes:
  • field ‘Area_ha’ contains the area of the polygon
  • field ‘TreeCount’ contains the number of trees found in that polygon
  • field ‘TreeDens’ contains the number of trees per ha
Parameters:
  • output_path – the absolute path to the output Shapefile
  • mask_path – the absolute path to the input vector file
  • trees_path – the absolute path to the input point vector file
  • log – log function
localmaxfilter.interfaces.exports.write_point_layer(output_path, points_dict, geo_transform, srs, mask_path=None, feedback=None, context=None)[source]
Convert the input point dictionary to a vector point layer using the image metadata (srs and geo_transform).
The points are clipped to fit the given mask. https://gis.stackexchange.com/questions/268395/converting-raster-tif-to-point-shapefile-using-python
Parameters:
  • output_path – the absolute path to the output vector file
  • points_dict – a dictionary containing the raster values and pixel locations Keys: RasterVal, Pixel with keys x and y
  • mask_path – the absolute path to the mask file for clipping
  • geo_transform – geo transformation coefficients
  • srs – spatial reference system
  • feedback – necessary for the processing tool
  • context – necessary for the processing tool
localmaxfilter.interfaces.exports.write_voronoi_layer(output_path, point_path, mask_path=None, feedback=None, context=None)[source]

Convert the input point vector to a Voronoi polygon layer and clip it by the mask layer

Parameters:
  • output_path – the absolute path to the output voronoi vector file
  • point_path – the absolute path to the input point vector file
  • mask_path – the absolute path to the overlay vector file
  • feedback – necessary for the processing tool
  • context – necessary for the processing tool
———————————————————————————————————————-
Date : August 2018
Copyright : © 2018 - 2020 by Tinne Cahy (Geo Solutions) and Ann Crabbé (KU Leuven)
Acknowledgements : Translated from Local Maximum Filter [C++ software]
Ghent University, Laboratory of Forest Management and Spatial Information Techniques
Lieven P.C. Verbeke

This file is part of the QGIS Tree Density Calculator plugin and treedensitycalculator python package.

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
License as published by the Free Software Foundation, either version 3 of the License, or any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License (COPYING.txt). If not see www.gnu.org/licenses.
———————————————————————————————————————-

CLI

A window is used to slide over an image and look for maximum reflectance values in the center of that window.

usage: treedensity [-h] [-m ] [-v] [-s ] [-o ] image sliding_window

Positional Arguments

image input reflectance image path
sliding_window length of the sliding window, in meters (default 5 m), should be a multiple of the image pixel size

Named Arguments

-m, --mask overlapping vector mask path
-v, --voronoi

create a Voronoi shapefile based on the tree locations (default off)

Default: False

-s, --snap give distance [m] for the snap tool, should be max half of the window size, tree tops which are closer to each other than the given distance will be taken as one
-o, --output path for output files, give base without extension (default: in same folder with extension _window_{}.shp)