Rev 117 | Rev 138 | 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); |
||
103 | //int iSpecies = headers.indexOf("species"); |
||
104 | //int iCount = headers.indexOf("count"); |
||
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"); |
||
110 | |||
111 | for (int i=1;i<lines.count();i++) { |
||
112 | QString &line = lines[i]; |
||
113 | //qDebug() << "line" << i << ":" << line; |
||
114 | Tree &tree = ru->newTree(); |
||
115 | QPointF f; |
||
116 | if (iX>=0 && iY>=0) { |
||
117 | f.setX( line.section(sep, iX, iX).toDouble() ); |
||
118 | f.setY( line.section(sep, iY, iY).toDouble() ); |
||
119 | f+=offset; |
||
120 | tree.setPosition(f); |
||
121 | } |
||
122 | if (iBhd>=0) |
||
123 | tree.setDbh(line.section(sep, iBhd, iBhd).toDouble()); |
||
124 | if (tree.dbh() < 5) |
||
125 | continue; // 5cm: lower threshold for the moment |
||
126 | if (iHeight>=0) |
||
127 | tree.setHeight(line.section(sep, iHeight, iHeight).toDouble()/100.); // convert from Picus-cm to m. |
||
128 | |||
129 | if (iSpecies>=0) { |
||
130 | int idx = picusSpeciesIds.indexOf(line.section(sep, iSpecies, iSpecies)); |
||
131 | QString speciesid="piab"; |
||
132 | if (idx>0) |
||
133 | speciesid = iLandSpeciesIds[idx]; |
||
134 | Species *s = speciesSet->species(speciesid); |
||
107 | Werner | 135 | |
136 | tree.setRU(ru); |
||
106 | Werner | 137 | tree.setSpecies(s); |
138 | } |
||
139 | |||
140 | tree.setup(); |
||
141 | } |
||
142 | //qDebug() << "loaded init-file contained" << lines.count() <<"lines."; |
||
143 | //qDebug() << "lines: " << lines; |
||
144 | |||
145 | } |