r3.info.main.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /***************************************************************************
  2. *
  3. * MODULE: r3.info
  4. *
  5. * AUTHOR(S): Roman Waupotitsch, Michael Shapiro, Helena Mitasova, Bill Brown,
  6. * Lubos Mitas, Jaro Hofierka
  7. *
  8. * PURPOSE: Outputs basic information about a user-specified 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. /* \todo
  18. * History support still not full implemented.
  19. * Only parts of the timestep functionality are implemented, the timzone is missed ;).
  20. */
  21. /*local prototype */
  22. int format_double(double value, char *buf);
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <grass/gis.h>
  27. #include <grass/raster3d.h>
  28. #include <grass/glocale.h>
  29. #define printline(x) fprintf (out," | %-74.74s |\n",x)
  30. #define divider(x) \
  31. fprintf (out," %c",x);\
  32. for (i = 0; i < 76; i++)\
  33. fprintf(out,"-");\
  34. fprintf (out,"%c\n",x)
  35. #define TMP_LENGTH 100
  36. static char *name;
  37. /**************************************************************************/
  38. int main(int argc, char *argv[])
  39. {
  40. const char *mapset;
  41. char *line = NULL;
  42. char tmp1[TMP_LENGTH], tmp2[TMP_LENGTH], tmp3[TMP_LENGTH];
  43. char timebuff[256];
  44. int i;
  45. FILE *out;
  46. RASTER3D_Region cellhd;
  47. RASTER3D_Map *g3map;
  48. struct Categories cats;
  49. struct History hist;
  50. struct TimeStamp ts;
  51. int head_ok;
  52. int cats_ok;
  53. int hist_ok;
  54. int time_ok = 0, first_time_ok = 0, second_time_ok = 0;
  55. struct Option *opt1;
  56. struct Flag *rflag;
  57. struct Flag *gflag;
  58. struct Flag *hflag;
  59. int data_type;
  60. struct GModule *module;
  61. double dmin, dmax;
  62. G_gisinit(argv[0]);
  63. module = G_define_module();
  64. G_add_keyword(_("raster3d"));
  65. G_add_keyword(_("metadata"));
  66. G_add_keyword(_("extent"));
  67. G_add_keyword(_("voxel"));
  68. module->description =
  69. _("Outputs basic information about a user-specified 3D raster map layer.");
  70. opt1 = G_define_standard_option(G_OPT_R3_MAP);
  71. gflag = G_define_flag();
  72. gflag->key = 'g';
  73. gflag->description = _("Print raster3d information in shell style");
  74. rflag = G_define_flag();
  75. rflag->key = 'r';
  76. rflag->description = _("Print range in shell style only");
  77. hflag = G_define_flag();
  78. hflag->key = 'h';
  79. hflag->description = _("Print raster history instead of info");
  80. if (G_parser(argc, argv))
  81. exit(EXIT_FAILURE);
  82. name = G_store(opt1->answer);
  83. mapset = G_find_raster3d(name, "");
  84. if (mapset == NULL)
  85. G_fatal_error(_("3D Raster map <%s> not found"), name);
  86. /*We need to open the map */
  87. g3map =
  88. Rast3d_open_cell_old(name, mapset, RASTER3D_DEFAULT_WINDOW,
  89. RASTER3D_TILE_SAME_AS_FILE, RASTER3D_NO_CACHE);
  90. if (NULL == g3map)
  91. G_fatal_error(_("Unable to open 3D raster map <%s>"), name);
  92. /*Get the maptype */
  93. data_type = Rast3d_file_type_map(g3map);
  94. head_ok = Rast3d_read_region_map(name, mapset, &cellhd) >= 0;
  95. hist_ok = Rast3d_read_history(name, mapset, &hist) >= 0;
  96. cats_ok = Rast3d_read_cats(name, mapset, &cats) >= 0;
  97. /*Check the Timestamp */
  98. time_ok = G_read_raster3d_timestamp(name, mapset, &ts) > 0;
  99. /*Check for valid entries, show none if no entire available! */
  100. if (time_ok) {
  101. if (ts.count > 0)
  102. first_time_ok = 1;
  103. if (ts.count > 1)
  104. second_time_ok = 1;
  105. }
  106. out = stdout;
  107. /*Show the info if no flag is set */
  108. if (!rflag->answer && !gflag->answer && !hflag->answer) {
  109. divider('+');
  110. if (G_asprintf(&line, "Layer: %-29.29s Date: %s", name,
  111. hist_ok ? Rast_get_history(&hist, HIST_MAPID) : "??") > 0)
  112. printline(line);
  113. else
  114. G_fatal_error(_("Cannot allocate memory for string"));
  115. if (G_asprintf(&line, "Mapset: %-29.29s Login of Creator: %s", mapset,
  116. hist_ok ? Rast_get_history(&hist, HIST_CREATOR) : "??") > 0)
  117. printline(line);
  118. else
  119. G_fatal_error(_("Cannot allocate memory for string"));
  120. if (G_asprintf(&line, "Location: %s", G_location()) > 0)
  121. printline(line);
  122. else
  123. G_fatal_error(_("Cannot allocate memory for string"));
  124. if (G_asprintf(&line, "DataBase: %s", G_gisdbase()) > 0)
  125. printline(line);
  126. else
  127. G_fatal_error(_("Cannot allocate memory for string"));
  128. if (G_asprintf(&line, "Title: %s",
  129. hist_ok ? Rast_get_history(&hist, HIST_TITLE) : "??") > 0)
  130. printline(line);
  131. else
  132. G_fatal_error(_("Cannot allocate memory for string"));
  133. if (G_asprintf(&line, "Units: %s", Rast3d_get_unit(g3map)))
  134. printline(line);
  135. else
  136. G_fatal_error(_("Cannot allocate memory for string"));
  137. if (G_asprintf(&line, "Vertical unit: %s", Rast3d_get_vertical_unit(g3map)))
  138. printline(line);
  139. else
  140. G_fatal_error(_("Cannot allocate memory for string"));
  141. /*This shows the TimeStamp */
  142. if (time_ok && (first_time_ok || second_time_ok)) {
  143. G_format_timestamp(&ts, timebuff);
  144. /*Create the r.info timestamp string */
  145. if (G_asprintf(&line, "Timestamp: %s", timebuff) > 0)
  146. printline(line);
  147. else
  148. G_fatal_error(_("Cannot allocate memory for string"));
  149. }
  150. else {
  151. if (G_asprintf(&line, "Timestamp: none") > 0)
  152. printline(line);
  153. else
  154. G_fatal_error(_("Cannot allocate memory for string"));
  155. }
  156. divider('|');
  157. printline("");
  158. if (cats_ok) {
  159. format_double((double)cats.num, tmp1);
  160. }
  161. if (G_asprintf(&line, " Type of Map: %-20.20s Number of Categories: %-9s",
  162. "3d cell", cats_ok ? tmp1 : "??") > 0)
  163. printline(line);
  164. else
  165. G_fatal_error(_("Cannot allocate memory for string"));
  166. if (G_asprintf(&line, " Data Type: %s",
  167. (data_type == FCELL_TYPE ? "FCELL" :
  168. (data_type == DCELL_TYPE ? "DCELL" : "??"))) > 0)
  169. printline(line);
  170. else
  171. G_fatal_error(_("Cannot allocate memory for string"));
  172. if (head_ok) {
  173. if (G_asprintf(&line, " Rows: %d", cellhd.rows) > 0)
  174. printline(line);
  175. else
  176. G_fatal_error(_("Cannot allocate memory for string"));
  177. if (G_asprintf(&line, " Columns: %d", cellhd.cols) > 0)
  178. printline(line);
  179. else
  180. G_fatal_error(_("Cannot allocate memory for string"));
  181. if (G_asprintf(&line, " Depths: %d", cellhd.depths) > 0)
  182. printline(line);
  183. else
  184. G_fatal_error(_("Cannot allocate memory for string"));
  185. if (G_asprintf
  186. (&line, " Total Cells: %ld",
  187. (long)cellhd.rows * cellhd.cols * cellhd.depths) > 0)
  188. printline(line);
  189. else
  190. G_fatal_error(_("Cannot allocate memory for string"));
  191. double totalSize = 0;
  192. for(i = 0; i < g3map->nTiles; i++)
  193. totalSize += g3map->tileLength[i];
  194. if (G_asprintf(&line, " Total size: %ld Bytes",
  195. (long)(totalSize)) > 0)
  196. printline(line);
  197. else
  198. G_fatal_error(_("Cannot allocate memory for string"));
  199. if (G_asprintf(&line, " Number of tiles: %d",
  200. g3map->nTiles) > 0)
  201. printline(line);
  202. else
  203. G_fatal_error(_("Cannot allocate memory for string"));
  204. if (G_asprintf(&line, " Mean tile size: %ld Bytes",
  205. (long)(totalSize/g3map->nTiles)) > 0)
  206. printline(line);
  207. else
  208. G_fatal_error(_("Cannot allocate memory for string"));
  209. int tileSize = 0;
  210. if(data_type == FCELL_TYPE)
  211. tileSize = sizeof(FCELL) * g3map->tileX * g3map->tileY *
  212. ((RASTER3D_Map* )g3map)->tileZ;
  213. if(data_type == DCELL_TYPE)
  214. tileSize = sizeof(DCELL) * g3map->tileX * g3map->tileY *
  215. g3map->tileZ;
  216. if (G_asprintf(&line, " Tile size in memory: %ld Bytes",
  217. (long)(tileSize)) > 0)
  218. printline(line);
  219. else
  220. G_fatal_error(_("Cannot allocate memory for string"));
  221. if (G_asprintf(&line, " Number of tiles in x, y and z: %d, %d, %d",
  222. g3map->nx, g3map->ny,
  223. g3map->nz) > 0)
  224. printline(line);
  225. else
  226. G_fatal_error(_("Cannot allocate memory for string"));
  227. if (G_asprintf(&line, " Dimension of a tile in x, y, z: %d, %d, %d",
  228. g3map->tileX, g3map->tileY,
  229. g3map->tileZ) > 0)
  230. printline(line);
  231. else
  232. G_fatal_error(_("Cannot allocate memory for string"));
  233. printline("");
  234. if (G_asprintf
  235. (&line, " Projection: %s (zone %d)",
  236. G_database_projection_name(), G_zone()) > 0)
  237. printline(line);
  238. else
  239. G_fatal_error(_("Cannot allocate memory for string"));
  240. G_format_northing(cellhd.north, tmp1, cellhd.proj);
  241. G_format_northing(cellhd.south, tmp2, cellhd.proj);
  242. G_format_resolution(cellhd.ns_res, tmp3, cellhd.proj);
  243. if (G_asprintf
  244. (&line, " N: %10s S: %10s Res: %5s", tmp1,
  245. tmp2, tmp3) > 0)
  246. printline(line);
  247. else
  248. G_fatal_error(_("Cannot allocate memory for string"));
  249. G_format_easting(cellhd.east, tmp1, cellhd.proj);
  250. G_format_easting(cellhd.west, tmp2, cellhd.proj);
  251. G_format_resolution(cellhd.ew_res, tmp3, cellhd.proj);
  252. if (G_asprintf
  253. (&line, " E: %10s W: %10s Res: %5s", tmp1,
  254. tmp2, tmp3) > 0)
  255. printline(line);
  256. else
  257. G_fatal_error(_("Cannot allocate memory for string"));
  258. format_double(cellhd.top, tmp1);
  259. format_double(cellhd.bottom, tmp2);
  260. format_double(cellhd.tb_res, tmp3);
  261. if (G_asprintf
  262. (&line, " T: %10s B: %10s Res: %5s", tmp1,
  263. tmp2, tmp3) > 0)
  264. printline(line);
  265. else
  266. G_fatal_error(_("Cannot allocate memory for string"));
  267. if (0 == Rast3d_range_load(g3map))
  268. G_fatal_error(_("Unable to read range of 3D raster map <%s>"), name);
  269. Rast3d_range_min_max(g3map, &dmin, &dmax);
  270. if(dmin != dmin)
  271. sprintf(tmp1, "%s", "NULL");
  272. else
  273. format_double(dmin, tmp1);
  274. if(dmax != dmax)
  275. sprintf(tmp2, "%s", "NULL");
  276. else
  277. format_double(dmax, tmp2);
  278. if (G_asprintf
  279. (&line, " Range of data: min = %10s max = %10s", tmp1,
  280. tmp2) > 0)
  281. printline(line);
  282. else
  283. G_fatal_error(_("Cannot allocate memory for string"));
  284. }
  285. printline("");
  286. if (hist_ok) {
  287. printline(" Data Source:");
  288. if (G_asprintf(&line, " %s", Rast_get_history(&hist, HIST_DATSRC_1)) > 0)
  289. printline(line);
  290. else
  291. G_fatal_error(_("Cannot allocate memory for string"));
  292. if (G_asprintf(&line, " %s", Rast_get_history(&hist, HIST_DATSRC_2)) > 0)
  293. printline(line);
  294. else
  295. G_fatal_error(_("Cannot allocate memory for string"));
  296. printline("");
  297. printline(" Data Description:");
  298. if (G_asprintf(&line, " %s", Rast_get_history(&hist, HIST_KEYWRD)) > 0)
  299. printline(line);
  300. else
  301. G_fatal_error(_("Cannot allocate memory for string"));
  302. printline("");
  303. if (Rast_history_length(&hist)) {
  304. printline(" Comments: ");
  305. for (i = 0; i < Rast_history_length(&hist); i++)
  306. /**************************************/
  307. {
  308. if (G_asprintf(&line, " %s", Rast_history_line(&hist, i)) > 0)
  309. printline(line);
  310. else
  311. G_fatal_error(_("Cannot allocate memory for string"));
  312. }
  313. }
  314. printline("");
  315. }
  316. divider('+');
  317. fprintf(out, "\n");
  318. }
  319. else {/* Print information in shell style*/
  320. if (gflag->answer) {
  321. sprintf(tmp1, "%f", cellhd.north);
  322. sprintf(tmp2, "%f", cellhd.south);
  323. G_trim_decimal(tmp1);
  324. G_trim_decimal(tmp2);
  325. fprintf(out, "north=%s\n", tmp1);
  326. fprintf(out, "south=%s\n", tmp2);
  327. sprintf(tmp1, "%f", cellhd.east);
  328. sprintf(tmp2, "%f", cellhd.west);
  329. G_trim_decimal(tmp1);
  330. G_trim_decimal(tmp2);
  331. fprintf(out, "east=%s\n", tmp1);
  332. fprintf(out, "west=%s\n", tmp2);
  333. fprintf(out, "bottom=%g\n", cellhd.bottom);
  334. fprintf(out, "top=%g\n", cellhd.top);
  335. G_format_resolution(cellhd.ns_res, tmp3, cellhd.proj);
  336. fprintf(out, "nsres=%s\n", tmp3);
  337. G_format_resolution(cellhd.ew_res, tmp3, cellhd.proj);
  338. fprintf(out, "ewres=%s\n", tmp3);
  339. fprintf(out, "tbres=%g\n", cellhd.tb_res);
  340. fprintf(out, "rows=%d\n", cellhd.rows);
  341. fprintf(out, "cols=%d\n", cellhd.cols);
  342. fprintf(out, "depths=%d\n", cellhd.depths);
  343. fprintf(out, "datatype=\"%s\"\n",
  344. data_type == FCELL_TYPE ? "FCELL" :
  345. data_type == DCELL_TYPE ? "DCELL" :
  346. "??");
  347. if (time_ok && (first_time_ok || second_time_ok)) {
  348. G_format_timestamp(&ts, timebuff);
  349. fprintf(out, "timestamp=\"%s\"\n", timebuff);
  350. }
  351. else {
  352. fprintf(out, "timestamp=\"none\"\n");
  353. }
  354. fprintf(out, "units=\"%s\"\n", Rast3d_get_unit(g3map));
  355. fprintf(out, "vertical_units=\"%s\"\n", Rast3d_get_vertical_unit(g3map));
  356. fprintf(out, "tilenumx=%d\n", g3map->nx);
  357. fprintf(out, "tilenumy=%d\n", g3map->ny);
  358. fprintf(out, "tilenumz=%d\n", g3map->nz);
  359. fprintf(out, "tiledimx=%d\n", g3map->tileX);
  360. fprintf(out, "tiledimy=%d\n", g3map->tileY);
  361. fprintf(out, "tiledimz=%d\n", g3map->tileZ);
  362. }
  363. if (rflag->answer) {
  364. if (0 == Rast3d_range_load(g3map))
  365. G_fatal_error(_("Unable to read range of 3D raster map <%s>"), name);
  366. Rast3d_range_min_max(g3map, &dmin, &dmax);
  367. if(dmin != dmin)
  368. fprintf(out, "min=NULL\n");
  369. else
  370. fprintf(out, "min=%f\n", dmin);
  371. if(dmax != dmax)
  372. fprintf(out, "max=NULL\n");
  373. else
  374. fprintf(out, "max=%f\n", dmax);
  375. }
  376. if (hflag->answer) {
  377. if (hist_ok) {
  378. fprintf(out, "Title:\n");
  379. fprintf(out, " %s\n", Rast_get_history(&hist, HIST_TITLE));
  380. fprintf(out, "Data Source:\n");
  381. fprintf(out, " %s\n", Rast_get_history(&hist, HIST_DATSRC_1));
  382. fprintf(out, " %s\n", Rast_get_history(&hist, HIST_DATSRC_2));
  383. fprintf(out, "Data Description:\n");
  384. fprintf(out, " %s\n", Rast_get_history(&hist, HIST_KEYWRD));
  385. if (Rast_history_length(&hist)) {
  386. fprintf(out, "Comments:\n");
  387. for (i = 0; i < Rast_history_length(&hist); i++)
  388. fprintf(out, " %s\n", Rast_history_line(&hist, i));
  389. }
  390. }
  391. else {
  392. G_fatal_error(_("Error while reading history file"));
  393. }
  394. }
  395. }
  396. /*Close the opened map */
  397. if (!Rast3d_close(g3map))
  398. G_fatal_error(_("Unable to close 3D raster map <%s>"), name);
  399. return 0;
  400. }
  401. /**************************************************************************/
  402. int format_double(double value, char *buf)
  403. {
  404. sprintf(buf, "%.8f", value);
  405. G_trim_decimal(buf);
  406. return 0;
  407. }