nodata.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /****************************************************************************
  2. *
  3. * MODULE: r.terraflow
  4. *
  5. * COPYRIGHT (C) 2007 Laura Toma
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. *****************************************************************************/
  18. #ifndef NODATA_H
  19. #define NODATA_H
  20. #include <assert.h>
  21. #include <grass/iostream/ami.h>
  22. #include "types.h"
  23. #include "option.h"
  24. /* somewhat of a hack. should read the GRASS nodata value instead
  25. (which normally is MAX_INT), but, is it worth it? */
  26. #define TERRAFLOW_INTERNAL_NODATA_VALUE -9999
  27. int is_nodata(elevation_type el);
  28. int is_nodata(int x);
  29. int is_nodata(float x);
  30. int is_void(elevation_type el);
  31. class nodataType : public ijBaseType {
  32. public: /* struct, so members public */
  33. cclabel_type label;
  34. bool valid;
  35. static elevation_type ELEVATION_BOUNDARY; /* means this cell is on
  36. * the grid boundary,
  37. * directly, or via
  38. * connect nodata cells */
  39. static elevation_type ELEVATION_NODATA; /* means we have no data for
  40. * this cell */
  41. public:
  42. nodataType(dimension_type gi, dimension_type gj, cclabel_type glab) :
  43. ijBaseType(gi,gj), label(glab), valid(true) {};
  44. nodataType() : valid(false) {};
  45. void invalidate() { valid = false; }
  46. elevation_type getElevation() {
  47. return (label == LABEL_BOUNDARY ? ELEVATION_BOUNDARY : ELEVATION_NODATA);
  48. }
  49. static char *printLabel(const nodataType &p) {
  50. static char buf[8];
  51. sprintf(buf, CCLABEL_FMT, p.label);
  52. return buf;
  53. }
  54. static void init(elevation_type nodata) {
  55. /* somewhat of a hack... */
  56. ELEVATION_NODATA = nodata;
  57. ELEVATION_BOUNDARY = nodata+1;
  58. /* ELEVATION_BOUNDARY = ELEVATION_MIN; */
  59. /* assert(ELEVATION_NODATA != ELEVATION_MIN); */
  60. }
  61. /* LAURA: i added this polymorph because Terraflow has now a FIXED
  62. internal value for nodata; it does not read GRASS nodata value */
  63. static void init() {
  64. /* somewhat of a hack... */
  65. ELEVATION_NODATA = TERRAFLOW_INTERNAL_NODATA_VALUE;
  66. ELEVATION_BOUNDARY = TERRAFLOW_INTERNAL_NODATA_VALUE + 1;
  67. /* ELEVATION_BOUNDARY = ELEVATION_MIN; */
  68. /* assert(ELEVATION_NODATA != ELEVATION_MIN); */
  69. }
  70. friend ostream& operator << (ostream& s, const nodataType &p) {
  71. if(p.valid) {
  72. return s << "[" << p.i << "," << p.j
  73. << "; lbl=" << p.label << "]";
  74. } else {
  75. return s << "[invalid]";
  76. }
  77. }
  78. };
  79. class nodataType2elevation_type {
  80. public:
  81. elevation_type operator()(nodataType n) { return n.getElevation(); }
  82. elevation_type operator()(elevation_type n) { return n; }
  83. };
  84. class labelCmpNodataType {
  85. public:
  86. static int compare(const nodataType &a, const nodataType &b) {
  87. if(a.label < b.label) return -1;
  88. if(a.label > b.label) return 1;
  89. return 0;
  90. }
  91. };
  92. class ijCmpNodataType {
  93. public:
  94. static int compare(const nodataType &a, const nodataType &b) {
  95. return ijBaseType::compare(a, b);
  96. }
  97. };
  98. AMI_STREAM<elevation_type> *
  99. classifyNodata(AMI_STREAM<elevation_type> *elstr);
  100. #endif