Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Project: Viewshed Generation 4 : * Purpose: Core algorithm implementation for viewshed generation. 5 : * Author: Tamas Szekeres, szekerest@gmail.com 6 : * 7 : ****************************************************************************** 8 : * 9 : * Permission is hereby granted, free of charge, to any person obtaining a 10 : * copy of this software and associated documentation files (the "Software"), 11 : * to deal in the Software without restriction, including without limitation 12 : * the rights to use, copy, modify, merge, publish, distribute, sublicense, 13 : * and/or sell copies of the Software, and to permit persons to whom the 14 : * Software is furnished to do so, subject to the following conditions: 15 : * 16 : * The above copyright notice and this permission notice shall be included 17 : * in all copies or substantial portions of the Software. 18 : * 19 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 20 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 22 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 : * DEALINGS IN THE SOFTWARE. 26 : ****************************************************************************/ 27 : 28 : #include <cstdint> 29 : #include <memory> 30 : #include <string> 31 : 32 : #include "cpl_progress.h" 33 : #include "gdal_priv.h" 34 : 35 : namespace gdal 36 : { 37 : 38 : /** 39 : * Class to support viewshed raster generation. 40 : */ 41 : class Viewshed 42 : { 43 : public: 44 : /** 45 : * Raster output mode. 46 : */ 47 : enum class OutputMode 48 : { 49 : Normal, //!< Normal output mode (visibility only) 50 : DEM, //!< Output height from DEM 51 : Ground //!< Output height from ground 52 : }; 53 : 54 : /** 55 : * Cell height calculation mode. 56 : */ 57 : enum class CellMode 58 : { 59 : Diagonal, //!< Diagonal Mode 60 : Edge, //!< Edge Mode 61 : Max, //!< Maximum value produced by Diagonal and Edge mode 62 : Min //!< Minimum value produced by Diagonal and Edge mode 63 : }; 64 : 65 : /** 66 : * A point. 67 : */ 68 : struct Point 69 : { 70 : double x; //!< X value 71 : double y; //!< Y value 72 : double z; //!< Z value 73 : }; 74 : 75 : /** 76 : * Options for viewshed generation. 77 : */ 78 : struct Options 79 : { 80 : Point observer{0, 0, 0}; //!< x, y, and z of the observer 81 : uint8_t visibleVal{255}; //!< raster output value for visible pixels. 82 : uint8_t invisibleVal{ 83 : 0}; //!< raster output value for non-visible pixels. 84 : uint8_t outOfRangeVal{ 85 : 0}; //!< raster output value for pixels outside of max distance. 86 : double nodataVal{-1}; //!< raster output value for pixels with no data 87 : double targetHeight{0.0}; //!< target height above the DEM surface 88 : double maxDistance{ 89 : 0.0}; //!< maximum distance from observer to compute value 90 : double curveCoeff{.85714}; //!< coefficient for atmospheric refraction 91 : OutputMode outputMode{OutputMode::Normal}; //!< Output information. 92 : //!< Normal, Height from DEM or Height from ground 93 : std::string outputFormat{}; //!< output raster format 94 : std::string outputFilename{}; //!< output raster filename 95 : CPLStringList creationOpts{}; //!< options for output raster creation 96 : CellMode cellMode{ 97 : CellMode::Edge}; //!< Mode of cell height calculation. 98 : }; 99 : 100 : /** 101 : * Constructor. 102 : * 103 : * @param opts Options to use when calculating viewshed. 104 : */ 105 12 : CPL_DLL explicit Viewshed(const Options &opts) : oOpts{opts}, poDstDS{} 106 : { 107 12 : } 108 : 109 : /** 110 : * Create the viewshed for the provided raster band. 111 : * 112 : * @param hBand Handle to the raster band. 113 : * @param pfnProgress Progress reporting callback function. 114 : * @param pProgressArg Argument to pass to the progress callback. 115 : */ 116 : CPL_DLL bool run(GDALRasterBandH hBand, GDALProgressFunc pfnProgress, 117 : void *pProgressArg = nullptr); 118 : 119 : /** 120 : * Fetch a pointer to the created raster band. 121 : * 122 : * @return Unique pointer to the viewshed dataset. 123 : */ 124 12 : CPL_DLL std::unique_ptr<GDALDataset> output() 125 : { 126 12 : return std::move(poDstDS); 127 : } 128 : 129 : private: 130 : Options oOpts; 131 : std::unique_ptr<GDALDataset> poDstDS; 132 : 133 : void setVisibility(int iPixel, double dfZ, double *padfZVal, 134 : std::vector<GByte> &vResult); 135 : double calcHeight(double dfZ, double dfZ2); 136 : }; 137 : 138 : } // namespace gdal