Viewshed 6.1.0
A C++ library for calculation of viewshed, inverse viewshed and visibility indices for both of those analyses.
Loading...
Searching...
No Matches
abstractviewshed.h
1#pragma once
2#include "viewshed_export.h"
3
4#include <limits>
5#include <mutex>
6#include <string>
7
8#include "BS_thread_pool.hpp"
9
10#include "simplerasters.h"
11
12#include "abstractviewshedalgorithm.h"
13#include "cellevent.h"
14#include "celleventposition.h"
15#include "defaultdatatypes.h"
16#include "los.h"
17#include "losnode.h"
18#include "viewshedtypes.h"
19#include "viewshedvalues.h"
20#include "visibility.h"
21
22using viewshed::LoS;
23using viewshed::ViewshedAlgorithms;
24
25namespace viewshed
26{
42 class DLL_API AbstractViewshed
43 {
44 public:
45 virtual ~AbstractViewshed() = default;
46
51 void initEventList();
52
57 void sortEventList();
58
65 void parseEventList( std::function<void( int, int )> progressCallback = []( int, int ) {} );
66
72 bool extractValuesFromEventList( std::shared_ptr<ProjectedSquareCellRaster> dem_, std::string fileName,
73 std::function<double( LoSNode )> func );
74
83 bool isInsideAngles( const double &eventEnterAngle, const double &eventExitAngle );
84
91 void setMaximalDistance( double distance );
92
99 void setAngles( double minAngle, double maxAngle );
100
106 void setMaxConcurentTasks( int maxTasks );
107
113 void setMaxThreads( int threads );
114
120
128 virtual void calculate(
129 std::function<void( std::string, double )> stepsTimingCallback = []( std::string text, double time ) {},
130 std::function<void( int, int )> progressCallback = []( int, int ) {} ) = 0;
131
141 virtual void addEventsFromCell( int &row, int &column, const double &pixelValue, bool &solveCell ) = 0;
142
148 virtual void submitToThreadpool( const CellEvent &e ) = 0;
149
156 std::shared_ptr<SingleBandRaster> resultRaster( int index = 0 );
157
165 bool saveResults( std::string location, std::string fileNamePrefix = "" );
166
174
182 OGRPoint point( int row, int col );
183
189 void setDefaultResultDataType( GDALDataType dataType );
190
196 long numberOfValidCells() { return mValidCells; };
197
203 long numberOfCellEvents() { return mCellEvents.size(); };
204
211 bool isValid() { return mValid; };
212
219 bool hasError()
220 {
221 std::lock_guard<std::mutex> lock( mTaskErrorMutex );
222 return !mTaskError.empty();
223 }
224
230 std::string errorMessage()
231 {
232 std::lock_guard<std::mutex> lock( mTaskErrorMutex );
233 return mTaskError;
234 }
235
241 void setVisibilityMask( std::shared_ptr<ProjectedSquareCellRaster> mask ) { mVisibilityMask = mask; }
242
248 long long totalCountOfLoSNodes() { return mTotalLosNodesCount; };
249
255 long long sizeOfEvents() { return sizeof( CellEvent ) * mCellEvents.size(); };
256
262 long long totalSizeOfLoS() { return sizeof( LoSNode ) * mTotalLosNodesCount; };
263
269 long long meanSizeOfLoS() { return ( totalSizeOfLoS() ) / mNumberOfLos; };
270
276 double initLastedSeconds() { return mTimeInit.count() / (double)1e9; }
277
283 double sortLastedSeconds() { return mTimeSort.count() / (double)1e9; }
284
290 double parseLastedSeconds() { return mTimeParse.count() / (double)1e9; }
291
298
305 {
306 if ( mResults->size() > 0 )
307 {
308 return mResults->at( 0 )->dataSize();
309 }
310 return 0;
311 }
312
318 int numberOfResultRasters() { return mResults->size(); }
319
326
333 CellEvent cellEvent( size_t i ) { return mCellEvents.at( i ); }
334
341 virtual void calculateVisibilityRaster() = 0;
342
349 bool saveVisibilityRaster( std::string filePath );
350
356
357 protected:
362 std::vector<LoSNode> mLosNodes;
363
368 std::vector<CellEvent> mCellEvents;
369
374 std::shared_ptr<ProjectedSquareCellRaster> mInputDsm;
375
380 std::shared_ptr<ProjectedSquareCellRaster> mVisibilityMask = nullptr;
381
387 std::shared_ptr<Point> mPoint;
388
393 std::shared_ptr<ViewshedAlgorithms> mVisibilityIndices;
394
399 GDALDataType mDataType = OUTPUT_RASTER_DATA_TYPE;
400
406 long mValidCells = 0;
407 long mTotalLosNodesCount = 0;
408 long mNumberOfLos = 0;
409
410 bool mInverseViewshed = false;
411
412 double mMaxDistance = std::numeric_limits<double>::max();
413 double mMinAngle = std::numeric_limits<double>::quiet_NaN();
414 double mMaxAngle = std::numeric_limits<double>::quiet_NaN();
415
421
422 double mCellSize = 0;
423 bool mValid = false;
424
429 std::mutex mTaskErrorMutex;
430
435 std::string mTaskError;
436
441 void recordTaskError( const std::string &message )
442 {
443 std::lock_guard<std::mutex> lock( mTaskErrorMutex );
444 if ( mTaskError.empty() )
445 {
446 mTaskError = message;
447 }
448 }
449
450 bool mCurvatureCorrections = false;
451 double mEarthDiameter = EARTH_DIAMETER;
452 double mRefractionCoefficient = REFRACTION_COEFFICIENT;
453
454 LoSNode mLosNodePoint;
455
456 std::vector<LoSNode> prepareLoSWithPoint( OGRPoint point );
457 bool validAngles();
458
463 BS::thread_pool mThreadPool;
464
465 std::shared_ptr<ResultRasters> mResults = std::make_shared<ResultRasters>();
466 std::shared_ptr<ProjectedSquareCellRaster> mVisibilityRaster = nullptr;
467
468 std::chrono::nanoseconds mTimeInit;
469 std::chrono::nanoseconds mTimeSort;
470 std::chrono::nanoseconds mTimeParse;
471
472 double mCellElevs[3];
473 double mAngleCenter, mAngleEnter, mAngleExit;
474 double mEventDistance;
475 CellEvent mEventCenter, mEventEnter, mEventExit = CellEvent();
476 CellEvent mEventEnterOpposite, mEventExitOpposite = CellEvent();
477 double mOppositeAngleEnter, mOppositeAngleExit;
478 LoSNode mLoSNodeTemp = LoSNode();
479 };
480
481} // namespace viewshed
void setMaximalDistance(double distance)
Set the Maximal Distance limit value. Limits event list creation to cells within this distance limit....
Definition abstractviewshed.cpp:91
bool isInsideAngles(const double &eventEnterAngle, const double &eventExitAngle)
Checks if cell with these enter and exit angles falls inside the angle limit.
Definition abstractviewshed.cpp:127
void initEventList()
Create event list for line sweep algorithm from input raster.
Definition abstractviewshed.cpp:32
void sortEventList()
Sort event list for parsing.
Definition abstractviewshed.cpp:151
void setMaxThreads(int threads)
Set the maximal number of threads that can be used for calculation.
Definition abstractviewshed.cpp:327
void setMaxConcurentTasks(int maxTasks)
Set the number of maximum Concurent Taks that can be prepared in thread pool for processing.
Definition abstractviewshed.cpp:325
void parseEventList(std::function< void(int, int)> progressCallback=[](int, int) {})
Parse event list by individual events.
Definition abstractviewshed.cpp:160
void setAngles(double minAngle, double maxAngle)
Set horizontal angle limits for viewshed calculation. 0.
Definition abstractviewshed.cpp:93
void prepareMemoryRasters()
Initialized output rasters in memory.
Definition abstractviewshed.cpp:12
bool extractValuesFromEventList(std::shared_ptr< ProjectedSquareCellRaster > dem_, std::string fileName, std::function< double(LoSNode)> func)
Extract values from event list into a raster and save it to file.
Definition abstractviewshed.cpp:240
Abstract class that represents viewshed calculation from this class specific implementations ( Viewsh...
Definition abstractviewshed.h:43
CellEvent cellEvent(size_t i)
Extract individual cell event from cell events.
Definition abstractviewshed.h:333
long long meanSizeOfLoS()
Mean size of solved LoS in bytes.
Definition abstractviewshed.h:269
virtual void addEventsFromCell(int &row, int &column, const double &pixelValue, bool &solveCell)=0
Function that creates events for event list from given cell.
long long totalCountOfLoSNodes()
Overall number of solved LoSNodes in all evaluated LoS.
Definition abstractviewshed.h:248
virtual void submitToThreadpool(const CellEvent &e)=0
Submit LoS with this CellEvent to threadpool for solving.
std::string mTaskError
First error that occurred during calculation, empty if there was none.
Definition abstractviewshed.h:435
bool saveResults(std::string location, std::string fileNamePrefix="")
Save all result rasters into the folder, optionally using name prefix.
Definition abstractviewshed.cpp:261
std::shared_ptr< ProjectedSquareCellRaster > mInputDsm
Input raster with digital surface model.
Definition abstractviewshed.h:374
long numberOfValidCells()
Number of valid cells in raster - cells not containing no data.
Definition abstractviewshed.h:196
bool isValid()
Check if the viewshed is valid, meaning that the important point is valid.
Definition abstractviewshed.h:211
void setDefaultResultDataType(GDALDataType dataType)
Set the Default Result Data Type to use in output rasters.
Definition abstractviewshed.cpp:23
void calculateVisibilityMask()
Calculate visibility mask raster.
Definition abstractviewshed.cpp:405
double parseLastedSeconds()
Number of seconds the parsing of event list lasted.
Definition abstractviewshed.h:290
int numberOfResultRasters()
Number of output rasters. Equals number of input algorithms.
Definition abstractviewshed.h:318
void recordTaskError(const std::string &message)
Store error message from a calculation task, keeping only the first one recorded.
Definition abstractviewshed.h:441
LoSNode statusNodeFromPoint(OGRPoint point)
Create LoSNode from OGRPoint.
Definition abstractviewshed.cpp:300
double initLastedSeconds()
Number of seconds the creation of event list lasted.
Definition abstractviewshed.h:276
std::string errorMessage()
Message of the first error that occurred during calculation, empty string if there was none.
Definition abstractviewshed.h:230
void setVisibilityMask(std::shared_ptr< ProjectedSquareCellRaster > mask)
Set the Visibility Mask for viewshed calculation.
Definition abstractviewshed.h:241
virtual void calculate(std::function< void(std::string, double)> stepsTimingCallback=[](std::string text, double time) {}, std::function< void(int, int)> progressCallback=[](int, int) {})=0
Calculate the viewshed. Covers all the steps - create list of events, sort list of events,...
long long totalSizeOfLoS()
Overall size of all solved LoS in bytes.
Definition abstractviewshed.h:262
long numberOfCellEvents()
Number of events in event list.
Definition abstractviewshed.h:203
virtual void calculateVisibilityRaster()=0
Calculate visibility mask of areas which are visible from point (visibility), or from which the point...
std::vector< LoSNode > mLosNodes
LoSNodes in currently solved LoS while parsing event list.
Definition abstractviewshed.h:362
OGRPoint point(int row, int col)
Create OGRPoint with coordinates of given row and column in the raster.
Definition abstractviewshed.cpp:290
GDALDataType mDataType
Data type of output rasters.
Definition abstractviewshed.h:399
int mDefaultBand
Default band to read from rasters.
Definition abstractviewshed.h:405
std::shared_ptr< Point > mPoint
Important point for visibility calculation. Either viewpoint (normal viewshed) or target point (inver...
Definition abstractviewshed.h:387
int mMaxNumberOfTasks
Maximal number of LoS to solve, that can be stored in threadpool.
Definition abstractviewshed.h:420
BS::thread_pool mThreadPool
Threadpool that takes care of preparing individual threads with LoS to be solved.
Definition abstractviewshed.h:463
long long sizeOfOutputRasters()
Size in bytes that all output raster takes.
Definition abstractviewshed.h:325
double sortLastedSeconds()
Number of seconds the sort of event list lasted.
Definition abstractviewshed.h:283
std::mutex mTaskErrorMutex
Guards access to mTaskError, which can be written from worker threads.
Definition abstractviewshed.h:429
long long sizeOfOutputRaster()
Size in bytes that one output raster takes.
Definition abstractviewshed.h:304
double processingLastedSeconds()
Number of seconds the calculation of viewshed indexes lasted.
Definition abstractviewshed.h:297
std::shared_ptr< SingleBandRaster > resultRaster(int index=0)
Extract individual result raster.
Definition abstractviewshed.cpp:259
std::shared_ptr< ProjectedSquareCellRaster > mVisibilityMask
Input raster with visibility calculation mask.
Definition abstractviewshed.h:380
std::shared_ptr< ViewshedAlgorithms > mVisibilityIndices
Visibility indexes to calculate while calculating this viewshed. As AbstractViewshedAlgorithm.
Definition abstractviewshed.h:393
bool hasError()
Check if an error occurred during calculation (e.g. in one of the worker threads).
Definition abstractviewshed.h:219
std::vector< CellEvent > mCellEvents
Event list. All cell events for the input raster.
Definition abstractviewshed.h:368
long long sizeOfEvents()
Size of cell event list in bytes.
Definition abstractviewshed.h:255
bool saveVisibilityRaster(std::string filePath)
Save visibility raster to file.
Definition abstractviewshed.cpp:388
Class representing cell events for Van Kreveld's plane sweep algorithm. Stores cell position (row and...
Definition cellevent.h:17
Representation of single cell that creates a point on LoS.
Definition losnode.h:19
Class that represents LoS for classic Viewshed calculation.
Definition los.h:25