Subversion Repositories public iLand

Rev

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

Rev Author Line No. Line
1
 
106 Werner 2
#include "global.h"
3
#include "standloader.h"
4
 
5
#include "grid.h"
6
#include "model.h"
7
#include "ressourceunit.h"
8
#include "speciesset.h"
9
 
10
#include "helper.h"
137 Werner 11
#include "expression.h"
140 Werner 12
#include "expressionwrapper.h"
106 Werner 13
 
14
#include <QtCore>
15
 
155 werner 16
QStringList picusSpeciesIds = QStringList() << "0" << "1" << "17";
17
QStringList iLandSpeciesIds = QStringList() << "piab" << "piab" << "fasy";
106 Werner 18
 
19
 
20
 
21
 
22
void StandLoader::processInit()
23
{
24
    GlobalSettings *g = GlobalSettings::instance();
25
    const XmlHelper &xml = g->settings();
26
    bool forEachCell = xml.hasNode("initialization.foreach");
27
    QString mode = xml.value("initialization.type", ""); // now only "picus"
28
    QString  fileName = xml.value("initialization.file", "");
29
    if (!QFile::exists(fileName))
30
        throw IException(QString("File %1 does not exist!").arg(fileName));
31
 
32
    Tree::resetStatistics();
33
    if (forEachCell) {
34
        loadFromPicus(fileName); // load in initial grid cell
113 Werner 35
        // we assume that all stands are equal, so wie simply COPY the trees and modify them afterwards
187 iland 36
        const Grid<ResourceUnit*> &ruGrid=mModel->RUgrid();
37
        ResourceUnit **p = ruGrid.begin();
106 Werner 38
        ++p; // skip the first...
39
        const QVector<Tree> &tocopy = mModel->ru()->trees();
40
        for (; p!=ruGrid.end(); ++p) {
41
            QRectF rect = (*p)->boundingBox();
42
            foreach(const Tree& tree, tocopy) {
43
                Tree &newtree = (*p)->newTree();
44
                newtree = tree; // copy tree data...
45
                newtree.setPosition(tree.position()+(*p)->boundingBox().topLeft());
107 Werner 46
                newtree.setRU(*p);
117 Werner 47
                newtree.setNewId();
106 Werner 48
            }
49
            //(*p)->trees()
50
            // loadFromPicus(fileName, rect.topLeft(), *p); -> do that for differing stands
51
            // copy
52
        }
53
        qDebug() << Tree::statCreated() << "trees loaded.";
137 Werner 54
    } else {
55
        // only one stand:
56
        loadFromPicus(fileName);
106 Werner 57
    }
137 Werner 58
 
59
    // evaluate debugging
60
    QString dbg_str = GlobalSettings::instance()->settings().paramValueString("debug_tree");
61
    if (!dbg_str.isEmpty()) {
140 Werner 62
       TreeWrapper tw;
63
       Expression dexp(dbg_str, &tw); // load expression dbg_str and enable external model variables
64
        //double *pid = dexp.addVar("id"); // binding
65
        //double *pru = dexp.addVar("ru"); // binding
137 Werner 66
        AllTreeIterator at(GlobalSettings::instance()->model());
67
        double result;
68
        while (Tree *t = at.next()) {
140 Werner 69
            tw.setTree(t);
70
            //*pid = t->id();
71
            //*pru = t->ru()->index();
137 Werner 72
            result = dexp.execute();
73
            if (result)
74
                t->enableDebugging();
75
        }
76
    }
106 Werner 77
}
78
 
187 iland 79
void StandLoader::loadFromPicus(const QString &fileName, QPointF offset, ResourceUnit *ru)
106 Werner 80
{
81
    if (!ru)
82
        ru = mModel->ru();
83
    SpeciesSet *speciesSet = mModel->ru()->speciesSet(); // of default RU
84
 
85
    QString text = Helper::loadTextFile(fileName);
86
    if (text.isEmpty()) {
87
        qDebug() << "file not found: " + fileName;
88
        return;
89
    }
90
 
91
    // cut out the <trees> </trees> part....
92
    QRegExp rx(".*<trees>(.*)</trees>.*");
93
    rx.indexIn(text, 0);
94
    if (rx.capturedTexts().count()<1)
95
        return;
96
    text = rx.cap(1).trimmed();
97
    QStringList lines=text.split('\n');
98
    if (lines.count()<2)
99
        return;
100
    char sep='\t';
101
    if (!lines[0].contains(sep))
102
        sep=';';
155 werner 103
    QStringList headers = lines[0].trimmed().split(sep);
138 Werner 104
 
106 Werner 105
    int iX = headers.indexOf("x");
106
    int iY = headers.indexOf("y");
107
    int iBhd = headers.indexOf("bhdfrom");
108
    int iHeight = headers.indexOf("treeheight");
109
    int iSpecies = headers.indexOf("species");
170 werner 110
    int iAge = headers.indexOf("age");
111
    if (iX==-1 || iY==-1 || iBhd==-1 || iSpecies==-1 || iHeight==-1 || iAge==-1)
138 Werner 112
        throw IException(QString("Initfile %1 is not valid!").arg(fileName));
106 Werner 113
 
138 Werner 114
    double dbh;
106 Werner 115
    for (int i=1;i<lines.count();i++) {
116
        QString &line = lines[i];
138 Werner 117
        dbh = line.section(sep, iBhd, iBhd).toDouble();
118
        if (dbh<5.)
119
            continue;
106 Werner 120
        Tree &tree = ru->newTree();
121
        QPointF f;
122
        if (iX>=0 && iY>=0) {
123
           f.setX( line.section(sep, iX, iX).toDouble() );
124
           f.setY( line.section(sep, iY, iY).toDouble() );
125
           f+=offset;
126
           tree.setPosition(f);
127
        }
128
 
138 Werner 129
        tree.setDbh(dbh);
130
        tree.setHeight(line.section(sep, iHeight, iHeight).toDouble()/100.); // convert from Picus-cm to m.
170 werner 131
        tree.setAge(line.section(sep, iAge, iAge).toInt());
138 Werner 132
        int idx = picusSpeciesIds.indexOf(line.section(sep, iSpecies, iSpecies));
133
        QString speciesid="piab";
134
        if (idx>0)
135
            speciesid = iLandSpeciesIds[idx];
136
        Species *s = speciesSet->species(speciesid);
137
        if (!ru || !s)
138
            throw IException("Loading init-file: either ressource unit or species invalid.");
107 Werner 139
 
138 Werner 140
        tree.setRU(ru);
141
        tree.setSpecies(s);
106 Werner 142
        tree.setup();
143
    }
144
    //qDebug() << "loaded init-file contained" << lines.count() <<"lines.";
145
    //qDebug() << "lines: " << lines;
146
 
147
}