File: pcrasterutil.cpp

package info (click to toggle)
gdal 1.10.1%2Bdfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 84,320 kB
  • ctags: 74,726
  • sloc: cpp: 677,199; ansic: 162,820; python: 13,816; cs: 11,163; sh: 10,446; java: 5,279; perl: 4,429; php: 2,971; xml: 1,500; yacc: 934; makefile: 494; sql: 112
file content (714 lines) | stat: -rw-r--r-- 17,562 bytes parent folder | download
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
/******************************************************************************
 * $Id: pcrasterutil.cpp 15450 2008-10-03 10:59:32Z kdejong $
 *
 * Project:  PCRaster Integration
 * Purpose:  PCRaster driver support functions.
 * Author:   Kor de Jong, k.dejong at geog.uu.nl
 *
 ******************************************************************************
 * Copyright (c) 2004, Kor de Jong
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 ****************************************************************************/

#ifndef INCLUDED_IOSTREAM
#include <iostream>
#define INCLUDED_IOSTREAM
#endif

#ifndef INCLUDED_ALGORITHM
#include <algorithm>
#define INCLUDED_ALGORITHM
#endif

#ifndef INCLUDED_FLOAT
#include <float.h>
#define INCLUDED_FLOAT
#endif

#ifndef INCLUDED_PCRTYPES
#include "pcrtypes.h"
#define INCLUDED_PCRTYPES
#endif

#ifndef INCLUDED_PCRASTERUTIL
#include "pcrasterutil.h"
#define INCLUDED_PCRASTERUTIL
#endif



//! Converts PCRaster data type to GDAL data type.
/*!
  \param     cellRepresentation Cell representation.
  \return    GDAL data type, GDT_Uknown if conversion is not possible.
*/
GDALDataType cellRepresentation2GDALType(
         CSF_CR cellRepresentation)
{
  GDALDataType type = GDT_Unknown;

  switch(cellRepresentation) {
    // CSF version 2. ----------------------------------------------------------
    case CR_UINT1: {
      type = GDT_Byte;
      break;
    }
    case CR_INT4: {
      type = GDT_Int32;
      break;
    }
    case CR_REAL4: {
      type = GDT_Float32;
      break;
    }
    case CR_REAL8: {
      type = GDT_Float64;
      break;
    }
    // CSF version 1. ----------------------------------------------------------
    case CR_INT1: {
      type = GDT_Byte;
      break;
    }
    case CR_INT2: {
      type = GDT_Int16;
      break;
    }
    case CR_UINT2: {
      type = GDT_UInt16;
      break;
    }
    case CR_UINT4: {
      type = GDT_UInt32;
      break;
    }
    default: {
      break;
    }
  }

  return type;
}



CSF_VS string2ValueScale(
         std::string const& string)
{
  CSF_VS valueScale = VS_UNDEFINED;

  // CSF version 2. ------------------------------------------------------------
  if(string == "VS_BOOLEAN") {
    valueScale = VS_BOOLEAN;
  }
  else if(string == "VS_NOMINAL") {
    valueScale = VS_NOMINAL;
  }
  else if(string == "VS_ORDINAL") {
    valueScale = VS_ORDINAL;
  }
  else if(string == "VS_SCALAR") {
    valueScale = VS_SCALAR;
  }
  else if(string == "VS_DIRECTION") {
    valueScale = VS_DIRECTION;
  }
  else if(string == "VS_LDD") {
    valueScale = VS_LDD;
  }
  // CSF version1. -------------------------------------------------------------
  else if(string == "VS_CLASSIFIED") {
    valueScale = VS_CLASSIFIED;
  }
  else if(string == "VS_CONTINUOUS") {
    valueScale = VS_CONTINUOUS;
  }
  else if(string == "VS_NOTDETERMINED") {
    valueScale = VS_NOTDETERMINED;
  }

  return valueScale;
}



std::string valueScale2String(
         CSF_VS valueScale)
{
  std::string result = "VS_UNDEFINED";

  switch(valueScale) {
    // CSF version 2. ----------------------------------------------------------
    case VS_BOOLEAN: {
      result = "VS_BOOLEAN";
      break;
    }
    case VS_NOMINAL: {
      result = "VS_NOMINAL";
      break;
    }
    case VS_ORDINAL: {
      result = "VS_ORDINAL";
      break;
    }
    case VS_SCALAR: {
      result = "VS_SCALAR";
      break;
    }
    case VS_DIRECTION: {
      result = "VS_DIRECTION";
      break;
    }
    case VS_LDD: {
      result = "VS_LDD";
      break;
    }
    // CSF version 1. ----------------------------------------------------------
    case VS_CLASSIFIED: {
      result = "VS_CLASSIFIED";
      break;
    }
    case VS_CONTINUOUS: {
      result = "VS_CONTINUOUS";
      break;
    }
    case VS_NOTDETERMINED: {
      result = "VS_NOTDETERMINED";
      break;
    }
    default: {
      break;
    }
  }

  return result;
}



