Subversion Repositories public iLand

Rev

Rev 893 | Rev 1014 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1
 
671 werner 2
/********************************************************************************************
3
**    iLand - an individual based forest landscape and disturbance model
4
**    http://iland.boku.ac.at
5
**    Copyright (C) 2009-  Werner Rammer, Rupert Seidl
6
**
7
**    This program is free software: you can redistribute it and/or modify
8
**    it under the terms of the GNU General Public License as published by
9
**    the Free Software Foundation, either version 3 of the License, or
10
**    (at your option) any later version.
11
**
12
**    This program is distributed in the hope that it will be useful,
13
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
**    GNU General Public License for more details.
16
**
17
**    You should have received a copy of the GNU General Public License
18
**    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
********************************************************************************************/
20
 
646 werner 21
#ifndef LAYEREDGRID_H
22
#define LAYEREDGRID_H
23
 
24
#include "grid.h"
25
 
873 werner 26
/** \class LayeredGrid
697 werner 27
    @ingroup tools
873 werner 28
    This is the base class for multi-layer grids in iLand. Use the LayeredGrid-template class
29
    for creating actual multi layer grids. The LayeredGridBase can be used for specializations.
646 werner 30
  */
31
 
32
class LayeredGridBase
33
{
34
public:
877 werner 35
    // layer description element
36
    class LayerElement {
37
    public:
38
        LayerElement() {}
889 werner 39
        LayerElement(QString aname, QString adesc, GridViewType type): name(aname), description(adesc), view_type(type) {}
877 werner 40
        QString name;
41
        QString description;
42
        GridViewType view_type;
43
    };
44
 
646 werner 45
    // access to properties
647 werner 46
    virtual int sizeX() const=0;
47
    virtual int sizeY() const=0;
48
    virtual QRectF metricRect() const=0;
49
    virtual QRectF cellRect(const QPoint &p) const=0;
962 werner 50
    virtual bool onClick(const QPointF &world_coord) const { Q_UNUSED(world_coord); return false; /*false: not handled*/ }
646 werner 51
    // available variables
877 werner 52
    /// list of stored layers
53
    virtual const QVector<LayeredGridBase::LayerElement> names() const=0;
54
    /// get layer index by name of the layer. returns -1 if layer is not available.
55
    virtual int indexOf(const QString &layer_name) const
56
    {
57
        for(int i=0;i<names().count();++i)
58
            if (names().at(i).name == layer_name)
59
                return i;
60
        return -1;
61
    }
646 werner 62
    // statistics
63
    /// retrieve min and max of variable 'index'
647 werner 64
    virtual void range(double &rMin, double &rMax, const int index) const=0;
646 werner 65
 
66
    // data access functions
647 werner 67
    virtual double value(const float x, const float y, const int index) const = 0;
648 werner 68
    virtual double value(const QPointF &world_coord, const int index) const = 0;
647 werner 69
    virtual double value(const int ix, const int iy, const int index) const = 0;
70
    virtual double value(const int grid_index, const int index) const = 0;
877 werner 71
    // for classified values
72
    virtual const QString labelvalue(const int value, const int index) const
73
    {
74
        Q_UNUSED(value)
75
        Q_UNUSED(index)
76
        return QLatin1Literal("-");
77
    }
78
 
646 werner 79
};
80
 
873 werner 81
/** \class LayeredGrid is a template for multi-layered grids in iLand.
82
 * Use your cell-class for T and provide at minium a value() and a names() function.
83
 * The names() provide the names of the individual layers (used e.g. in the GUI), the value() function
84
 * returns a cell-specific value for a specific layer (given by 'index' parameter).
85
 * */
646 werner 86
template <class T>
87
class LayeredGrid: public LayeredGridBase
88
{
89
public:
90
    LayeredGrid(const Grid<T>& grid) { mGrid = &grid; }
717 werner 91
    LayeredGrid() { mGrid = 0;  }
647 werner 92
    QRectF cellRect(const QPoint &p) const { return mGrid->cellRect(p); }
93
    QRectF metricRect() const { return mGrid->metricRect(); }
717 werner 94
    float cellsize() const { return mGrid->cellsize(); }
647 werner 95
    int sizeX() const { return mGrid->sizeX(); }
96
    int sizeY() const { return mGrid->sizeY();}
97
 
98
    virtual double value(const T& data, const int index) const = 0;
646 werner 99
    double value(const T* ptr, const int index) const { return value(mGrid->constValueAtIndex(mGrid->indexOf(ptr)), index);  }
100
    double value(const int grid_index, const int index) const { return value(mGrid->constValueAtIndex(grid_index), index); }
101
    double value(const float x, const float y, const int index) const { return value(mGrid->constValueAt(x,y), index); }
877 werner 102
    double value(const QPointF &world_coord, const int index) const { return mGrid->coordValid(world_coord)?value(mGrid->constValueAt(world_coord), index) : 0.; }
647 werner 103
    double value(const int ix, const int iy, const int index) const { return value(mGrid->constValueAtIndex(ix, iy), index); }
104
    void range(double &rMin, double &rMax, const int index) const { rMin=9999999999.; rMax=-99999999999.;
646 werner 105
                                                              for (int i=0;i<mGrid->count(); ++i) {
106
                                                                  rMin=qMin(rMin, value(i, index));
107
                                                                  rMax=qMax(rMax, value(i,index));}}
108
 
962 werner 109
 
646 werner 110
protected:
111
    const Grid<T> *mGrid;
112
};
113
 
717 werner 114
void modelToWorld(const Vector3D &From, Vector3D &To);
115
 
116
/** translate
117
 
118
  */
119
template <class T>
120
    QString gridToESRIRaster(const LayeredGrid<T> &grid, const QString name)
121
{
877 werner 122
        int index = grid.indexOf(name);
717 werner 123
        if (index<0)
124
            return QString();
125
        Vector3D model(grid.metricRect().left(), grid.metricRect().top(), 0.);
126
        Vector3D world;
127
        modelToWorld(model, world);
128
        QString result = QString("ncols %1\r\nnrows %2\r\nxllcorner %3\r\nyllcorner %4\r\ncellsize %5\r\nNODATA_value %6\r\n")
129
                                .arg(grid.sizeX())
130
                                .arg(grid.sizeY())
131
                                .arg(world.x(),0,'f').arg(world.y(),0,'f')
132
                                .arg(grid.cellsize()).arg(-9999);
133
 
134
        QString res;
135
        QTextStream ts(&res);
136
        QChar sep = QChar(' ');
137
        for (int y=grid.sizeY()-1;y>=0;--y){
138
            for (int x=0;x<grid.sizeX();x++){
139
                ts << grid.value(x,y,index) << sep;
140
            }
141
            ts << "\r\n";
142
        }
143
 
144
        return result + res;
145
}
146
 
147
 
877 werner 148
 
646 werner 149
#endif // LAYEREDGRID_H
877 werner 150
 
151
 
893 werner 152
 
153
 
154