main.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /***************************************************************************
  2. *
  3. * MODULE: v.out.vtk
  4. * AUTHOR(S): Soeren Gebbert
  5. *
  6. * PURPOSE: v.out.vtk: writes ASCII VTK file
  7. * this module is based on v.out.ascii
  8. * COPYRIGHT: (C) 2000 by the GRASS Development Team
  9. *
  10. * This program is free software under the GNU General Public
  11. * License (>=v2). Read the file COPYING that comes with GRASS
  12. * for details.
  13. *
  14. ****************************************************************************/
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <unistd.h>
  18. #include <string.h>
  19. #include <grass/gis.h>
  20. #include <grass/Vect.h>
  21. #include <grass/glocale.h>
  22. #include <string.h>
  23. #define MAIN
  24. #include "local_proto.h"
  25. int main(int argc, char *argv[])
  26. {
  27. FILE *ascii;
  28. struct Option *input, *output, *type_opt, *dp_opt, *layer_opt, *scale;
  29. struct Flag *coorcorr;
  30. int *types = NULL, typenum = 0, dp, i;
  31. struct Map_info Map;
  32. struct GModule *module;
  33. int layer;
  34. struct Cell_head region;
  35. double zscale = 1.0, llscale = 1.0;
  36. G_gisinit(argv[0]);
  37. module = G_define_module();
  38. module->keywords = _("vector");
  39. module->description =
  40. _("Converts a GRASS binary vector map to VTK ASCII output.");
  41. input = G_define_standard_option(G_OPT_V_INPUT);
  42. output = G_define_option();
  43. output->key = "output";
  44. output->type = TYPE_STRING;
  45. output->required = NO;
  46. output->multiple = NO;
  47. output->gisprompt = "new_file,file,output";
  48. output->description = _("Path to resulting VTK file");
  49. type_opt = G_define_standard_option(G_OPT_V_TYPE);
  50. type_opt->answer = "point,kernel,centroid,line,boundary,area,face";
  51. type_opt->options = "point,kernel,centroid,line,boundary,area,face";
  52. dp_opt = G_define_option();
  53. dp_opt->key = "dp";
  54. dp_opt->type = TYPE_INTEGER;
  55. dp_opt->required = NO;
  56. dp_opt->description =
  57. _("Number of significant digits (floating point only)");
  58. scale = G_define_option();
  59. scale->key = "scale";
  60. scale->type = TYPE_DOUBLE;
  61. scale->required = NO;
  62. scale->description = _("Scale factor for elevation");
  63. scale->answer = "1.0";
  64. layer_opt = G_define_option();
  65. layer_opt->key = "layer";
  66. layer_opt->type = TYPE_INTEGER;
  67. layer_opt->required = NO;
  68. layer_opt->answer = "1";
  69. layer_opt->description = _("Layer number");
  70. coorcorr = G_define_flag();
  71. coorcorr->key = 'c';
  72. coorcorr->description =
  73. _("Correct the coordinates to fit the VTK-OpenGL precision");
  74. if (G_parser(argc, argv))
  75. exit(EXIT_FAILURE);
  76. for (i = 0; type_opt->answers && type_opt->answers[i]; i++)
  77. typenum++;
  78. if (typenum > 0) {
  79. types = (int *)calloc(typenum, sizeof(int));
  80. }
  81. else {
  82. G_fatal_error("Usage: Wrong vector type");
  83. }
  84. i = 0;
  85. while (type_opt->answers[i]) {
  86. types[i] = -1;
  87. switch (type_opt->answers[i][0]) {
  88. case 'p':
  89. types[i] = GV_POINT;
  90. break;
  91. case 'k':
  92. types[i] = GV_KERNEL;
  93. break;
  94. case 'c':
  95. types[i] = GV_CENTROID;
  96. break;
  97. case 'l':
  98. types[i] = GV_LINE;
  99. break;
  100. case 'b':
  101. types[i] = GV_BOUNDARY;
  102. break;
  103. case 'a':
  104. types[i] = GV_AREA;
  105. break;
  106. case 'f':
  107. types[i] = GV_FACE;
  108. break;
  109. }
  110. i++;
  111. }
  112. G_get_set_window(&region);
  113. /*Correct the coordinates, so the precision of VTK is not hurt :( */
  114. if (coorcorr->answer) {
  115. /*Get the default region for coordiante correction */
  116. G_get_default_window(&region);
  117. /*Use the center of the current region as extent */
  118. y_extent = (region.north + region.south) / 2;
  119. x_extent = (region.west + region.east) / 2;
  120. }
  121. else {
  122. x_extent = 0;
  123. y_extent = 0;
  124. }
  125. /* read and compute the scale factor */
  126. sscanf(scale->answer, "%lf", &zscale);
  127. /*if LL projection, convert the elevation values to degrees */
  128. if (region.proj == PROJECTION_LL) {
  129. llscale = M_PI / (180) * 6378137;
  130. zscale /= llscale;
  131. printf("Scale %g\n", zscale);
  132. }
  133. /*We need level 2 functions */
  134. Vect_set_open_level(2);
  135. Vect_open_old(&Map, input->answer, "");
  136. if (output->answer) {
  137. ascii = fopen(output->answer, "w");
  138. if (ascii == NULL) {
  139. G_fatal_error(_("Unable to open file <%s>"), output->answer);
  140. }
  141. }
  142. else {
  143. ascii = stdout;
  144. }
  145. /*The precision of the output */
  146. if (dp_opt->answer) {
  147. if (sscanf(dp_opt->answer, "%d", &dp) != 1)
  148. G_fatal_error(_("Failed to interprete 'dp' parameter as an integer"));
  149. if (dp > 8 || dp < 0)
  150. G_fatal_error(_("dp has to be from 0 to 8"));
  151. }
  152. else {
  153. dp = 8; /*This value is taken from the lib settings in G_feature_easting */
  154. }
  155. /*The Layer */
  156. if (layer_opt->answer) {
  157. if (sscanf(layer_opt->answer, "%d", &layer) != 1)
  158. G_fatal_error(_("Failed to interprete 'layer' parameter as an integer"));
  159. }
  160. else {
  161. layer = 1;
  162. }
  163. /*Write the header */
  164. write_vtk_head(ascii, &Map);
  165. /*Write the geometry and data */
  166. write_vtk(ascii, &Map, layer, types, typenum, dp, zscale);
  167. if (ascii != NULL)
  168. fclose(ascii);
  169. Vect_close(&Map);
  170. exit(EXIT_SUCCESS);
  171. }