link.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include <stdio.h>
  2. #include <grass/gis.h>
  3. #include <grass/glocale.h>
  4. #include "proto.h"
  5. void make_cell(const char *output, int maptype)
  6. {
  7. FILE *fp;
  8. fp = G_fopen_new("cell", output);
  9. if (!fp)
  10. G_fatal_error(_("Unable to create cell/%s file"), output);
  11. fclose(fp);
  12. if (maptype == CELL_TYPE)
  13. return;
  14. fp = G_fopen_new("fcell", output);
  15. if (!fp)
  16. G_fatal_error(_("Unable to create fcell/%s file"), output);
  17. fclose(fp);
  18. }
  19. void make_link(const struct input *inputs, int num_inputs,
  20. const char *output)
  21. {
  22. int i;
  23. FILE *fp;
  24. fp = G_fopen_new_misc("cell_misc", "vrt", output);
  25. if (!fp)
  26. G_fatal_error(_("Unable to create cell_misc/%s/vrt file"), output);
  27. for (i = 0; i < num_inputs; i++) {
  28. const struct input *p = &inputs[i];
  29. fprintf(fp, "%s@%s\n", p->name, p->mapset);
  30. }
  31. fclose(fp);
  32. }
  33. void write_fp_format(const char *output, int maptype)
  34. {
  35. struct Key_Value *key_val;
  36. const char *type;
  37. FILE *fp;
  38. if (maptype == CELL_TYPE)
  39. return;
  40. key_val = G_create_key_value();
  41. type = (maptype == FCELL_TYPE)
  42. ? "float"
  43. : "double";
  44. G_set_key_value("type", type, key_val);
  45. G_set_key_value("byte_order", "xdr", key_val);
  46. fp = G_fopen_new_misc("cell_misc", "f_format", output);
  47. if (!fp)
  48. G_fatal_error(_("Unable to create cell_misc/%s/f_format file"), output);
  49. if (G_fwrite_key_value(fp, key_val) < 0)
  50. G_fatal_error(_("Error writing cell_misc/%s/f_format file"), output);
  51. fclose(fp);
  52. G_free_key_value(key_val);
  53. }
  54. void write_fp_quant(const char *output)
  55. {
  56. struct Quant quant;
  57. Rast_quant_init(&quant);
  58. Rast_quant_round(&quant);
  59. Rast_write_quant(output, G_mapset(), &quant);
  60. }
  61. void create_map(const struct input *inputs, int num_inputs, const char *output,
  62. struct Cell_head *cellhd, int maptype, DCELL dmin, DCELL dmax,
  63. int have_stats, struct R_stats *ostats, const char *title)
  64. {
  65. struct History history;
  66. struct Categories cats;
  67. struct Colors colors;
  68. char buf[1024];
  69. Rast_put_cellhd(output, cellhd);
  70. make_cell(output, maptype);
  71. make_link(inputs, num_inputs, output);
  72. if (maptype == CELL_TYPE) {
  73. struct Range range;
  74. range.min = (CELL)dmin;
  75. range.max = (CELL)dmax;
  76. range.first_time = 0;
  77. Rast_write_range(output, &range);
  78. }
  79. else {
  80. struct FPRange fprange;
  81. fprange.min = dmin;
  82. fprange.max = dmax;
  83. fprange.first_time = 0;
  84. Rast_write_fp_range(output, &fprange);
  85. write_fp_format(output, maptype);
  86. write_fp_quant(output);
  87. }
  88. G_remove_misc("cell_misc", "stats", output);
  89. if (have_stats)
  90. Rast_write_rstats(output, ostats);
  91. G_verbose_message(_("Creating support files for %s"), output);
  92. Rast_short_history(output, "virtual", &history);
  93. Rast_command_history(&history);
  94. Rast_format_history(&history, HIST_KEYWRD, _("virtual raster generated by %s"), G_program_name());
  95. sprintf(buf, "%d raster maps", num_inputs);
  96. Rast_set_history(&history, HIST_DATSRC_1, buf);
  97. Rast_write_history(output, &history);
  98. if (Rast_read_colors(inputs[0].name, inputs[0].mapset, &colors) == 1)
  99. Rast_write_colors(output, G_mapset(), &colors);
  100. Rast_init_cats(NULL, &cats);
  101. Rast_write_cats((char *)output, &cats);
  102. if (title)
  103. Rast_put_cell_title(output, title);
  104. G_done_msg(_("Link to raster map <%s> created."), output);
  105. }