Rev 388 | Rev 393 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 388 | Rev 389 | ||
---|---|---|---|
Line 143... | Line 143... | ||
143 | return; |
143 | return; |
144 | }
|
144 | }
|
145 | loadSingleTreeList(content, ru, fileName); |
145 | loadSingleTreeList(content, ru, fileName); |
146 | }
|
146 | }
|
147 | 147 | ||
148 | void StandLoader::loadSingleTreeList(const QString &content, ResourceUnit*ru, const QString &fileName) |
- | |
- | 148 | /** load a list of trees (given by content) to a resource unit. Param fileName is just for error reporting.
|
|
- | 149 | returns the number of loaded trees.
|
|
- | 150 | */
|
|
- | 151 | int StandLoader::loadSingleTreeList(const QString &content, ResourceUnit *ru, const QString &fileName) |
|
149 | {
|
152 | {
|
150 | if (!ru) |
153 | if (!ru) |
151 | ru = mModel->ru(); |
154 | ru = mModel->ru(); |
152 | Q_ASSERT(ru!=0); |
155 | Q_ASSERT(ru!=0); |
153 | 156 | ||
Line 158... | Line 161... | ||
158 | // cut out the <trees> </trees> part if present
|
161 | // cut out the <trees> </trees> part if present
|
159 | if (content.contains("<trees>")) { |
162 | if (content.contains("<trees>")) { |
160 | QRegExp rx(".*<trees>(.*)</trees>.*"); |
163 | QRegExp rx(".*<trees>(.*)</trees>.*"); |
161 | rx.indexIn(content, 0); |
164 | rx.indexIn(content, 0); |
162 | if (rx.capturedTexts().count()<1) |
165 | if (rx.capturedTexts().count()<1) |
163 | return; |
- | |
- | 166 | return 0; |
|
164 | my_content = rx.cap(1).trimmed(); |
167 | my_content = rx.cap(1).trimmed(); |
165 | }
|
168 | }
|
166 | 169 | ||
167 | QStringList lines=my_content.split('\n'); |
170 | QStringList lines=my_content.split('\n'); |
168 | if (lines.count()<2) |
171 | if (lines.count()<2) |
169 | return; |
- | |
- | 172 | return 0; |
|
170 | // drop comments
|
173 | // drop comments
|
171 | while (!lines.isEmpty() && lines.front().startsWith('#') ) |
174 | while (!lines.isEmpty() && lines.front().startsWith('#') ) |
172 | lines.pop_front(); |
175 | lines.pop_front(); |
173 | while (!lines.isEmpty() && lines.last().isEmpty()) |
176 | while (!lines.isEmpty() && lines.last().isEmpty()) |
174 | lines.removeLast(); |
177 | lines.removeLast(); |
Line 195... | Line 198... | ||
195 | if (iX==-1 || iY==-1 || iBhd==-1 || iSpecies==-1 || iHeight==-1) |
198 | if (iX==-1 || iY==-1 || iBhd==-1 || iSpecies==-1 || iHeight==-1) |
196 | throw IException(QString("Initfile %1 is not valid!\nObligatory columns are: x,y, bhdfrom or dbh, species, treeheight or height.").arg(fileName)); |
199 | throw IException(QString("Initfile %1 is not valid!\nObligatory columns are: x,y, bhdfrom or dbh, species, treeheight or height.").arg(fileName)); |
197 | 200 | ||
198 | double dbh; |
201 | double dbh; |
199 | bool ok; |
202 | bool ok; |
- | 203 | int cnt=0; |
|
200 | QString speciesid;
|
204 | QString speciesid;
|
201 | for (int i=1;i<lines.count();i++) { |
205 | for (int i=1;i<lines.count();i++) { |
202 | QString &line = lines[i]; |
206 | QString &line = lines[i]; |
203 | dbh = line.section(sep, iBhd, iBhd).toDouble(); |
207 | dbh = line.section(sep, iBhd, iBhd).toDouble(); |
204 | if (dbh<5.) |
208 | if (dbh<5.) |
Line 220... | Line 224... | ||
220 | tree.setId(line.section(sep, iID, iID).toInt() ); |
224 | tree.setId(line.section(sep, iID, iID).toInt() ); |
221 | 225 | ||
222 | tree.setDbh(dbh); |
226 | tree.setDbh(dbh); |
223 | tree.setHeight(line.section(sep, iHeight, iHeight).toDouble()/height_conversion); // convert from Picus-cm to m if necessary |
227 | tree.setHeight(line.section(sep, iHeight, iHeight).toDouble()/height_conversion); // convert from Picus-cm to m if necessary |
224 | 228 | ||
225 | speciesid = line.section(sep, iSpecies, iSpecies); |
- | |
- | 229 | speciesid = line.section(sep, iSpecies, iSpecies).trimmed(); |
|
226 | int picusid = speciesid.toInt(&ok); |
230 | int picusid = speciesid.toInt(&ok); |
227 | if (ok) { |
231 | if (ok) { |
228 | int idx = picusSpeciesIds.indexOf(picusid); |
232 | int idx = picusSpeciesIds.indexOf(picusid); |
229 | if (idx==-1) |
233 | if (idx==-1) |
230 | throw IException(QString("Loading init-file: invalid Picus-species-id. Species: %1").arg(picusid)); |
234 | throw IException(QString("Loading init-file: invalid Picus-species-id. Species: %1").arg(picusid)); |
231 | speciesid = iLandSpeciesIds[idx]; |
235 | speciesid = iLandSpeciesIds[idx]; |
232 | }
|
236 | }
|
233 | Species *s = speciesSet->species(speciesid); |
237 | Species *s = speciesSet->species(speciesid); |
234 | if (!ru || !s) |
238 | if (!ru || !s) |
235 | throw IException(QString("Loading init-file: either ressource unit or species invalid. Species: %1").arg(speciesid)); |
- | |
- | 239 | throw IException(QString("Loading init-file: either resource unit or species invalid. Species: %1").arg(speciesid)); |
|
236 | tree.setSpecies(s); |
240 | tree.setSpecies(s); |
237 | 241 | ||
238 | ok = true; |
242 | ok = true; |
239 | if (iAge>=0) |
243 | if (iAge>=0) |
240 | tree.setAge(line.section(sep, iAge, iAge).toInt(&ok), tree.height()); // this is a *real* age |
244 | tree.setAge(line.section(sep, iAge, iAge).toInt(&ok), tree.height()); // this is a *real* age |
241 | if (iAge<0 || !ok || tree.age()==0) |
245 | if (iAge<0 || !ok || tree.age()==0) |
242 | tree.setAge(0, tree.height()); // no real tree age available |
246 | tree.setAge(0, tree.height()); // no real tree age available |
243 | 247 | ||
244 | tree.setRU(ru); |
248 | tree.setRU(ru); |
245 | tree.setup(); |
249 | tree.setup(); |
- | 250 | cnt++; |
|
246 | }
|
251 | }
|
- | 252 | return cnt; |
|
247 | //qDebug() << "loaded init-file contained" << lines.count() <<"lines.";
|
253 | //qDebug() << "loaded init-file contained" << lines.count() <<"lines.";
|
248 | //qDebug() << "lines: " << lines;
|
254 | //qDebug() << "lines: " << lines;
|
249 | }
|
255 | }
|
250 | 256 | ||
251 | void StandLoader::loadDistributionList(const QString &content, ResourceUnit *ru, const QString &fileName) |
257 | void StandLoader::loadDistributionList(const QString &content, ResourceUnit *ru, const QString &fileName) |