Rev 194 | Rev 247 | 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" |
||
189 | iland | 7 | #include "resourceunit.h" |
106 | Werner | 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(); |
||
192 | werner | 25 | XmlHelper xml(g->settings().node("model.initialization")); |
191 | werner | 26 | |
192 | werner | 27 | bool for_each_ru = xml.valueBool("foreach"); |
191 | werner | 28 | |
192 | werner | 29 | QString mode = xml.value("type", ""); // now only "picus" |
30 | QString fileName = xml.value("file", ""); |
||
106 | Werner | 31 | if (!QFile::exists(fileName)) |
32 | throw IException(QString("File %1 does not exist!").arg(fileName)); |
||
33 | |||
34 | Tree::resetStatistics(); |
||
191 | werner | 35 | if (for_each_ru) { |
106 | Werner | 36 | loadFromPicus(fileName); // load in initial grid cell |
113 | Werner | 37 | // we assume that all stands are equal, so wie simply COPY the trees and modify them afterwards |
187 | iland | 38 | const Grid<ResourceUnit*> &ruGrid=mModel->RUgrid(); |
39 | ResourceUnit **p = ruGrid.begin(); |
||
194 | werner | 40 | if (!p) |
41 | throw IException("Standloader: invalid resource unit pointer!"); |
||
106 | Werner | 42 | ++p; // skip the first... |
43 | const QVector<Tree> &tocopy = mModel->ru()->trees(); |
||
44 | for (; p!=ruGrid.end(); ++p) { |
||
45 | QRectF rect = (*p)->boundingBox(); |
||
46 | foreach(const Tree& tree, tocopy) { |
||
47 | Tree &newtree = (*p)->newTree(); |
||
48 | newtree = tree; // copy tree data... |
||
49 | newtree.setPosition(tree.position()+(*p)->boundingBox().topLeft()); |
||
107 | Werner | 50 | newtree.setRU(*p); |
117 | Werner | 51 | newtree.setNewId(); |
106 | Werner | 52 | } |
53 | //(*p)->trees() |
||
54 | // loadFromPicus(fileName, rect.topLeft(), *p); -> do that for differing stands |
||
55 | // copy |
||
56 | } |
||
57 | qDebug() << Tree::statCreated() << "trees loaded."; |
||
137 | Werner | 58 | } else { |
59 | // only one stand: |
||
60 | loadFromPicus(fileName); |
||
106 | Werner | 61 | } |
137 | Werner | 62 | |
63 | // evaluate debugging |
||
64 | QString dbg_str = GlobalSettings::instance()->settings().paramValueString("debug_tree"); |
||
65 | if (!dbg_str.isEmpty()) { |
||
140 | Werner | 66 | TreeWrapper tw; |
67 | Expression dexp(dbg_str, &tw); // load expression dbg_str and enable external model variables |
||
68 | //double *pid = dexp.addVar("id"); // binding |
||
69 | //double *pru = dexp.addVar("ru"); // binding |
||
137 | Werner | 70 | AllTreeIterator at(GlobalSettings::instance()->model()); |
71 | double result; |
||
72 | while (Tree *t = at.next()) { |
||
140 | Werner | 73 | tw.setTree(t); |
74 | //*pid = t->id(); |
||
75 | //*pru = t->ru()->index(); |
||
137 | Werner | 76 | result = dexp.execute(); |
77 | if (result) |
||
78 | t->enableDebugging(); |
||
79 | } |
||
80 | } |
||
106 | Werner | 81 | } |
82 | |||
187 | iland | 83 | void StandLoader::loadFromPicus(const QString &fileName, QPointF offset, ResourceUnit *ru) |
106 | Werner | 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=';'; |
||
155 | werner | 107 | QStringList headers = lines[0].trimmed().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"); |
||
170 | werner | 114 | int iAge = headers.indexOf("age"); |
204 | werner | 115 | if (iX==-1 || iY==-1 || iBhd==-1 || iSpecies==-1 || iHeight==-1) |
138 | Werner | 116 | throw IException(QString("Initfile %1 is not valid!").arg(fileName)); |
106 | Werner | 117 | |
138 | Werner | 118 | double dbh; |
106 | Werner | 119 | for (int i=1;i<lines.count();i++) { |
120 | QString &line = lines[i]; |
||
138 | Werner | 121 | dbh = line.section(sep, iBhd, iBhd).toDouble(); |
122 | if (dbh<5.) |
||
123 | continue; |
||
106 | Werner | 124 | Tree &tree = ru->newTree(); |
125 | QPointF f; |
||
126 | if (iX>=0 && iY>=0) { |
||
127 | f.setX( line.section(sep, iX, iX).toDouble() ); |
||
128 | f.setY( line.section(sep, iY, iY).toDouble() ); |
||
129 | f+=offset; |
||
130 | tree.setPosition(f); |
||
131 | } |
||
132 | |||
138 | Werner | 133 | tree.setDbh(dbh); |
134 | tree.setHeight(line.section(sep, iHeight, iHeight).toDouble()/100.); // convert from Picus-cm to m. |
||
204 | werner | 135 | if (iAge>=0) |
136 | tree.setAge(line.section(sep, iAge, iAge).toInt()); |
||
137 | else |
||
138 | tree.setAge(10); |
||
138 | Werner | 139 | int idx = picusSpeciesIds.indexOf(line.section(sep, iSpecies, iSpecies)); |
140 | QString speciesid="piab"; |
||
141 | if (idx>0) |
||
142 | speciesid = iLandSpeciesIds[idx]; |
||
143 | Species *s = speciesSet->species(speciesid); |
||
144 | if (!ru || !s) |
||
145 | throw IException("Loading init-file: either ressource unit or species invalid."); |
||
107 | Werner | 146 | |
138 | Werner | 147 | tree.setRU(ru); |
148 | tree.setSpecies(s); |
||
106 | Werner | 149 | tree.setup(); |
150 | } |
||
151 | //qDebug() << "loaded init-file contained" << lines.count() <<"lines."; |
||
152 | //qDebug() << "lines: " << lines; |
||
153 | |||
154 | } |