Subversion Repositories public iLand

Rev

Rev 550 | Rev 568 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 550 Rev 567
Line 1... Line 1...
1
Redirecting to URL 'https://iland.boku.ac.at/svn/iland/tags/release_1.0/src/core/environment.cpp':
1
Redirecting to URL 'https://iland.boku.ac.at/svn/iland/tags/release_1.0/src/core/environment.cpp':
2
#include "global.h"
2
#include "global.h"
3
#include "environment.h"
3
#include "environment.h"
4
#include "helper.h"
4
#include "helper.h"
5
#include "csvfile.h"
5
#include "csvfile.h"
6
-
 
-
 
6
#include "gisgrid.h"
7
7
8
#include "climate.h"
8
#include "climate.h"
9
#include "speciesset.h"
9
#include "speciesset.h"
10
10
11
/** Represents the physical simulation site with regard to climate, soil properties and such.
11
/** Represents the physical simulation site with regard to climate, soil properties and such.
12
    Data is read from various sources and presented to the core model with a standardized interface.
12
    Data is read from various sources and presented to the core model with a standardized interface.
13
*/
13
*/
14
Environment::Environment()
14
Environment::Environment()
15
{
15
{
16
    mInfile=0;
-
 
-
 
16
    mInfile = 0;
-
 
17
    mGrid = 0;
-
 
18
    mGridMode = false;
-
 
19
    mCurrentSpeciesSet = 0;
-
 
20
    mCurrentClimate = 0;
17
}
21
}
18
Environment::~Environment()
22
Environment::~Environment()
19
{
23
{
20
    if (mInfile) {
24
    if (mInfile) {
21
        delete mInfile;
25
        delete mInfile;
22
    }
26
    }
-
 
27
    if (mGrid)
-
 
28
        delete mGrid;
23
}
29
}
24
30
25
bool Environment::loadFromFile(const QString &fileName)
31
bool Environment::loadFromFile(const QString &fileName)
26
{
32
{
27
    QString source = Helper::loadTextFile(GlobalSettings::instance()->path(fileName));
33
    QString source = Helper::loadTextFile(GlobalSettings::instance()->path(fileName));
Line 49... Line 55...
49
        mClimate.clear();
55
        mClimate.clear();
50
        mRowCoordinates.clear();
56
        mRowCoordinates.clear();
51
        mCreatedObjects.clear();
57
        mCreatedObjects.clear();
52
58
53
        int index;
59
        int index;
54
        // setup coordinates (x,y)
-
 
55
        int ix,iy;
-
 
56
        ix = mInfile->columnIndex("x");
-
 
57
        iy = mInfile->columnIndex("y");
-
 
58
        if (ix<0 || iy<0)
-
 
59
            throw IException("Environment:: input file has no x/y coordinates!");
-
 
60
        for (int row=0;row<mInfile->rowCount();row++) {
-
 
61
            QString key=QString("%1_%2")
-
 
-
 
60
        if (mGridMode) {
-
 
61
            int id = mInfile->columnIndex("id");
-
 
62
            if (id<0)
-
 
63
                throw IException("Environment:: (grid mode) input file has no 'id' column!");
-
 
64
            for (int row=0;row<mInfile->rowCount();row++) {
-
 
65
                mRowCoordinates[mInfile->value(row, id).toString()] = row;
-
 
66
            }
-
 
67
-
 
68
        } else {
-
 
69
            // ***  Matrix mode ******
-
 
70
            // each row must contain 'x' and 'y' coordinates
-
 
71
            // setup coordinates (x,y)
-
 
72
            int ix,iy;
-
 
73
            ix = mInfile->columnIndex("x");
-
 
74
            iy = mInfile->columnIndex("y");
-
 
75
            if (ix<0 || iy<0)
-
 
76
                throw IException("Environment:: (matrix mode) input file has no x/y coordinates!");
-
 
77
            for (int row=0;row<mInfile->rowCount();row++) {
-
 
78
                QString key=QString("%1_%2")
62
                        .arg(mInfile->value(row, ix).toString())
79
                        .arg(mInfile->value(row, ix).toString())
63
                        .arg(mInfile->value(row, iy).toString());
80
                        .arg(mInfile->value(row, iy).toString());
64
            mRowCoordinates[key] = row;
-
 
-
 
81
                mRowCoordinates[key] = row;
-
 
82
            }
65
        }
83
        }
66
84
67
85
68
86
69
        // ******** setup of Species Sets *******
87
        // ******** setup of Species Sets *******
Line 109... Line 127...
109
            Climate *c = new Climate();
127
            Climate *c = new Climate();
110
            mClimate.push_back(c);
128
            mClimate.push_back(c);
111
            c->setup();
129
            c->setup();
112
            mCurrentClimate = c;
130
            mCurrentClimate = c;
113
        }
131
        }
-
 
