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