main.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /****************************************************************************
  2. *
  3. * MODULE: r3.out.bin
  4. *
  5. * AUTHOR(S): Soeren Gebbert
  6. * Based on r.out.bin from: Bob Covill and Glynn Clements
  7. *
  8. * PURPOSE: Exports a GRASS 3D raster map to a binary array.
  9. *
  10. * COPYRIGHT: (C) 2012 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/raster3d.h>
  23. #include <grass/glocale.h>
  24. RASTER3D_Map *map;
  25. RASTER3D_Region region;
  26. static void swap_4(void *p) {
  27. unsigned char *q = p;
  28. unsigned char t;
  29. t = q[0];
  30. q[0] = q[3];
  31. q[3] = t;
  32. t = q[1];
  33. q[1] = q[2];
  34. q[2] = t;
  35. }
  36. static void swap_8(void *p) {
  37. unsigned char *q = p;
  38. unsigned char t;
  39. t = q[0];
  40. q[0] = q[7];
  41. q[7] = t;
  42. t = q[1];
  43. q[1] = q[6];
  44. q[6] = t;
  45. t = q[2];
  46. q[2] = q[5];
  47. q[5] = t;
  48. t = q[3];
  49. q[3] = q[4];
  50. q[4] = t;
  51. }
  52. static void write_float(FILE *fp, int swap_flag, float x) {
  53. if (swap_flag)
  54. swap_4(&x);
  55. if (fwrite(&x, 4, 1, fp) != 1)
  56. G_fatal_error(_("Error writing data"));
  57. }
  58. static void write_double(FILE *fp, int swap_flag, double x) {
  59. if (swap_flag)
  60. swap_8(&x);
  61. if (fwrite(&x, 8, 1, fp) != 1)
  62. G_fatal_error(_("Error writing data"));
  63. }
  64. void raster3d_to_bin(FILE * fp, DCELL null_value, int byte_swap, int row_swap,
  65. int depth_swap) {
  66. DCELL dvalue;
  67. FCELL fvalue;
  68. int x, y, z;
  69. int rows, cols, depths, typeIntern;
  70. int col, row, depth;
  71. rows = region.rows;
  72. cols = region.cols;
  73. depths = region.depths;
  74. typeIntern = Rast3d_tile_type_map(map);
  75. for (z = 0; z < depths; z++) {
  76. G_percent(z, depths, 1);
  77. for (y = 0; y < rows; y++) { /* g3d rows count from south to north */
  78. for (x = 0; x < cols; x++) {
  79. /* From west to east */
  80. col = x;
  81. /* The default is to write rows from north to south
  82. to be r.in.ascii compatible
  83. */
  84. row = y;
  85. /* From bottom to the top */
  86. depth = z;
  87. /* Write rows from south to north */
  88. if (row_swap)
  89. row = rows - y - 1;
  90. /* write XY layer from top to bottom */
  91. if (depth_swap)
  92. depth = depths - z - 1;
  93. if (typeIntern == FCELL_TYPE) {
  94. Rast3d_get_value(map, col, row, depth, &fvalue, FCELL_TYPE);
  95. if (Rast3d_is_null_value_num(&fvalue, FCELL_TYPE))
  96. write_float(fp, byte_swap, (float)null_value);
  97. else
  98. write_float(fp, byte_swap, fvalue);
  99. } else {
  100. Rast3d_get_value(map, col, row, depth, &dvalue, DCELL_TYPE);
  101. if (Rast3d_is_null_value_num(&dvalue, DCELL_TYPE))
  102. write_double(fp, byte_swap, null_value);
  103. else
  104. write_double(fp, byte_swap, dvalue);
  105. }
  106. }
  107. }
  108. }
  109. G_percent(1, 1, 1);
  110. G_percent_reset();
  111. }
  112. int main(int argc, char *argv[]) {
  113. struct GModule *module;
  114. struct {
  115. struct Option *input;
  116. struct Option *output;
  117. struct Option *null;
  118. struct Option *order;
  119. } parm;
  120. struct {
  121. struct Flag *swap, *row, *depth;
  122. } flag;
  123. char *name;
  124. char *outfile;
  125. double null_val;
  126. int do_stdout;
  127. int order;
  128. int swap_flag;
  129. FILE *fp;
  130. G_gisinit(argv[0]);
  131. module = G_define_module();
  132. G_add_keyword(_("3D raster"));
  133. G_add_keyword(_("export"));
  134. module->description = _("Exports a GRASS 3D raster map to a binary array.");
  135. /* Define the different options */
  136. parm.input = G_define_standard_option(G_OPT_R3_INPUT);
  137. parm.output = G_define_standard_option(G_OPT_F_OUTPUT);
  138. parm.null = G_define_option();
  139. parm.null->key = "null";
  140. parm.null->type = TYPE_DOUBLE;
  141. parm.null->required = NO;
  142. parm.null->answer = "0";
  143. parm.null->description = _("Value to write out for null");
  144. parm.order = G_define_option();
  145. parm.order->key = "order";
  146. parm.order->type = TYPE_STRING;
  147. parm.order->required = NO;
  148. parm.order->options = "big,little,native,swap";
  149. parm.order->description = _("Output byte order");
  150. parm.order->answer = "native";
  151. flag.swap = G_define_flag();
  152. flag.swap->key = 's';
  153. flag.swap->description = _("Byte swap output");
  154. flag.row = G_define_flag();
  155. flag.row->key = 'r';
  156. flag.row->description = _("Switch the row order in output from "
  157. "north->south to south->north");
  158. flag.depth = G_define_flag();
  159. flag.depth->key = 'd';
  160. flag.depth->description = _("Switch the depth order in output "
  161. "from bottom->top to top->bottom");
  162. if (G_parser(argc, argv))
  163. exit(EXIT_FAILURE);
  164. if (sscanf(parm.null->answer, "%lf", &null_val) != 1)
  165. G_fatal_error(_("Invalid value for null (integers only)"));
  166. name = parm.input->answer;
  167. if (parm.output->answer)
  168. outfile = parm.output->answer;
  169. else {
  170. outfile = G_malloc(strlen(name) + 4 + 1);
  171. G_snprintf(outfile, sizeof(outfile), "%s.bin", name);
  172. }
  173. if (G_strcasecmp(parm.order->answer, "big") == 0)
  174. order = 0;
  175. else if (G_strcasecmp(parm.order->answer, "little") == 0)
  176. order = 1;
  177. else if (G_strcasecmp(parm.order->answer, "native") == 0)
  178. order = G_is_little_endian() ? 1 : 0;
  179. else if (G_strcasecmp(parm.order->answer, "swap") == 0)
  180. order = G_is_little_endian() ? 0 : 1;
  181. if (flag.swap->answer) {
  182. if (strcmp(parm.order->answer, "native") != 0)
  183. G_fatal_error(_("order= and -s are mutually exclusive"));
  184. order = G_is_little_endian() ? 0 : 1;
  185. }
  186. swap_flag = order == (G_is_little_endian() ? 0 : 1);
  187. do_stdout = strcmp("-", outfile) == 0;
  188. if (NULL == G_find_raster3d(parm.input->answer, ""))
  189. Rast3d_fatal_error(_("3D raster map <%s> not found"),
  190. parm.input->answer);
  191. /* Initiate the default settings */
  192. Rast3d_init_defaults();
  193. /* Figure out the current region settings */
  194. Rast3d_get_window(&region);
  195. /* Open the map and use XY cache mode */
  196. map = Rast3d_open_cell_old(parm.input->answer,
  197. G_find_raster3d(parm.input->answer, ""), &region,
  198. RASTER3D_TILE_SAME_AS_FILE, RASTER3D_USE_CACHE_DEFAULT);
  199. if (map == NULL)
  200. Rast3d_fatal_error(_("Error opening 3d raster map <%s>"),
  201. parm.input->answer);
  202. /* open bin file for writing */
  203. if (do_stdout)
  204. fp = stdout;
  205. else if (NULL == (fp = fopen(outfile, "w")))
  206. G_fatal_error(_("Unable to create file <%s>"), outfile);
  207. G_verbose_message(_("Using the current region settings..."));
  208. G_verbose_message(_("north=%f"), region.north);
  209. G_verbose_message(_("south=%f"), region.south);
  210. G_verbose_message(_("east=%f"), region.east);
  211. G_verbose_message(_("west=%f"), region.west);
  212. G_verbose_message(_("top=%f"), region.top);
  213. G_verbose_message(_("bottom=%f"), region.bottom);
  214. G_verbose_message(_("rows=%d"), region.rows);
  215. G_verbose_message(_("cols=%d"), region.cols);
  216. G_verbose_message(_("depths=%d"), region.depths);
  217. raster3d_to_bin(fp, null_val, swap_flag, flag.row->answer,
  218. flag.depth->answer);
  219. Rast3d_close(map);
  220. fclose(fp);
  221. return EXIT_SUCCESS;
  222. }