main.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /****************************************************************************
  2. *
  3. * MODULE: r.buildvrt
  4. *
  5. * AUTHOR(S): Markus Metz, based on r.external
  6. *
  7. * PURPOSE: Build a VRT (Virtual Raster) that is a mosaic of the
  8. * list of input raster maps.
  9. *
  10. * COPYRIGHT: (C) 2018 by the GRASS Development Team
  11. *
  12. * This program is free software under the GNU General Public
  13. * License (>=v2). Read the file COPYING that comes with GRASS
  14. * for details.
  15. *
  16. *****************************************************************************/
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <math.h>
  20. #include <string.h>
  21. #include <grass/gis.h>
  22. #include <grass/raster.h>
  23. #include <grass/glocale.h>
  24. #include "proto.h"
  25. int cmp_wnd(const void *a, const void *b)
  26. {
  27. struct Cell_head *cellhda = &((struct input *) a)->cellhd;
  28. struct Cell_head *cellhdb = &((struct input *) b)->cellhd;
  29. /* sort from descending N to S, then ascending from W to E */
  30. if (cellhda->south > cellhdb->south)
  31. return -1;
  32. if (cellhda->south < cellhdb->south)
  33. return 1;
  34. if (cellhda->north > cellhdb->north)
  35. return -1;
  36. if (cellhda->north < cellhdb->north)
  37. return 1;
  38. if (cellhda->west < cellhdb->west)
  39. return -1;
  40. if (cellhda->west > cellhdb->west)
  41. return 1;
  42. if (cellhda->east < cellhdb->east)
  43. return -1;
  44. if (cellhda->east > cellhdb->east)
  45. return 1;
  46. return 0;
  47. }
  48. int main(int argc, char *argv[])
  49. {
  50. const char *output;
  51. char *title;
  52. struct Cell_head cellhd;
  53. struct GModule *module;
  54. struct {
  55. struct Option *input, *file, *output, *title;
  56. } parm;
  57. int i, j, num_inputs;
  58. struct input *inputs = NULL;
  59. char nsresstr[1024], ewresstr[1024];
  60. int maptype;
  61. struct FPRange fprange;
  62. DCELL dmin, dmax;
  63. int have_stats;
  64. struct R_stats rstats, ostats;
  65. G_gisinit(argv[0]);
  66. module = G_define_module();
  67. G_add_keyword(_("raster"));
  68. G_add_keyword(_("mosaic"));
  69. G_add_keyword(_("virtual raster"));
  70. module->description =
  71. _("Build a VRT (Virtual Raster) from the list of input raster maps.");
  72. parm.input = G_define_standard_option(G_OPT_R_INPUTS);
  73. parm.input->description = _("Name of input raster files");
  74. parm.input->required = NO;
  75. parm.input->guisection = _("Input");
  76. parm.file = G_define_standard_option(G_OPT_F_INPUT);
  77. parm.file->key = "file";
  78. parm.file->description = _("Input file with one raster map name per line");
  79. parm.file->required = NO;
  80. parm.file->guisection = _("Input");
  81. parm.output = G_define_standard_option(G_OPT_R_OUTPUT);
  82. parm.output->guisection = _("Output");
  83. parm.title = G_define_option();
  84. parm.title->key = "title";
  85. parm.title->key_desc = "phrase";
  86. parm.title->type = TYPE_STRING;
  87. parm.title->required = NO;
  88. parm.title->description = _("Title for resultant raster map");
  89. parm.title->guisection = _("Output");
  90. if (G_parser(argc, argv))
  91. exit(EXIT_FAILURE);
  92. if (parm.input->answer && parm.file->answer)
  93. G_fatal_error(_("%s= and %s= are mutually exclusive"),
  94. parm.input->key, parm.file->key);
  95. if (!parm.input->answer && !parm.file->answer)
  96. G_fatal_error(_("Please specify %s= or %s="),
  97. parm.input->key, parm.file->key);
  98. /* read input maps from file */
  99. if (parm.file->answer) {
  100. FILE *in;
  101. int max_inputs;
  102. if (strcmp(parm.file->answer, "-") == 0)
  103. in = stdin;
  104. else {
  105. in = fopen(parm.file->answer, "r");
  106. if (!in)
  107. G_fatal_error(_("Unable to open input file <%s>"), parm.file->answer);
  108. }
  109. num_inputs = 0;
  110. max_inputs = 0;
  111. for (;;) {
  112. char buf[GNAME_MAX];
  113. char *name;
  114. const char *mapset;
  115. struct input *p;
  116. if (!G_getl2(buf, sizeof(buf), in))
  117. break;
  118. /* Ignore empty lines */
  119. if (!*buf)
  120. continue;
  121. name = buf;
  122. if ((mapset = G_find_raster(name, "")) == NULL)
  123. G_fatal_error(_("Input raster map <%s> not found"), name);
  124. if (strcmp(name, parm.output->answer) == 0)
  125. G_fatal_error(_("Input and output raster map can not be identical"));
  126. Rast_read_fp_range(name, mapset, &fprange);
  127. dmin = fprange.min;
  128. if (Rast_is_d_null_value(&dmin)) {
  129. G_verbose_message(_("Input map <%s@%s> is all NULL, skipping"),
  130. name, mapset);
  131. continue;
  132. }
  133. if (num_inputs >= max_inputs) {
  134. max_inputs += 100;
  135. inputs = G_realloc(inputs, max_inputs * sizeof(struct input));
  136. }
  137. p = &inputs[num_inputs++];
  138. p->name = G_store(name);
  139. p->mapset = G_store(mapset);
  140. p->maptype = Rast_map_type(p->name, p->mapset);
  141. Rast_get_cellhd(p->name, p->mapset, &(p->cellhd));
  142. }
  143. fclose(in);
  144. if (num_inputs < 1)
  145. G_fatal_error(_("No raster map name found in input file"));
  146. if (num_inputs == 1)
  147. G_fatal_error(_("Only one raster map name found in input file"));
  148. }
  149. else {
  150. for (i = 0; parm.input->answers[i]; i++)
  151. ;
  152. num_inputs = i;
  153. if (num_inputs < 1)
  154. G_fatal_error(_("Raster map not found"));
  155. if (num_inputs == 1)
  156. G_fatal_error(_("Only one raster map name found"));
  157. inputs = G_malloc(num_inputs * sizeof(struct input));
  158. j = 0;
  159. for (i = 0; i < num_inputs; i++) {
  160. char *name;
  161. const char *mapset;
  162. struct input *p = &inputs[i];
  163. name = parm.input->answers[i];
  164. if ((mapset = G_find_raster(name, "")) == NULL)
  165. G_fatal_error(_("Input raster map <%s> not found"), name);
  166. if (strcmp(name, parm.output->answer) == 0)
  167. G_fatal_error(_("Input and output raster map can not be identical"));
  168. Rast_read_fp_range(name, mapset, &fprange);
  169. dmin = fprange.min;
  170. if (Rast_is_d_null_value(&dmin)) {
  171. G_verbose_message(_("Input map <%s@%s> is all NULL, skipping"),
  172. name, mapset);
  173. continue;
  174. }
  175. p = &inputs[j++];
  176. p->name = G_store(name);
  177. p->mapset = G_store(mapset);
  178. p->maptype = Rast_map_type(p->name, p->mapset);
  179. Rast_get_cellhd(p->name, p->mapset, &(p->cellhd));
  180. }
  181. num_inputs = j;
  182. }
  183. qsort(inputs, num_inputs, sizeof(struct input), cmp_wnd);
  184. /* check resolution and maptype of input maps */
  185. cellhd = inputs[0].cellhd;
  186. cellhd.compressed = 0;
  187. G_format_resolution(cellhd.ns_res, nsresstr, G_projection());
  188. G_format_resolution(cellhd.ew_res, ewresstr, G_projection());
  189. maptype = inputs[0].maptype;
  190. Rast_set_d_null_value(&dmin, 1);
  191. Rast_set_d_null_value(&dmax, 1);
  192. if (Rast_read_fp_range(inputs[0].name, inputs[0].mapset, &fprange) == 1) {
  193. dmin = fprange.min;
  194. dmax = fprange.max;
  195. }
  196. Rast_set_d_null_value(&(ostats.sum), 1);
  197. Rast_set_d_null_value(&(ostats.sumsq), 1);
  198. ostats.count = 0;
  199. have_stats = 1;
  200. if (Rast_read_rstats(inputs[0].name, inputs[0].mapset, &rstats) == 1) {
  201. ostats.sum = rstats.sum;
  202. ostats.sumsq = rstats.sumsq;
  203. ostats.count = rstats.count;
  204. }
  205. else
  206. have_stats = 0;
  207. for (i = 1; i < num_inputs; i++) {
  208. char tnsresstr[1024], tewresstr[1024];
  209. int tmaptype;
  210. struct input *p = &inputs[i];
  211. G_format_resolution(p->cellhd.ns_res, tnsresstr, G_projection());
  212. G_format_resolution(p->cellhd.ew_res, tewresstr, G_projection());
  213. tmaptype = p->maptype;
  214. if (tmaptype != maptype)
  215. G_warning(_("Input maptypes are different"));
  216. if (strcmp(nsresstr, tnsresstr) != 0)
  217. G_warning(_("Input ns resolutions are different"));
  218. if (strcmp(ewresstr, tewresstr) != 0)
  219. G_warning(_("Input ns resolutions are different"));
  220. if (cellhd.north < p->cellhd.north)
  221. cellhd.north = p->cellhd.north;
  222. if (cellhd.south > p->cellhd.south)
  223. cellhd.south = p->cellhd.south;
  224. if (cellhd.east < p->cellhd.east)
  225. cellhd.east = p->cellhd.east;
  226. if (cellhd.west > p->cellhd.west)
  227. cellhd.west = p->cellhd.west;
  228. if (Rast_read_fp_range(p->name, p->mapset, &fprange) == 1) {
  229. if (Rast_is_d_null_value(&dmin)) {
  230. dmin = fprange.min;
  231. dmax = fprange.max;
  232. }
  233. else {
  234. if (dmin > fprange.min)
  235. dmin = fprange.min;
  236. if (dmax < fprange.max)
  237. dmax = fprange.max;
  238. }
  239. }
  240. if (have_stats &&
  241. Rast_read_rstats(inputs[0].name, inputs[0].mapset, &rstats) == 1) {
  242. ostats.sum += rstats.sum;
  243. ostats.sumsq += rstats.sumsq;
  244. ostats.count += rstats.count;
  245. }
  246. else
  247. have_stats = 0;
  248. }
  249. G_adjust_Cell_head(&cellhd, 0, 0);
  250. if (maptype == CELL_TYPE)
  251. cellhd.format = 3;
  252. else
  253. cellhd.format = -1;
  254. output = parm.output->answer;
  255. title = NULL;
  256. if (parm.title->answer) {
  257. title = G_store(parm.title->answer);
  258. G_strip(title);
  259. }
  260. create_map(inputs, num_inputs, output, &cellhd, maptype, dmin, dmax,
  261. have_stats, &ostats, title);
  262. exit(EXIT_SUCCESS);
  263. }