std::string cellRepresentation2String(
         CSF_CR cellRepresentation)
{
  std::string result = "CR_UNDEFINED";

  switch(cellRepresentation) {

    // CSF version 2. ----------------------------------------------------------
    case CR_UINT1: {
      result = "CR_UINT1";
      break;
    }
    case CR_INT4: {
      result = "CR_INT4";
      break;
    }
    case CR_REAL4: {
      result = "CR_REAL4";
      break;
    }
    case CR_REAL8: {
      result = "CR_REAL8";
      break;
    }
    // CSF version 1. ----------------------------------------------------------
    case CR_INT1: {
      result = "CR_INT1";
      break;
    }
    case CR_INT2: {
      result = "CR_INT2";
      break;
    }
    case CR_UINT2: {
      result = "CR_UINT2";
      break;
    }
    case CR_UINT4: {
      result = "CR_UINT4";
      break;
    }
    default: {
      break;
    }
  }

  return result;
}



//! Converts GDAL data type to PCRaster value scale.
/*!
  \param     type GDAL data type.
  \return    Value scale.
  \warning   \a type must be one of the standard numerical types and not
             complex.

  GDAL byte is regarded as PCRaster boolean, integral as nominal and float
  as scalar. This function will never return VS_LDD, VS_ORDINAL or
  VS_DIRECTION.
*/
CSF_VS GDALType2ValueScale(
         GDALDataType type)
{
  CSF_VS valueScale = VS_UNDEFINED;

  switch(type) {
    case GDT_Byte: {
      // A foreign dataset is unlikely to support our LDD's.
      valueScale = VS_BOOLEAN;
      break;
    }
    case GDT_UInt16:
    case GDT_UInt32:
    case GDT_Int16:
    case GDT_Int32: {
      valueScale = VS_NOMINAL;
      break;
    }
    case GDT_Float32: {
      // A foreign dataset is unlikely to support our directional.
      valueScale = VS_SCALAR;
      break;
    }
    case GDT_Float64: {
      // A foreign dataset is unlikely to support our directional.
      valueScale = VS_SCALAR;
      break;
    }
    default: {
      CPLAssert(false);
      break;
    }
  }

  return valueScale;
}



//! Converts a GDAL type to a PCRaster cell representation.
/*!
  \param     type GDAL type.
  \param     exact Whether an exact match or a CSF2.0 supported cell
                   representation should be returned.
  \return    Cell representation.
  \warning   \a type must be one of the standard numerical types and not
             complex.

  If exact is false, conversion to CSF2.0 types will take place. This is
  useful for in file cell representations. If exact is true, and exact match
  is made. This is useful for in app cell representations.

  If exact is false, this function always returns one of CR_UINT1, CR_INT4
  or CR_REAL4.
*/
CSF_CR GDALType2CellRepresentation(
         GDALDataType type,
         bool exact)
{
  CSF_CR cellRepresentation = CR_UNDEFINED;

  switch(type) {
    case GDT_Byte: {
      cellRepresentation = CR_UINT1;
      break;
    }
    case GDT_UInt16: {
      cellRepresentation = exact ? CR_UINT2: CR_UINT1;
      break;
    }
    case GDT_UInt32: {
      cellRepresentation = exact ? CR_UINT4: CR_UINT1;
      break;
    }
    case GDT_Int16: {
      cellRepresentation = exact ? CR_INT2: CR_INT4;
      break;
    }
    case GDT_Int32: {
      cellRepresentation = CR_INT4;
      break;
    }
    case GDT_Float32: {
      cellRepresentation = CR_REAL4;
      break;
    }
    case GDT_Float64: {
      cellRepresentation = exact ? CR_REAL8: CR_REAL4;
      break;
    }
    default: {
      break;
    }
  }

  return cellRepresentation;
}



