iLand
spatialanalysis.h
Go to the documentation of this file.
1#ifndef SPATIALANALYSIS_H
2#define SPATIALANALYSIS_H
3
4/********************************************************************************************
5** iLand - an individual based forest landscape and disturbance model
6** http://iland-model.org
7** Copyright (C) 2009- Werner Rammer, Rupert Seidl
8**
9** This program is free software: you can redistribute it and/or modify
10** it under the terms of the GNU General Public License as published by
11** the Free Software Foundation, either version 3 of the License, or
12** (at your option) any later version.
13**
14** This program is distributed in the hope that it will be useful,
15** but WITHOUT ANY WARRANTY; without even the implied warranty of
16** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17** GNU General Public License for more details.
18**
19** You should have received a copy of the GNU General Public License
20** along with this program. If not, see <http://www.gnu.org/licenses/>.
21********************************************************************************************/
22#include "grid.h"
23#include "layeredgrid.h"
24
25#include <QObject>
26#include <QJSValue>
27
28class RumpleIndex; // forward
29class SpatialLayeredGrid; // forward
35class SpatialAnalysis: public QObject
36{
37 Q_OBJECT
38 Q_PROPERTY(double rumpleIndex READ rumpleIndexFullArea)
39 Q_PROPERTY(QList<int> patchsizes READ patchsizes)
40
41public:
42 SpatialAnalysis(QObject *parent=0): QObject(parent), mRumple(0) {}
44 static void addToScriptEngine();
45
46 double rumpleIndexFullArea();
49 QList<int> extractPatches(Grid<double> &src, int min_size, QString fileName);
50 QList<int> patchsizes() const { return mLastPatches; }
51
52 static void runCrownProjection2m(FloatGrid *agrid=nullptr);
53
54public slots:
55 // API for Javascript
56 void saveRumpleGrid(QString fileName);
57 void saveCrownCoverGrid(QString fileName);
58 void saveCrownCoverGrid(QString fileName, QJSValue grid);
59
60 QJSValue patches(QJSValue grid, int min_size);
61
62private:
63 void calculateCrownCoverRU();
64 RumpleIndex *mRumple;
65 SpatialLayeredGrid *mLayers;
66 FloatGrid mCrownCoverGrid;
67 Grid<int> mClumpGrid;
68 QList<int> mLastPatches;
69 friend class SpatialLayeredGrid;
70
71};
72
79{
80public:
81 RumpleIndex(): mLastYear(-1) {}
82 void setup();
83 void calculate();
84 double value(const bool force_recalculate=false);
85 const FloatGrid &rumpleGrid() { value(); /* calculate if necessary */ return mRumpleGrid; }
86 //
87 double test_triangle_area();
88private:
89 double calculateSurfaceArea(const float *heights, const float cellsize);
90 FloatGrid mRumpleGrid;
91 double mRumpleIndex;
92 int mLastYear;
93};
94
95
96
98{
99public:
101 void setup();
102 int addGrid(const QString name, FloatGrid *grid);
103 const QStringList &gridNames() { return mGridNames; }
104
105 double value(const float x, const float y, const int index) const { checkGrid(index); return mGrids[index]->constValueAt(x,y); }
106 double value(const QPointF &world_coord, const int index) const { checkGrid(index); return mGrids[index]->constValueAt(world_coord); }
107 double value(const int ix, const int iy, const int index) const { checkGrid(index); return mGrids[index]->constValueAtIndex(ix,iy);}
108 double value(const int grid_index, const int index) const { checkGrid(index); return mGrids[index]->constValueAtIndex(grid_index);}
109 void range(double &rMin, double &rMax, const int index) const { rMin=9999999999.; rMax=-99999999999.;
110 for (int i=0;i<mGrids[index]->count(); ++i) {
111 rMin=qMin(rMin, value(i, index));
112 rMax=qMax(rMax, value(i,index));}}
113
114private:
115 inline void checkGrid(const int grid_index) const { if (mGrids[grid_index]==0) const_cast<SpatialLayeredGrid*>(this)->createGrid(grid_index); }
116 void createGrid(const int grid_index);
117 QStringList mGridNames;
118 QVector<FloatGrid*> mGrids;
119
120
121};
122#endif // SPATIALANALYSIS_H
Grid class (template).
Definition: grid.h:44
Definition: layeredgrid.h:32
The RumpleIndex is a spatial index relating surface area to ground area.
Definition: spatialanalysis.h:79
void setup()
sets up the grid
Definition: spatialanalysis.cpp:276
double test_triangle_area()
Definition: spatialanalysis.cpp:346
void calculate()
calculates (or re-calculates) index values
Definition: spatialanalysis.cpp:287
const FloatGrid & rumpleGrid()
return the rumple index for the full project area
Definition: spatialanalysis.h:85
double value(const bool force_recalculate=false)
calculates rumple index for the full project area
Definition: spatialanalysis.cpp:339
RumpleIndex()
Definition: spatialanalysis.h:81
The SpatialAnalysis class is the scripting class related to extra spatial analysis functions.
Definition: spatialanalysis.h:36
SpatialAnalysis(QObject *parent=0)
Definition: spatialanalysis.h:42
~SpatialAnalysis()
Definition: spatialanalysis.cpp:38
static void addToScriptEngine()
Definition: spatialanalysis.cpp:31
QList< int > patchsizes() const
Definition: spatialanalysis.h:50
double rumpleIndexFullArea()
retrieve the rumple index for the full landscape (one value)
Definition: spatialanalysis.cpp:44
QJSValue patches(QJSValue grid, int min_size)
Definition: spatialanalysis.cpp:188
static void runCrownProjection2m(FloatGrid *agrid=nullptr)
internal function that prepares crown cover for the whole landscape
Definition: spatialanalysis.cpp:233
QList< int > patchsizes
Definition: spatialanalysis.h:39
void saveRumpleGrid(QString fileName)
save a grid of rumple index values (resource unit level) to a ESRI grid file (ascii)
Definition: spatialanalysis.cpp:137
double rumpleIndex
Definition: spatialanalysis.h:38
QList< int > extractPatches(Grid< double > &src, int min_size, QString fileName)
extract patches ('clumps') and save the resulting grid to 'fileName' (if not empty).
Definition: spatialanalysis.cpp:55
void saveCrownCoverGrid(QString fileName)
save a grid if crown cover percentages (RU level) to a ESRI grid file (ascii)
Definition: spatialanalysis.cpp:146
Definition: spatialanalysis.h:98
void range(double &rMin, double &rMax, const int index) const
retrieve min and max of variable 'index'
Definition: spatialanalysis.h:109
double value(const int ix, const int iy, const int index) const
Definition: spatialanalysis.h:107
void setup()
initial setup of the grid
Definition: spatialanalysis.cpp:413
double value(const QPointF &world_coord, const int index) const
Definition: spatialanalysis.h:106
double value(const float x, const float y, const int index) const
Definition: spatialanalysis.h:105
double value(const int grid_index, const int index) const
Definition: spatialanalysis.h:108
int addGrid(const QString name, FloatGrid *grid)
adds a 'grid' named 'name'. returns index of the newly added grid.
Definition: spatialanalysis.cpp:424
const QStringList & gridNames()
Definition: spatialanalysis.h:103
SpatialLayeredGrid()
Definition: spatialanalysis.h:100