132
        if (!mCurrentClimate && mClimate.count()>0)
-
 
133
            mCurrentClimate = mClimate[0];
-
 
134
        if (!mCurrentSpeciesSet && mSpeciesSets.count()>0)
-
 
135
            mCurrentSpeciesSet = mSpeciesSets[0];
114
        return true;
136
        return true;
115
137
116
    } catch(const IException &e) {
138
    } catch(const IException &e) {
117
        QString addMsg;
139
        QString addMsg;
118
        if (!mClimate.isEmpty())
140
        if (!mClimate.isEmpty())
Line 132... Line 154...
132
void Environment::setPosition(const QPointF position)
154
void Environment::setPosition(const QPointF position)
133
{
155
{
134
    // no changes occur, when the "environment" is not loaded
156
    // no changes occur, when the "environment" is not loaded
135
    if (!isSetup())
157
    if (!isSetup())
136
        return;
158
        return;
-
 
159
    QString key;
-
 
160
    int ix, iy, id;
-
 
161
    if (mGridMode) {
-
 
162
        // grid mode
-
 
163
        id = mGrid->value(position);
-
 
164
        key = QString::number(id);
-
 
165
        if (id==-1)
-
 
166
            return; // no data for the resource unit
-
 
167
    } else {
-
 
168
        // access data in the matrix by resource unit indices
-
 
169
        ix = int(position.x() / 100.); // suppose size of 1 ha for each coordinate
-
 
170
        iy = int(position.y() / 100.);
-
 
171
        key=QString("%1_%2").arg(ix).arg(iy);
-
 
172
    }
137
173
138
    int ix, iy;
-
 
139
    ix = int(position.x() / 100.); // suppose size of 1 ha for each coordinate
-
 
140
    iy = int(position.y() / 100.);
-
 
141
    QString key=QString("%1_%2").arg(ix).arg(iy);
-
 
142
    if (mRowCoordinates.contains(key)) {
174
    if (mRowCoordinates.contains(key)) {
143
        XmlHelper xml(GlobalSettings::instance()->settings());
175
        XmlHelper xml(GlobalSettings::instance()->settings());
144
        int row = mRowCoordinates[key];
176
        int row = mRowCoordinates[key];
145
        QString value;
177
        QString value;
146
        if (logLevelInfo()) qDebug() << "settting up point" << position << "with row" << row;
178
        if (logLevelInfo()) qDebug() << "settting up point" << position << "with row" << row;
147
        for (int col=0;col<mInfile->colCount(); col++) {
179
        for (int col=0;col<mInfile->colCount(); col++) {
148
            if (mKeys[col]=="x" || mKeys[col]=="y") // ignore "x" and "y" keys
-
 
-
 
180
            if (mKeys[col]=="x" || mKeys[col]=="y" || mKeys[col]=="id") // ignore "x" and "y" keys
149
                continue;
181
                continue;
150
            value = mInfile->value(row,col).toString();
182
            value = mInfile->value(row,col).toString();
151
            if (logLevelInfo()) qDebug() << "set" << mKeys[col] << "to" << value;
183
            if (logLevelInfo()) qDebug() << "set" << mKeys[col] << "to" << value;
152
            xml.setNodeValue(mKeys[col], value);
184
            xml.setNodeValue(mKeys[col], value);
153
            // special handling for constructed objects:
185
            // special handling for constructed objects:
Line 156... Line 188...
156
            if (mKeys[col]==climateKey)
188
            if (mKeys[col]==climateKey)
157
                mCurrentClimate = (Climate*)mCreatedObjects[value];
189
                mCurrentClimate = (Climate*)mCreatedObjects[value];
158
190
159
        }
191
        }
160
192
161
    } else
-
 
162
        throw IException(QString("Environment:setposition: invalid coordinates (or not present in input file): %1m/%2m (mapped to indices %3/%4).")
-
 
163
                         .arg(position.x()).arg(position.y()).arg(ix).arg(iy));
-
 
-
 
193
    } else {
-
 
194
        if (mGridMode)
-
 
195
            throw IException(QString("Environment:setposition: invalid grid id (or not present in input file): %1m/%2m (mapped to id %3).")
-
 
196
                             .arg(position.x()).arg(position.y()).arg(id));
-
 
197
        else
-
 
198
            throw IException(QString("Environment:setposition: invalid coordinates (or not present in input file): %1m/%2m (mapped to indices %3/%4).")
-
 
199
                             .arg(position.x()).arg(position.y()).arg(ix).arg(iy));
-
 
200
    }
-
 
201
}
-
 
202
-
 
203
bool Environment::setGridMode(const QString &grid_file_name)
-
 
204
{
-
 
205
    mGrid = new GisGrid();
-
 
206
    mGrid->loadFromFile(grid_file_name);
-
 
207
    mGridMode = true;
-
 
208
    return true;
164
}
209
}