//! Determines a missing value to use for data of \a cellRepresentation.
/*!
  \param     cellRepresentation Cell representation of the data.
  \return    Missing value.
  \exception .
  \sa        .
*/
double missingValue(
         CSF_CR cellRepresentation)
{
  // It turns out that the missing values set here should be equal to the ones
  // used in gdal's code to do data type conversion. Otherwise missing values
  // in the source raster will be lost in the destination raster. It seems that
  // when assigning new missing values gdal uses its own nodata values instead
  // of the value set in the dataset.

  double missingValue = 0.0;

  switch(cellRepresentation) {
    // CSF version 2. ----------------------------------------------------------
    case CR_UINT1: {
      // missingValue = static_cast<double>(MV_UINT1);
      missingValue = UINT1(255);
      break;
    }
    case CR_INT4: {
      // missingValue = static_cast<double>(MV_INT4);
      missingValue = INT4(-2147483647);
      break;
    }
    case CR_REAL4: {
      // using <limits> breaks on gcc 2.95
      // CPLAssert(std::numeric_limits<REAL4>::is_iec559);
      // missingValue = -std::numeric_limits<REAL4>::max();
      missingValue = -FLT_MAX;
      break;
    }
    // CSF version 1. ----------------------------------------------------------
    case CR_INT1: {
      missingValue = static_cast<double>(MV_INT1);
      break;
    }
    case CR_INT2: {
      missingValue = static_cast<double>(MV_INT2);
      break;
    }
    case CR_UINT2: {
      missingValue = static_cast<double>(MV_UINT2);
      break;
    }
    case CR_UINT4: {
      missingValue = static_cast<double>(MV_UINT4);
      break;
    }
    default: {
      CPLAssert(false);
      break;
    }
  }

  return missingValue;
}



//! Opens the raster in \a filename using mode \a mode.
/*!
  \param     filename Filename of raster to open.
  \return    Pointer to CSF MAP structure.
  \exception .
  \warning   .
  \sa        .
*/
MAP* mapOpen(
         std::string const& filename,
         MOPEN_PERM mode)
{
  MAP* map = Mopen(filename.c_str(), mode);

  return map;
}



void alterFromStdMV(
         void* buffer,
         size_t size,
         CSF_CR cellRepresentation,
         double missingValue)
{
  switch(cellRepresentation) {
    // CSF version 2. ----------------------------------------------------------
    case(CR_UINT1): {
      std::for_each(static_cast<UINT1*>(buffer),
         static_cast<UINT1*>(buffer) + size,
         pcr::AlterFromStdMV<UINT1>(static_cast<UINT1>(missingValue)));
      break;
    }
    case(CR_INT4): {
      std::for_each(static_cast<INT4*>(buffer),
         static_cast<INT4*>(buffer) + size,
         pcr::AlterFromStdMV<INT4>(static_cast<INT4>(missingValue)));
      break;
    }
    case(CR_REAL4): {
      std::for_each(static_cast<REAL4*>(buffer),
         static_cast<REAL4*>(buffer) + size,
         pcr::AlterFromStdMV<REAL4>(static_cast<REAL4>(missingValue)));
      break;
    }
    case(CR_REAL8): {
      std::for_each(static_cast<REAL8*>(buffer),
         static_cast<REAL8*>(buffer) + size,
         pcr::AlterFromStdMV<REAL8>(static_cast<REAL8>(missingValue)));
      break;
    }
    // CSF version 1. ----------------------------------------------------------
    case CR_INT1: {
      std::for_each(static_cast<INT1*>(buffer),
         static_cast<INT1*>(buffer) + size,
         pcr::AlterFromStdMV<INT1>(static_cast<INT1>(missingValue)));
      break;
    }
    case CR_INT2: {
      std::for_each(static_cast<INT2*>(buffer),
         static_cast<INT2*>(buffer) + size,
         pcr::AlterFromStdMV<INT2>(static_cast<INT2>(missingValue)));
      break;
    }
    case CR_UINT2: {
      std::for_each(static_cast<UINT2*>(buffer),
         static_cast<UINT2*>(buffer) + size,
         pcr::AlterFromStdMV<UINT2>(static_cast<UINT2>(missingValue)));
      break;
    }
    case CR_UINT4: {
      std::for_each(static_cast<UINT4*>(buffer),
         static_cast<UINT4*>(buffer) + size,
         pcr::AlterFromStdMV<UINT4>(static_cast<UINT4>(missingValue)));
      break;
    }
    default: {
      CPLAssert(false);
      break;
    }
  }
}



