main.c 12 KB

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