Subversion Repositories public iLand

Rev

Rev 889 | Rev 1011 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 889 Rev 919
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
/********************************************************************************************
2
/********************************************************************************************
3
**    iLand - an individual based forest landscape and disturbance model
3
**    iLand - an individual based forest landscape and disturbance model
4
**    http://iland.boku.ac.at
4
**    http://iland.boku.ac.at
5
**    Copyright (C) 2009-  Werner Rammer, Rupert Seidl
5
**    Copyright (C) 2009-  Werner Rammer, Rupert Seidl
6
**
6
**
7
**    This program is free software: you can redistribute it and/or modify
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
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
9
**    the Free Software Foundation, either version 3 of the License, or
10
**    (at your option) any later version.
10
**    (at your option) any later version.
11
**
11
**
12
**    This program is distributed in the hope that it will be useful,
12
**    This program is distributed in the hope that it will be useful,
13
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
**    GNU General Public License for more details.
15
**    GNU General Public License for more details.
16
**
16
**
17
**    You should have received a copy of the GNU General Public License
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/>.
18
**    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
********************************************************************************************/
19
********************************************************************************************/
20
20
21
#include "global.h"
21
#include "global.h"
22
#include "environment.h"
22
#include "environment.h"
23
#include "debugtimer.h"
23
#include "debugtimer.h"
24
#include "helper.h"
24
#include "helper.h"
25
#include "csvfile.h"
25
#include "csvfile.h"
26
#include "gisgrid.h"
26
#include "gisgrid.h"
27
27
28
#include "climate.h"
28
#include "climate.h"
29
#include "speciesset.h"
29
#include "speciesset.h"
30
30
31
31
32
/** Represents the input of various variables with regard to climate, soil properties and more.
32
/** Represents the input of various variables with regard to climate, soil properties and more.
33
  @ingroup tools
33
  @ingroup tools
34
    Data is read from various sources and presented to the core model with a standardized interface.
34
    Data is read from various sources and presented to the core model with a standardized interface.
35
    see http://iland.boku.ac.at/simulation+extent
35
    see http://iland.boku.ac.at/simulation+extent
36
*/
36
*/
37
Environment::Environment()
37
Environment::Environment()
38
{
38
{
39
    mInfile = 0;
39
    mInfile = 0;
40
    mGrid = 0;
40
    mGrid = 0;
41
    mGridMode = false;
41
    mGridMode = false;
42
    mCurrentSpeciesSet = 0;
42
    mCurrentSpeciesSet = 0;
43
    mCurrentClimate = 0;
43
    mCurrentClimate = 0;
44
    mCurrentID = 0;
44
    mCurrentID = 0;
45
}
45
}
46
Environment::~Environment()
46
Environment::~Environment()
47
{
47
{
48
    if (mInfile) {
48
    if (mInfile) {
49
        delete mInfile;
49
        delete mInfile;
50
    }
50
    }
51
    if (mGrid)
51
    if (mGrid)
52
        delete mGrid;
52
        delete mGrid;
53
}
53
}
54
54
55
bool Environment::loadFromFile(const QString &fileName)
55
bool Environment::loadFromFile(const QString &fileName)
56
{
56
{
57
    QString source = Helper::loadTextFile(GlobalSettings::instance()->path(fileName));
57
    QString source = Helper::loadTextFile(GlobalSettings::instance()->path(fileName));
58
    if (source.isEmpty())
58
    if (source.isEmpty())
59
        throw IException(QString("Environment: input file does not exist or is empty (%1)").arg(fileName));
59
        throw IException(QString("Environment: input file does not exist or is empty (%1)").arg(fileName));
60
    return loadFromString(source);
60
    return loadFromString(source);
61
}
61
}
62
62
63
// ******** specific keys *******
63
// ******** specific keys *******
64
const QString speciesKey = "model.species.source";
64
const QString speciesKey = "model.species.source";
65
const QString climateKey = "model.climate.tableName";
65
const QString climateKey = "model.climate.tableName";
66
66
67
bool Environment::loadFromString(const QString &source)
67
bool Environment::loadFromString(const QString &source)
68
{
68
{
69
    try {
69
    try {
70
        if (mInfile)
70
        if (mInfile)
71
            delete mInfile;
71
            delete mInfile;
72
        mInfile = new CSVFile();
72
        mInfile = new CSVFile();
73
73
74
        mInfile->loadFromString(source);
74
        mInfile->loadFromString(source);
75
        mKeys = mInfile->captions();
75
        mKeys = mInfile->captions();
76
76
77
        XmlHelper xml(GlobalSettings::instance()->settings());
77
        XmlHelper xml(GlobalSettings::instance()->settings());
78
        mSpeciesSets.clear(); // note: the objects are not destroyed - potential memory leak.
78
        mSpeciesSets.clear(); // note: the objects are not destroyed - potential memory leak.
79
        mClimate.clear();
79
        mClimate.clear();
80
        mRowCoordinates.clear();
80
        mRowCoordinates.clear();
81
        mCreatedObjects.clear();
81
        mCreatedObjects.clear();
82
        mCurrentID = 0;
82
        mCurrentID = 0;
83
83
84
        int index;
84
        int index;
85
        if (mGridMode) {
85
        if (mGridMode) {
86
            int id = mInfile->columnIndex("id");
86
            int id = mInfile->columnIndex("id");
87
            if (id<0)
87
            if (id<0)
88
                throw IException("Environment:: (grid mode) input file has no 'id' column!");
88
                throw IException("Environment:: (grid mode) input file has no 'id' column!");
89
            for (int row=0;row<mInfile->rowCount();row++) {
89
            for (int row=0;row<mInfile->rowCount();row++) {
90
                mRowCoordinates[mInfile->value(row, id).toString()] = row;
90
                mRowCoordinates[mInfile->value(row, id).toString()] = row;
91
            }
91
            }
92
92
93
        } else {
93
        } else {
94
            // ***  Matrix mode ******
94
            // ***  Matrix mode ******
95
            // each row must contain 'x' and 'y' coordinates
95
            // each row must contain 'x' and 'y' coordinates
96
            // setup coordinates (x,y)
96
            // setup coordinates (x,y)
97
            int ix,iy;
97
            int ix,iy;
98
            ix = mInfile->columnIndex("x");
98
            ix = mInfile->columnIndex("x");
99
            iy = mInfile->columnIndex("y");
99
            iy = mInfile->columnIndex("y");
100
            if (ix<0 || iy<0)
100
            if (ix<0 || iy<0)
101
                throw IException("Environment:: (matrix mode) input file has no x/y coordinates!");
101
                throw IException("Environment:: (matrix mode) input file has no x/y coordinates!");
102
            for (int row=0;row<mInfile->rowCount();row++) {
102
            for (int row=0;row<mInfile->rowCount();row++) {
103
                QString key=QString("%1_%2")
103
                QString key=QString("%1_%2")
104
                        .arg(mInfile->value(row, ix).toString())
104
                        .arg(mInfile->value(row, ix).toString())
105
                        .arg(mInfile->value(row, iy).toString());
105
                        .arg(mInfile->value(row, iy).toString());
106
                mRowCoordinates[key] = row;
106
                mRowCoordinates[key] = row;
107
            }
107
            }
108
        }
108
        }
109
109
110
110
111
111
112
        // ******** setup of Species Sets *******
112
        // ******** setup of Species Sets *******
113
        if ((index = mKeys.indexOf(speciesKey))>-1) {
113
        if ((index = mKeys.indexOf(speciesKey))>-1) {
114
            DebugTimer t("environment:load species");
114
            DebugTimer t("environment:load species");
115
            QStringList speciesNames = mInfile->column(index);
115
            QStringList speciesNames = mInfile->column(index);
116
            speciesNames.removeDuplicates();
116
            speciesNames.removeDuplicates();
117
            qDebug() << "creating species sets:" << speciesNames;
117
            qDebug() << "creating species sets:" << speciesNames;
118
            foreach (const QString &name, speciesNames) {
118
            foreach (const QString &name, speciesNames) {
119
                xml.setNodeValue(speciesKey,name); // set xml value
119
                xml.setNodeValue(speciesKey,name); // set xml value
120
                // create species sets
120
                // create species sets
121
                SpeciesSet *set = new SpeciesSet();
121
                SpeciesSet *set = new SpeciesSet();
122
                mSpeciesSets.push_back(set);
122
                mSpeciesSets.push_back(set);
123
                mCreatedObjects[name] = (void*)set;
123
                mCreatedObjects[name] = (void*)set;
124
                set->setup();
124
                set->setup();
125
            }
125
            }
126
            qDebug() << mSpeciesSets.count() << "species sets created.";
126
            qDebug() << mSpeciesSets.count() << "species sets created.";
127
        } else {
127
        } else {
128
            // no species sets specified
128
            // no species sets specified
129
            SpeciesSet *speciesSet = new SpeciesSet();
129
            SpeciesSet *speciesSet = new SpeciesSet();
130
            mSpeciesSets.push_back(speciesSet);
130
            mSpeciesSets.push_back(speciesSet);
131
            speciesSet->setup();
131
            speciesSet->setup();
132
            mCurrentSpeciesSet = speciesSet;
132
            mCurrentSpeciesSet = speciesSet;
133
        }
133
        }
134
134
135
        // ******** setup of Climate *******
135
        // ******** setup of Climate *******
136
        if ((index = mKeys.indexOf(climateKey))>-1) {
136
        if ((index = mKeys.indexOf(climateKey))>-1) {
137
            DebugTimer t("environment:load climate");
137
            DebugTimer t("environment:load climate");
138
            QStringList climateNames = mInfile->column(index);
138
            QStringList climateNames = mInfile->column(index);
139
            climateNames.removeDuplicates();
139
            climateNames.removeDuplicates();
140
            qDebug() << "creating climatae: " << climateNames;
140
            qDebug() << "creating climatae: " << climateNames;
141
            foreach (QString name, climateNames) {
141
            foreach (QString name, climateNames) {
142
                xml.setNodeValue(climateKey,name); // set xml value
142
                xml.setNodeValue(climateKey,name); // set xml value
143
                // create climate sets
143
                // create climate sets
144
                Climate *climate = new Climate();
144
                Climate *climate = new Climate();
145
                mClimate.push_back(climate);
145
                mClimate.push_back(climate);
146
                mCreatedObjects[name]=(void*)climate;
146
                mCreatedObjects[name]=(void*)climate;
147
                climate->setup();
147
                climate->setup();
148
            }
148
            }
149
            qDebug() << mClimate.count() << "climates created";
149
            qDebug() << mClimate.count() << "climates created";
150
        } else {
150
        } else {
151
            // no climate defined - setup default climate
151
            // no climate defined - setup default climate
152
            Climate *c = new Climate();
152
            Climate *c = new Climate();
153
            mClimate.push_back(c);
153
            mClimate.push_back(c);
154
            c->setup();
154
            c->setup();
155
            mCurrentClimate = c;
155
            mCurrentClimate = c;
156
        }
156
        }
157
        if (!mCurrentClimate && mClimate.count()>0)
157
        if (!mCurrentClimate && mClimate.count()>0)
158
            mCurrentClimate = mClimate[0];
158
            mCurrentClimate = mClimate[0];
159
        if (!mCurrentSpeciesSet && mSpeciesSets.count()>0)
159
        if (!mCurrentSpeciesSet && mSpeciesSets.count()>0)
160
            mCurrentSpeciesSet = mSpeciesSets[0];
160
            mCurrentSpeciesSet = mSpeciesSets[0];
161
        return true;
161
        return true;
162
162
163
    } catch(const IException &e) {
163
    } catch(const IException &e) {
164
        QString addMsg;
164
        QString addMsg;
165
        if (!mClimate.isEmpty())
165
        if (!mClimate.isEmpty())
166
            addMsg = QString("last Climate: %1 ").arg(mClimate.last()->name());
166
            addMsg = QString("last Climate: %1 ").arg(mClimate.last()->name());
167
        if (!mSpeciesSets.isEmpty())
167
        if (!mSpeciesSets.isEmpty())
168
            addMsg += QString("last Speciesset table: %1").arg(mSpeciesSets.last()->name());
168
            addMsg += QString("last Speciesset table: %1").arg(mSpeciesSets.last()->name());
169
        QString error_msg = QString("An error occured during the setup of the environment: \n%1\n%2").arg(e.message()).arg(addMsg);
169
        QString error_msg = QString("An error occured during the setup of the environment: \n%1\n%2").arg(e.message()).arg(addMsg);
170
        qDebug() << error_msg;
170
        qDebug() << error_msg;
171
        Helper::msg(error_msg);
171
        Helper::msg(error_msg);
172
        return false;
172
        return false;
173
    }
173
    }
174
}
174
}
175
175
176
/** sets the "pointer" to a "position" (metric coordinates).
176
/** sets the "pointer" to a "position" (metric coordinates).
177
    All specified values are set (also the climate/species-set pointers).
177
    All specified values are set (also the climate/species-set pointers).
178
*/
178
*/
179
void Environment::setPosition(const QPointF position)
179
void Environment::setPosition(const QPointF position)
180
{
180
{
181
    // no changes occur, when the "environment" is not loaded
181
    // no changes occur, when the "environment" is not loaded
182
    if (!isSetup())
182
    if (!isSetup())
183
        return;
183
        return;
184
    QString key;
184
    QString key;
185
    int ix=-1, iy=-1, id=-1;
185
    int ix=-1, iy=-1, id=-1;
186
    if (mGridMode) {
186
    if (mGridMode) {
187
        // grid mode
187
        // grid mode
188
        id = mGrid->value(position);
188
        id = mGrid->value(position);
189
        mCurrentID = id;
189
        mCurrentID = id;
190
        key = QString::number(id);
190
        key = QString::number(id);
191
        if (id==-1)
191
        if (id==-1)
192
            return; // no data for the resource unit
192
            return; // no data for the resource unit
193
    } else {
193
    } else {
194
        // access data in the matrix by resource unit indices
194
        // access data in the matrix by resource unit indices
195
        ix = int(position.x() / 100.); // suppose size of 1 ha for each coordinate
195
        ix = int(position.x() / 100.); // suppose size of 1 ha for each coordinate
196
        iy = int(position.y() / 100.);
196
        iy = int(position.y() / 100.);
197
        mCurrentID++; // to have Ids for each resource unit
197
        mCurrentID++; // to have Ids for each resource unit
198
198
199
        key=QString("%1_%2").arg(ix).arg(iy);
199
        key=QString("%1_%2").arg(ix).arg(iy);
200
    }
200
    }
201
201
202
    if (mRowCoordinates.contains(key)) {
202
    if (mRowCoordinates.contains(key)) {
203
        XmlHelper xml(GlobalSettings::instance()->settings());
203
        XmlHelper xml(GlobalSettings::instance()->settings());
204
        int row = mRowCoordinates[key];
204
        int row = mRowCoordinates[key];
205
        QString value;
205
        QString value;
206
        if (logLevelInfo()) qDebug() << "settting up point" << position << "with row" << row;
206
        if (logLevelInfo()) qDebug() << "settting up point" << position << "with row" << row;
207
        for (int col=0;col<mInfile->colCount(); col++) {
207
        for (int col=0;col<mInfile->colCount(); col++) {
208
            if (mKeys[col]=="x" || mKeys[col]=="y" || mKeys[col]=="id") // ignore "x" and "y" keys
-
 
-
 
208
            if (mKeys[col]=="id") {
-
 
209
                mCurrentID = mInfile->value(row, col).toInt();
-
 
210
                continue;
-
 
211
            }
-
 
212
            if (mKeys[col]=="x" || mKeys[col]=="y") // ignore "x" and "y" keys
209
                continue;
213
                continue;
210
            value = mInfile->value(row,col).toString();
214
            value = mInfile->value(row,col).toString();
211
            if (logLevelInfo()) qDebug() << "set" << mKeys[col] << "to" << value;
215
            if (logLevelInfo()) qDebug() << "set" << mKeys[col] << "to" << value;
212
            xml.setNodeValue(mKeys[col], value);
216
            xml.setNodeValue(mKeys[col], value);
213
            // special handling for constructed objects:
217
            // special handling for constructed objects:
214
            if (mKeys[col]==speciesKey)
218
            if (mKeys[col]==speciesKey)
215
                mCurrentSpeciesSet = (SpeciesSet*)mCreatedObjects[value];
219
                mCurrentSpeciesSet = (SpeciesSet*)mCreatedObjects[value];
216
            if (mKeys[col]==climateKey)
220
            if (mKeys[col]==climateKey)
217
                mCurrentClimate = (Climate*)mCreatedObjects[value];
221
                mCurrentClimate = (Climate*)mCreatedObjects[value];
218
222
219
        }
223
        }
220
224
221
    } else {
225
    } else {
222
        if (mGridMode)
226
        if (mGridMode)
223
            throw IException(QString("Environment:setposition: invalid grid id (or not present in input file): %1m/%2m (mapped to id %3).")
227
            throw IException(QString("Environment:setposition: invalid grid id (or not present in input file): %1m/%2m (mapped to id %3).")
224
                             .arg(position.x()).arg(position.y()).arg(id));
228
                             .arg(position.x()).arg(position.y()).arg(id));
225
        else
229
        else
226
            throw IException(QString("Environment:setposition: invalid coordinates (or not present in input file): %1m/%2m (mapped to indices %3/%4).")
230
            throw IException(QString("Environment:setposition: invalid coordinates (or not present in input file): %1m/%2m (mapped to indices %3/%4).")
227
                             .arg(position.x()).arg(position.y()).arg(ix).arg(iy));
231
                             .arg(position.x()).arg(position.y()).arg(ix).arg(iy));
228
    }
232
    }
229
}
233
}
230
234
231
bool Environment::setGridMode(const QString &grid_file_name)
235
bool Environment::setGridMode(const QString &grid_file_name)
232
{
236
{
233
    mGrid = new GisGrid();
237
    mGrid = new GisGrid();
234
    mGrid->loadFromFile(grid_file_name);
238
    mGrid->loadFromFile(grid_file_name);
235
    mGridMode = true;
239
    mGridMode = true;
236
    return true;
240
    return true;
237
}
241
}
238
 
242