Subversion Repositories public iLand

Rev

Rev 1221 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1211 werner 1
/********************************************************************************************
2
**    iLand - an individual based forest landscape and disturbance model
3
**    http://iland.boku.ac.at
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
********************************************************************************************/
1081 werner 19
#ifndef SCRIPTGRID_H
20
#define SCRIPTGRID_H
21
 
22
 
23
#include <QObject>
24
#include <QJSValue>
25
 
26
#include "grid.h"
27
 
28
class 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)
37
public:
38
    explicit ScriptGrid(QObject *parent = 0);
1085 werner 39
    explicit ScriptGrid(Grid<double> *grid) { mVariableName="x"; mGrid = grid; mCreated++; }
1081 werner 40
    void setGrid(Grid<double> *grid) { mGrid = grid; }
41
    ~ScriptGrid();
42
    static QJSValue createGrid(Grid<double> *grid, QString name=QString());
43
 
44
    QString name() const {return mVariableName;}
45
    Grid<double> *grid() { return mGrid; }
46
 
47
    int width() const { return mGrid?mGrid->sizeX():-1; }
48
    int height() const { return mGrid?mGrid->sizeY():-1; }
49
    int count() const { return mGrid?mGrid->count():-1; }
50
    int cellsize() const { return mGrid?mGrid->cellsize():-1; }
51
    bool isValid() const { return mGrid?!mGrid->isEmpty():false; }
52
 
53
signals:
54
 
55
public slots:
56
    void setName(QString arg) { mVariableName = arg; }
57
    /// create a copy of the current grid and return a new script object
58
    QJSValue copy();
59
    /// fill the grid with 0-values
60
    void clear();
61
 
62
    /// draw the map
63
    void paint(double min_val, double max_val);
64
 
1090 werner 65
    QString info();
66
 
67
    /// save to a file as ESRI ASC raster grid (relative to project file)
1081 werner 68
    void save(QString fileName);
69
 
1090 werner 70
    /// load from a file (ESRI ASC raster grid), relative to project root.
71
    /// return true on success.
72
    bool load(QString fileName);
73
 
1081 werner 74
    /// apply a function on the values of the grid, thus modifiying the grid (see the copy() function).
75
    /// The function is given as an Expression and is run for each cell of the grid.
76
    void apply(QString expression);
77
 
78
    /// combine multiple grids, and calculate the result of 'expression'
79
    void combine(QString expression, QJSValue grid_object);
80
 
1084 werner 81
 
1081 werner 82
    /// apply the expression "expression" on all pixels of the grid and return the sum of the values
83
    double sum(QString expression);
84
 
85
    /// access values of the grid
1082 werner 86
    double value(int x, int y) const {return (isValid() && mGrid->isIndexValid(x,y)) ? mGrid->valueAtIndex(x,y) : -1.;}
1081 werner 87
    /// write values to the grid
88
    void setValue(int x, int y, double value) const { if(isValid() && mGrid->isIndexValid(x,y)) mGrid->valueAtIndex(x,y)=value;}
89
 
90
 
91
private:
92
    Grid<double> *mGrid;
93
    QString mVariableName;
1085 werner 94
    static int mCreated;
95
    static int mDeleted;
1081 werner 96
};
97
 
98
#endif // SCRIPTGRID_H