main.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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-2015 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 <cpl_conv.h>
  28. #include "proto.h"
  29. int main(int argc, char *argv[])
  30. {
  31. const char *input, *source, *output;
  32. char *title;
  33. struct Cell_head cellhd;
  34. GDALDatasetH hDS;
  35. GDALRasterBandH hBand;
  36. struct GModule *module;
  37. struct {
  38. struct Option *input, *source, *output, *band, *title;
  39. } parm;
  40. struct {
  41. struct Flag *o, *f, *e, *h, *v, *t;
  42. } flag;
  43. int min_band, max_band, band;
  44. struct band_info info;
  45. int flip;
  46. struct Ref reference;
  47. G_gisinit(argv[0]);
  48. module = G_define_module();
  49. G_add_keyword(_("raster"));
  50. G_add_keyword(_("import"));
  51. G_add_keyword(_("external"));
  52. module->description =
  53. _("Links GDAL supported raster data as a pseudo GRASS raster map.");
  54. parm.input = G_define_standard_option(G_OPT_F_INPUT);
  55. parm.input->description = _("Name of raster file to be linked");
  56. parm.input->required = NO;
  57. parm.input->guisection = _("Input");
  58. parm.source = G_define_option();
  59. parm.source->key = "source";
  60. parm.source->description = _("Name of non-file GDAL data source");
  61. parm.source->required = NO;
  62. parm.source->type = TYPE_STRING;
  63. parm.source->key_desc = "name";
  64. parm.source->guisection = _("Input");
  65. parm.output = G_define_standard_option(G_OPT_R_OUTPUT);
  66. parm.band = G_define_option();
  67. parm.band->key = "band";
  68. parm.band->type = TYPE_INTEGER;
  69. parm.band->required = NO;
  70. parm.band->description = _("Band to select (default is all bands)");
  71. parm.band->guisection = _("Input");
  72. parm.title = G_define_option();
  73. parm.title->key = "title";
  74. parm.title->key_desc = "phrase";
  75. parm.title->type = TYPE_STRING;
  76. parm.title->required = NO;
  77. parm.title->description = _("Title for resultant raster map");
  78. parm.title->guisection = _("Metadata");
  79. flag.f = G_define_flag();
  80. flag.f->key = 'f';
  81. flag.f->description = _("List supported formats and exit");
  82. flag.f->guisection = _("Print");
  83. flag.f->suppress_required = YES;
  84. flag.o = G_define_flag();
  85. flag.o->key = 'o';
  86. flag.o->label =
  87. _("Override projection check (use current location's projection)");
  88. flag.o->description =
  89. _("Assume that the dataset has same projection as the current location");
  90. flag.e = G_define_flag();
  91. flag.e->key = 'e';
  92. flag.e->label = _("Extend region extents based on new dataset");
  93. flag.e->description = _("Also updates the default region if in the PERMANENT mapset");
  94. flag.h = G_define_flag();
  95. flag.h->key = 'h';
  96. flag.h->description = _("Flip horizontally");
  97. flag.v = G_define_flag();
  98. flag.v->key = 'v';
  99. flag.v->description = _("Flip vertically");
  100. flag.t = G_define_flag();
  101. flag.t->key = 't';
  102. flag.t->label =
  103. _("List available bands including band type in dataset and exit");
  104. flag.t->description = _("Format: band number,type,projection check");
  105. flag.t->guisection = _("Print");
  106. flag.t->suppress_required = YES;
  107. if (G_parser(argc, argv))
  108. exit(EXIT_FAILURE);
  109. GDALAllRegister();
  110. if (flag.f->answer) {
  111. list_formats();
  112. exit(EXIT_SUCCESS);
  113. }
  114. input = parm.input->answer;
  115. source = parm.source->answer;
  116. output = parm.output->answer;
  117. flip = 0;
  118. if (flag.h->answer)
  119. flip |= FLIP_H;
  120. if (flag.v->answer)
  121. flip |= FLIP_V;
  122. if (parm.title->answer) {
  123. title = G_store(parm.title->answer);
  124. G_strip(title);
  125. }
  126. else
  127. title = NULL;
  128. if (!input && !source)
  129. G_fatal_error(_("%s= or %s= must be given"),
  130. parm.input->key, parm.source->key);
  131. if (input && source)
  132. G_fatal_error(_("%s= and %s= are mutually exclusive"),
  133. parm.input->key, parm.source->key);
  134. if (input && !G_is_absolute_path(input)) {
  135. char path[GPATH_MAX], *cwd;
  136. cwd = CPLGetCurrentDir();
  137. if (!cwd)
  138. G_fatal_error(_("Unable to get current working directory"));
  139. G_snprintf(path, GPATH_MAX, "%s%c%s", cwd, HOST_DIRSEP, input);
  140. input = G_store(path);
  141. CPLFree(cwd);
  142. }
  143. if (!input)
  144. input = source;
  145. hDS = GDALOpen(input, GA_ReadOnly);
  146. if (hDS == NULL)
  147. return 1;
  148. setup_window(&cellhd, hDS, &flip);
  149. if (flag.t->answer) {
  150. list_bands(&cellhd, hDS);
  151. /* close the GDALDataset to avoid segfault in libgdal */
  152. GDALClose(hDS);
  153. exit(EXIT_SUCCESS);
  154. }
  155. check_projection(&cellhd, hDS, flag.o->answer);
  156. Rast_set_window(&cellhd);
  157. if (parm.band->answer)
  158. min_band = max_band = atoi(parm.band->answer);
  159. else
  160. min_band = 1, max_band = GDALGetRasterCount(hDS);
  161. G_verbose_message(_("Proceeding with import..."));
  162. if (max_band > min_band) {
  163. if (I_find_group(output) == 1)
  164. G_warning(_("Imagery group <%s> already exists and will be overwritten."), output);
  165. I_init_group_ref(&reference);
  166. }
  167. for (band = min_band; band <= max_band; band++) {
  168. char *output2, *title2 = NULL;
  169. G_message(_("Reading band %d of %d..."),
  170. band, GDALGetRasterCount( hDS ));
  171. hBand = GDALGetRasterBand(hDS, band);
  172. if (!hBand)
  173. G_fatal_error(_("Selected band (%d) does not exist"), band);
  174. if (max_band > min_band) {
  175. G_asprintf(&output2, "%s.%d", output, band);
  176. if (title)
  177. G_asprintf(&title2, "%s (band %d)", title, band);
  178. G_debug(1, "Adding raster map <%s> to group <%s>", output2, output);
  179. I_add_file_to_group_ref(output2, G_mapset(), &reference);
  180. }
  181. else {
  182. output2 = G_store(output);
  183. if (title)
  184. title2 = G_store(title);
  185. }
  186. query_band(hBand, output2, &cellhd, &info);
  187. create_map(input, band, output2, &cellhd, &info, title, flip);
  188. G_free(output2);
  189. G_free(title2);
  190. }
  191. /* close the GDALDataset to avoid segfault in libgdal */
  192. GDALClose(hDS);
  193. if (flag.e->answer)
  194. update_default_window(&cellhd);
  195. /* Create the imagery group if multiple bands are imported */
  196. if (max_band > min_band) {
  197. I_put_group_ref(output, &reference);
  198. I_put_group(output);
  199. G_message(_("Imagery group <%s> created"), output);
  200. }
  201. exit(EXIT_SUCCESS);
  202. }