main.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /****************************************************************************
  2. *
  3. * MODULE: r.out.vtk
  4. *
  5. * AUTHOR(S): Original author
  6. * Soeren Gebbert soerengebbert@gmx.de
  7. * 08 23 2005 Berlin
  8. * PURPOSE: Converts raster maps into the VTK-Ascii format
  9. *
  10. * COPYRIGHT: (C) 2005 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 <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <grass/gis.h>
  21. #include <grass/raster.h>
  22. #include <grass/glocale.h>
  23. #include <grass/config.h>
  24. #include "writeascii.h"
  25. #include "parameters.h"
  26. #include "globaldefs.h"
  27. paramType param; /*Parameters */
  28. double x_extent;
  29. double y_extent;
  30. /* ************************************************************************* */
  31. /* MAIN ******************************************************************** */
  32. /* ************************************************************************* */
  33. int main(int argc, char *argv[])
  34. {
  35. struct Cell_head region;
  36. struct Cell_head default_region;
  37. FILE *fp = NULL;
  38. struct GModule *module;
  39. int i = 0, polytype = 0;
  40. char *null_value;
  41. int out_type;
  42. int fd; /*Normale maps ;) */
  43. int rgbfd[3];
  44. int vectfd[3];
  45. int celltype[3] = { 0, 0, 0 };
  46. int headertype;
  47. double scale = 1.0, llscale = 1.0, eleval = 0.0;
  48. int digits = 12;
  49. /* Initialize GRASS */
  50. G_gisinit(argv[0]);
  51. module = G_define_module();
  52. G_add_keyword(_("raster"));
  53. G_add_keyword(_("export"));
  54. G_add_keyword("VTK");
  55. module->description = _("Converts raster maps into the VTK-ASCII format.");
  56. /* Get parameters from user */
  57. set_params();
  58. /* Have GRASS get inputs */
  59. if (G_parser(argc, argv))
  60. exit(EXIT_FAILURE);
  61. if (param.input->answers == NULL && param.rgbmaps->answers == NULL &&
  62. param.vectmaps->answers == NULL) {
  63. G_fatal_error(_("No input maps specified. You need to specify at least one input map or three vector maps or three rgb maps."));
  64. }
  65. /*open the output */
  66. if (param.output->answer) {
  67. fp = fopen(param.output->answer, "w");
  68. if (fp == NULL) {
  69. perror(param.output->answer);
  70. G_usage();
  71. exit(EXIT_FAILURE);
  72. }
  73. }
  74. else
  75. fp = stdout;
  76. /*Correct the coordinates, so the precision of VTK is not hurt :( */
  77. if (param.coorcorr->answer) {
  78. /*Get the default region for coordiante correction */
  79. G_get_default_window(&default_region);
  80. /*Use the center of the current region as extent */
  81. y_extent = (default_region.north + default_region.south) / 2;
  82. x_extent = (default_region.west + default_region.east) / 2;
  83. }
  84. else {
  85. x_extent = 0;
  86. y_extent = 0;
  87. }
  88. /* Figure out the region from the map */
  89. G_get_window(&region);
  90. /*Set the null Value, maybe i have to check this? */
  91. null_value = param.null_val->answer;
  92. /*number of significant digits */
  93. sscanf(param.decimals->answer, "%i", &digits);
  94. /* read and compute the scale factor */
  95. sscanf(param.elevscale->answer, "%lf", &scale);
  96. sscanf(param.elev->answer, "%lf", &eleval);
  97. /*if LL projection, convert the elevation values to degrees */
  98. if (region.proj == PROJECTION_LL) {
  99. llscale = M_PI / (180) * 6378137;
  100. scale /= llscale;
  101. }
  102. /********************* WRITE ELEVATION *************************************/
  103. if (param.elevationmap->answer) {
  104. /*If the elevation is set, write the correct Header */
  105. if (param.usestruct->answer) {
  106. write_vtk_structured_elevation_header(fp, region);
  107. }
  108. else {
  109. write_vtk_polygonal_elevation_header(fp, region);
  110. }
  111. G_debug(3, _("Open Raster file %s"), param.elevationmap->answer);
  112. /* open raster map */
  113. fd = Rast_open_old(param.elevationmap->answer, "");
  114. out_type = Rast_get_map_type(fd);
  115. /*The write the Coordinates */
  116. if (param.usestruct->answer) {
  117. write_vtk_structured_coordinates(fd, fp,
  118. param.elevationmap->answer,
  119. region, out_type, null_value,
  120. scale, digits);
  121. }
  122. else {
  123. polytype = QUADS; /*The default */
  124. if (param.usetriangle->answer)
  125. polytype = TRIANGLE_STRIPS;
  126. if (param.usevertices->answer)
  127. polytype = VERTICES;
  128. write_vtk_polygonal_coordinates(fd, fp,
  129. param.elevationmap->answer,
  130. region, out_type, null_value,
  131. scale, polytype, digits);
  132. }
  133. Rast_close(fd);
  134. }
  135. else {
  136. /*Should pointdata or celldata be written */
  137. if (param.point->answer)
  138. headertype = 1;
  139. else
  140. headertype = 0;
  141. /*If no elevation is given, write the normal Header */
  142. if (param.origin->answer)
  143. write_vtk_normal_header(fp, region, scale * eleval, headertype);
  144. else
  145. write_vtk_normal_header(fp, region, eleval / llscale, headertype);
  146. }
  147. /******************** WRITE THE POINT OR CELL DATA HEADER ******************/
  148. if (param.input->answers != NULL || param.rgbmaps->answers != NULL) {
  149. if (param.point->answer || param.elevationmap->answer)
  150. write_vtk_pointdata_header(fp, region);
  151. else
  152. write_vtk_celldata_header(fp, region);
  153. }
  154. /********************** WRITE NORMAL DATA; CELL OR POINT *******************/
  155. /*Loop over all input maps! */
  156. if (param.input->answers != NULL) {
  157. for (i = 0; param.input->answers[i] != NULL; i++) {
  158. G_debug(3, _("Open Raster file %s"), param.input->answers[i]);
  159. /* open raster map */
  160. fd = Rast_open_old(param.input->answers[i], "");
  161. out_type = Rast_get_map_type(fd);
  162. /*Now write the data */
  163. write_vtk_data(fd, fp, param.input->answers[i], region, out_type,
  164. null_value, digits);
  165. Rast_close(fd);
  166. }
  167. }
  168. /********************** WRITE RGB IMAGE DATA; CELL OR POINT ****************/
  169. if (param.rgbmaps->answers != NULL) {
  170. if (param.rgbmaps->answers[0] != NULL &&
  171. param.rgbmaps->answers[1] != NULL &&
  172. param.rgbmaps->answers[2] != NULL) {
  173. /*Loop over all three rgb input maps! */
  174. for (i = 0; i < 3; i++) {
  175. G_debug(3, _("Open Raster file %s"),
  176. param.rgbmaps->answers[i]);
  177. /* open raster map */
  178. rgbfd[i] = Rast_open_old(param.rgbmaps->answers[i], "");
  179. celltype[i] = Rast_get_map_type(rgbfd[i]);
  180. }
  181. /*Maps have to be from the same type */
  182. if (celltype[0] == celltype[1] && celltype[0] == celltype[2]) {
  183. G_debug(3, _("Writing VTK ImageData\n"));
  184. out_type = celltype[0];
  185. /*Now write the data */
  186. write_vtk_rgb_image_data(rgbfd[0], rgbfd[1], rgbfd[2], fp,
  187. "RGB_Image", region, out_type,
  188. digits);
  189. }
  190. else {
  191. G_warning(_("Wrong RGB maps. Maps should have the same type! RGB output not added!"));
  192. /*do nothing */
  193. }
  194. /*Close the maps */
  195. for (i = 0; i < 3; i++)
  196. Rast_close(rgbfd[i]);
  197. }
  198. }
  199. /********************** WRITE VECTOR DATA; CELL OR POINT ****************/
  200. if (param.vectmaps->answers != NULL) {
  201. if (param.vectmaps->answers[0] != NULL &&
  202. param.vectmaps->answers[1] != NULL &&
  203. param.vectmaps->answers[2] != NULL) {
  204. /*Loop over all three vect input maps! */
  205. for (i = 0; i < 3; i++) {
  206. G_debug(3, _("Open Raster file %s"),
  207. param.vectmaps->answers[i]);
  208. /* open raster map */
  209. vectfd[i] = Rast_open_old(param.vectmaps->answers[i], "");
  210. celltype[i] = Rast_get_map_type(vectfd[i]);
  211. }
  212. /*Maps have to be from the same type */
  213. if (celltype[0] == celltype[1] && celltype[0] == celltype[2]) {
  214. G_debug(3, _("Writing VTK Vector Data\n"));
  215. out_type = celltype[0];
  216. /*Now write the data */
  217. write_vtk_vector_data(vectfd[0], vectfd[1], vectfd[2], fp,
  218. "Vector_Data", region, out_type,
  219. digits);
  220. }
  221. else {
  222. G_warning(_("Wrong vector maps. Maps should have the same type! Vector output not added!"));
  223. /*do nothing */
  224. }
  225. /*Close the maps */
  226. for (i = 0; i < 3; i++)
  227. Rast_close(vectfd[i]);
  228. }
  229. }
  230. if (param.output->answer && fp != NULL)
  231. if (fclose(fp)) {
  232. G_fatal_error(_("Error closing VTK-ASCII file"));
  233. }
  234. return 0;
  235. }