iLand
scriptgrid.h
Go to the documentation of this file.
1/********************************************************************************************
2** iLand - an individual based forest landscape and disturbance model
3** http://iland-model.org
4** Copyright (C) 2009- Werner Rammer, Rupert Seidl
5**
6** This program is free software: you can redistribute it and/or modify
7** it under the terms of the GNU General Public License as published by
8** the Free Software Foundation, either version 3 of the License, or
9** (at your option) any later version.
10**
11** This program is distributed in the hope that it will be useful,
12** but WITHOUT ANY WARRANTY; without even the implied warranty of
13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14** GNU General Public License for more details.
15**
16** You should have received a copy of the GNU General Public License
17** along with this program. If not, see <http://www.gnu.org/licenses/>.
18********************************************************************************************/
19#ifndef SCRIPTGRID_H
20#define SCRIPTGRID_H
21
22
23#include <QObject>
24#include <QJSValue>
25
26#include "grid.h"
27
28class ScriptGrid : public QObject
29{
30 Q_OBJECT
31 Q_PROPERTY(QString name READ name WRITE setName)
32 Q_PROPERTY(int width READ width)
33 Q_PROPERTY(int height READ height)
34 Q_PROPERTY(int count READ count)
35 Q_PROPERTY(int cellsize READ cellsize)
36 Q_PROPERTY(bool isValid READ isValid)
37public:
38 Q_INVOKABLE ScriptGrid(QObject *parent = nullptr);
39 Q_INVOKABLE ScriptGrid(QString fileName): ScriptGrid() { load(fileName); }
40
41 explicit ScriptGrid(Grid<double> *grid) { mVariableName="x"; mGrid = grid; mCreated++; mOwner=true; }
42
43 void setGrid(Grid<double> *grid) { mGrid = grid; mOwner=true; }
44 void setOwnership(bool should_delete) { mOwner = should_delete; }
46 static QJSValue createGrid(Grid<double> *grid, QString name=QString());
47
48 QString name() const {return mVariableName;}
49 Grid<double> *grid() { return mGrid; }
50
51 int width() const { return mGrid?mGrid->sizeX():-1; }
52 int height() const { return mGrid?mGrid->sizeY():-1; }
53 int count() const { return mGrid?mGrid->count():-1; }
54 int cellsize() const { return mGrid?mGrid->cellsize():-1; }
55 bool isValid() const { return mGrid?!mGrid->isEmpty():false; }
56
57 static void addToScriptEngine(QJSEngine *engine);
58signals:
59
60public slots:
63 bool create(int awidth, int aheight, int acellsize);
64
65 void setName(QString arg) { mVariableName = arg; }
68 void setOrigin(double x, double y);
70 QJSValue copy();
72 void clear();
73
75 void paint(double min_val, double max_val);
76
77 QString info();
78
80 void save(QString fileName);
81
84 bool load(QString fileName);
85
88 void apply(QString expression);
89
91 void combine(QString expression, QJSValue grid_object);
92
96 QJSValue resample(QJSValue grid_object);
97
98 void aggregate(int factor);
99
100
102 double sum(QString expression);
103
105 void sumTrees(QString expression, QString filter);
106
108 double value(int x, int y) const {return (isValid() && mGrid->isIndexValid(x,y)) ? mGrid->valueAtIndex(x,y) : -1.;}
110 void setValue(int x, int y, double value) const { if(isValid() && mGrid->isIndexValid(x,y)) mGrid->valueAtIndex(x,y)=value;}
111
113 double valueAt(double x, double y) const { return (isValid() && mGrid->coordValid(x,y)) ? mGrid->valueAt(x,y) : -1; }
115 void setValueAt(double x, double y, double value) const { if (isValid() && mGrid->coordValid(x,y)) mGrid->valueAt(x,y)=value; }
116
117 // coordinate functions
119 double metricX(int indexx) const { return isValid() ? mGrid->cellCenterPoint(QPoint(indexx, 0)).x() : 0.; }
121 double metricY(int indexy) const { return isValid() ? mGrid->cellCenterPoint(QPoint(0, indexy)).y() : 0.; }
123 int indexX(double meterx) const { return isValid() ? mGrid->indexAt(QPointF(meterx, 0)).x() : -1; }
125 int indexY(double metery) const { return isValid() ? mGrid->indexAt(QPointF(0., metery)).y() : -1; }
127 bool isIndexValid(int x, int y) const {return isValid() ? mGrid->isIndexValid(x,y): false; }
129 bool isCoordValid(double x, double y) const {return isValid() ? mGrid->coordValid(x, y) : false; }
130
131private:
132 Grid<double> *mGrid;
133 QString mVariableName;
134 bool mOwner; // true if grid should be deleted
135 static int mCreated;
136 static int mDeleted;
137};
138
139#endif // SCRIPTGRID_H
Grid class (template).
Definition: grid.h:44
Definition: scriptgrid.h:29
QJSValue resample(QJSValue grid_object)
resamples the grid to the extent/cellsize given by the grid 'grid_object' Resampling is "brute-force"...
Definition: scriptgrid.cpp:235
Q_INVOKABLE ScriptGrid(QString fileName)
Definition: scriptgrid.h:39
void clear()
fill the grid with 0-values
Definition: scriptgrid.cpp:104
QJSValue copy()
create a copy of the current grid and return a new script object
Definition: scriptgrid.cpp:90
bool load(QString fileName)
load from a file (ESRI ASC raster grid), relative to project root.
Definition: scriptgrid.cpp:134
void setOwnership(bool should_delete)
Definition: scriptgrid.h:44
double sum(QString expression)
apply the expression "expression" on all pixels of the grid and return the sum of the values
Definition: scriptgrid.cpp:286
int width() const
Definition: scriptgrid.h:51
QString name
Definition: scriptgrid.h:31
void paint(double min_val, double max_val)
draw the map
Definition: scriptgrid.cpp:110
double metricX(int indexx) const
convert a cellindex in x-direction to a metric value (x) representing the center of the cell
Definition: scriptgrid.h:119
ScriptGrid(Grid< double > *grid)
Definition: scriptgrid.h:41
void save(QString fileName)
save to a file as ESRI ASC raster grid (relative to project file)
Definition: scriptgrid.cpp:124
int height
Definition: scriptgrid.h:33
void setValueAt(double x, double y, double value) const
write values to the grid at metric coordinates x and y.
Definition: scriptgrid.h:115
QString info()
Definition: scriptgrid.cpp:117
int height() const
Definition: scriptgrid.h:52
static void addToScriptEngine(QJSEngine *engine)
Definition: scriptgrid.cpp:63
int width
Definition: scriptgrid.h:32
void apply(QString expression)
apply a function on the values of the grid, thus modifiying the grid (see the copy() function).
Definition: scriptgrid.cpp:165
bool isValid
Definition: scriptgrid.h:36
QString name() const
Definition: scriptgrid.h:48
int indexY(double metery) const
convert a metric value (y-axis) to an index for the y-axis (see also isIndexValid(),...
Definition: scriptgrid.h:125
void combine(QString expression, QJSValue grid_object)
combine multiple grids, and calculate the result of 'expression'
Definition: scriptgrid.cpp:188
static QJSValue createGrid(Grid< double > *grid, QString name=QString())
Definition: scriptgrid.cpp:54
int cellsize
Definition: scriptgrid.h:35
Grid< double > * grid()
Definition: scriptgrid.h:49
~ScriptGrid()
Definition: scriptgrid.cpp:45
void setValue(int x, int y, double value) const
write values to the grid
Definition: scriptgrid.h:110
double metricY(int indexy) const
convert a cellindex in y-direction to a metric value (y) representing the center of the cell
Definition: scriptgrid.h:121
bool create(int awidth, int aheight, int acellsize)
creates a grid with cellsize / width / height the offset of the grid (lower left corner) is given by ...
Definition: scriptgrid.cpp:72
void setOrigin(double x, double y)
set the origin of the grid (iLand coordiantes).
Definition: scriptgrid.cpp:81
void setName(QString arg)
Definition: scriptgrid.h:65
void sumTrees(QString expression, QString filter)
loop over all trees and create a sum of 'expression' for each cell. Filter trees with 'filter'
Definition: scriptgrid.cpp:310
bool isCoordValid(double x, double y) const
check if a given pair of metric coordinates (x/y) is valid for the grid
Definition: scriptgrid.h:129
bool isIndexValid(int x, int y) const
check if a given pair of indices (x/y) is valid for the grid
Definition: scriptgrid.h:127
double valueAt(double x, double y) const
access value of the grid in metric coordinates
Definition: scriptgrid.h:113
int cellsize() const
Definition: scriptgrid.h:54
int indexX(double meterx) const
convert a metric value (x-axis) to an index for the x-axis (see also isIndexValid(),...
Definition: scriptgrid.h:123
void aggregate(int factor)
Definition: scriptgrid.cpp:271
void setGrid(Grid< double > *grid)
Definition: scriptgrid.h:43
Q_INVOKABLE ScriptGrid(QObject *parent=nullptr)
Definition: scriptgrid.cpp:37
bool isValid() const
Definition: scriptgrid.h:55
int count
Definition: scriptgrid.h:34
int count() const
Definition: scriptgrid.h:53
double value(int x, int y) const
access values of the grid
Definition: scriptgrid.h:108