main.c 8.0 KB

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