main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /***************************************************************************
  2. *
  3. * MODULE: r3.in.ascii
  4. *
  5. * AUTHOR(S): Roman Waupotitsch, Michael Shapiro, Helena Mitasova, Bill Brown,
  6. * Lubos Mitas, Jaro Hofierka, Soeren Gebbert
  7. *
  8. * PURPOSE: Convert a 3D ASCII raster text file into a (binary) 3D raster map layer
  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/raster3d.h>
  22. #include <grass/glocale.h>
  23. #define ROW_ORDER_NORTH_TO_SOUTH 1
  24. #define ROW_ORDER_SOUTH_TO_NORTH 2
  25. #define DEPTH_ORDER_BOTTOM_TO_TOP 3
  26. #define DEPTH_ORDER_TOP_TO_BOTTOM 4
  27. /*- prototypes --------------------------------------------------------------*/
  28. static void fatalError(char *errorMsg); /*Simple Error message */
  29. static void setParams(); /*Fill the paramType structure */
  30. /*Puts the userdefined parameters into easier handable variables */
  31. static void getParams(char **input, char **output, int *convertNull,
  32. char *nullValue);
  33. /*reads a g3d ascii-file headerfile-string */
  34. static void readHeaderString(FILE * fp, char *valueString, double *value);
  35. static FILE *openAscii(char *asciiFile, RASTER3D_Region * region); /*open the g3d ascii file */
  36. /*This function does all the work, it reads the values from the g3d ascii-file and put
  37. it into an g3d-map */
  38. static void asciiToG3d(FILE * fp, RASTER3D_Region * region, int convertNull,
  39. char *nullValue);
  40. /*---------------------------------------------------------------------------*/
  41. /* global variables */
  42. void *map = NULL;
  43. int rowOrder;
  44. int depthOrder;
  45. extern void *Rast3d_open_new_param();
  46. /*---------------------------------------------------------------------------*/
  47. void fatalError(char *errorMsg)
  48. {
  49. if (map != NULL) {
  50. /* should unopen map here! */
  51. Rast3d_close(map);
  52. }
  53. Rast3d_fatal_error(errorMsg);
  54. }
  55. /*---------------------------------------------------------------------------*/
  56. typedef struct {
  57. struct Option *input, *output, *nv;
  58. } paramType;
  59. static paramType param;
  60. static void setParams()
  61. {
  62. param.input = G_define_standard_option(G_OPT_F_INPUT);
  63. param.input->required = YES;
  64. param.input->description = _("Name of input file to be imported");
  65. param.output = G_define_standard_option(G_OPT_R3_OUTPUT);
  66. param.nv = G_define_option();
  67. param.nv->key = "nv";
  68. param.nv->type = TYPE_STRING;
  69. param.nv->required = NO;
  70. param.nv->multiple = NO;
  71. param.nv->answer = "*";
  72. param.nv->description = /* TODO: '*' or 'none' in the msg ?? */
  73. _("String representing NULL value data cell (use 'none' if no such value)");
  74. }
  75. /*---------------------------------------------------------------------------*/
  76. void
  77. getParams(char **input, char **output, int *convertNull, char *nullValue)
  78. {
  79. *input = param.input->answer;
  80. *output = param.output->answer;
  81. *convertNull = (strcmp(param.nv->answer, "none") != 0);
  82. if (*convertNull)
  83. if (sscanf(param.nv->answer, "%s", nullValue) != 1)
  84. fatalError("getParams: NULL-value value invalid");
  85. G_debug(3, "getParams: input: %s, output: %s", *input, *output);
  86. }
  87. /*---------------------------------------------------------------------------*/
  88. void readHeaderString(FILE * fp, char *valueString, double *value)
  89. {
  90. static char format[100];
  91. char line_buff[1024];
  92. /* to avoid buffer overflows we use G_snprintf */
  93. G_snprintf(format, 100, "%s %%lf", valueString);
  94. G_getl2(line_buff, 1024, fp);
  95. if (sscanf(line_buff, format, value) != 1) {
  96. G_debug(3, "bad value for [%s]", valueString);
  97. fatalError("readHeaderString: header value invalid");
  98. }
  99. }
  100. /*---------------------------------------------------------------------------*/
  101. FILE *openAscii(char *asciiFile, RASTER3D_Region * region)
  102. {
  103. FILE *fp;
  104. double tmp;
  105. char buff[1024];
  106. char line_buff[1024];
  107. G_debug(3, "openAscii: opens the ascii file and reads the header");
  108. fp = fopen(asciiFile, "r");
  109. if (fp == NULL) {
  110. perror(asciiFile);
  111. G_usage();
  112. exit(EXIT_FAILURE);
  113. }
  114. /* Initialize the default order */
  115. rowOrder = ROW_ORDER_NORTH_TO_SOUTH;
  116. depthOrder = DEPTH_ORDER_BOTTOM_TO_TOP;
  117. /* Read the first line and check for grass version */
  118. G_getl2(line_buff, 1024, fp);
  119. /* First check for new ascii format*/
  120. if (sscanf(line_buff, "version: %s", buff) == 1) {
  121. G_message("Found version information: %s\n", buff);
  122. if (G_strcasecmp(buff, "grass7") == 0) {
  123. /* Parse the row and depth order */
  124. G_getl2(line_buff, 1024, fp);
  125. if (sscanf(line_buff, "order: %s", buff) != 1)
  126. fatalError("Unable to parse the row and depth order");
  127. if (G_strcasecmp(buff, "nsbt") == 0) {
  128. rowOrder = ROW_ORDER_NORTH_TO_SOUTH;
  129. depthOrder = DEPTH_ORDER_BOTTOM_TO_TOP;
  130. G_message("Found north -> south, bottom -> top order (nsbt)");
  131. }
  132. if (G_strcasecmp(buff, "snbt") == 0) {
  133. rowOrder = ROW_ORDER_SOUTH_TO_NORTH;
  134. depthOrder = DEPTH_ORDER_BOTTOM_TO_TOP;
  135. G_message("Found south -> north, bottom -> top order (snbt)");
  136. }
  137. if (G_strcasecmp(buff, "nstb") == 0) {
  138. rowOrder = ROW_ORDER_NORTH_TO_SOUTH;
  139. depthOrder = DEPTH_ORDER_TOP_TO_BOTTOM;
  140. G_message("Found north -> south, top -> bottom order (nstb)");
  141. }
  142. if (G_strcasecmp(buff, "sntb") == 0) {
  143. rowOrder = ROW_ORDER_SOUTH_TO_NORTH;
  144. depthOrder = DEPTH_ORDER_TOP_TO_BOTTOM;
  145. G_message("Found south -> north, top -> bottom order (sntb)");
  146. }
  147. } else {
  148. G_fatal_error(_("Unsupported GRASS version %s"), buff);
  149. }
  150. } else {
  151. /* Rewind the stream if no grass version info found */
  152. rewind(fp);
  153. }
  154. Rast3d_get_window(region);
  155. readHeaderString(fp, "north:", &(region->north));
  156. readHeaderString(fp, "south:", &(region->south));
  157. readHeaderString(fp, "east:", &(region->east));
  158. readHeaderString(fp, "west:", &(region->west));
  159. readHeaderString(fp, "top:", &(region->top));
  160. readHeaderString(fp, "bottom:", &(region->bottom));
  161. readHeaderString(fp, "rows:", &tmp);
  162. region->rows = (int) tmp;
  163. readHeaderString(fp, "cols:", &tmp);
  164. region->cols = (int) tmp;
  165. readHeaderString(fp, "levels:", &tmp);
  166. region->depths = (int) tmp;
  167. return fp;
  168. }
  169. /*---------------------------------------------------------------------------*/
  170. #define MAX(a,b) (a > b ? a : b)
  171. void
  172. asciiToG3d(FILE * fp, RASTER3D_Region * region, int convertNull, char *nullValue)
  173. {
  174. int x, y, z;
  175. int col, row, depth;
  176. double value;
  177. char buff[256];
  178. int tileX, tileY, tileZ;
  179. Rast3d_get_tile_dimensions_map(map, &tileX, &tileY, &tileZ);
  180. Rast3d_min_unlocked(map, RASTER3D_USE_CACHE_X);
  181. Rast3d_autolock_on(map);
  182. Rast3d_unlock_all(map);
  183. G_message(_("Loading data ... (%dx%dx%d)"), region->cols, region->rows,
  184. region->depths);
  185. G_debug(3,
  186. "asciiToG3d: writing the 3D raster map, with rows %i cols %i depths %i",
  187. region->rows, region->cols, region->depths);
  188. for (z = 0; z < region->depths; z++) {
  189. G_percent(z, region->depths, 1);
  190. if ((z % tileZ) == 0)
  191. Rast3d_unlock_all(map);
  192. for (y = 0; y < region->rows; y++) /* go south to north */
  193. for (x = 0; x < region->cols; x++) {
  194. /* From west to east */
  195. col = x;
  196. /* The default is to read rows from north to south */
  197. row = y;
  198. /* From bottom to the top */
  199. depth = z;
  200. /* Read rows as from south to north */
  201. if (rowOrder == ROW_ORDER_SOUTH_TO_NORTH)
  202. row = region->rows - y - 1;
  203. /* Read XY layer from top to bottom */
  204. if (depthOrder == DEPTH_ORDER_TOP_TO_BOTTOM)
  205. depth = region->depths - z - 1;
  206. if (fscanf(fp, "%s", buff) != 1) {
  207. if (feof(fp))
  208. G_warning(_("End of file reached while still loading data."));
  209. G_debug(3,
  210. "missing data at col=%d row=%d depth=%d last_value=[%.4f]",
  211. x + 1, y + 1, z + 1, value);
  212. fatalError("asciiToG3d: read failed");
  213. }
  214. /* Check for null value */
  215. if (convertNull && strncmp(buff, nullValue, strlen(nullValue)) == 0) {
  216. Rast3d_set_null_value(&value, 1, DCELL_TYPE);
  217. } else {
  218. if (sscanf(buff, "%lf", &value) != 1) {
  219. G_warning(_("Invalid value detected"));
  220. G_debug(1, "invalid value at col=%d row=%d depth=%d last_value=[%s]",
  221. x + 1, y + 1, z + 1, buff);
  222. fatalError("asciiToG3d: read failed");
  223. }
  224. }
  225. /* Write the data */
  226. Rast3d_put_double(map, col, row, depth, value);
  227. }
  228. }
  229. if (fscanf(fp, "%lf", &value) == 1) {
  230. G_warning(_("Data exists in input file after fully importing "
  231. "expected data. [%.4f ...]"), value);
  232. }
  233. if (!Rast3d_flush_all_tiles(map))
  234. fatalError("asciiTog3d: error flushing tiles");
  235. Rast3d_autolock_off(map);
  236. Rast3d_unlock_all(map);
  237. G_percent(1, 1, 1);
  238. }
  239. /*---------------------------------------------------------------------------*/
  240. int main(int argc, char *argv[])
  241. {
  242. char *input, *output;
  243. int convertNull;
  244. char nullValue[256];
  245. int useTypeDefault, type, useCompressionDefault, doCompression;
  246. int usePrecisionDefault, precision, useDimensionDefault, tileX, tileY,
  247. tileZ;
  248. RASTER3D_Region region;
  249. FILE *fp;
  250. struct GModule *module;
  251. struct History history;
  252. map = NULL;
  253. G_gisinit(argv[0]);
  254. module = G_define_module();
  255. G_add_keyword(_("raster3d"));
  256. G_add_keyword(_("voxel"));
  257. G_add_keyword(_("import"));
  258. G_add_keyword(_("conversion"));
  259. G_add_keyword("ASCII");
  260. module->description =
  261. _("Converts a 3D ASCII raster text file into a (binary) 3D raster map.");
  262. setParams();
  263. Rast3d_set_standard3d_input_params();
  264. if (G_parser(argc, argv))
  265. exit(EXIT_FAILURE);
  266. getParams(&input, &output, &convertNull, nullValue);
  267. if (!Rast3d_get_standard3d_params(&useTypeDefault, &type,
  268. &useCompressionDefault, &doCompression,
  269. &usePrecisionDefault, &precision,
  270. &useDimensionDefault, &tileX, &tileY,
  271. &tileZ))
  272. fatalError("Error getting standard parameters");
  273. Rast3d_init_defaults();
  274. fp = openAscii(input, &region);
  275. /*Open the new RASTER3D map */
  276. map = Rast3d_open_new_param(output, RASTER3D_TILE_SAME_AS_FILE, RASTER3D_USE_CACHE_XY,
  277. &region,
  278. type, doCompression, precision, tileX, tileY,
  279. tileZ);
  280. if (map == NULL)
  281. fatalError(_("Unable to open 3D raster map"));
  282. /*Create the new RASTER3D Map */
  283. asciiToG3d(fp, &region, convertNull, nullValue);
  284. if (!Rast3d_close(map))
  285. fatalError(_("Unable to close 3D raster map"));
  286. /* write input name to map history */
  287. Rast3d_read_history(output, G_mapset(), &history);
  288. Rast_command_history(&history);
  289. Rast_set_history(&history, HIST_DATSRC_1, input);
  290. Rast3d_write_history(output, &history);
  291. map = NULL;
  292. if (fclose(fp))
  293. fatalError(_("Unable to close ASCII file"));
  294. return EXIT_SUCCESS;
  295. }