-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLandscape.cpp
More file actions
3368 lines (3037 loc) · 91 KB
/
Landscape.cpp
File metadata and controls
3368 lines (3037 loc) · 91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*----------------------------------------------------------------------------
*
* Copyright (C) 2026 Greta Bocedi, Stephen C.F. Palmer, Justin M.J. Travis, Anne-Kathleen Malchow, Roslyn Henry, Théo Pannetier, Jette Wolff, Damaris Zurell
*
* This file is part of RangeShifter.
*
* RangeShifter is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RangeShifter is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RangeShifter. If not, see <https://www.gnu.org/licenses/>.
*
--------------------------------------------------------------------------*/
//---------------------------------------------------------------------------
#include "Landscape.h"
//---------------------------------------------------------------------------
ifstream landscape;
ofstream outConnMat;
ofstream outvisits;
#if RS_RCPP
ofstream outMovePaths;
#endif // RS_RCPP
//---------------------------------------------------------------------------
// Initial species distribution functions
InitDist::InitDist(Species* pSp)
{
pSpecies = pSp;
resol = 1;
maxX = 0;
maxY = 0;
minEast = 0.0;
minNorth = 0.0;
}
InitDist::~InitDist() {
int ncells = (int)cells.size();
for (int i = 0; i < ncells; i++)
if (cells[i] != NULL) delete cells[i];
cells.clear();
}
void InitDist::setDistribution(int nInit) {
int rr = 0;
int ncells = (int)cells.size();
if (nInit == 0) { // set all cells to be initialised
for (int i = 0; i < ncells; i++) {
cells[i]->setCell(true);
}
}
else { // set specified number of cells at random to be initialised
if (nInit > ncells / 2) { // use backwards selection method
for (int i = 0; i < ncells; i++) cells[i]->setCell(true);
for (int i = 0; i < (ncells - nInit); i++) {
do {
rr = pRandom->IRandom(0, ncells - 1);
} while (!cells[rr]->selected());
cells[rr]->setCell(false);
}
}
else { // use forwards selection method
for (int i = 0; i < ncells; i++) cells[i]->setCell(false);
for (int i = 0; i < nInit; i++) {
do {
rr = pRandom->IRandom(0, ncells - 1);
} while (cells[rr]->selected());
cells[rr]->setCell(true);
}
}
}
}
// Set a specified cell (by position in cells vector)
void InitDist::setDistCell(int ix, bool init) {
cells[ix]->setCell(init);
}
// Set a specified cell (by co-ordinates)
void InitDist::setDistCell(locn loc, bool init) {
locn cellloc;
int ncells = (int)cells.size();
for (int i = 0; i < ncells; i++) {
cellloc = cells[i]->getLocn();
if (cellloc.x == loc.x && cellloc.y == loc.y) {
cells[i]->setCell(init);
i = ncells;
}
}
}
// Specified location is within the initial distribution?
bool InitDist::inInitialDist(locn loc) {
int ncells = (int)cells.size();
for (int i = 0; i < ncells; i++) {
if (cells[i]->toInitialise(loc)) { // cell is to be initialised
return true;
}
}
return false;
}
int InitDist::cellCount(void) {
return (int)cells.size();
}
// Return the co-ordinates of a specified initial distribution cell
locn InitDist::getCell(int ix) {
locn loc;
if (ix >= 0 && ix < (int)cells.size()) {
loc = cells[ix]->getLocn();
}
else {
loc.x = loc.y = -666; // indicates invalid index specified
}
return loc;
}
// Return the co-ordinates of a specified initial distribution cell if it has been
// selected - otherwise return negative co-ordinates
locn InitDist::getSelectedCell(int ix) {
locn loc; loc.x = loc.y = -666;
if (ix < (int)cells.size()) {
if (cells[ix]->selected()) {
loc = cells[ix]->getLocn();
}
}
return loc;
}
locn InitDist::getDimensions(void) {
locn d; d.x = maxX; d.y = maxY; return d;
}
void InitDist::resetDistribution(void) {
int ncells = (int)cells.size();
for (int i = 0; i < ncells; i++) {
cells[i]->setCell(false);
}
}
//---------------------------------------------------------------------------
// Read species initial distribution file
int InitDist::readDistribution(string distfile) {
#if RS_RCPP
wstring header;
#else
string header;
#endif
int p, nodata;
int ncols, nrows;
#if RS_RCPP
wifstream dfile; // species distribution file input stream
#else
ifstream dfile; // species distribution file input stream
#endif
// open distribution file
dfile.open(distfile.c_str());
if (!dfile.is_open()) return 21;
// read landscape data from header records of distribution file
// NB headers of all files have already been compared
double tmpresol;
dfile >> header >> ncols >> header >> nrows >> header >> minEast >> header >> minNorth
>> header >> tmpresol >> header >> nodata;
resol = (int) tmpresol;
#if RS_RCPP
if (!dfile.good()) {
// corrupt file stream
StreamErrorR(distfile);
dfile.close();
dfile.clear();
return 144;
}
#endif
maxX = ncols - 1; maxY = nrows - 1;
// set up bad integer value to ensure that valid values are read
int badvalue = -9; if (nodata == -9) badvalue = -99;
for (int y = nrows - 1; y >= 0; y--) {
for (int x = 0; x < ncols; x++) {
p = badvalue;
#if RS_RCPP
if (dfile >> p) {
#else
dfile >> p;
#endif
if (p == nodata || p == 0 || p == 1) { // only valid values
if (p == 1) { // species present
cells.push_back(new DistCell(x, y));
}
}
else { // error in file
dfile.close(); dfile.clear();
return 22;
}
#if RS_RCPP
}
else {
// corrupt file stream
#if RS_RCPP && !R_CMD
Rcpp::Rcout << "At (x,y) = " << x << "," << y << " :" << std::endl;
#endif
StreamErrorR(distfile);
dfile.close();
dfile.clear();
return 144;
}
#endif
}
}
#if RS_RCPP
dfile >> p;
if (!dfile.eof()) EOFerrorR(distfile);
#endif
dfile.close(); dfile.clear();
return 0;
}
// Read species initial distribution file for threadsafe option
#if RS_RCPP
int InitDist::readDistribution(Rcpp::NumericMatrix distfile, landOrigin habfile_origin, int spResol) {
int d=0;
double dfloat=0;
int ncols,nrows;
ncols = distfile.ncol();
nrows = distfile.nrow();
minEast = habfile_origin.minEast;
minNorth = habfile_origin.minNorth;
resol = spResol;
maxX = ncols-1;
maxY = nrows-1;
for (int y = nrows-1; y >= 0; y--) {
for (int x = 0; x < ncols; x++) {
dfloat = distfile(nrows-1-y,x);
if ( !R_IsNA(dfloat) ){ // check for NA
d = (int)dfloat;
if ( d == 0 || d == 1) { // only valid values
if (d == 1) { // species present
cells.push_back(new DistCell(x,y));
}
}
else { // error in file
#if RS_RCPP && !R_CMD
Rcpp::Rcout << "Found invalid value in species distribution raster." << std::endl;
#endif
return 22;
}
}
}
}
return 0;
}
#endif
//---------------------------------------------------------------------------
// Landscape functions
Landscape::Landscape(void) {
patchModel = false; spDist = false; generated = false; fractal = false; continuous = false;
dynamic = false; habIndexed = false;
spatialdemog = false;
resol = spResol = 1; landNum = 0;
rasterType = 0;
nHab = nHabMax = 0;
dimX = dimY = 100;
minX = minY = 0;
maxX = maxY = 99;
minPct = maxPct = propSuit = hurst = 0.0;
maxCells = 100;
gpix = 1.0;
pix = (int)gpix;
minEast = minNorth = 0.0;
cells = 0;
connectMatrix = 0;
epsGlobal = 0;
patchChgMatrix = 0;
costsChgMatrix = 0;
}
Landscape::~Landscape() {
if (cells != 0) {
for (int y = dimY - 1; y >= 0; y--) {
for (int x = 0; x < dimX; x++) {
if (cells[y][x] != 0) delete cells[y][x];
}
if (cells[y] != 0) {
delete[] cells[y];
}
}
delete[] cells;
cells = 0;
}
int npatches = (int)patches.size();
for (int i = 0; i < npatches; i++)
if (patches[i] != NULL) delete patches[i];
patches.clear();
int ndistns = (int)distns.size();
for (int i = 0; i < ndistns; i++)
if (distns[i] != NULL) delete distns[i];
distns.clear();
int ninitcells = (int)initcells.size();
for (int i = 0; i < ninitcells; i++)
if (initcells[i] != NULL) delete initcells[i];
initcells.clear();
habCodes.clear();
landchanges.clear();
patchchanges.clear();
deleteConnectMatrix();
deletePatchChgMatrix();
if (epsGlobal != 0) delete[] epsGlobal;
}
// Remove all patches and cells
// Used for replicating artificial landscape without deleting the landscape itself
void Landscape::resetLand(void) {
resetLandLimits();
int npatches = (int)patches.size();
for (int i = 0; i < npatches; i++) if (patches[i] != NULL) delete patches[i];
patches.clear();
if (cells != 0) {
for (int y = dimY - 1; y >= 0; y--) {
for (int x = 0; x < dimX; x++) {
if (cells[y][x] != 0) delete cells[y][x];
}
if (cells[y] != 0) {
delete[] cells[y];
}
}
delete[] cells;
cells = 0;
}
}
void Landscape::setLandParams(landParams ppp, bool batchMode)
{
generated = ppp.generated;
patchModel = ppp.patchModel;
spDist = ppp.spDist;
dynamic = ppp.dynamic;
landNum = ppp.landNum;
spatialdemog = ppp.spatialdemog;
if (ppp.resol > 0) resol = ppp.resol;
if (ppp.spResol > 0 && ppp.spResol % ppp.resol == 0) spResol = ppp.spResol;
if ((ppp.rasterType >= 0 && ppp.rasterType <= 2) || ppp.rasterType == 9)
rasterType = ppp.rasterType;
if (ppp.nHab >= 1) nHab = ppp.nHab;
if (ppp.nHabMax >= 1) nHabMax = ppp.nHabMax;
if (ppp.dimX > 0) dimX = ppp.dimX;
if (ppp.dimY > 0) dimY = ppp.dimY;
if (ppp.minX >= 0 && ppp.maxX >= 0 && ppp.minX <= ppp.maxX && ppp.maxX < dimX) {
minX = ppp.minX; maxX = ppp.maxX;
}
else {
minX = 0; maxX = dimX - 1;
}
if (ppp.minY >= 0 && ppp.maxY >= 0 && ppp.minY <= ppp.maxY && ppp.maxY < dimY) {
minY = ppp.minY; maxY = ppp.maxY;
}
else {
minY = 0; maxY = dimY - 1;
}
if (batchMode && rasterType == 0) {
// in batch mode, set up sequential habitat codes if not already present
if (habCodes.size() == 0) {
for (int i = 0; i < nHabMax; i++) {
habCodes.push_back(i + 1);
}
}
}
}
landParams Landscape::getLandParams(void)
{
landParams ppp;
ppp.generated = generated; ppp.patchModel = patchModel; ppp.spDist = spDist;
ppp.dynamic = dynamic;
ppp.spatialdemog = spatialdemog;
ppp.landNum = landNum;
ppp.resol = resol; ppp.spResol = spResol;
ppp.rasterType = rasterType;
ppp.nHab = nHab; ppp.nHabMax = nHabMax;
ppp.dimX = dimX; ppp.dimY = dimY;
ppp.minX = minX; ppp.minY = minY;
ppp.maxX = maxX; ppp.maxY = maxY;
return ppp;
}
landData Landscape::getLandData(void) {
landData dd;
dd.resol = resol;
dd.dimX = dimX; dd.dimY = dimY;
dd.minX = minX; dd.minY = minY;
dd.maxX = maxX; dd.maxY = maxY;
return dd;
}
void Landscape::setGenLandParams(genLandParams ppp)
{
fractal = ppp.fractal;
continuous = ppp.continuous;
if (ppp.minPct > 0.0 && ppp.minPct < 100.0) minPct = ppp.minPct;
if (ppp.maxPct > 0.0 && ppp.maxPct <= 100.0) maxPct = ppp.maxPct;
if (ppp.propSuit >= 0.0 && ppp.propSuit <= 1.0) propSuit = ppp.propSuit;
if (ppp.hurst > 0.0 && ppp.hurst < 1.0) hurst = ppp.hurst;
if (ppp.maxCells > 0) maxCells = ppp.maxCells;
}
genLandParams Landscape::getGenLandParams(void)
{
genLandParams ppp;
ppp.fractal = fractal; ppp.continuous = continuous;
ppp.minPct = minPct; ppp.maxPct = maxPct; ppp.propSuit = propSuit; ppp.hurst = hurst;
ppp.maxCells = maxCells;
return ppp;
}
void Landscape::setLandLimits(int x0, int y0, int x1, int y1) {
if (x0 >= 0 && x1 >= 0 && x0 <= x1 && x1 < dimX
&& y0 >= 0 && y1 >= 0 && y0 <= y1 && y1 < dimY) {
minX = x0; maxX = x1; minY = y0; maxY = y1;
}
}
void Landscape::resetLandLimits(void) {
minX = minY = 0; maxX = dimX - 1; maxY = dimY - 1;
}
//---------------------------------------------------------------------------
void Landscape::setLandPix(landPix p) {
if (p.pix > 0) pix = p.pix;
if (p.gpix > 0.0) gpix = p.gpix;
}
landPix Landscape::getLandPix(void) {
landPix p;
p.pix = pix; p.gpix = gpix;
return p;
}
void Landscape::setOrigin(landOrigin origin) {
minEast = origin.minEast; minNorth = origin.minNorth;
}
landOrigin Landscape::getOrigin(void) {
landOrigin origin;
origin.minEast = minEast; origin.minNorth = minNorth;
return origin;
}
//---------------------------------------------------------------------------
// Functions to handle habitat codes
bool Landscape::habitatsIndexed(void) { return habIndexed; }
void Landscape::listHabCodes(void) {
int nhab = (int)habCodes.size();
#if RS_RCPP && !R_CMD
Rcpp::Rcout << endl;
for (int i = 0; i < nhab; i++) {
Rcpp::Rcout << "Habitat code[ " << i << "] = " << habCodes[i] << endl;
}
Rcpp::Rcout << endl;
#else
cout << endl;
for (int i = 0; i < nhab; i++) {
cout << "Habitat code[ " << i << "] = " << habCodes[i] << endl;
}
cout << endl;
#endif
}
void Landscape::addHabCode(int hab) {
int nhab = (int)habCodes.size();
bool addCode = true;
for (int i = 0; i < nhab; i++) {
if (hab == habCodes[i]) {
addCode = false; i = nhab + 1;
}
}
if (addCode) { habCodes.push_back(hab); nHab++; }
}
// Get the index number of the specified habitat in the habitats vector
int Landscape::findHabCode(int hab) {
int nhab = (int)habCodes.size();
for (int i = 0; i < nhab; i++) {
if (hab == habCodes[i]) return i;
}
return -999;
}
// Get the specified habitat code
int Landscape::getHabCode(int ixhab) {
if (ixhab < (int)habCodes.size()) return habCodes[ixhab];
else return -999;
}
void Landscape::clearHabitats(void) {
habCodes.clear();
}
//---------------------------------------------------------------------------
void Landscape::setCellArray(void) {
if (cells != 0) resetLand();
//cells = new Cell **[maxY+1];
cells = new Cell * *[dimY];
for (int y = dimY - 1; y >= 0; y--) {
cells[y] = new Cell * [dimX];
for (int x = 0; x < dimX; x++) {
cells[y][x] = 0;
}
}
}
//---------------------------------------------------------------------------
/* Create an artificial landscape (random or fractal), which can be
either binary (habitat index 0 is the matrix, 1 is suitable habitat)
or continuous (0 is the matrix, >0 is suitable habitat) */
void Landscape::generatePatches()
{
int x, y, ncells;
double p;
Patch* pPatch;
Cell* pCell;
vector <land> ArtLandscape;
setCellArray();
int patchnum = 0; // initial patch number for cell-based landscape
// create patch 0 - the matrix patch (even if there is no matrix)
newPatch(patchnum++);
// as landscape generator returns cells in a random sequence, first set up all cells
// in the landscape in the correct sequence, then update them and create patches for
// habitat cells
for (int yy = dimY - 1; yy >= 0; yy--) {
for (int xx = 0; xx < dimX; xx++) {
addNewCellToLand(xx, yy, 0);
}
}
if (continuous) rasterType = 2;
else rasterType = 0;
if (fractal) {
p = 1.0 - propSuit;
// fractal_landscape() requires Max_prop > 1 (but does not check it!)
// as in turn it calls runif(1.0,Max_prop)
double maxpct;
if (maxPct < 1.0) maxpct = 100.0; else maxpct = maxPct;
ArtLandscape = fractal_landscape(dimY, dimX, hurst, p, maxpct, minPct);
vector<land>::iterator iter = ArtLandscape.begin();
while (iter != ArtLandscape.end()) {
x = iter->y_coord; y = iter->x_coord;
pCell = findCell(x, y);
if (continuous) {
if (iter->value > 0.0) { // habitat
pPatch = newPatch(patchnum++);
addCellToPatch(pCell, pPatch, iter->value);
}
else { // matrix
addCellToPatch(pCell, patches[0], iter->value);
}
}
else { // discrete
if (iter->avail == 0) { // matrix
addCellToPatch(pCell, patches[0]);
}
else { // habitat
pPatch = newPatch(patchnum++);
addCellToPatch(pCell, pPatch);
pCell->changeHabIndex(0, 1);
}
}
iter++;
}
}
else { // random landscape
int hab = 0;
ncells = (int)((float)(dimX) * (float)(dimY)*propSuit + 0.00001); // no. of cells to initialise
int i = 0;
do {
do {
x = pRandom->IRandom(0, dimX - 1); y = pRandom->IRandom(0, dimY - 1);
pCell = findCell(x, y);
hab = pCell->getHabIndex(0);
} while (hab > 0);
pPatch = newPatch(patchnum++);
pCell = findCell(x, y);
addCellToPatch(pCell, pPatch);
pCell->changeHabIndex(0, 1);
if (continuous) {
pCell->setHabitat((float)(minPct + pRandom->Random() * (maxPct - minPct)));
}
i++;
} while (i < ncells);
// remaining cells need to be added to the matrix patch
p = 0.0;
x = 0;
for (int yy = dimY - 1; yy >= 0; yy--) {
for (int xx = 0; xx < dimX; xx++) {
pCell = findCell(xx, yy);
if (continuous) {
if (pCell->getHabitat(0) <= 0.0)
{
addCellToPatch(pCell, patches[0], (float)p);
}
}
else { // discrete
if (pCell->getHabIndex(0) == 0) {
addCellToPatch(pCell, patches[0], x);
}
}
}
}
}
}
//---------------------------------------------------------------------------
// Landscape patch-management functions
//---------------------------------------------------------------------------
/* Create a patch for each suitable cell of a cell-based landscape (all other
habitat cells are added to the matrix patch) */
void Landscape::allocatePatches(Species* pSpecies)
{
float habK;
Patch* pPatch;
Cell* pCell;
// delete all existing patches
int npatches = (int)patches.size();
for (int i = 0; i < npatches; i++) {
if (patches[i] != NULL) delete patches[i];
}
patches.clear();
// create the matrix patch
patches.push_back(new Patch(0, 0));
Patch* matrixPatch = patches[0];
int patchnum = 1;
switch (rasterType) {
case 0: // habitat codes
for (int y = dimY - 1; y >= 0; y--) {
for (int x = 0; x < dimX; x++) {
if (cells[y][x] != 0) { // not no-data cell
pCell = cells[y][x];
habK = 0.0;
int nhab = pCell->nHabitats();
for (int i = 0; i < nhab; i++) {
habK += pSpecies->getHabK(pCell->getHabIndex(i));
}
if (habK > 0.0) { // cell is suitable - create a patch for it
pPatch = newPatch(patchnum++);
addCellToPatch(pCell, pPatch);
}
else { // cell is not suitable - add to the matrix patch
addCellToPatch(pCell, matrixPatch);
pPatch = 0;
}
}
}
}
break;
case 1: // habitat cover
for (int y = dimY - 1; y >= 0; y--) {
for (int x = 0; x < dimX; x++) {
if (cells[y][x] != 0) { // not no-data cell
pCell = cells[y][x];
habK = 0.0;
int nhab = pCell->nHabitats();
for (int i = 0; i < nhab; i++)
{
habK += pSpecies->getHabK(i) * pCell->getHabitat(i) / 100.0f;
}
if (habK > 0.0) { // cell is suitable - create a patch for it
pPatch = newPatch(patchnum++);
addCellToPatch(pCell, pPatch);
}
else { // cell is not suitable - add to the matrix patch
addCellToPatch(pCell, matrixPatch);
pPatch = 0;
}
}
}
}
break;
case 2: // habitat quality
for (int y = dimY - 1; y >= 0; y--) {
for (int x = 0; x < dimX; x++) {
if (cells[y][x] != 0) { // not no-data cell
pCell = cells[y][x];
habK = 0.0;
int nhab = pCell->nHabitats();
for (int i = 0; i < nhab; i++)
{
habK += pSpecies->getHabK(0) * pCell->getHabitat(i) / 100.0f;
}
if (habK > 0.0) { // cell is suitable (at some time) - create a patch for it
pPatch = newPatch(patchnum++);
addCellToPatch(pCell, pPatch);
}
else { // cell is never suitable - add to the matrix patch
addCellToPatch(pCell, matrixPatch);
pPatch = 0;
}
}
}
}
break;
} // end of switch (rasterType)
}
Patch* Landscape::newPatch(int num)
{
int npatches = (int)patches.size();
patches.push_back(new Patch(num, num));
return patches[npatches];
}
Patch* Landscape::newPatch(int seqnum, int num)
{
int npatches = (int)patches.size();
patches.push_back(new Patch(seqnum, num));
return patches[npatches];
}
void Landscape::resetPatches(void) {
int npatches = (int)patches.size();
for (int i = 0; i < npatches; i++) {
patches[i]->resetLimits();
}
}
void Landscape::addNewCellToLand(int x, int y, float q) {
if (q < 0.0) // no-data cell - no Cell created
cells[y][x] = 0;
else
cells[y][x] = new Cell(x, y, 0, q);
}
void Landscape::addNewCellToLand(int x, int y, int hab) {
if (hab < 0) // no-data cell - no Cell created
cells[y][x] = 0;
else
cells[y][x] = new Cell(x, y, 0, hab);
}
void Landscape::addCellToLand(Cell* c) {
if (cells == 0) throw runtime_error("Landscape cells member is uninitialised.");
if (c->getHabIndex(0) < 0.0)
throw logic_error("Can't add no-data cell to landscape.");
locn l = c->getLocn();
cells[l.y][l.x] = c;
}
void Landscape::addNewCellToPatch(Patch* pPatch, int x, int y, float q) {
if (q < 0.0) { // no-data cell - no Cell created
cells[y][x] = 0;
}
else { // create the new cell
cells[y][x] = new Cell(x, y, pPatch, q);
if (pPatch != 0) { // not the matrix patch
// add the cell to the patch
pPatch->addCell(cells[y][x], x, y);
}
}
}
void Landscape::addNewCellToPatch(Patch* pPatch, int x, int y, int hab) {
if (hab < 0) // no-data cell - no Cell created
cells[y][x] = 0;
else { // create the new cell
cells[y][x] = new Cell(x, y, pPatch, hab);
if (pPatch != 0) { // not the matrix patch
// add the cell to the patch
pPatch->addCell(cells[y][x], x, y);
}
}
}
void Landscape::addCellToPatch(Cell* pCell, Patch* pPatch) {
pCell->setPatch(pPatch);
locn loc = pCell->getLocn();
// add the cell to the patch
pPatch->addCell(pCell, loc.x, loc.y);
}
void Landscape::addCellToPatch(Cell* pCell, Patch* pPatch, float q) {
pCell->setPatch(pPatch);
// update the habitat type of the cell
pCell->setHabitat(q);
locn loc = pCell->getLocn();
// add the cell to the patch
pPatch->addCell(pCell, loc.x, loc.y);
}
void Landscape::addCellToPatch(Cell* pCell, Patch* pPatch, int hab) {
pCell->setPatch(pPatch);
// update the habitat type of the cell
pCell->setHabIndex(hab);
locn loc = pCell->getLocn();
// add the cell to the patch
pPatch->addCell(pCell, loc.x, loc.y);
}
patchData Landscape::getPatchData(int ix) {
patchData ppp;
ppp.pPatch = patches[ix]; ppp.patchNum = patches[ix]->getPatchNum();
ppp.nCells = patches[ix]->getNCells();
locn randloc; randloc.x = -666; randloc.y = -666;
Cell* pCell = patches[ix]->getRandomCell();
if (pCell != 0) {
randloc = pCell->getLocn();
}
ppp.x = randloc.x; ppp.y = randloc.y;
return ppp;
}
bool Landscape::existsPatch(int num) {
int npatches = (int)patches.size();
for (int i = 0; i < npatches; i++) {
if (num == patches[i]->getPatchNum()) return true;
}
return false;
}
Patch* Landscape::findPatch(int num) {
int npatches = (int)patches.size();
for (int i = 0; i < npatches; i++) {
if (num == patches[i]->getPatchNum()) return patches[i];
}
return 0;
}
set<int> Landscape::getPatchNbs() const {
set<int> patchNbs;
for (auto& p : patches)
patchNbs.emplace(p->getPatchNum());
return patchNbs;
}
set<int> Landscape::samplePatches(const string& samplingOption, int nbToSample, Species* pSpecies) {
vector<int> sampledPatches;
vector<int> eligiblePatches;
// Get list of viable patches where the species is present
for (auto p : patches) {
if (p->getPatchNum() == 0) continue; // skip patch 0, the matrix
if (samplingOption == "random" // then all patches are eligible
|| p->speciesIsPresent(pSpecies)) // otherwise only patches with at least 1 ind
eligiblePatches.push_back(p->getPatchNum());
}
if (samplingOption == "all") {
sampledPatches = eligiblePatches;
}
else if (samplingOption == "random_occupied" || samplingOption == "random") {
if (nbToSample > eligiblePatches.size())
nbToSample = eligiblePatches.size();
auto rng = pRandom->getRNG();
sample(eligiblePatches.begin(), eligiblePatches.end(), std::back_inserter(sampledPatches),
nbToSample, rng);
}
else {
throw logic_error("Sampling option should be random, random_occupied or all when sampling patches.");
}
set<int> patchIds;
copy(sampledPatches.begin(), sampledPatches.end(), inserter(patchIds, patchIds.end()));
return patchIds;
}
void Landscape::resetPatchPopns(void) {
int npatches = (int)patches.size();
for (int i = 0; i < npatches; i++) {
patches[i]->resetPopn();
}
}
void Landscape::updateCarryingCapacity(Species* pSpecies, int yr, short landIx) {
envGradParams grad = paramsGrad->getGradient();
bool gradK = false;
if (grad.gradient && grad.gradType == 1) gradK = true; // gradient in carrying capacity
patchLimits landlimits;
landlimits.xMin = minX; landlimits.xMax = maxX;
landlimits.yMin = minY; landlimits.yMax = maxY;
int npatches = (int)patches.size();
for (int i = 0; i < npatches; i++) {
if (patches[i]->getPatchNum() != 0) { // not matrix patch
patches[i]->setCarryingCapacity(pSpecies, landlimits,
getGlobalStoch(yr), nHab, rasterType, landIx, gradK);
}
}
}
void Landscape::updateDemoScalings(short landIx) {
patchLimits landlimits;
landlimits.xMin = minX; landlimits.xMax = maxX;
landlimits.yMin = minY; landlimits.yMax = maxY;
if(spatialdemog && rasterType == 2) {// demographic scaling only implemented for habitat quality maps
int npatches = (int)patches.size(); // new: for (auto& p : patches)
for (int i = 0; i < npatches; i++) {
if (patches[i]->getPatchNum() != 0) { // not matrix patch
// calculate local scaling for each patch from its constituent cells
patches[i]->setPatchDemoScaling(landIx, landlimits);
}
}
}
}
Cell* Landscape::findCell(int x, int y) {
if (x >= 0 && x < dimX && y >= 0 && y < dimY) return cells[y][x];
else return 0;
}
bool Landscape::checkDataCell(int x, int y) {
Cell* pCell;
pCell = findCell(x, y);
return true;
}
int Landscape::patchCount(void) {
return (int)patches.size();
}
void Landscape::listPatches(void) {
patchLimits p;
int npatches = (int)patches.size();
#if RS_RCPP && !R_CMD
Rcpp::Rcout << endl;
for (int i = 0; i < npatches; i++) {
p = patches[i]->getLimits();
Rcpp::Rcout << "Patch " << patches[i]->getPatchNum()
<< " xMin = " << p.xMin << " xMax = " << p.xMax
<< " \tyMin = " << p.yMin << " yMax = " << p.yMax
<< endl;
}
Rcpp::Rcout << endl;
#else
cout << endl;
for (int i = 0; i < npatches; i++) {
p = patches[i]->getLimits();
cout << "Patch " << patches[i]->getPatchNum()
<< " xMin = " << p.xMin << " xMax = " << p.xMax
<< " \tyMin = " << p.yMin << " yMax = " << p.yMax
<< endl;
}
cout << endl;
#endif
}
// Check that total cover of any cell does not exceed 100%
// and identify matrix cells
int Landscape::checkTotalCover(void) {