link.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #include <grass/gis.h>
  2. #include <grass/glocale.h>
  3. #include <gdal.h>
  4. #include "proto.h"
  5. void query_band(GDALRasterBandH hBand, const char *output,
  6. struct Cell_head *cellhd, struct band_info *info)
  7. {
  8. int bGotMin, bGotMax;
  9. info->gdal_type = GDALGetRasterDataType(hBand);
  10. info->null_val = GDALGetRasterNoDataValue(hBand, &info->has_null);
  11. cellhd->compressed = 0;
  12. switch (info->gdal_type) {
  13. case GDT_Float32:
  14. info->data_type = FCELL_TYPE;
  15. cellhd->format = -1;
  16. break;
  17. case GDT_Float64:
  18. info->data_type = DCELL_TYPE;
  19. cellhd->format = -1;
  20. break;
  21. case GDT_Byte:
  22. info->data_type = CELL_TYPE;
  23. cellhd->format = 0;
  24. break;
  25. case GDT_Int16:
  26. case GDT_UInt16:
  27. info->data_type = CELL_TYPE;
  28. cellhd->format = 1;
  29. break;
  30. case GDT_Int32:
  31. case GDT_UInt32:
  32. info->data_type = CELL_TYPE;
  33. cellhd->format = 3;
  34. break;
  35. default:
  36. G_fatal_error(_("Complex types not supported"));
  37. break;
  38. }
  39. info->range[0] = GDALGetRasterMinimum(hBand, &bGotMin);
  40. info->range[1] = GDALGetRasterMaximum(hBand, &bGotMax);
  41. if(!(bGotMin && bGotMax))
  42. GDALComputeRasterMinMax(hBand, 0, info->range);
  43. Rast_init_colors(&info->colors);
  44. if (GDALGetRasterColorTable(hBand) != NULL) {
  45. GDALColorTableH hCT;
  46. int count, i;
  47. G_verbose_message(_("Copying color table for %s"), output);
  48. hCT = GDALGetRasterColorTable(hBand);
  49. count = GDALGetColorEntryCount(hCT);
  50. for (i = 0; i < count; i++) {
  51. GDALColorEntry sEntry;
  52. GDALGetColorEntryAsRGB(hCT, i, &sEntry);
  53. if (sEntry.c4 == 0)
  54. continue;
  55. Rast_set_c_color(i, sEntry.c1, sEntry.c2, sEntry.c3, &info->colors);
  56. }
  57. }
  58. else {
  59. if (info->gdal_type == GDT_Byte) {
  60. /* set full 0..255 range to grey scale: */
  61. G_verbose_message(_("Setting grey color table for <%s> (full 8bit range)"),
  62. output);
  63. Rast_make_grey_scale_colors(&info->colors, 0, 255);
  64. }
  65. else {
  66. /* set data range to grey scale: */
  67. G_verbose_message(_("Setting grey color table for <%s> (data range)"),
  68. output);
  69. Rast_make_grey_scale_colors(&info->colors,
  70. (int) info->range[0], (int) info->range[1]);
  71. }
  72. }
  73. }
  74. void make_cell(const char *output, const struct band_info *info)
  75. {
  76. FILE *fp;
  77. fp = G_fopen_new("cell", output);
  78. if (!fp)
  79. G_fatal_error(_("Unable to create cell/%s file"), output);
  80. fclose(fp);
  81. if (info->data_type == CELL_TYPE)
  82. return;
  83. fp = G_fopen_new("fcell", output);
  84. if (!fp)
  85. G_fatal_error(_("Unable to create fcell/%s file"), output);
  86. fclose(fp);
  87. }
  88. void make_link(const char *input, const char *output, int band,
  89. const struct band_info *info, int flip)
  90. {
  91. struct Key_Value *key_val = G_create_key_value();
  92. char null_str[256], type_str[8], band_str[8];
  93. FILE *fp;
  94. sprintf(band_str, "%d", band);
  95. if (info->has_null) {
  96. if (info->data_type == CELL_TYPE)
  97. sprintf(null_str, "%d", (int) info->null_val);
  98. else
  99. sprintf(null_str, "%.22g", info->null_val);
  100. }
  101. else
  102. strcpy(null_str, "none");
  103. sprintf(type_str, "%d", info->gdal_type);
  104. G_set_key_value("file", input, key_val);
  105. G_set_key_value("band", band_str, key_val);
  106. G_set_key_value("null", null_str, key_val);
  107. G_set_key_value("type", type_str, key_val);
  108. if (flip & FLIP_H)
  109. G_set_key_value("hflip", "yes", key_val);
  110. if (flip & FLIP_V)
  111. G_set_key_value("vflip", "yes", key_val);
  112. fp = G_fopen_new_misc("cell_misc", "gdal", output);
  113. if (!fp)
  114. G_fatal_error(_("Unable to create cell_misc/%s/gdal file"), output);
  115. if (G_fwrite_key_value(fp, key_val) < 0)
  116. G_fatal_error(_("Error writing cell_misc/%s/gdal file"), output);
  117. fclose(fp);
  118. }
  119. void write_fp_format(const char *output, const struct band_info *info)
  120. {
  121. struct Key_Value *key_val;
  122. const char *type;
  123. FILE *fp;
  124. if (info->data_type == CELL_TYPE)
  125. return;
  126. key_val = G_create_key_value();
  127. type = (info->data_type == FCELL_TYPE)
  128. ? "float"
  129. : "double";
  130. G_set_key_value("type", type, key_val);
  131. G_set_key_value("byte_order", "xdr", key_val);
  132. fp = G_fopen_new_misc("cell_misc", "f_format", output);
  133. if (!fp)
  134. G_fatal_error(_("Unable to create cell_misc/%s/f_format file"), output);
  135. if (G_fwrite_key_value(fp, key_val) < 0)
  136. G_fatal_error(_("Error writing cell_misc/%s/f_format file"), output);
  137. fclose(fp);
  138. G_free_key_value(key_val);
  139. }
  140. void write_fp_quant(const char *output)
  141. {
  142. struct Quant quant;
  143. Rast_quant_init(&quant);
  144. Rast_quant_round(&quant);
  145. Rast_write_quant(output, G_mapset(), &quant);
  146. }
  147. void create_map(const char *input, int band, const char *output,
  148. struct Cell_head *cellhd, struct band_info *info,
  149. const char *title, int flip)
  150. {
  151. struct History history;
  152. struct Categories cats;
  153. Rast_put_cellhd(output, cellhd);
  154. make_cell(output, info);
  155. make_link(input, output, band, info, flip);
  156. if (info->data_type == CELL_TYPE) {
  157. struct Range range;
  158. range.min = (CELL)info->range[0];
  159. range.max = (CELL)info->range[1];
  160. range.first_time = 0;
  161. Rast_write_range(output, &range);
  162. }
  163. else {
  164. struct FPRange fprange;
  165. fprange.min = info->range[0];
  166. fprange.max = info->range[1];
  167. fprange.first_time = 0;
  168. Rast_write_fp_range(output, &fprange);
  169. write_fp_format(output, info);
  170. write_fp_quant(output);
  171. }
  172. G_verbose_message(_("Creating support files for %s"), output);
  173. Rast_short_history(output, "raster", &history);
  174. Rast_command_history(&history);
  175. Rast_write_history(output, &history);
  176. Rast_write_colors(output, G_mapset(), &info->colors);
  177. Rast_init_cats(NULL, &cats);
  178. Rast_write_cats((char *)output, &cats);
  179. if (title)
  180. Rast_put_cell_title(output, title);
  181. G_done_msg(_("Link to raster map <%s> created."), output);
  182. }