main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /****************************************************************************
  2. *
  3. * MODULE: r3.in.bin
  4. *
  5. * AUTHOR(S): Soeren Gebbert
  6. * Based on r.in.bin from: Bob Covill
  7. *
  8. * PURPOSE: Imports a binary raster file into a GRASS 3D raster map.
  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 <stdlib.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #include <sys/stat.h>
  21. #include <grass/gis.h>
  22. #include <grass/raster.h>
  23. #include <grass/raster3d.h>
  24. #include <grass/glocale.h>
  25. /* Some global variables */
  26. RASTER3D_Map *map;
  27. RASTER3D_Region region;
  28. FILE *fp;
  29. unsigned char *in_cell;
  30. static void swap_2(void *p) {
  31. unsigned char *q = p;
  32. unsigned char t;
  33. t = q[0];
  34. q[0] = q[1];
  35. q[1] = t;
  36. }
  37. static void swap_4(void *p) {
  38. unsigned char *q = p;
  39. unsigned char t;
  40. t = q[0];
  41. q[0] = q[3];
  42. q[3] = t;
  43. t = q[1];
  44. q[1] = q[2];
  45. q[2] = t;
  46. }
  47. static void swap_8(void *p) {
  48. unsigned char *q = p;
  49. unsigned char t;
  50. t = q[0];
  51. q[0] = q[7];
  52. q[7] = t;
  53. t = q[1];
  54. q[1] = q[6];
  55. q[6] = t;
  56. t = q[2];
  57. q[2] = q[5];
  58. q[5] = t;
  59. t = q[3];
  60. q[3] = q[4];
  61. q[4] = t;
  62. }
  63. static void read_cell(DCELL *out_cell, int is_integer, int is_signed, int bytes,
  64. int byte_swap) {
  65. if (fread(in_cell, bytes, 1, fp) != 1)
  66. G_fatal_error(_("Error reading binary data"));
  67. if (byte_swap) {
  68. switch (bytes) {
  69. case 1:
  70. break;
  71. case 2:
  72. swap_2(in_cell);
  73. break;
  74. case 4:
  75. swap_4(in_cell);
  76. break;
  77. case 8:
  78. swap_8(in_cell);
  79. break;
  80. }
  81. }
  82. if (!is_integer) {
  83. switch (bytes) {
  84. case 4:
  85. *out_cell = (DCELL) *(float *) in_cell;
  86. break;
  87. case 8:
  88. *out_cell = (DCELL) *(double *) in_cell;
  89. break;
  90. }
  91. } else if (is_signed) {
  92. switch (bytes) {
  93. case 1:
  94. *out_cell = (DCELL) *(signed char *) in_cell;
  95. break;
  96. case 2:
  97. *out_cell = (DCELL) *(short *) in_cell;
  98. break;
  99. case 4:
  100. *out_cell = (DCELL) *(int *) in_cell;
  101. break;
  102. #ifdef HAVE_LONG_LONG_INT
  103. case 8:
  104. *out_cell = (DCELL) *(long long *) in_cell;
  105. break;
  106. #endif
  107. }
  108. } else {
  109. switch (bytes) {
  110. case 1:
  111. *out_cell = (DCELL) *(unsigned char *) in_cell;
  112. break;
  113. case 2:
  114. *out_cell = (DCELL) *(unsigned short *) in_cell;
  115. break;
  116. case 4:
  117. *out_cell = (DCELL) *(unsigned int *) in_cell;
  118. break;
  119. #ifdef HAVE_LONG_LONG_INT
  120. case 8:
  121. *out_cell = (DCELL) *(unsigned long long *) in_cell;
  122. break;
  123. #endif
  124. }
  125. }
  126. }
  127. static void bin_to_raster3d(char *null, int map_type, int is_integer,
  128. int is_signed, int bytes, int byte_swap, int row_swap, int depth_swap) {
  129. int x, y, z;
  130. int col, row, depth;
  131. DCELL value;
  132. FCELL fvalue;
  133. DCELL null_value;
  134. int tileX, tileY, tileZ;
  135. if (null)
  136. null_value = atof(null);
  137. Rast3d_get_tile_dimensions_map(map, &tileX, &tileY, &tileZ);
  138. Rast3d_min_unlocked(map, RASTER3D_USE_CACHE_X);
  139. Rast3d_autolock_on(map);
  140. Rast3d_unlock_all(map);
  141. G_message(_("Loading %s data with %i bytes ... (%dx%dx%d)"),
  142. (is_integer? "integer":"floating point "), bytes, region.cols,
  143. region.rows, region.depths);
  144. for (z = 0; z < region.depths; z++) {
  145. G_percent(z, region.depths, 1);
  146. if ((z % tileZ) == 0)
  147. Rast3d_unlock_all(map);
  148. for (y = 0; y < region.rows; y++) {/* go south to north */
  149. for (x = 0; x < region.cols; x++) {
  150. /* From west to east */
  151. col = x;
  152. /* The default is to read rows from north to south */
  153. row = y;
  154. /* From bottom to the top */
  155. depth = z;
  156. /* Read rows as from south to north */
  157. if (row_swap)
  158. row = region.rows - y - 1;
  159. /* Read XY layer from top to bottom */
  160. if (depth_swap)
  161. depth = region.depths - z - 1;
  162. /* Read value from binary file */
  163. read_cell(&value, is_integer, is_signed, bytes, byte_swap);
  164. /* Write value to the 3D raster map */
  165. if (map_type == DCELL_TYPE) {
  166. if (null && value == null_value)
  167. Rast3d_set_null_value(&value, 1, DCELL_TYPE);
  168. Rast3d_put_double(map, col, row, depth, value);
  169. } else {
  170. fvalue = (FCELL) value;
  171. if (null && value == null_value)
  172. Rast3d_set_null_value(&fvalue, 1, FCELL_TYPE);
  173. Rast3d_put_double(map, col, row, depth, fvalue);
  174. }
  175. }
  176. }
  177. }
  178. if (!Rast3d_flush_all_tiles(map))
  179. G_fatal_error(_("Error flushing tiles"));
  180. Rast3d_autolock_off(map);
  181. Rast3d_unlock_all(map);
  182. G_percent(1, 1, 1);
  183. }
  184. int main(int argc, char *argv[]) {
  185. struct GModule *module;
  186. struct {
  187. struct Option *input;
  188. struct Option *output;
  189. struct Option *null;
  190. struct Option *bytes;
  191. struct Option *order;
  192. struct Option *north;
  193. struct Option *south;
  194. struct Option *top;
  195. struct Option *bottom;
  196. struct Option *east;
  197. struct Option *west;
  198. struct Option *rows;
  199. struct Option *cols;
  200. struct Option *depths;
  201. } parm;
  202. struct {
  203. struct Flag *integer_in;
  204. struct Flag *sign;
  205. struct Flag *depth;
  206. struct Flag *row;
  207. } flag;
  208. const char *input;
  209. const char *output;
  210. int is_integer;
  211. int is_signed;
  212. int bytes;
  213. int order;
  214. int byte_swap;
  215. RASTER_MAP_TYPE map_type;
  216. off_t file_size;
  217. struct History history;
  218. off_t expected;
  219. /* Need to be allocated later */
  220. in_cell = NULL;
  221. G_gisinit(argv[0]);
  222. /* Set description */
  223. module = G_define_module();
  224. G_add_keyword(_("raster3d"));
  225. G_add_keyword(_("import"));
  226. module->description =
  227. _("Imports a binary raster file into a GRASS 3D raster map.");
  228. parm.input = G_define_standard_option(G_OPT_F_INPUT);
  229. parm.input->description = _("Name of binary 3D raster file to be imported");
  230. parm.input->gisprompt = "old,bin,file";
  231. parm.output = G_define_standard_option(G_OPT_R3_OUTPUT);
  232. parm.bytes = G_define_option();
  233. parm.bytes->key = "bytes";
  234. parm.bytes->type = TYPE_INTEGER;
  235. parm.bytes->required = YES;
  236. parm.bytes->options = "1,2,4,8";
  237. parm.bytes->description = _("Number of bytes per cell in binary file");
  238. parm.bytes->guisection = _("Settings");
  239. parm.order = G_define_option();
  240. parm.order->key = "order";
  241. parm.order->type = TYPE_STRING;
  242. parm.order->required = NO;
  243. parm.order->options = "big,little,native,swap";
  244. parm.order->description = _("Byte order in binary file");
  245. parm.order->answer = "native";
  246. parm.north = G_define_option();
  247. parm.north->key = "north";
  248. parm.north->type = TYPE_DOUBLE;
  249. parm.north->required = YES;
  250. parm.north->description =
  251. _("Northern limit of geographic region (outer edge)");
  252. parm.north->guisection = _("Bounds");
  253. parm.south = G_define_option();
  254. parm.south->key = "south";
  255. parm.south->type = TYPE_DOUBLE;
  256. parm.south->required = YES;
  257. parm.south->description =
  258. _("Southern limit of geographic region (outer edge)");
  259. parm.south->guisection = _("Bounds");
  260. parm.east = G_define_option();
  261. parm.east->key = "east";
  262. parm.east->type = TYPE_DOUBLE;
  263. parm.east->required = YES;
  264. parm.east->description =
  265. _("Eastern limit of geographic region (outer edge)");
  266. parm.east->guisection = _("Bounds");
  267. parm.west = G_define_option();
  268. parm.west->key = "west";
  269. parm.west->type = TYPE_DOUBLE;
  270. parm.west->required = YES;
  271. parm.west->description =
  272. _("Western limit of geographic region (outer edge)");
  273. parm.west->guisection = _("Bounds");
  274. parm.bottom = G_define_option();
  275. parm.bottom->key = "bottom";
  276. parm.bottom->type = TYPE_DOUBLE;
  277. parm.bottom->required = YES;
  278. parm.bottom->description =
  279. _("Bottom limit of geographic region (outer edge)");
  280. parm.bottom->guisection = _("Bounds");
  281. parm.top = G_define_option();
  282. parm.top->key = "top";
  283. parm.top->type = TYPE_DOUBLE;
  284. parm.top->required = YES;
  285. parm.top->description = _("Top limit of geographic region (outer edge)");
  286. parm.top->guisection = _("Bounds");
  287. parm.rows = G_define_option();
  288. parm.rows->key = "rows";
  289. parm.rows->type = TYPE_INTEGER;
  290. parm.rows->required = YES;
  291. parm.rows->description = _("Number of rows");
  292. parm.rows->guisection = _("Bounds");
  293. parm.cols = G_define_option();
  294. parm.cols->key = "cols";
  295. parm.cols->type = TYPE_INTEGER;
  296. parm.cols->required = YES;
  297. parm.cols->description = _("Number of columns");
  298. parm.cols->guisection = _("Bounds");
  299. parm.depths = G_define_option();
  300. parm.depths->key = "depths";
  301. parm.depths->type = TYPE_INTEGER;
  302. parm.depths->required = YES;
  303. parm.depths->description = _("Number of depths");
  304. parm.depths->guisection = _("Bounds");
  305. parm.null = G_define_option();
  306. parm.null->key = "null";
  307. parm.null->type = TYPE_DOUBLE;
  308. parm.null->required = NO;
  309. parm.null->description = _("Set Value to NULL");
  310. parm.null->guisection = _("Settings");
  311. flag.row = G_define_flag();
  312. flag.row->key = 'r';
  313. flag.row->description = _("Switch the row order in output from "
  314. "north->south to south->north");
  315. flag.depth = G_define_flag();
  316. flag.depth->key = 'd';
  317. flag.depth->description = _("Switch the depth order in output "
  318. "from bottom->top to top->bottom");
  319. flag.integer_in = G_define_flag();
  320. flag.integer_in->key = 'i';
  321. flag.integer_in->description =
  322. _("Binary data is of type integer");
  323. flag.sign = G_define_flag();
  324. flag.sign->key = 's';
  325. flag.sign->description = _("Signed data (two's complement)");
  326. flag.sign->guisection = _("Settings");
  327. if (G_parser(argc, argv))
  328. exit(EXIT_FAILURE);
  329. input = parm.input->answer;
  330. output = parm.output->answer;
  331. if (G_strcasecmp(parm.order->answer, "big") == 0)
  332. order = 0;
  333. else if (G_strcasecmp(parm.order->answer, "little") == 0)
  334. order = 1;
  335. else if (G_strcasecmp(parm.order->answer, "native") == 0)
  336. order = G_is_little_endian() ? 1 : 0;
  337. else if (G_strcasecmp(parm.order->answer, "swap") == 0)
  338. order = G_is_little_endian() ? 0 : 1;
  339. byte_swap = order == (G_is_little_endian() ? 0 : 1);
  340. is_signed = !!flag.sign->answer;
  341. is_integer = 0;
  342. bytes = 8;
  343. if (parm.bytes->answer)
  344. bytes = atoi(parm.bytes->answer);
  345. if (!flag.integer_in->answer) {
  346. if (bytes && bytes < 4)
  347. G_fatal_error(
  348. _("bytes=%d; must be 4 or 8 in case of floating point input"),
  349. bytes);
  350. if (!bytes)
  351. bytes = 4;
  352. } else {
  353. is_integer = 1;
  354. }
  355. #ifndef HAVE_LONG_LONG_INT
  356. if (is_integer && bytes > 4)
  357. G_fatal_error(_("Integer input doesn't support size=8 in this build"));
  358. #endif
  359. if (bytes != 1 && bytes != 2 && bytes != 4 && bytes != 8)
  360. G_fatal_error(_("bytes= must be 1, 2, 4 or 8"));
  361. region.zone = G_zone();
  362. region.proj = G_projection();
  363. region.rows = atoi(parm.rows->answer);
  364. region.cols = atoi(parm.cols->answer);
  365. region.depths = atoi(parm.depths->answer);
  366. region.top = atof(parm.top->answer);
  367. region.bottom = atof(parm.bottom->answer);
  368. if (!G_scan_northing(parm.north->answer, &region.north, region.proj))
  369. G_fatal_error(_("Illegal north coordinate <%s>"), parm.north->answer);
  370. if (!G_scan_northing(parm.south->answer, &region.south, region.proj))
  371. G_fatal_error(_("Illegal south coordinate <%s>"), parm.south->answer);
  372. if (!G_scan_easting(parm.east->answer, &region.east, region.proj))
  373. G_fatal_error(_("Illegal east coordinate <%s>"), parm.east->answer);
  374. if (!G_scan_easting(parm.west->answer, &region.west, region.proj))
  375. G_fatal_error(_("Illegal west coordinate <%s>"), parm.west->answer);
  376. Rast3d_adjust_region(&region);
  377. expected = (off_t) region.rows * region.cols * region.depths * bytes;
  378. fp = fopen(input, "rb");
  379. if (!fp)
  380. G_fatal_error(_("Unable to open <%s>"), input);
  381. /* Find File Size in Byte and Check against byte size */
  382. G_fseek(fp, 0, SEEK_END);
  383. file_size = G_ftell(fp);
  384. G_fseek(fp, 0, SEEK_SET);
  385. if (file_size != expected) {
  386. G_warning(_("File Size %lld ... Total Bytes %lld"),
  387. (long long int) file_size, (long long int) expected);
  388. G_fatal_error(_("Bytes do not match file size"));
  389. }
  390. map_type = (bytes > 4 ? DCELL_TYPE : FCELL_TYPE);
  391. if(is_integer && bytes >= 4)
  392. map_type = DCELL_TYPE;
  393. Rast3d_init_defaults();
  394. /*Open the new 3D raster map */
  395. map = Rast3d_open_new_opt_tile_size(output, RASTER3D_USE_CACHE_DEFAULT,
  396. &region, map_type, 32);
  397. if (map == NULL)
  398. G_fatal_error(_("Unable to open 3D raster map"));
  399. in_cell = G_malloc(bytes);
  400. bin_to_raster3d(parm.null->answer, map_type, is_integer, is_signed, bytes,
  401. byte_swap, flag.row->answer, flag.depth->answer);
  402. if (!Rast3d_close(map))
  403. G_fatal_error(_("Unable to close 3D raster map"));
  404. /* write input name to map history */
  405. Rast3d_read_history(output, G_mapset(), &history);
  406. Rast_set_history(&history, HIST_DATSRC_1, input);
  407. Rast3d_write_history(output, &history);
  408. fclose(fp);
  409. if (in_cell)
  410. G_free(in_cell);
  411. return EXIT_SUCCESS;
  412. }