void alterToStdMV(
         void* buffer,
         size_t size,
         CSF_CR cellRepresentation,
         double missingValue)
{
  switch(cellRepresentation) {
    // CSF version 2. ----------------------------------------------------------
    case(CR_UINT1): {
      std::for_each(static_cast<UINT1*>(buffer),
         static_cast<UINT1*>(buffer) + size,
         pcr::AlterToStdMV<UINT1>(static_cast<UINT1>(missingValue)));
      break;
    }
    case(CR_INT4): {
      std::for_each(static_cast<INT4*>(buffer),
         static_cast<INT4*>(buffer) + size,
         pcr::AlterToStdMV<INT4>(static_cast<INT4>(missingValue)));
      break;
    }
    case(CR_REAL4): {
      std::for_each(static_cast<REAL4*>(buffer),
         static_cast<REAL4*>(buffer) + size,
         pcr::AlterToStdMV<REAL4>(static_cast<REAL4>(missingValue)));
      break;
    }
    case(CR_REAL8): {
      std::for_each(static_cast<REAL8*>(buffer),
         static_cast<REAL8*>(buffer) + size,
         pcr::AlterToStdMV<REAL8>(static_cast<REAL8>(missingValue)));
      break;
    }
    // CSF version 1. ----------------------------------------------------------
    case CR_INT1: {
      std::for_each(static_cast<INT1*>(buffer),
         static_cast<INT1*>(buffer) + size,
         pcr::AlterToStdMV<INT1>(static_cast<INT1>(missingValue)));
      break;
    }
    case CR_INT2: {
      std::for_each(static_cast<INT2*>(buffer),
         static_cast<INT2*>(buffer) + size,
         pcr::AlterToStdMV<INT2>(static_cast<INT2>(missingValue)));
      break;
    }
    case CR_UINT2: {
      std::for_each(static_cast<UINT2*>(buffer),
         static_cast<UINT2*>(buffer) + size,
         pcr::AlterToStdMV<UINT2>(static_cast<UINT2>(missingValue)));
      break;
    }
    case CR_UINT4: {
      std::for_each(static_cast<UINT4*>(buffer),
         static_cast<UINT4*>(buffer) + size,
         pcr::AlterToStdMV<UINT4>(static_cast<UINT4>(missingValue)));
      break;
    }
    default: {
      CPLAssert(false);
      break;
    }
  }
}



CSF_VS fitValueScale(
         CSF_VS valueScale,
         CSF_CR cellRepresentation)
{
  CSF_VS result = valueScale;

  switch(cellRepresentation) {
    case CR_UINT1: {
      switch(valueScale) {
        case VS_LDD: {
          result = VS_LDD;
          break;
        }
        default: {
          result = VS_BOOLEAN;
          break;
        }
      }
      break;
    }
    case CR_INT4: {
      switch(valueScale) {
        case VS_BOOLEAN: {
          result = VS_NOMINAL;
          break;
        }
        case VS_SCALAR: {
          result = VS_ORDINAL;
          break;
        }
        case VS_DIRECTION: {
          result = VS_ORDINAL;
          break;
        }
        case VS_LDD: {
          result = VS_NOMINAL;
          break;
        }
        default: {
          result = valueScale;
          break;
        }
      }
      break;
    }
    case CR_REAL4: {
      switch(valueScale) {
        case VS_DIRECTION: {
          result = VS_DIRECTION;
          break;
        }
        default: {
          result = VS_SCALAR;
          break;
        }
      }
      break;
    }
    default: {
      break;
    }
  }

  return result;
}



void castValuesToBooleanRange(
         void* buffer,
         size_t size,
         CSF_CR cellRepresentation)
{
  switch(cellRepresentation) {
    // CSF version 2. ----------------------------------------------------------
    case(CR_UINT1): {
      std::for_each(static_cast<UINT1*>(buffer),
         static_cast<UINT1*>(buffer) + size,
         CastToBooleanRange<UINT1>());
      break;
    }
    case(CR_INT4): {
      std::for_each(static_cast<INT4*>(buffer),
         static_cast<INT4*>(buffer) + size,
         CastToBooleanRange<INT4>());
      break;
    }
    case(CR_REAL4): {
      std::for_each(static_cast<REAL4*>(buffer),
         static_cast<REAL4*>(buffer) + size,
         CastToBooleanRange<REAL4>());
      break;
    }
    case(CR_REAL8): {
      std::for_each(static_cast<REAL8*>(buffer),
         static_cast<REAL8*>(buffer) + size,
         CastToBooleanRange<REAL8>());
      break;
    }
    // CSF version 1. ----------------------------------------------------------
    case CR_INT1: {
      std::for_each(static_cast<INT1*>(buffer),
         static_cast<INT1*>(buffer) + size,
         CastToBooleanRange<INT1>());
      break;
    }
    case CR_INT2: {
      std::for_each(static_cast<INT2*>(buffer),
         static_cast<INT2*>(buffer) + size,
         CastToBooleanRange<INT2>());
      break;
    }
    case CR_UINT2: {
      std::for_each(static_cast<UINT2*>(buffer),
         static_cast<UINT2*>(buffer) + size,
         CastToBooleanRange<UINT2>());
      break;
    }
    case CR_UINT4: {
      std::for_each(static_cast<UINT4*>(buffer),
         static_cast<UINT4*>(buffer) + size,
         CastToBooleanRange<UINT4>());
      break;
    }
    default: {
      CPLAssert(false);
      break;
    }
  }
}