Rev 1085 | Rev 1095 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1085 | Rev 1088 | ||
---|---|---|---|
Line 23... | Line 23... | ||
23 | #include "barkbeetlemodule.h"
|
23 | #include "barkbeetlemodule.h"
|
24 | #include "outputmanager.h"
|
24 | #include "outputmanager.h"
|
25 | #include "helper.h"
|
25 | #include "helper.h"
|
26 | #include "spatialanalysis.h"
|
26 | #include "spatialanalysis.h"
|
27 | #include "scriptgrid.h"
|
27 | #include "scriptgrid.h"
|
- | 28 | #include "mapgrid.h"
|
|
- | 29 | #include "scriptglobal.h"
|
|
28 | 30 | ||
29 | 31 | ||
30 | BarkBeetleScript::BarkBeetleScript(QObject *) |
32 | BarkBeetleScript::BarkBeetleScript(QObject *) |
31 | {
|
33 | {
|
32 | mBeetle = 0; |
34 | mBeetle = 0; |
Line 146... | Line 148... | ||
146 | {
|
148 | {
|
147 | int idx = mBeetle->mLayers.indexOf(type); |
149 | int idx = mBeetle->mLayers.indexOf(type); |
148 | if (idx<0) |
150 | if (idx<0) |
149 | qDebug() << "ERROR: BarkBeetleScript:grid(): invalid grid" << type; |
151 | qDebug() << "ERROR: BarkBeetleScript:grid(): invalid grid" << type; |
150 | // this is a copy
|
152 | // this is a copy
|
151 | Grid<double> *damage_grid = mBeetle->mLayers.grid(idx); |
- | |
- | 153 | Grid<double> *damage_grid = mBeetle->mLayers.copyGrid(idx); |
|
152 | 154 | ||
153 | QJSValue g = ScriptGrid::createGrid(damage_grid, type); |
155 | QJSValue g = ScriptGrid::createGrid(damage_grid, type); |
154 | return g; |
156 | return g; |
155 | }
|
157 | }
|
156 | 158 | ||
157 | int BarkBeetleScript::damagedArea(int threshold, QString fileName) |
159 | int BarkBeetleScript::damagedArea(int threshold, QString fileName) |
158 | {
|
160 | {
|
159 | // get damage grid:
|
161 | // get damage grid:
|
160 | Grid<double> *damage_grid = mBeetle->mLayers.grid(mBeetle->mLayers.indexOf("dead")); |
- | |
- | 162 | Grid<double> *damage_grid = mBeetle->mLayers.copyGrid(mBeetle->mLayers.indexOf("dead")); |
|
161 | SpatialAnalysis spat;
|
163 | SpatialAnalysis spat;
|
162 | QList<int> patches = spat.extractPatches(*damage_grid, threshold+1, fileName); |
164 | QList<int> patches = spat.extractPatches(*damage_grid, threshold+1, fileName); |
163 | int n=0, size=0; |
165 | int n=0, size=0; |
164 | for (int i=0;i<patches.count();++i) |
166 | for (int i=0;i<patches.count();++i) |
165 | if (patches[i]>threshold) { |
167 | if (patches[i]>threshold) { |
Line 168... | Line 170... | ||
168 | }
|
170 | }
|
169 | qDebug() << "BarkBeetleScript:damagedArea:" << n << "patches (area=" << size << ") above threshold" << threshold; |
171 | qDebug() << "BarkBeetleScript:damagedArea:" << n << "patches (area=" << size << ") above threshold" << threshold; |
170 | delete damage_grid; |
172 | delete damage_grid; |
171 | return size; |
173 | return size; |
172 | 174 | ||
- | 175 | }
|
|
- | 176 | ||
- | 177 | int BarkBeetleScript::clearInfestedPixels(QJSValue standmap, int stand_id, double fraction) |
|
- | 178 | {
|
|
- | 179 | MapGridWrapper *gr = qobject_cast<MapGridWrapper*> ( standmap.toQObject() ); |
|
- | 180 | if (!gr || !gr->map()) { |
|
- | 181 | qDebug() << "BarkBeetleScript::clearInfestedPixels: no valid stand-map!"; |
|
- | 182 | return 0; |
|
- | 183 | }
|
|
- | 184 | QRectF box = gr->map()->boundingBox(stand_id); |
|
- | 185 | //GridRunner<BarkBeetleCell> runner(mBeetle->mGrid, box);
|
|
- | 186 | GridRunner<int> runner(gr->map()->grid(), box); |
|
- | 187 | int n_cleared = 0; |
|
- | 188 | while (runner.next()) { |
|
- | 189 | if (*runner.current() == stand_id) { |
|
- | 190 | BarkBeetleCell &bbc = mBeetle->mGrid.valueAt( runner.currentCoord() ); |
|
- | 191 | if (bbc.infested) { |
|
- | 192 | if (fraction==1. || drandom()<fraction) { |
|
- | 193 | bbc.infested = false; |
|
- | 194 | n_cleared++; |
|
- | 195 | }
|
|
- | 196 | }
|
|
- | 197 | }
|
|
- | 198 | }
|
|
- | 199 | return n_cleared; |
|
173 | }
|
200 | }
|
174 | 201 | ||
175 | bool BarkBeetleScript::simulate() |
202 | bool BarkBeetleScript::simulate() |
176 | {
|
203 | {
|
177 | return mBeetle->simulate(); |
204 | return mBeetle->simulate(); |