Rev 138 | Rev 141 | 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 | |||
16 | QStringList picusSpeciesIds = QStringList() << "0" << "17"; |
||
17 | QStringList iLandSpeciesIds = QStringList() << "piab" << "fasy"; |
||
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 |
106 | Werner | 36 | const Grid<RessourceUnit*> &ruGrid=mModel->RUgrid(); |
37 | RessourceUnit **p = ruGrid.begin(); |
||
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 | |||
65 | |||
66 | //double *pid = dexp.addVar("id"); // binding |
||
67 | //double *pru = dexp.addVar("ru"); // binding |
||
137 | Werner | 68 | AllTreeIterator at(GlobalSettings::instance()->model()); |
69 | double result; |
||
70 | while (Tree *t = at.next()) { |
||
140 | Werner | 71 | tw.setTree(t); |
72 | //*pid = t->id(); |
||
73 | //*pru = t->ru()->index(); |
||
137 | Werner | 74 | result = dexp.execute(); |
75 | if (result) |
||
76 | t->enableDebugging(); |
||
77 | } |
||
78 | |||
79 | |||
80 | } |
||
106 | Werner | 81 | } |
82 | |||
83 | void StandLoader::loadFromPicus(const QString &fileName, QPointF offset, RessourceUnit *ru) |
||
84 | { |
||
85 | if (!ru) |
||
86 | ru = mModel->ru(); |
||
87 | SpeciesSet *speciesSet = mModel->ru()->speciesSet(); // of default RU |
||
88 | |||
89 | QString text = Helper::loadTextFile(fileName); |
||
90 | if (text.isEmpty()) { |
||
91 | qDebug() << "file not found: " + fileName; |
||
92 | return; |
||
93 | } |
||
94 | |||
95 | // cut out the <trees> </trees> part.... |
||
96 | QRegExp rx(".*<trees>(.*)</trees>.*"); |
||
97 | rx.indexIn(text, 0); |
||
98 | if (rx.capturedTexts().count()<1) |
||
99 | return; |
||
100 | text = rx.cap(1).trimmed(); |
||
101 | QStringList lines=text.split('\n'); |
||
102 | if (lines.count()<2) |
||
103 | return; |
||
104 | char sep='\t'; |
||
105 | if (!lines[0].contains(sep)) |
||
106 | sep=';'; |
||
107 | QStringList headers = lines[0].split(sep); |
||
138 | Werner | 108 | |
106 | Werner | 109 | int iX = headers.indexOf("x"); |
110 | int iY = headers.indexOf("y"); |
||
111 | int iBhd = headers.indexOf("bhdfrom"); |
||
112 | int iHeight = headers.indexOf("treeheight"); |
||
113 | int iSpecies = headers.indexOf("species"); |
||
138 | Werner | 114 | if (iX==-1 || iY==-1 || iBhd==-1 || iSpecies==-1 || iHeight==-1) |
115 | throw IException(QString("Initfile %1 is not valid!").arg(fileName)); |
||
106 | Werner | 116 | |
138 | Werner | 117 | double dbh; |
106 | Werner | 118 | for (int i=1;i<lines.count();i++) { |
119 | QString &line = lines[i]; |
||
138 | Werner | 120 | dbh = line.section(sep, iBhd, iBhd).toDouble(); |
121 | if (dbh<5.) |
||
122 | continue; |
||
106 | Werner | 123 | Tree &tree = ru->newTree(); |
124 | QPointF f; |
||
125 | if (iX>=0 && iY>=0) { |
||
126 | f.setX( line.section(sep, iX, iX).toDouble() ); |
||
127 | f.setY( line.section(sep, iY, iY).toDouble() ); |
||
128 | f+=offset; |
||
129 | tree.setPosition(f); |
||
130 | } |
||
131 | |||
138 | Werner | 132 | tree.setDbh(dbh); |
133 | tree.setHeight(line.section(sep, iHeight, iHeight).toDouble()/100.); // convert from Picus-cm to m. |
||
134 | int idx = picusSpeciesIds.indexOf(line.section(sep, iSpecies, iSpecies)); |
||
135 | QString speciesid="piab"; |
||
136 | if (idx>0) |
||
137 | speciesid = iLandSpeciesIds[idx]; |
||
138 | Species *s = speciesSet->species(speciesid); |
||
139 | if (!ru || !s) |
||
140 | throw IException("Loading init-file: either ressource unit or species invalid."); |
||
107 | Werner | 141 | |
138 | Werner | 142 | tree.setRU(ru); |
143 | tree.setSpecies(s); |
||
106 | Werner | 144 | tree.setup(); |
145 | } |
||
146 | //qDebug() << "loaded init-file contained" << lines.count() <<"lines."; |
||
147 | //qDebug() << "lines: " << lines; |
||
148 | |||
149 | } |