main.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /****************************************************************************
  2. *
  3. * MODULE: v.to.rast
  4. * AUTHOR(S): Original code: Michael Shapiro, U.S. Army Construction Engineering Research Laboratory
  5. * Stream directions: Jaro Hofierka and Helena Mitasova
  6. * Radim Blazek <radim.blazek gmail.com> (GRASS 6 update)
  7. * Brad Douglas <rez touchofmadness.com>, Glynn Clements <glynn gclements.plus.com>,
  8. * Hamish Bowman <hamish_b yahoo.com>, Markus Neteler <neteler itc.it>
  9. * OGR support by Martin Landa <landa.martin gmail.com>
  10. * Markus Metz (labelcol, cats, where options)
  11. * PURPOSE: Converts vector map to raster map
  12. * COPYRIGHT: (C) 2003-2012 by the GRASS Development Team
  13. *
  14. * This program is free software under the GNU General Public
  15. * License (>=v2). Read the file COPYING that comes with GRASS
  16. * for details.
  17. *
  18. *****************************************************************************/
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <grass/gis.h>
  23. #include <grass/dbmi.h>
  24. #include <grass/vector.h>
  25. #include <grass/glocale.h>
  26. #include "local.h"
  27. int main(int argc, char *argv[])
  28. {
  29. struct GModule *module;
  30. struct Option *input, *output, *memory, *col, *use_opt, *val_opt,
  31. *field_opt, *type_opt, *where_opt, *cats_opt,
  32. *rgbcol_opt, *label_opt;
  33. struct Flag *dense_flag;
  34. int cache_mb, use, value_type, type;
  35. double value;
  36. char *desc;
  37. G_gisinit(argv[0]);
  38. module = G_define_module();
  39. G_add_keyword(_("vector"));
  40. G_add_keyword(_("conversion"));
  41. G_add_keyword(_("raster"));
  42. G_add_keyword(_("rasterization"));
  43. module->description = _("Converts (rasterize) a vector map into a raster map.");
  44. input = G_define_standard_option(G_OPT_V_INPUT);
  45. field_opt = G_define_standard_option(G_OPT_V_FIELD);
  46. type_opt = G_define_standard_option(G_OPT_V_TYPE);
  47. type_opt->options = "point,line,area";
  48. type_opt->answer = "point,line,area";
  49. type_opt->guisection = _("Selection");
  50. cats_opt = G_define_standard_option(G_OPT_V_CATS);
  51. cats_opt->guisection = _("Selection");
  52. where_opt = G_define_standard_option(G_OPT_DB_WHERE);
  53. where_opt->guisection = _("Selection");
  54. output = G_define_standard_option(G_OPT_R_OUTPUT);
  55. use_opt = G_define_option();
  56. use_opt->key = "use";
  57. use_opt->type = TYPE_STRING;
  58. use_opt->required = YES;
  59. use_opt->multiple = NO;
  60. use_opt->options = "attr,cat,val,z,dir";
  61. use_opt->description = _("Source of raster values");
  62. desc = NULL;
  63. G_asprintf(&desc,
  64. "attr;%s;cat;%s;val;%s;z;%s;dir;%s",
  65. _("read values from attribute table"),
  66. _("use category values"),
  67. _("use value specified by value option"),
  68. _("use z coordinate (points or contours only)"),
  69. _("output as flow direction (lines only)"));
  70. use_opt->descriptions = desc;
  71. col = G_define_standard_option(G_OPT_DB_COLUMN);
  72. col->key = "attribute_column";
  73. col->description =
  74. _("Name of column for 'attr' parameter (data type must be numeric)");
  75. col->guisection = _("Attributes");
  76. rgbcol_opt = G_define_standard_option(G_OPT_DB_COLUMN);
  77. rgbcol_opt->key = "rgb_column";
  78. rgbcol_opt->description =
  79. _("Name of color definition column (with RRR:GGG:BBB entries)");
  80. rgbcol_opt->guisection = _("Attributes");
  81. label_opt = G_define_standard_option(G_OPT_DB_COLUMN);
  82. label_opt->key = "label_column";
  83. label_opt->description =
  84. _("Name of column used as raster category labels");
  85. label_opt->guisection = _("Attributes");
  86. val_opt = G_define_option();
  87. val_opt->key = "value";
  88. val_opt->type = TYPE_DOUBLE;
  89. val_opt->required = NO;
  90. val_opt->multiple = NO;
  91. val_opt->answer = "1";
  92. val_opt->description = _("Raster value (for use=val)");
  93. memory = G_define_option();
  94. memory->key = "memory";
  95. memory->type = TYPE_INTEGER;
  96. memory->required = NO;
  97. memory->multiple = NO;
  98. memory->answer = "300";
  99. memory->label = _("Maximum memory to be used (in MB)");
  100. memory->description = _("Cache size for raster rows");
  101. dense_flag = G_define_flag();
  102. dense_flag->key = 'd';
  103. dense_flag->label = _("Create densified lines (default: thin lines)");
  104. dense_flag->description = _("All cells touched by the line will be set, "
  105. "not only those on the render path");
  106. if (G_parser(argc, argv))
  107. exit(EXIT_FAILURE);
  108. type = Vect_option_to_types(type_opt);
  109. cache_mb = atoi(memory->answer);
  110. if (cache_mb < 1) {
  111. G_warning(_("Cache size must be at least 1 MiB, changing %d to 1"),
  112. cache_mb);
  113. cache_mb = 1;
  114. }
  115. switch (use_opt->answer[0]) {
  116. case 'a':
  117. use = USE_ATTR;
  118. if (!col->answer)
  119. G_fatal_error(_("Column parameter missing (or use value parameter)"));
  120. break;
  121. case 'c':
  122. use = USE_CAT;
  123. if (col->answer)
  124. G_fatal_error(_("Column parameter cannot be combined with use of category values option"));
  125. break;
  126. case 'v':
  127. use = USE_VAL;
  128. if (col->answer || label_opt->answer || rgbcol_opt->answer)
  129. G_fatal_error(_("Column parameter cannot be combined with use of value option"));
  130. break;
  131. case 'z':
  132. use = USE_Z;
  133. if (col->answer || label_opt->answer || rgbcol_opt->answer)
  134. G_fatal_error(_("Column parameter cannot be combined with use of z coordinate"));
  135. break;
  136. case 'd':
  137. use = USE_D;
  138. break;
  139. default:
  140. G_fatal_error(_("Unknown option '%s'"), use_opt->answer);
  141. break;
  142. }
  143. value = atof(val_opt->answer);
  144. value_type = (strchr(val_opt->answer, '.')) ? DCELL_TYPE : CELL_TYPE;
  145. if (vect_to_rast(input->answer, output->answer, field_opt->answer,
  146. col->answer, cache_mb, use, value, value_type,
  147. rgbcol_opt->answer, label_opt->answer, type,
  148. where_opt->answer, cats_opt->answer, dense_flag->answer)) {
  149. exit(EXIT_FAILURE);
  150. }
  151. G_done_msg(" ");
  152. exit(EXIT_SUCCESS);
  153. }