Subversion Repositories public iLand

Rev

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