attr.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #include <grass/gis.h>
  2. #include <grass/raster.h>
  3. #include <grass/glocale.h>
  4. #include "cpl_string.h"
  5. #include "gdal.h"
  6. #include "local_proto.h"
  7. int export_attr(GDALDatasetH hMEMDS, int band,
  8. const char *name, const char *mapset,
  9. RASTER_MAP_TYPE maptype)
  10. {
  11. struct Categories cats;
  12. CELL CellMin;
  13. CELL CellMax;
  14. DCELL dfCellMin;
  15. DCELL dfCellMax;
  16. char *label;
  17. struct Colors sGrassColors;
  18. int rcount;
  19. int i;
  20. int ret = 0;
  21. GDALRasterAttributeTableH hrat;
  22. GDALRasterBandH hBand;
  23. Rast_init_cats("Labels", &cats);
  24. if (Rast_read_cats(name, mapset, &cats))
  25. return -1;
  26. rcount = 0;
  27. Rast_init_colors(&sGrassColors);
  28. if (Rast_read_colors(name, mapset, &sGrassColors) >= 0) {
  29. rcount = Rast_colors_count(&sGrassColors);
  30. }
  31. if (cats.ncats == 0 && rcount == 0)
  32. return 0;
  33. /* Get raster band */
  34. hBand = GDALGetRasterBand(hMEMDS, band);
  35. /* Field usage of raster attribute table:
  36. * GFU_Generic = 0, GFU_PixelCount = 1, GFU_Name = 2,
  37. * GFU_Min = 3, GFU_Max = 4, GFU_MinMax = 5,
  38. * GFU_Red = 6, GFU_Green = 7, GFU_Blue = 8, GFU_Alpha = 9,
  39. * GFU_RedMin = 10, GFU_GreenMin = 11, GFU_BlueMin = 12, GFU_AlphaMin = 13,
  40. * GFU_RedMax = 14, GFU_GreenMax = 15, GFU_BlueMax = 16, GFU_AlphaMax = 17,
  41. * GFU_MaxCount
  42. */
  43. /* TODO: cats.ncats > 0 && rcount > 0
  44. * how to merge categories and color rules ?
  45. * what to do for a cell value that has a category but no color rule ?
  46. * what to do for a cell value that has a color rule but no category ?
  47. */
  48. if (cats.ncats > 0) {
  49. int use_minmax = 0;
  50. if (maptype == CELL_TYPE) {
  51. for (i = 0; i < cats.ncats; i++) {
  52. label = Rast_get_ith_c_cat(&cats, i, &CellMin, &CellMax);
  53. if (CellMin != CellMax) {
  54. use_minmax = 1;
  55. break;
  56. }
  57. }
  58. }
  59. else {
  60. for (i = 0; i < cats.ncats; i++) {
  61. label = Rast_get_ith_d_cat(&cats, i, &dfCellMin, &dfCellMax);
  62. if (dfCellMin != dfCellMax) {
  63. use_minmax = 1;
  64. break;
  65. }
  66. }
  67. }
  68. /* create new raster attribute table */
  69. hrat = GDALCreateRasterAttributeTable();
  70. if (use_minmax) {
  71. if (maptype == CELL_TYPE) {
  72. GDALRATCreateColumn(hrat, "min", GFT_Integer, GFU_Min);
  73. GDALRATCreateColumn(hrat, "max", GFT_Integer, GFU_Max);
  74. }
  75. else {
  76. GDALRATCreateColumn(hrat, "min", GFT_Real, GFU_Min);
  77. GDALRATCreateColumn(hrat, "max", GFT_Real, GFU_Max);
  78. }
  79. GDALRATCreateColumn(hrat, "label", GFT_String, GFU_Name);
  80. GDALRATSetRowCount(hrat, cats.ncats);
  81. if (maptype == CELL_TYPE) {
  82. for (i = 0; i < cats.ncats; i++) {
  83. label = Rast_get_ith_c_cat(&cats, i, &CellMin, &CellMax);
  84. GDALRATSetValueAsInt(hrat, i, 0, CellMin);
  85. GDALRATSetValueAsInt(hrat, i, 1, CellMax);
  86. GDALRATSetValueAsString(hrat, i, 2, label);
  87. }
  88. }
  89. else {
  90. for (i = 0; i < cats.ncats; i++) {
  91. label = Rast_get_ith_d_cat(&cats, i, &dfCellMin, &dfCellMax);
  92. GDALRATSetValueAsDouble(hrat, i, 0, dfCellMin);
  93. GDALRATSetValueAsDouble(hrat, i, 1, dfCellMax);
  94. GDALRATSetValueAsString(hrat, i, 2, label);
  95. }
  96. }
  97. }
  98. else {
  99. if (maptype == CELL_TYPE) {
  100. GDALRATCreateColumn(hrat, "value", GFT_Integer, GFU_MinMax);
  101. }
  102. else {
  103. GDALRATCreateColumn(hrat, "value", GFT_Real, GFU_MinMax);
  104. }
  105. GDALRATCreateColumn(hrat, "label", GFT_String, GFU_Name);
  106. GDALRATSetRowCount(hrat, cats.ncats);
  107. if (maptype == CELL_TYPE) {
  108. for (i = 0; i < cats.ncats; i++) {
  109. label = Rast_get_ith_c_cat(&cats, i, &CellMin, &CellMax);
  110. GDALRATSetValueAsInt(hrat, i, 0, CellMin);
  111. GDALRATSetValueAsString(hrat, i, 1, label);
  112. }
  113. }
  114. else {
  115. for (i = 0; i < cats.ncats; i++) {
  116. label = Rast_get_ith_d_cat(&cats, i, &dfCellMin, &dfCellMax);
  117. GDALRATSetValueAsDouble(hrat, i, 0, dfCellMin);
  118. GDALRATSetValueAsString(hrat, i, 1, label);
  119. }
  120. }
  121. }
  122. if (GDALSetDefaultRAT(hBand, hrat) != CE_None) {
  123. G_warning(_("Failed to set raster attribute table"));
  124. ret = -1;
  125. }
  126. /* GDALRATDumpReadable(hrat, stdout); */
  127. GDALDestroyRasterAttributeTable(hrat);
  128. }
  129. else if (cats.ncats == 0 && rcount > 0) {
  130. unsigned char r1, g1, b1, r2, g2, b2;
  131. /* create new raster attribute table */
  132. hrat = GDALCreateRasterAttributeTable();
  133. GDALRATCreateColumn(hrat, "min", GFT_Real, GFU_Min);
  134. GDALRATCreateColumn(hrat, "max", GFT_Real, GFU_Max);
  135. GDALRATCreateColumn(hrat, "redmin", GFT_Integer, GFU_RedMin);
  136. GDALRATCreateColumn(hrat, "redmax", GFT_Integer, GFU_RedMax);
  137. GDALRATCreateColumn(hrat, "greenmin", GFT_Integer, GFU_GreenMin);
  138. GDALRATCreateColumn(hrat, "greenmax", GFT_Integer, GFU_GreenMax);
  139. GDALRATCreateColumn(hrat, "bluemin", GFT_Integer, GFU_BlueMin);
  140. GDALRATCreateColumn(hrat, "bluemax", GFT_Integer, GFU_BlueMax);
  141. for (i = 0; i < rcount; i++) {
  142. Rast_get_fp_color_rule(&dfCellMin, &r1, &g1, &b1,
  143. &dfCellMax, &r2, &g2, &b2,
  144. &sGrassColors, i);
  145. GDALRATSetValueAsDouble(hrat, i, 0, dfCellMin);
  146. GDALRATSetValueAsDouble(hrat, i, 1, dfCellMax);
  147. GDALRATSetValueAsInt(hrat, i, 2, r1);
  148. GDALRATSetValueAsInt(hrat, i, 3, r2);
  149. GDALRATSetValueAsInt(hrat, i, 4, g1);
  150. GDALRATSetValueAsInt(hrat, i, 5, g2);
  151. GDALRATSetValueAsInt(hrat, i, 6, b1);
  152. GDALRATSetValueAsInt(hrat, i, 7, b2);
  153. }
  154. if (GDALSetDefaultRAT(hBand, hrat) != CE_None) {
  155. G_warning(_("Failed to set raster attribute table"));
  156. ret = -1;
  157. }
  158. /* GDALRATDumpReadable(hrat, stdout); */
  159. GDALDestroyRasterAttributeTable(hrat);
  160. }
  161. Rast_free_cats(&cats);
  162. Rast_free_colors(&sGrassColors);
  163. return ret;
  164. }