main.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /****************************************************************************
  2. *
  3. * MODULE: r.external
  4. *
  5. * AUTHOR(S): Glynn Clements, based on r.in.gdal
  6. * List GDAL layers by Martin Landa <landa.martin gmail.com> 8/2011
  7. *
  8. * PURPOSE: Link raster map into GRASS utilizing the GDAL library.
  9. *
  10. * COPYRIGHT: (C) 2008, 2010-2011 by Glynn Clements and 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 <unistd.h>
  19. #include <math.h>
  20. #include <string.h>
  21. #include <grass/gis.h>
  22. #include <grass/raster.h>
  23. #include <grass/imagery.h>
  24. #include <grass/glocale.h>
  25. #include <gdal.h>
  26. #include <ogr_srs_api.h>
  27. #include "proto.h"
  28. int main(int argc, char *argv[])
  29. {
  30. const char *input, *source, *output;
  31. char *title;
  32. struct Cell_head cellhd;
  33. GDALDatasetH hDS;
  34. GDALRasterBandH hBand;
  35. struct GModule *module;
  36. struct {
  37. struct Option *input, *source, *output, *band, *title;
  38. } parm;
  39. struct {
  40. struct Flag *o, *f, *e, *h, *v;
  41. } flag;
  42. int min_band, max_band, band;
  43. struct band_info info;
  44. int flip;
  45. struct Ref reference;
  46. G_gisinit(argv[0]);
  47. module = G_define_module();
  48. G_add_keyword(_("raster"));
  49. G_add_keyword(_("import"));
  50. G_add_keyword(_("external"));
  51. module->description =
  52. _("Links GDAL supported raster data as a pseudo GRASS raster map.");
  53. parm.input = G_define_standard_option(G_OPT_F_INPUT);
  54. parm.input->description = _("Name of raster file to be linked");
  55. parm.input->required = NO;
  56. parm.input->guisection = _("Input");
  57. parm.source = G_define_option();
  58. parm.source->key = "source";
  59. parm.source->description = _("Name of non-file GDAL data source");
  60. parm.source->required = NO;
  61. parm.source->type = TYPE_STRING;
  62. parm.source->key_desc = "name";
  63. parm.source->guisection = _("Input");
  64. parm.output = G_define_standard_option(G_OPT_R_OUTPUT);
  65. parm.band = G_define_option();
  66. parm.band->key = "band";
  67. parm.band->type = TYPE_INTEGER;
  68. parm.band->required = NO;
  69. parm.band->description = _("Band to select (default is all bands)");
  70. parm.band->guisection = _("Input");
  71. parm.title = G_define_option();
  72. parm.title->key = "title";
  73. parm.title->key_desc = "phrase";
  74. parm.title->type = TYPE_STRING;
  75. parm.title->required = NO;
  76. parm.title->description = _("Title for resultant raster map");
  77. parm.title->guisection = _("Metadata");
  78. flag.f = G_define_flag();
  79. flag.f->key = 'f';
  80. flag.f->description = _("List supported formats and exit");
  81. flag.f->guisection = _("Print");
  82. flag.f->suppress_required = YES;
  83. flag.o = G_define_flag();
  84. flag.o->key = 'o';
  85. flag.o->description =
  86. _("Override projection (use location's projection)");
  87. flag.e = G_define_flag();
  88. flag.e->key = 'e';
  89. flag.e->label = _("Extend region extents based on new dataset");
  90. flag.e->description = _("Also updates the default region if in the PERMANENT mapset");
  91. flag.h = G_define_flag();
  92. flag.h->key = 'h';
  93. flag.h->description = _("Flip horizontally");
  94. flag.v = G_define_flag();
  95. flag.v->key = 'v';
  96. flag.v->description = _("Flip vertically");
  97. if (G_parser(argc, argv))
  98. exit(EXIT_FAILURE);
  99. GDALAllRegister();
  100. if (flag.f->answer) {
  101. list_formats();
  102. exit(EXIT_SUCCESS);
  103. }
  104. input = parm.input->answer;
  105. source = parm.source->answer;
  106. output = parm.output->answer;
  107. flip = 0;
  108. if (flag.h->answer)
  109. flip |= FLIP_H;
  110. if (flag.v->answer)
  111. flip |= FLIP_V;
  112. if (parm.title->answer) {
  113. title = G_store(parm.title->answer);
  114. G_strip(title);
  115. }
  116. else
  117. title = NULL;
  118. if (!input && !source)
  119. G_fatal_error(_("One of options <%s> or <%s> must be given"),
  120. parm.input->key, parm.source->key);
  121. if (input && source)
  122. G_fatal_error(_("Option <%s> and <%s> are mutually exclusive"),
  123. parm.input->key, parm.source->key);
  124. if (input && !G_is_absolute_path(input)) {
  125. char path[GPATH_MAX];
  126. getcwd(path, sizeof(path));
  127. strcat(path, "/");
  128. strcat(path, input);
  129. input = G_store(path);
  130. }
  131. if (!input)
  132. input = source;
  133. hDS = GDALOpen(input, GA_ReadOnly);
  134. if (hDS == NULL)
  135. return 1;
  136. setup_window(&cellhd, hDS, &flip);
  137. check_projection(&cellhd, hDS, flag.o->answer);
  138. Rast_set_window(&cellhd);
  139. if (parm.band->answer)
  140. min_band = max_band = atoi(parm.band->answer);
  141. else
  142. min_band = 1, max_band = GDALGetRasterCount(hDS);
  143. G_verbose_message(_("Proceeding with import..."));
  144. if (max_band > min_band) {
  145. if (I_find_group(output) == 1)
  146. G_warning(_("Imagery group <%s> already exists and will be overwritten."), output);
  147. I_init_group_ref(&reference);
  148. }
  149. for (band = min_band; band <= max_band; band++) {
  150. char *output2, *title2 = NULL;
  151. G_message(_("Reading band %d of %d..."),
  152. band, GDALGetRasterCount( hDS ));
  153. hBand = GDALGetRasterBand(hDS, band);
  154. if (!hBand)
  155. G_fatal_error(_("Selected band (%d) does not exist"), band);
  156. if (max_band > min_band) {
  157. G_asprintf(&output2, "%s.%d", output, band);
  158. if (title)
  159. G_asprintf(&title2, "%s (band %d)", title, band);
  160. G_debug(1, "Adding raster map <%s> to group <%s>", output2, output);
  161. I_add_file_to_group_ref(output2, G_mapset(), &reference);
  162. }
  163. else {
  164. output2 = G_store(output);
  165. if (title)
  166. title2 = G_store(title);
  167. }
  168. query_band(hBand, output2, &cellhd, &info);
  169. create_map(input, band, output2, &cellhd, &info, title, flip);
  170. G_free(output2);
  171. G_free(title2);
  172. }
  173. /* close the GDALDataset to avoid segfault in libgdal */
  174. GDALClose(hDS);
  175. if (flag.e->answer)
  176. update_default_window(&cellhd);
  177. /* Create the imagery group if multiple bands are imported */
  178. if (max_band > min_band) {
  179. I_put_group_ref(output, &reference);
  180. I_put_group(output);
  181. G_message(_("Imagery group <%s> created"), output);
  182. }
  183. exit(EXIT_SUCCESS);
  184. }