header.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include <grass/raster3d.h>
  6. #include <grass/glocale.h>
  7. #include "raster3d_intern.h"
  8. /*---------------------------------------------------------------------------*/
  9. void *tmpCompress;
  10. int tmpCompressLength;
  11. void *xdr;
  12. int xdrLength;
  13. /*---------------------------------------------------------------------------*/
  14. #define RASTER3D_HEADER_TILEX "TileDimensionX"
  15. #define RASTER3D_HEADER_TILEY "TileDimensionY"
  16. #define RASTER3D_HEADER_TILEZ "TileDimensionZ"
  17. #define RASTER3D_HEADER_TYPE "CellType"
  18. #define RASTER3D_HEADER_COMPRESSION "useCompression"
  19. #define RASTER3D_HEADER_USERLE "useRle"
  20. #define RASTER3D_HEADER_USELZW "useLzw"
  21. #define RASTER3D_HEADER_PRECISION "Precision"
  22. #define RASTER3D_HEADER_DATA_OFFSET "nofHeaderBytes"
  23. #define RASTER3D_HEADER_USEXDR "useXdr"
  24. #define RASTER3D_HEADER_HASINDEX "hasIndex"
  25. #define RASTER3D_HEADER_UNIT "Units"
  26. #define RASTER3D_HEADER_VERTICAL_UNIT "VerticalUnits"
  27. #define RASTER3D_HEADER_VERSION "Version"
  28. /*---------------------------------------------------------------------------*/
  29. static int
  30. Rast3d_readWriteHeader(struct Key_Value *headerKeys, int doRead, int *proj,
  31. int *zone, double *north, double *south, double *east,
  32. double *west, double *top, double *bottom, int *rows,
  33. int *cols, int *depths, double *ew_res, double *ns_res,
  34. double *tb_res, int *tileX, int *tileY, int *tileZ,
  35. int *type, int *compression, int *useRle, int *useLzw,
  36. int *precision, int *dataOffset, int *useXdr,
  37. int *hasIndex, char **unit, int *vertical_unit, int *version)
  38. {
  39. int returnVal;
  40. int (*headerInt) (), (*headerDouble) (), (*headerValue) ();
  41. int (*headerString) ();
  42. if (doRead) {
  43. headerDouble = Rast3d_key_get_double;
  44. headerInt = Rast3d_key_get_int;
  45. headerString = Rast3d_key_get_string;
  46. headerValue = Rast3d_key_get_value;
  47. }
  48. else {
  49. headerDouble = Rast3d_key_set_double;
  50. headerInt = Rast3d_key_set_int;
  51. headerString = Rast3d_key_set_string;
  52. headerValue = Rast3d_key_set_value;
  53. }
  54. returnVal = 1;
  55. returnVal &= headerInt(headerKeys, RASTER3D_REGION_PROJ, proj);
  56. returnVal &= headerInt(headerKeys, RASTER3D_REGION_ZONE, zone);
  57. returnVal &= headerDouble(headerKeys, RASTER3D_REGION_NORTH, north);
  58. returnVal &= headerDouble(headerKeys, RASTER3D_REGION_SOUTH, south);
  59. returnVal &= headerDouble(headerKeys, RASTER3D_REGION_EAST, east);
  60. returnVal &= headerDouble(headerKeys, RASTER3D_REGION_WEST, west);
  61. returnVal &= headerDouble(headerKeys, RASTER3D_REGION_TOP, top);
  62. returnVal &= headerDouble(headerKeys, RASTER3D_REGION_BOTTOM, bottom);
  63. returnVal &= headerInt(headerKeys, RASTER3D_REGION_ROWS, rows);
  64. returnVal &= headerInt(headerKeys, RASTER3D_REGION_COLS, cols);
  65. returnVal &= headerInt(headerKeys, RASTER3D_REGION_DEPTHS, depths);
  66. returnVal &= headerDouble(headerKeys, RASTER3D_REGION_NSRES, ns_res);
  67. returnVal &= headerDouble(headerKeys, RASTER3D_REGION_EWRES, ew_res);
  68. returnVal &= headerDouble(headerKeys, RASTER3D_REGION_TBRES, tb_res);
  69. returnVal &= headerInt(headerKeys, RASTER3D_HEADER_TILEX, tileX);
  70. returnVal &= headerInt(headerKeys, RASTER3D_HEADER_TILEY, tileY);
  71. returnVal &= headerInt(headerKeys, RASTER3D_HEADER_TILEZ, tileZ);
  72. returnVal &= headerValue(headerKeys, RASTER3D_HEADER_TYPE,
  73. "double", "float", DCELL_TYPE, FCELL_TYPE, type);
  74. returnVal &= headerValue(headerKeys, RASTER3D_HEADER_COMPRESSION,
  75. "0", "1", 0, 1, compression);
  76. returnVal &= headerValue(headerKeys, RASTER3D_HEADER_USERLE,
  77. "0", "1", 0, 1, useRle);
  78. returnVal &= headerValue(headerKeys, RASTER3D_HEADER_USELZW,
  79. "0", "1", 0, 1, useLzw);
  80. returnVal &= headerInt(headerKeys, RASTER3D_HEADER_PRECISION, precision);
  81. returnVal &= headerInt(headerKeys, RASTER3D_HEADER_DATA_OFFSET, dataOffset);
  82. returnVal &= headerValue(headerKeys, RASTER3D_HEADER_USEXDR,
  83. "0", "1", 0, 1, useXdr);
  84. returnVal &= headerValue(headerKeys, RASTER3D_HEADER_HASINDEX,
  85. "0", "1", 0, 1, hasIndex);
  86. returnVal &= headerString(headerKeys, RASTER3D_HEADER_UNIT, unit);
  87. /* New format and API changes */
  88. if(!headerInt(headerKeys, RASTER3D_HEADER_VERTICAL_UNIT, vertical_unit))
  89. G_warning("You are using an old raster3d data format, the vertical unit is undefined. "
  90. "Please use r3.support to define the vertical unit to avoid this warning.");
  91. /* New format and API changes */
  92. if(!headerInt(headerKeys, RASTER3D_HEADER_VERSION, version)) {
  93. G_warning("You are using an old raster3d data format, the version is undefined.");
  94. *version = 1;
  95. }
  96. if (returnVal)
  97. return 1;
  98. Rast3d_error("Rast3d_readWriteHeader: error reading/writing header");
  99. return 0;
  100. }
  101. /*---------------------------------------------------------------------------*/
  102. int
  103. Rast3d_read_header(RASTER3D_Map * map, int *proj, int *zone, double *north,
  104. double *south, double *east, double *west, double *top,
  105. double *bottom, int *rows, int *cols, int *depths,
  106. double *ew_res, double *ns_res, double *tb_res, int *tileX,
  107. int *tileY, int *tileZ, int *type, int *compression,
  108. int *useRle, int *useLzw, int *precision, int *dataOffset,
  109. int *useXdr, int *hasIndex, char **unit, int *vertical_unit,
  110. int *version)
  111. {
  112. struct Key_Value *headerKeys;
  113. char path[GPATH_MAX];
  114. Rast3d_filename(path, RASTER3D_HEADER_ELEMENT, map->fileName, map->mapset);
  115. if (access(path, R_OK) != 0) {
  116. Rast3d_error("Rast3d_read_header: unable to find [%s]", path);
  117. return 0;
  118. }
  119. headerKeys = G_read_key_value_file(path);
  120. if (!Rast3d_readWriteHeader(headerKeys, 1,
  121. proj, zone,
  122. north, south, east, west, top, bottom,
  123. rows, cols, depths,
  124. ew_res, ns_res, tb_res,
  125. tileX, tileY, tileZ,
  126. type, compression, useRle, useLzw, precision,
  127. dataOffset, useXdr, hasIndex, unit, vertical_unit, version)) {
  128. Rast3d_error("Rast3d_read_header: error extracting header key(s) of file %s",
  129. path);
  130. return 0;
  131. }
  132. G_free_key_value(headerKeys);
  133. return 1;
  134. }
  135. /*---------------------------------------------------------------------------*/
  136. int
  137. Rast3d_write_header(RASTER3D_Map * map, int proj, int zone, double north, double south,
  138. double east, double west, double top, double bottom, int rows,
  139. int cols, int depths, double ew_res, double ns_res,
  140. double tb_res, int tileX, int tileY, int tileZ, int type,
  141. int compression, int useRle, int useLzw, int precision,
  142. int dataOffset, int useXdr, int hasIndex, char *unit, int vertical_unit,
  143. int version)
  144. {
  145. struct Key_Value *headerKeys;
  146. char path[GPATH_MAX];
  147. headerKeys = G_create_key_value();
  148. if (!Rast3d_readWriteHeader(headerKeys, 0,
  149. &proj, &zone,
  150. &north, &south, &east, &west, &top, &bottom,
  151. &rows, &cols, &depths,
  152. &ew_res, &ns_res, &tb_res,
  153. &tileX, &tileY, &tileZ,
  154. &type, &compression, &useRle, &useLzw,
  155. &precision, &dataOffset, &useXdr, &hasIndex,
  156. &unit, &vertical_unit, &version)) {
  157. Rast3d_error("Rast3d_write_header: error adding header key(s) for file %s",
  158. path);
  159. return 0;
  160. }
  161. Rast3d_filename(path, RASTER3D_HEADER_ELEMENT, map->fileName, map->mapset);
  162. Rast3d_make_mapset_map_directory(map->fileName);
  163. G_write_key_value_file(path, headerKeys);
  164. G_free_key_value(headerKeys);
  165. return 1;
  166. }
  167. /*---------------------------------------------------------------------------*/
  168. int
  169. Rast3d_rewrite_header(RASTER3D_Map * map)
  170. {
  171. if (!Rast3d_write_header(map,
  172. map->region.proj, map->region.zone,
  173. map->region.north, map->region.south,
  174. map->region.east, map->region.west,
  175. map->region.top, map->region.bottom,
  176. map->region.rows, map->region.cols,
  177. map->region.depths,
  178. map->region.ew_res, map->region.ns_res,
  179. map->region.tb_res,
  180. map->tileX, map->tileY, map->tileZ,
  181. map->type,
  182. map->compression, map->useRle, map->useLzw,
  183. map->precision, map->offset, map->useXdr,
  184. map->hasIndex, map->unit, map->vertical_unit,
  185. map->version)) {
  186. G_warning(_("Unable to write header for 3D raster map <%s>"), map->fileName);
  187. return 0;
  188. }
  189. return 1;
  190. }
  191. /*---------------------------------------------------------------------------*/
  192. /*!
  193. * \brief
  194. *
  195. * Returns a number
  196. * which encodes multiplicity <em>n</em> of <em>cacheCode</em>. This value can be used
  197. * to specify the size of the cache.
  198. * If <em>cacheCode</em> is the size (in tiles) of the cache the function returns
  199. * <em>cacheCode * n</em>.
  200. * If <em>cacheCode</em> is RASTER3D_USE_CACHE_DEFAULT the function returns
  201. * RASTER3D_USE_CACHE_DEFAULT.
  202. * If <em>cacheCode</em> is RASTER3D_USE_CACHE_??? the function returns a value
  203. * encoding RASTER3D_USE_CACHE_??? and <em>n</em>. Here RASTER3D_USE_CACHE_??? is one
  204. * of RASTER3D_USE_CACHE_X, RASTER3D_USE_CACHE_Y, RASTER3D_USE_CACHE_Z,
  205. * RASTER3D_USE_CACHE_XY, RASTER3D_USE_CACHE_XZ, RASTER3D_USE_CACHE_YZ, or
  206. * RASTER3D_USE_CACHE_XYZ, where e.g. RASTER3D_USE_CACHE_X specifies that the cache
  207. * should store as many tiles as there exist in one row along the x-axis of the
  208. * tile cube, and RASTER3D_USE_CACHE_XY specifies that the cache should store as
  209. * many tiles as there exist in one slice of the tile cube with constant Z
  210. * coordinate.
  211. *
  212. * \param cacheCode
  213. * \param n
  214. * \return int
  215. */
  216. int Rast3d_cache_size_encode(int cacheCode, int n)
  217. {
  218. if (cacheCode >= RASTER3D_NO_CACHE)
  219. return cacheCode * n;
  220. if (cacheCode == RASTER3D_USE_CACHE_DEFAULT)
  221. return cacheCode;
  222. if (cacheCode < RASTER3D_USE_CACHE_XYZ)
  223. Rast3d_fatal_error("Rast3d_cache_size_encode: invalid cache code");
  224. return n * (-10) + cacheCode;
  225. }
  226. /*---------------------------------------------------------------------------*/
  227. int Rast3d__compute_cache_size(RASTER3D_Map * map, int cacheCode)
  228. {
  229. int n, size;
  230. if (cacheCode >= RASTER3D_NO_CACHE)
  231. return cacheCode;
  232. if (cacheCode == RASTER3D_USE_CACHE_DEFAULT)
  233. return RASTER3D_MIN(g3d_cache_default, map->nTiles);
  234. n = -(cacheCode / 10);
  235. n = RASTER3D_MAX(1, n);
  236. cacheCode = -((-cacheCode) % 10);
  237. if (cacheCode == RASTER3D_USE_CACHE_X)
  238. size = map->nx * n;
  239. else if (cacheCode == RASTER3D_USE_CACHE_Y)
  240. size = map->ny * n;
  241. else if (cacheCode == RASTER3D_USE_CACHE_Z)
  242. size = map->nz * n;
  243. else if (cacheCode == RASTER3D_USE_CACHE_XY)
  244. size = map->nxy * n;
  245. else if (cacheCode == RASTER3D_USE_CACHE_XZ)
  246. size = map->nx * map->nz * n;
  247. else if (cacheCode == RASTER3D_USE_CACHE_YZ)
  248. size = map->ny * map->nz * n;
  249. else if (cacheCode == RASTER3D_USE_CACHE_XYZ)
  250. size = map->nTiles;
  251. else
  252. Rast3d_fatal_error("Rast3d__compute_cache_size: invalid cache code");
  253. return RASTER3D_MIN(size, map->nTiles);
  254. }
  255. /*---------------------------------------------------------------------------*/
  256. /* this function does actually more than filling the header fields of the */
  257. /* RASTER3D-Map structure. It also allocates memory for compression and xdr, */
  258. /* and initializes the index and cache. This function should be taken apart. */
  259. int
  260. Rast3d_fill_header(RASTER3D_Map * map, int operation, int compression, int useRle,
  261. int useLzw, int type, int precision, int cache, int hasIndex,
  262. int useXdr, int typeIntern, int nofHeaderBytes, int tileX,
  263. int tileY, int tileZ, int proj, int zone, double north,
  264. double south, double east, double west, double top,
  265. double bottom, int rows, int cols, int depths, double ew_res,
  266. double ns_res, double tb_res, char *unit, int vertical_unit,
  267. int version)
  268. {
  269. if (!RASTER3D_VALID_OPERATION(operation))
  270. Rast3d_fatal_error("Rast3d_fill_header: operation not valid\n");
  271. map->version = version;
  272. map->operation = operation;
  273. map->unit = G_store(unit);
  274. map->vertical_unit = vertical_unit;
  275. map->region.proj = proj;
  276. map->region.zone = zone;
  277. map->region.north = north;
  278. map->region.south = south;
  279. map->region.east = east;
  280. map->region.west = west;
  281. map->region.top = top;
  282. map->region.bottom = bottom;
  283. map->region.rows = rows;
  284. map->region.cols = cols;
  285. map->region.depths = depths;
  286. map->region.ew_res = ew_res;
  287. map->region.ns_res = ns_res;
  288. map->region.tb_res = tb_res;
  289. Rast3d_adjust_region(&(map->region));
  290. map->tileX = tileX;
  291. map->tileY = tileY;
  292. map->tileZ = tileZ;
  293. map->tileXY = map->tileX * map->tileY;
  294. map->tileSize = map->tileXY * map->tileZ;
  295. map->nx = (map->region.cols - 1) / tileX + 1;
  296. map->ny = (map->region.rows - 1) / tileY + 1;
  297. map->nz = (map->region.depths - 1) / tileZ + 1;
  298. map->nxy = map->nx * map->ny;
  299. map->nTiles = map->nxy * map->nz;
  300. if ((map->region.cols) % map->tileX != 0)
  301. map->clipX = map->nx - 1;
  302. else
  303. map->clipX = -1;
  304. if ((map->region.rows) % map->tileY != 0)
  305. map->clipY = map->ny - 1;
  306. else
  307. map->clipY = -1;
  308. if ((map->region.depths) % map->tileZ != 0)
  309. map->clipZ = map->nz - 1;
  310. else
  311. map->clipZ = -1;
  312. if ((type != FCELL_TYPE) && (type != DCELL_TYPE))
  313. Rast3d_fatal_error("Rast3d_fill_header: invalid type");
  314. map->type = type;
  315. if ((typeIntern != FCELL_TYPE) && (typeIntern != DCELL_TYPE))
  316. Rast3d_fatal_error("Rast3d_fill_header: invalid type");
  317. map->typeIntern = typeIntern;
  318. if (!RASTER3D_VALID_XDR_OPTION(useXdr))
  319. Rast3d_fatal_error("Rast3d_fill_header: invalid xdr option");
  320. map->useXdr = useXdr; /* Only kept for backward compatibility */
  321. map->offset = nofHeaderBytes;
  322. if ((map->fileEndPtr = lseek(map->data_fd, (long)0, SEEK_END)) == -1) {
  323. Rast3d_error("Rast3d_fill_header: can't position file");
  324. return 0;
  325. }
  326. map->useCache = (cache != RASTER3D_NO_CACHE);
  327. map->numLengthIntern = Rast3d_length(map->typeIntern);
  328. map->numLengthExtern = Rast3d_extern_length(map->type);
  329. map->compression = compression;
  330. map->useRle = useRle; /* Only kept for backward compatibility */
  331. map->useLzw = useLzw; /* Only kept for backward compatibility */
  332. map->precision = precision;
  333. #define RLE_STATUS_BYTES 2
  334. if (map->compression != RASTER3D_NO_COMPRESSION) {
  335. if (tmpCompress == NULL) {
  336. tmpCompressLength = map->tileSize *
  337. RASTER3D_MAX(map->numLengthIntern, map->numLengthExtern) +
  338. RLE_STATUS_BYTES;
  339. tmpCompress = Rast3d_malloc(tmpCompressLength);
  340. if (tmpCompress == NULL) {
  341. Rast3d_error("Rast3d_fill_header: error in Rast3d_malloc");
  342. return 0;
  343. }
  344. }
  345. else if (map->tileSize *
  346. RASTER3D_MAX(map->numLengthIntern, map->numLengthExtern)
  347. + RLE_STATUS_BYTES > tmpCompressLength) {
  348. tmpCompressLength = map->tileSize *
  349. RASTER3D_MAX(map->numLengthIntern, map->numLengthExtern) +
  350. RLE_STATUS_BYTES;
  351. tmpCompress = Rast3d_realloc(tmpCompress, tmpCompressLength);
  352. if (tmpCompress == NULL) {
  353. Rast3d_error("Rast3d_fill_header: error in Rast3d_realloc");
  354. return 0;
  355. }
  356. }
  357. }
  358. #define XDR_MISUSE_BYTES 10
  359. if (!Rast3d_init_fp_xdr(map, XDR_MISUSE_BYTES)) {
  360. Rast3d_error("Rast3d_fill_header: error in Rast3d_init_fp_xdr");
  361. return 0;
  362. }
  363. if ((!map->useCache) ||
  364. ((cache == RASTER3D_USE_CACHE_DEFAULT) && (g3d_cache_default == 0))) {
  365. map->useCache = 0;
  366. map->cache = NULL;
  367. /* allocate one tile buffer */
  368. map->data = Rast3d_malloc(map->tileSize * map->numLengthIntern);
  369. if (map->data == NULL) {
  370. Rast3d_error("Rast3d_fill_header: error in Rast3d_malloc");
  371. return 0;
  372. }
  373. map->currentIndex = -1;
  374. }
  375. else {
  376. if (!Rast3d_init_cache(map,
  377. RASTER3D_MAX(1,
  378. RASTER3D_MIN(Rast3d__compute_cache_size(map, cache),
  379. g3d_cache_max /
  380. map->tileSize /
  381. map->numLengthIntern)))) {
  382. Rast3d_error("Rast3d_fill_header: error in Rast3d_init_cache");
  383. return 0;
  384. }
  385. }
  386. if (!Rast3d_init_index(map, hasIndex)) {
  387. Rast3d_error("Rast3d_fill_header: error in Rast3d_init_index");
  388. return 0;
  389. }
  390. return 1;
  391. }