-
Notifications
You must be signed in to change notification settings - Fork 11
/
prpl-cellspaceInfo.cpp
652 lines (565 loc) · 16.3 KB
/
prpl-cellspaceInfo.cpp
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
#include "prpl-cellspaceInfo.h"
pRPL::CellspaceInfo::
CellspaceInfo()
:_dTypeName("UNKNOWN"),
_dTypeSize(0),
_pNoDataVal(NULL),
_pGeoInfo(NULL),
_tileSize(0) {}
pRPL::CellspaceInfo::
CellspaceInfo(const pRPL::CellspaceInfo &rhs)
:_dTypeName("UNKNOWN"),
_dTypeSize(0),
_pNoDataVal(NULL),
_pGeoInfo(NULL),
_tileSize(0) {
if(!init(rhs._dims, rhs._dTypeName.c_str(), rhs._dTypeSize, rhs._pGeoInfo, rhs._tileSize)) {
exit(-1);
}
memcpy(_pNoDataVal, rhs._pNoDataVal, _dTypeSize);
}
pRPL::CellspaceInfo::
CellspaceInfo(const pRPL::SpaceDims &dims,
const char *aTypeName,
size_t typeSize,
const pRPL::CellspaceGeoinfo *pGeoinfo,
long tileSize)
:_dTypeName("UNKNOWN"),
_dTypeSize(0),
_pNoDataVal(NULL),
_pGeoInfo(NULL),
_tileSize(0) {
if(!init(dims, aTypeName, typeSize, pGeoinfo, tileSize)) {
exit(-1);
}
}
pRPL::CellspaceInfo::
~CellspaceInfo() {
clear();
}
pRPL::CellspaceInfo& pRPL::CellspaceInfo::
operator=(const pRPL::CellspaceInfo &rhs) {
if(this != &rhs) {
if(!init(rhs._dims, rhs._dTypeName.c_str(), rhs._dTypeSize, rhs._pGeoInfo, rhs._tileSize)) {
exit(-1);
}
memcpy(_pNoDataVal, rhs._pNoDataVal, _dTypeSize);
}
return *this;
}
bool pRPL::CellspaceInfo::
operator==(const pRPL::CellspaceInfo &rhs) {
return (_dims == rhs._dims &&
_dTypeName == rhs._dTypeName &&
_dTypeSize == rhs._dTypeSize &&
*_pGeoInfo == *(rhs._pGeoInfo) &&
_tileSize == rhs._tileSize &&
strcmp ((const char *)_pNoDataVal, (const char *)(rhs._pNoDataVal)) == 0);
}
bool pRPL::CellspaceInfo::
operator!=(const pRPL::CellspaceInfo &rhs) {
return !(operator==(rhs));
}
bool pRPL::CellspaceInfo::
init(const pRPL::SpaceDims &dims,
const char *aTypeName,
size_t typeSize,
const pRPL::CellspaceGeoinfo *pGeoinfo,
long tileSize) {
clear();
if(dims.isNone()) {
cerr << __FILE__ << " function:" << __FUNCTION__ \
<< " Error: invalid spatial dimensions (" << dims \
<< ") for the CellspaceInfo" \
<< endl;
return false;
}
else if(typeSize <= 0) {
cerr << __FILE__ << " function:" << __FUNCTION__ \
<< " Error: invalid data type size (" << typeSize \
<< ") for the CellspaceInfo" \
<< endl;
return false;
}
else if(!initDefVal(_pNoDataVal, string(aTypeName)) ||
_pNoDataVal == NULL) {
cerr << __FILE__ << " function:" << __FUNCTION__ \
<< " Error: NoData value is not properly initialized"
<< endl;
return false;
}
_dims = dims;
_dTypeName = string(aTypeName);
_dTypeSize = typeSize;
if(pGeoinfo != NULL) {
_pGeoInfo = new pRPL::CellspaceGeoinfo(*pGeoinfo);
}
setTileSize(tileSize);
return true;
}
bool pRPL::CellspaceInfo::
initByGDAL(GDALDataset *pDataset,
int iBand,
bool warning) {
if(pDataset == NULL) {
if(warning) {
cerr << __FILE__ << " function:" << __FUNCTION__ \
<< " Error: NULL GDAL Dataset" << endl;
}
return false;
}
int nBands = pDataset->GetRasterCount();
if(iBand < 1 || iBand > nBands) {
if(warning) {
cerr << __FILE__ << " function:" << __FUNCTION__ \
<< " Error: invalid band index (" << iBand \
<< ") in the GDAL Dataset with " << nBands << " bands" << endl;
}
return false;
}
GDALRasterBand *pBand = pDataset->GetRasterBand(iBand);
GDALDataType gdalType = pBand->GetRasterDataType();
string dataTypeName;
size_t dataTypeSize;
gdalType2cppType(dataTypeName, dataTypeSize, gdalType);
if(dataTypeName == "UNKNOWN") {
if(warning) {
cerr << __FILE__ << " function:" << __FUNCTION__ \
<< " Error: unknown data type from the GDAL Dataset" << endl;
}
return false;
}
long nRows = pDataset->GetRasterYSize();
long nCols = pDataset->GetRasterXSize();
if(!init(pRPL::SpaceDims(nRows, nCols), dataTypeName.c_str(), dataTypeSize)) {
return false;
}
double noDataVal = pBand->GetNoDataValue();
setNoDataValAs<double>(noDataVal);
pRPL::CellspaceGeoinfo geoInfo;
if(geoInfo.initByGDAL(pDataset, warning)) {
georeference(geoInfo);
}
setTileSize(TILEWIDTH);
return true;
}
bool pRPL::CellspaceInfo::
initByPGTIOL(PGTIOLDataset *pDataset,
int iBand,
bool warning) {
if(pDataset == NULL) {
if(warning) {
cerr << __FILE__ << " function:" << __FUNCTION__ \
<< " Error: NULL pGTIOL Dataset" << endl;
}
return false;
}
int nBands = pDataset->GetRasterCount();
if(iBand < 1 || iBand > nBands) {
if(warning) {
cerr << __FILE__ << " function:" << __FUNCTION__ \
<< " Error: invalid band index (" << iBand \
<< ") in the pGTIOL Dataset with " << nBands << " bands" << endl;
}
return false;
}
pDataset->SetRasterBand(iBand);
GDALDataType gdalType = pDataset->GetRasterDataType();
string dataTypeName;
size_t dataTypeSize;
gdalType2cppType(dataTypeName, dataTypeSize,gdalType);
if(dataTypeName == "UNKNOWN") {
if(warning) {
cerr << __FILE__ << " function:" << __FUNCTION__ \
<< " Error: unknown data type from the pGTIOL Dataset" << endl;
}
return false;
}
long nRows = pDataset->GetRasterYSize();
long nCols = pDataset->GetRasterXSize();
if(!init(pRPL::SpaceDims(nRows, nCols), dataTypeName.c_str(), dataTypeSize)) {
return false;
}
double noDataVal = pDataset->GetNoDataValue();
setNoDataValAs<double>(noDataVal);
pRPL::CellspaceGeoinfo geoInfo;
if(geoInfo.initByPGTIOL(pDataset, warning)) {
georeference(geoInfo);
}
long tileSize = pDataset->GetTileWidth();
setTileSize(tileSize);
return true;
}
void pRPL::CellspaceInfo::
clear() {
_dims = pRPL::SpaceDims();
_dTypeName.assign("UNKNOWN");
_dTypeSize = 0;
if(_pNoDataVal != NULL) {
free(_pNoDataVal);
_pNoDataVal = NULL;
}
if(_pGeoInfo != NULL) {
delete _pGeoInfo;
_pGeoInfo = NULL;
}
_tileSize = 0;
}
void pRPL::CellspaceInfo::
add2Buf(vector<char> &buf) {
if(_pGeoInfo != NULL) {
_pGeoInfo->add2Buf(buf);
buf.push_back('y'); // indicates that geoinfo exists
}
else {
buf.push_back('n'); // indicates that geoinfo doesn't exist
}
size_t bufPtr = buf.size();
buf.resize(bufPtr + 2*sizeof(size_t) + _dTypeName.size() + _dTypeSize + sizeof(long));
memcpy((void *)&(buf[bufPtr]), _pNoDataVal, _dTypeSize);
bufPtr += _dTypeSize;
memcpy((void *)&(buf[bufPtr]), &_dTypeSize, sizeof(size_t));
bufPtr += sizeof(size_t);
size_t lenTypeName = _dTypeName.size();
memcpy((void *)&(buf[bufPtr]), &(_dTypeName[0]), lenTypeName);
bufPtr += lenTypeName;
memcpy((void *)&(buf[bufPtr]), &lenTypeName, sizeof(size_t));
bufPtr += sizeof(size_t);
memcpy((void *)&(buf[bufPtr]), &_tileSize, sizeof(long));
_dims.add2Buf(buf);
}
bool pRPL::CellspaceInfo::
initFromBuf(vector<char> &buf) {
clear();
size_t lenTypeName;
char ifGeoref;
if(!_dims.initFromBuf(buf)) {
return false;
}
if(!_dims.valid()) {
cerr << __FILE__ << " function:" << __FUNCTION__ \
<< " Error: invalid spatial dimensions (" << _dims \
<< ")" << endl;
return false;
}
int bufPtr = buf.size() - sizeof(long);
if(bufPtr < 0) {
cerr << __FILE__ << " function:" << __FUNCTION__ \
<< " Error: the buffer has insufficient memory to extract the tile size of Cellspace " \
<< endl;
return false;
}
memcpy(&_tileSize, (void *)&(buf[bufPtr]), sizeof(long));
bufPtr -= sizeof(size_t);
if(bufPtr < 0) {
cerr << __FILE__ << " function:" << __FUNCTION__ \
<< " Error: the buffer has insufficient memory to extract the length of the data type name of a Cellspace" \
<< endl;
return false;
}
memcpy(&lenTypeName, (void *)&(buf[bufPtr]), sizeof(size_t));
bufPtr -= lenTypeName;
if(bufPtr < 0) {
cerr << __FILE__ << " function:" << __FUNCTION__ \
<< " Error: the buffer has insufficient memory to extract the data type name of a Cellspace" \
<< endl;
return false;
}
_dTypeName.resize(lenTypeName);
memcpy(&(_dTypeName[0]), (void *)&(buf[bufPtr]), lenTypeName);
bufPtr -= sizeof(size_t);
if(bufPtr < 0) {
cerr << __FILE__ << " function:" << __FUNCTION__ \
<< " Error: the buffer has insufficient memory to extract the data type size of a Cellspace" \
<< endl;
return false;
}
memcpy(&_dTypeSize, (void *)&(buf[bufPtr]), sizeof(size_t));
if(_dTypeSize <= 0) {
cerr << __FILE__ << " function:" << __FUNCTION__ \
<< " Error: invalid data type size (" << _dTypeSize << ")" \
<< endl;
return false;
}
bufPtr -= _dTypeSize;
if(bufPtr < 0) {
cerr << __FILE__ << " function:" << __FUNCTION__ \
<< " Error: the buffer has insufficient memory to extract the NoData value of a Cellspace" \
<< endl;
return false;
}
_pNoDataVal = (void *)calloc(1, _dTypeSize);
memcpy(_pNoDataVal, (void *)&(buf[bufPtr]), _dTypeSize);
bufPtr -= 1;
if(bufPtr < 0) {
cerr << __FILE__ << " function:" << __FUNCTION__ \
<< " Error: the buffer has insufficient memory to extract the Georeference flag of a Cellspace" \
<< endl;
return false;
}
ifGeoref = buf[bufPtr];
buf.erase(buf.begin()+bufPtr, buf.end());
if(ifGeoref == 'y') {
CellspaceGeoinfo geoInfo;
if(!geoInfo.initFromBuf(buf)) {
cerr << __FILE__ << " function:" << __FUNCTION__ \
<< " Error: failed to extract the Georeference information of a Cellspace" \
<< endl;
return false;
}
_pGeoInfo = new pRPL::CellspaceGeoinfo(geoInfo);
}
else if(ifGeoref != 'n') {
cerr << __FILE__ << " function:" << __FUNCTION__ \
<< " Error: invalid Georeference flag (" << ifGeoref \
<< ")" << endl;
return false;
}
return true;
}
void pRPL::CellspaceInfo::
georeference(const pRPL::CellspaceGeoinfo& geoinfo) {
if(_pGeoInfo != NULL) {
delete _pGeoInfo;
_pGeoInfo = NULL;
}
_pGeoInfo = new pRPL::CellspaceGeoinfo(geoinfo);
}
const pRPL::CellspaceGeoinfo* pRPL::CellspaceInfo::
georeference() const {
return _pGeoInfo;
}
bool pRPL::CellspaceInfo::
isGeoreferenced(bool warning) const {
bool isGeofed = true;
if(_pGeoInfo == NULL) {
isGeofed = false;
if(warning) {
cerr << __FILE__ << " function:" << __FUNCTION__ \
<< "Warning: CellspaceInfo is NOT georeferenced yet" << endl;
}
}
return isGeofed;
}
bool pRPL::CellspaceInfo::
isEmpty(bool warning) const {
bool empty = false;
if(_dims.isNone()) {
if(warning) {
cerr << __FILE__ << " function:" << __FUNCTION__ \
<< " Warning: EMPTY Cellspace" << endl;
}
empty = true;
}
return empty;
}
const pRPL::SpaceDims& pRPL::CellspaceInfo::
dims() const {
return _dims;
}
long pRPL::CellspaceInfo::
nRows() const {
return _dims.nRows();
}
long pRPL::CellspaceInfo::
nCols() const {
return _dims.nCols();
}
long pRPL::CellspaceInfo::
tileSize() const {
return _tileSize;
}
long pRPL::CellspaceInfo::
size() const {
return _dims.size();
}
const char* pRPL::CellspaceInfo::
dataType() const {
return _dTypeName.c_str();
}
size_t pRPL::CellspaceInfo::
dataSize() const {
return _dTypeSize;
}
void pRPL::CellspaceInfo::
setTileSize(long tileSize) {
_tileSize = tileSize;
if(_tileSize <= 1 || _tileSize >= _dims.nRows() || _tileSize >= _dims.nCols()) {
_tileSize = TILEWIDTH;
}
}
long pRPL::CellspaceInfo::
coord2idx(const pRPL::CellCoord& coord) const {
return coord.toIdx(_dims);
}
long pRPL::CellspaceInfo::
coord2idx(long iRow, long iCol) const {
return coord2idx(CellCoord(iRow, iCol));
}
pRPL::CellCoord pRPL::CellspaceInfo::
idx2coord(long idx) const {
return pRPL::CellCoord(idx, _dims);
}
bool pRPL::CellspaceInfo::
validCoord(const pRPL::CellCoord& coord,
bool warning) const {
bool valid = true;
if(!coord.valid(_dims)) {
valid = false;
if(warning) {
cerr << __FILE__ << " function:" << __FUNCTION__ \
<< " Error: coordinates [" << coord \
<< "] out of CellspaceInfo boundary [" \
<< _dims << "]" << endl;
}
}
return valid;
}
bool pRPL::CellspaceInfo::
validCoord(long iRow, long iCol,
bool warning) const {
return validCoord(pRPL::CellCoord(iRow, iCol), warning);
}
bool pRPL::CellspaceInfo::
validIdx(long idx,
bool warning) const {
bool valid = true;
if(!_dims.validIdx(idx)) {
valid = false;
if(warning) {
cerr << __FILE__ << " function:" << __FUNCTION__ \
<< " Error: index [" << idx \
<< "] out of CellspaceInfo size [" \
<< size() << "]" \
<< endl;
}
}
return valid;
}
bool pRPL::CellspaceInfo::
validBR(const pRPL::CoordBR& rectangle,
bool warning) const {
bool valid = true;
if(!rectangle.valid(_dims)) {
valid = false;
if(warning) {
cerr << __FILE__ << " function:" << __FUNCTION__ \
<< " Error: rectangle [" << rectangle \
<< "] out of CellspaceInfo boundary [" << _dims << "]" << endl;
}
}
return valid;
}
pRPL::GeoCoord pRPL::CellspaceInfo::
cellCoord2geoCoord(const pRPL::CellCoord &cCoord) const {
pRPL::GeoCoord gCoord;
if(!isEmpty(true) &&
isGeoreferenced(true) &&
validCoord(cCoord, true)) {
gCoord = _pGeoInfo->cellCoord2geoCoord(cCoord);
}
return gCoord;
}
pRPL::GeoCoord pRPL::CellspaceInfo::
cellCoord2geoCoord(long iRow, long iCol) const {
pRPL::GeoCoord gCoord;
if(!isEmpty(true) &&
isGeoreferenced(true) &&
validCoord(iRow, iCol, true)) {
gCoord = _pGeoInfo->cellCoord2geoCoord(iRow, iCol);
}
return gCoord;
}
pRPL::CellCoord pRPL::CellspaceInfo::
geoCoord2cellCoord(const pRPL::GeoCoord &gCoord) const {
pRPL::CellCoord cCoord;
if(!isEmpty(true) && isGeoreferenced(true)) {
cCoord = _pGeoInfo->geoCoord2cellCoord(gCoord);
if(!validCoord(cCoord, true)) {
cCoord = CellCoord();
}
}
return cCoord;
}
pRPL::CellCoord pRPL::CellspaceInfo::
geoCoord2cellCoord(double x, double y) const {
pRPL::CellCoord cCoord;
if(!isEmpty(true) && isGeoreferenced(true)) {
cCoord = _pGeoInfo->geoCoord2cellCoord(x, y);
if(!validCoord(cCoord, true)) {
cCoord = CellCoord();
}
}
return cCoord;
}
bool pRPL::CellspaceInfo::
calcWorkBR(pRPL::CoordBR *pWorkBR,
const pRPL::Neighborhood *pNbrhd,
bool warning) const {
bool done = true;
if(pWorkBR == NULL) {
if(warning) {
cerr << __FILE__ << " function:" << __FUNCTION__ \
<< " Error: NULL pointer to the work CoordBR" << endl;
}
done = false;
}
else if(pNbrhd == NULL || pNbrhd->isEmpty()) {
pWorkBR->nwCorner(0, 0);
pWorkBR->seCorner(_dims.nRows()-1, _dims.nCols()-1);
}
else {
done = pNbrhd->calcWorkBR(*pWorkBR, _dims);
}
return done;
}
bool pRPL::CellspaceInfo::
validNbrhd(const pRPL::Neighborhood &nbrhd,
const pRPL::CellCoord &ctrCoord,
bool warning) const {
if(nbrhd.isEmpty()) {
if(warning) {
cerr << __FILE__ << " function: " << __FUNCTION__ \
<< " Error: empty Neighborhood" << endl;
}
return false;
}
if(nbrhd.edgeOption() == pRPL::FORBID_VIRTUAL_EDGES) {
if(ctrCoord.iRow() < -(nbrhd.minIRow()) ||
ctrCoord.iRow() > nRows()-1-nbrhd.maxIRow() ||
ctrCoord.iCol() < -(nbrhd.minICol()) ||
ctrCoord.iCol() > nCols()-1-nbrhd.maxICol()) {
if(warning) {
cerr << __FILE__ << " function: " << __FUNCTION__ \
<< " Error: invalid center coordinate[" \
<< ctrCoord << "] for a Neighborhood in CellspaceInfo (" \
<< _dims << ")"<< endl;
}
return false;
}
}
else {
if(!ctrCoord.valid(_dims)) {
if(warning) {
cerr << __FILE__ << " function: " << __FUNCTION__ \
<< " Error: invalid center coordinate[" \
<< ctrCoord << "] for a Neighborhood in CellspaceInfo (" \
<< _dims << ")"<< endl;
}
return false;
}
}
return true;
}
bool pRPL::CellspaceInfo::
validNbrhd(const pRPL::Neighborhood &nbrhd,
long ctrRow, long ctrCol,
bool warning) const {
return validNbrhd(nbrhd, pRPL::CellCoord(ctrRow, ctrCol), warning);
}
bool pRPL::CellspaceInfo::
validNbrhd(const pRPL::Neighborhood &nbrhd,
long idx,
bool warning) const {
return validNbrhd(nbrhd, pRPL::CellCoord(idx, _dims), warning);
}