|
| 1 | +/****************************************************************************** |
| 2 | + * |
| 3 | + * Project: GDAL |
| 4 | + * Purpose: gdal "raster viewshed" subcommand |
| 5 | + * Author: Even Rouault <even dot rouault at spatialys.com> |
| 6 | + * |
| 7 | + ****************************************************************************** |
| 8 | + * Copyright (c) 2025, Even Rouault <even dot rouault at spatialys.com> |
| 9 | + * |
| 10 | + * SPDX-License-Identifier: MIT |
| 11 | + ****************************************************************************/ |
| 12 | + |
| 13 | +#include "gdalalg_raster_viewshed.h" |
| 14 | + |
| 15 | +#include "cpl_conv.h" |
| 16 | +#include "cpl_vsi_virtual.h" |
| 17 | + |
| 18 | +#include "commonutils.h" |
| 19 | +#include "gdal_priv.h" |
| 20 | + |
| 21 | +#include "viewshed/cumulative.h" |
| 22 | +#include "viewshed/viewshed.h" |
| 23 | + |
| 24 | +//! @cond Doxygen_Suppress |
| 25 | + |
| 26 | +#ifndef _ |
| 27 | +#define _(x) (x) |
| 28 | +#endif |
| 29 | + |
| 30 | +/************************************************************************/ |
| 31 | +/* GDALRasterViewshedAlgorithm::GDALRasterViewshedAlgorithm() */ |
| 32 | +/************************************************************************/ |
| 33 | + |
| 34 | +GDALRasterViewshedAlgorithm::GDALRasterViewshedAlgorithm() |
| 35 | + : GDALAlgorithm(NAME, DESCRIPTION, HELP_URL) |
| 36 | +{ |
| 37 | + AddProgressArg(); |
| 38 | + |
| 39 | + AddOpenOptionsArg(&m_openOptions); |
| 40 | + AddInputFormatsArg(&m_inputFormats) |
| 41 | + .AddMetadataItem(GAAMDI_REQUIRED_CAPABILITIES, {GDAL_DCAP_RASTER}); |
| 42 | + AddInputDatasetArg(&m_inputDataset, GDAL_OF_RASTER); |
| 43 | + |
| 44 | + AddOutputDatasetArg(&m_outputDataset, GDAL_OF_RASTER); |
| 45 | + AddOutputFormatArg(&m_format, /* bStreamAllowed = */ false, |
| 46 | + /* bGDALGAllowed = */ false) |
| 47 | + .AddMetadataItem(GAAMDI_REQUIRED_CAPABILITIES, |
| 48 | + {GDAL_DCAP_RASTER, GDAL_DCAP_CREATE}); |
| 49 | + AddCreationOptionsArg(&m_creationOptions); |
| 50 | + AddOverwriteArg(&m_overwrite); |
| 51 | + |
| 52 | + AddArg("position", 'p', _("Observer position"), &m_observerPos) |
| 53 | + .AddAlias("pos") |
| 54 | + .SetMetaVar("<X,Y> or <X,Y,H>") |
| 55 | + .SetMinCount(2) |
| 56 | + .SetMaxCount(3) |
| 57 | + .SetRequired() |
| 58 | + .SetRepeatedArgAllowed(false); |
| 59 | + AddArg("target-height", 0, |
| 60 | + _("Height of the target above the DEM surface in the height unit of " |
| 61 | + "the DEM."), |
| 62 | + &m_targetHeight) |
| 63 | + .SetDefault(m_targetHeight); |
| 64 | + AddArg("mode", 0, _("Sets what information the output contains."), |
| 65 | + &m_outputMode) |
| 66 | + .SetChoices("normal", "DEM", "ground", "cumulative") |
| 67 | + .SetDefault(m_outputMode); |
| 68 | + |
| 69 | + AddArg("max-distance", 0, |
| 70 | + _("Maximum distance from observer to compute visibility. It is also " |
| 71 | + "used to clamp the extent of the output raster."), |
| 72 | + &m_maxDistance) |
| 73 | + .SetMinValueIncluded(0); |
| 74 | + AddArg("curvature-coefficient", 0, |
| 75 | + _("Coefficient to consider the effect of the curvature and " |
| 76 | + "refraction."), |
| 77 | + &m_curveCoefficient) |
| 78 | + .SetMinValueIncluded(0); |
| 79 | + |
| 80 | + AddBandArg(&m_band).SetDefault(m_band); |
| 81 | + AddArg("visible-value", 0, _("Pixel value to set for visible areas"), |
| 82 | + &m_visibleVal) |
| 83 | + .SetDefault(m_visibleVal) |
| 84 | + .SetMinValueIncluded(0) |
| 85 | + .SetMaxValueIncluded(255); |
| 86 | + AddArg("invisible-value", 0, _("Pixel value to set for invisible areas"), |
| 87 | + &m_invisibleVal) |
| 88 | + .SetDefault(m_invisibleVal) |
| 89 | + .SetMinValueIncluded(0) |
| 90 | + .SetMaxValueIncluded(255); |
| 91 | + AddArg("out-of-range-value", 0, |
| 92 | + _("Pixel value to set for the cells that fall outside of the range " |
| 93 | + "specified by the observer location and the maximum distance"), |
| 94 | + &m_outOfRangeVal) |
| 95 | + .SetDefault(m_outOfRangeVal) |
| 96 | + .SetMinValueIncluded(0) |
| 97 | + .SetMaxValueIncluded(255); |
| 98 | + AddArg("dstnodata", 0, |
| 99 | + _("The value to be set for the cells in the output raster that have " |
| 100 | + "no data."), |
| 101 | + &m_dstNoData) |
| 102 | + .SetMinValueIncluded(0) |
| 103 | + .SetMaxValueIncluded(255); |
| 104 | + AddArg("observer-spacing", 0, _("Cell Spacing between observers"), |
| 105 | + &m_observerSpacing) |
| 106 | + .SetDefault(m_observerSpacing) |
| 107 | + .SetMinValueIncluded(1); |
| 108 | + AddArg("num-threads", 'j', _("Number of computation threads to use"), |
| 109 | + &m_numThreads) |
| 110 | + .SetDefault(m_numThreads) |
| 111 | + .SetMinValueIncluded(1) |
| 112 | + .SetMaxValueIncluded(255); |
| 113 | +} |
| 114 | + |
| 115 | +/************************************************************************/ |
| 116 | +/* GDALRasterViewshedAlgorithm::RunImpl() */ |
| 117 | +/************************************************************************/ |
| 118 | + |
| 119 | +bool GDALRasterViewshedAlgorithm::RunImpl(GDALProgressFunc pfnProgress, |
| 120 | + void *pProgressData) |
| 121 | +{ |
| 122 | + if (m_outputDataset.GetDatasetRef()) |
| 123 | + { |
| 124 | + ReportError(CE_Failure, CPLE_NotSupported, |
| 125 | + "gdal raster viewshed does not support outputting to an " |
| 126 | + "already opened output dataset"); |
| 127 | + return false; |
| 128 | + } |
| 129 | + |
| 130 | + VSIStatBufL sStat; |
| 131 | + if (!m_overwrite && !m_outputDataset.GetName().empty() && |
| 132 | + (VSIStatL(m_outputDataset.GetName().c_str(), &sStat) == 0 || |
| 133 | + std::unique_ptr<GDALDataset>( |
| 134 | + GDALDataset::Open(m_outputDataset.GetName().c_str())))) |
| 135 | + { |
| 136 | + ReportError(CE_Failure, CPLE_AppDefined, |
| 137 | + "File '%s' already exists. Specify the --overwrite " |
| 138 | + "option to overwrite it.", |
| 139 | + m_outputDataset.GetName().c_str()); |
| 140 | + return false; |
| 141 | + } |
| 142 | + |
| 143 | + gdal::viewshed::Options opts; |
| 144 | + |
| 145 | + opts.observer.x = m_observerPos[0]; |
| 146 | + opts.observer.y = m_observerPos[1]; |
| 147 | + if (m_observerPos.size() == 3) |
| 148 | + opts.observer.z = m_observerPos[2]; |
| 149 | + else |
| 150 | + opts.observer.z = 2; |
| 151 | + |
| 152 | + opts.targetHeight = m_targetHeight; |
| 153 | + |
| 154 | + opts.maxDistance = m_maxDistance; |
| 155 | + |
| 156 | + opts.curveCoeff = m_curveCoefficient; |
| 157 | + if (!GetArg("curvature-coefficient")->IsExplicitlySet()) |
| 158 | + { |
| 159 | + opts.curveCoeff = gdal::viewshed::adjustCurveCoeff( |
| 160 | + opts.curveCoeff, |
| 161 | + GDALDataset::ToHandle(m_inputDataset.GetDatasetRef())); |
| 162 | + } |
| 163 | + |
| 164 | + opts.visibleVal = m_visibleVal; |
| 165 | + opts.invisibleVal = m_invisibleVal; |
| 166 | + opts.outOfRangeVal = m_outOfRangeVal; |
| 167 | + opts.nodataVal = m_dstNoData; |
| 168 | + |
| 169 | + if (m_outputMode == "normal") |
| 170 | + opts.outputMode = gdal::viewshed::OutputMode::Normal; |
| 171 | + else if (m_outputMode == "DEM") |
| 172 | + opts.outputMode = gdal::viewshed::OutputMode::DEM; |
| 173 | + else if (m_outputMode == "ground") |
| 174 | + opts.outputMode = gdal::viewshed::OutputMode::Ground; |
| 175 | + else if (m_outputMode == "cumulative") |
| 176 | + opts.outputMode = gdal::viewshed::OutputMode::Cumulative; |
| 177 | + |
| 178 | + opts.observerSpacing = m_observerSpacing; |
| 179 | + opts.numJobs = static_cast<uint8_t>(m_numThreads); |
| 180 | + |
| 181 | + opts.outputFilename = m_outputDataset.GetName(); |
| 182 | + opts.outputFormat = m_format; |
| 183 | + if (opts.outputFormat.empty()) |
| 184 | + { |
| 185 | + opts.outputFormat = |
| 186 | + GetOutputDriverForRaster(opts.outputFilename.c_str()); |
| 187 | + if (opts.outputFormat.empty()) |
| 188 | + { |
| 189 | + ReportError(CE_Failure, CPLE_AppDefined, |
| 190 | + "Cannot guess output driver from output filename"); |
| 191 | + return false; |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + opts.creationOpts = CPLStringList(m_creationOptions); |
| 196 | + |
| 197 | + if (opts.outputMode == gdal::viewshed::OutputMode::Cumulative) |
| 198 | + { |
| 199 | + auto poSrcDS = m_inputDataset.GetDatasetRef(); |
| 200 | + auto poSrcDriver = poSrcDS->GetDriver(); |
| 201 | + if (EQUAL(poSrcDS->GetDescription(), "") || !poSrcDriver || |
| 202 | + EQUAL(poSrcDriver->GetDescription(), "MEM")) |
| 203 | + { |
| 204 | + ReportError( |
| 205 | + CE_Failure, CPLE_AppDefined, |
| 206 | + "In cumulative mode, the input dataset must be opened by name"); |
| 207 | + return false; |
| 208 | + } |
| 209 | + if (EQUAL(opts.outputFormat.c_str(), "MEM")) |
| 210 | + { |
| 211 | + ReportError(CE_Failure, CPLE_AppDefined, |
| 212 | + "In cumulative mode, the output dataset cannot be a " |
| 213 | + "MEM dataset"); |
| 214 | + return false; |
| 215 | + } |
| 216 | + gdal::viewshed::Cumulative oViewshed(opts); |
| 217 | + const bool bSuccess = oViewshed.run( |
| 218 | + m_inputDataset.GetName().c_str(), |
| 219 | + pfnProgress ? pfnProgress : GDALDummyProgress, pProgressData); |
| 220 | + if (bSuccess) |
| 221 | + { |
| 222 | + m_outputDataset.Set(std::unique_ptr<GDALDataset>( |
| 223 | + GDALDataset::Open(opts.outputFilename.c_str(), |
| 224 | + GDAL_OF_RASTER | GDAL_OF_VERBOSE_ERROR, |
| 225 | + nullptr, nullptr, nullptr))); |
| 226 | + } |
| 227 | + return bSuccess; |
| 228 | + } |
| 229 | + else |
| 230 | + { |
| 231 | + gdal::viewshed::Viewshed oViewshed(opts); |
| 232 | + const bool bSuccess = oViewshed.run( |
| 233 | + GDALRasterBand::ToHandle( |
| 234 | + m_inputDataset.GetDatasetRef()->GetRasterBand(m_band)), |
| 235 | + pfnProgress ? pfnProgress : GDALDummyProgress, pProgressData); |
| 236 | + if (bSuccess) |
| 237 | + { |
| 238 | + m_outputDataset.Set(oViewshed.output()); |
| 239 | + } |
| 240 | + |
| 241 | + return bSuccess; |
| 242 | + } |
| 243 | +} |
| 244 | + |
| 245 | +//! @endcond |
0 commit comments