header.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include <rpc/types.h>
  6. #include <rpc/xdr.h>
  7. #include <grass/raster3d.h>
  8. #include "raster3d_intern.h"
  9. /*---------------------------------------------------------------------------*/
  10. void *tmpCompress;
  11. int tmpCompressLength;
  12. void *xdr;
  13. int xdrLength;
  14. /*---------------------------------------------------------------------------*/
  15. #define G3D_HEADER_TILEX "TileDimensionX"
  16. #define G3D_HEADER_TILEY "TileDimensionY"
  17. #define G3D_HEADER_TILEZ "TileDimensionZ"
  18. #define G3D_HEADER_TYPE "CellType"
  19. #define G3D_HEADER_COMPRESSION "useCompression"
  20. #define G3D_HEADER_USERLE "useRle"
  21. #define G3D_HEADER_USELZW "useLzw"
  22. #define G3D_HEADER_PRECISION "Precision"
  23. #define G3D_HEADER_DATA_OFFSET "nofHeaderBytes"
  24. #define G3D_HEADER_USEXDR "useXdr"
  25. #define G3D_HEADER_HASINDEX "hasIndex"
  26. #define G3D_HEADER_UNIT "Units"
  27. /*---------------------------------------------------------------------------*/
  28. static int
  29. G3d_readWriteHeader(struct Key_Value *headerKeys, int doRead, int *proj,
  30. int *zone, double *north, double *south, double *east,
  31. double *west, double *top, double *bottom, int *rows,
  32. int *cols, int *depths, double *ew_res, double *ns_res,
  33. double *tb_res, int *tileX, int *tileY, int *tileZ,
  34. int *type, int *compression, int *useRle, int *useLzw,
  35. int *precision, int *dataOffset, int *useXdr,
  36. int *hasIndex, char **unit)
  37. {
  38. int returnVal;
  39. int (*headerInt) (), (*headerDouble) (), (*headerValue) ();
  40. int (*headerString) ();
  41. if (doRead) {
  42. headerDouble = G3d_keyGetDouble;
  43. headerInt = G3d_keyGetInt;
  44. headerString = G3d_keyGetString;
  45. headerValue = G3d_keyGetValue;
  46. }
  47. else {
  48. headerDouble = G3d_keySetDouble;
  49. headerInt = G3d_keySetInt;
  50. headerString = G3d_keySetString;
  51. headerValue = G3d_keySetValue;
  52. }
  53. returnVal = 1;
  54. returnVal &= headerInt(headerKeys, G3D_REGION_PROJ, proj);
  55. returnVal &= headerInt(headerKeys, G3D_REGION_ZONE, zone);
  56. returnVal &= headerDouble(headerKeys, G3D_REGION_NORTH, north);
  57. returnVal &= headerDouble(headerKeys, G3D_REGION_SOUTH, south);
  58. returnVal &= headerDouble(headerKeys, G3D_REGION_EAST, east);
  59. returnVal &= headerDouble(headerKeys, G3D_REGION_WEST, west);
  60. returnVal &= headerDouble(headerKeys, G3D_REGION_TOP, top);
  61. returnVal &= headerDouble(headerKeys, G3D_REGION_BOTTOM, bottom);
  62. returnVal &= headerInt(headerKeys, G3D_REGION_ROWS, rows);
  63. returnVal &= headerInt(headerKeys, G3D_REGION_COLS, cols);
  64. returnVal &= headerInt(headerKeys, G3D_REGION_DEPTHS, depths);
  65. returnVal &= headerDouble(headerKeys, G3D_REGION_NSRES, ns_res);
  66. returnVal &= headerDouble(headerKeys, G3D_REGION_EWRES, ew_res);
  67. returnVal &= headerDouble(headerKeys, G3D_REGION_TBRES, tb_res);
  68. returnVal &= headerInt(headerKeys, G3D_HEADER_TILEX, tileX);
  69. returnVal &= headerInt(headerKeys, G3D_HEADER_TILEY, tileY);
  70. returnVal &= headerInt(headerKeys, G3D_HEADER_TILEZ, tileZ);
  71. returnVal &= headerValue(headerKeys, G3D_HEADER_TYPE,
  72. "double", "float", DCELL_TYPE, FCELL_TYPE, type);
  73. returnVal &= headerValue(headerKeys, G3D_HEADER_COMPRESSION,
  74. "0", "1", 0, 1, compression);
  75. returnVal &= headerValue(headerKeys, G3D_HEADER_USERLE,
  76. "0", "1", 0, 1, useRle);
  77. returnVal &= headerValue(headerKeys, G3D_HEADER_USELZW,
  78. "0", "1", 0, 1, useLzw);
  79. returnVal &= headerInt(headerKeys, G3D_HEADER_PRECISION, precision);
  80. returnVal &= headerInt(headerKeys, G3D_HEADER_DATA_OFFSET, dataOffset);
  81. returnVal &= headerValue(headerKeys, G3D_HEADER_USEXDR,
  82. "0", "1", 0, 1, useXdr);
  83. returnVal &= headerValue(headerKeys, G3D_HEADER_HASINDEX,
  84. "0", "1", 0, 1, hasIndex);
  85. returnVal &= headerString(headerKeys, G3D_HEADER_UNIT, unit);
  86. if (returnVal)
  87. return 1;
  88. G3d_error("G3d_readWriteHeader: error writing header");
  89. return 0;
  90. }
  91. /*---------------------------------------------------------------------------*/
  92. int
  93. G3d_readHeader(G3D_Map * map, int *proj, int *zone, double *north,
  94. double *south, double *east, double *west, double *top,
  95. double *bottom, int *rows, int *cols, int *depths,
  96. double *ew_res, double *ns_res, double *tb_res, int *tileX,
  97. int *tileY, int *tileZ, int *type, int *compression,
  98. int *useRle, int *useLzw, int *precision, int *dataOffset,
  99. int *useXdr, int *hasIndex, char **unit)
  100. {
  101. struct Key_Value *headerKeys;
  102. char path[GPATH_MAX];
  103. G3d_filename(path, G3D_HEADER_ELEMENT, map->fileName, map->mapset);
  104. if (access(path, R_OK) != 0) {
  105. G3d_error("G3d_readHeader: unable to find [%s]", path);
  106. return 0;
  107. }
  108. headerKeys = G_read_key_value_file(path);
  109. if (!G3d_readWriteHeader(headerKeys, 1,
  110. proj, zone,
  111. north, south, east, west, top, bottom,
  112. rows, cols, depths,
  113. ew_res, ns_res, tb_res,
  114. tileX, tileY, tileZ,
  115. type, compression, useRle, useLzw, precision,
  116. dataOffset, useXdr, hasIndex, unit)) {
  117. G3d_error("G3d_readHeader: error extracting header key(s) of file %s",
  118. path);
  119. return 0;
  120. }
  121. G_free_key_value(headerKeys);
  122. return 1;
  123. }
  124. /*---------------------------------------------------------------------------*/
  125. int
  126. G3d_writeHeader(G3D_Map * map, int proj, int zone, double north, double south,
  127. double east, double west, double top, double bottom, int rows,
  128. int cols, int depths, double ew_res, double ns_res,
  129. double tb_res, int tileX, int tileY, int tileZ, int type,
  130. int compression, int useRle, int useLzw, int precision,
  131. int dataOffset, int useXdr, int hasIndex, char *unit)
  132. {
  133. struct Key_Value *headerKeys;
  134. char path[GPATH_MAX];
  135. headerKeys = G_create_key_value();
  136. if (!G3d_readWriteHeader(headerKeys, 0,
  137. &proj, &zone,
  138. &north, &south, &east, &west, &top, &bottom,
  139. &rows, &cols, &depths,
  140. &ew_res, &ns_res, &tb_res,
  141. &tileX, &tileY, &tileZ,
  142. &type, &compression, &useRle, &useLzw,
  143. &precision, &dataOffset, &useXdr, &hasIndex,
  144. &unit)) {
  145. G3d_error("G3d_writeHeader: error adding header key(s) for file %s",
  146. path);
  147. return 0;
  148. }
  149. G3d_filename(path, G3D_HEADER_ELEMENT, map->fileName, map->mapset);
  150. G3d_makeMapsetMapDirectory(map->fileName);
  151. G_write_key_value_file(path, headerKeys);
  152. G_free_key_value(headerKeys);
  153. return 1;
  154. }
  155. /*---------------------------------------------------------------------------*/
  156. /*!
  157. * \brief
  158. *
  159. * Returns a number
  160. * which encodes multiplicity <em>n</em> of <em>cacheCode</em>. This value can be used
  161. * to specify the size of the cache.
  162. * If <em>cacheCode</em> is the size (in tiles) of the cache the function returns
  163. * <em>cacheCode * n</em>.
  164. * If <em>cacheCode</em> is G3D_USE_CACHE_DEFAULT the function returns
  165. * G3D_USE_CACHE_DEFAULT.
  166. * If <em>cacheCode</em> is G3D_USE_CACHE_??? the function returns a value
  167. * encoding G3D_USE_CACHE_??? and <em>n</em>. Here G3D_USE_CACHE_??? is one
  168. * of G3D_USE_CACHE_X, G3D_USE_CACHE_Y, G3D_USE_CACHE_Z,
  169. * G3D_USE_CACHE_XY, G3D_USE_CACHE_XZ, G3D_USE_CACHE_YZ, or
  170. * G3D_USE_CACHE_XYZ, where e.g. G3D_USE_CACHE_X specifies that the cache
  171. * should store as many tiles as there exist in one row along the x-axis of the
  172. * tile cube, and G3D_USE_CACHE_XY specifies that the cache should store as
  173. * many tiles as there exist in one slice of the tile cube with constant Z
  174. * coordinate.
  175. *
  176. * \param cacheCode
  177. * \param n
  178. * \return int
  179. */
  180. int G3d_cacheSizeEncode(int cacheCode, int n)
  181. {
  182. if (cacheCode >= G3D_NO_CACHE)
  183. return cacheCode * n;
  184. if (cacheCode == G3D_USE_CACHE_DEFAULT)
  185. return cacheCode;
  186. if (cacheCode < G3D_USE_CACHE_XYZ)
  187. G3d_fatalError("G3d_cacheSizeEncode: invalid cache code");
  188. return n * (-10) + cacheCode;
  189. }
  190. /*---------------------------------------------------------------------------*/
  191. int G3d__computeCacheSize(G3D_Map * map, int cacheCode)
  192. {
  193. int n, size;
  194. if (cacheCode >= G3D_NO_CACHE)
  195. return cacheCode;
  196. if (cacheCode == G3D_USE_CACHE_DEFAULT)
  197. return G3D_MIN(g3d_cache_default, map->nTiles);
  198. n = -(cacheCode / 10);
  199. n = G3D_MAX(1, n);
  200. cacheCode = -((-cacheCode) % 10);
  201. if (cacheCode == G3D_USE_CACHE_X)
  202. size = map->nx * n;
  203. else if (cacheCode == G3D_USE_CACHE_Y)
  204. size = map->ny * n;
  205. else if (cacheCode == G3D_USE_CACHE_Z)
  206. size = map->nz * n;
  207. else if (cacheCode == G3D_USE_CACHE_XY)
  208. size = map->nxy * n;
  209. else if (cacheCode == G3D_USE_CACHE_XZ)
  210. size = map->nx * map->nz * n;
  211. else if (cacheCode == G3D_USE_CACHE_YZ)
  212. size = map->ny * map->nz * n;
  213. else if (cacheCode == G3D_USE_CACHE_XYZ)
  214. size = map->nTiles;
  215. else
  216. G3d_fatalError("G3d__computeCacheSize: invalid cache code");
  217. return G3D_MIN(size, map->nTiles);
  218. }
  219. /*---------------------------------------------------------------------------*/
  220. /* this function does actually more than filling the header fields of the */
  221. /* G3D-Map structure. It also allocates memory for compression and xdr, */
  222. /* and initializes the index and cache. This function should be taken apart. */
  223. int
  224. G3d_fillHeader(G3D_Map * map, int operation, int compression, int useRle,
  225. int useLzw, int type, int precision, int cache, int hasIndex,
  226. int useXdr, int typeIntern, int nofHeaderBytes, int tileX,
  227. int tileY, int tileZ, int proj, int zone, double north,
  228. double south, double east, double west, double top,
  229. double bottom, int rows, int cols, int depths, double ew_res,
  230. double ns_res, double tb_res, char *unit)
  231. {
  232. if (!G3D_VALID_OPERATION(operation))
  233. G3d_fatalError("G3d_fillHeader: operation not valid\n");
  234. map->operation = operation;
  235. map->unit = G_store(unit);
  236. map->region.proj = proj;
  237. map->region.zone = zone;
  238. map->region.north = north;
  239. map->region.south = south;
  240. map->region.east = east;
  241. map->region.west = west;
  242. map->region.top = top;
  243. map->region.bottom = bottom;
  244. map->region.rows = rows;
  245. map->region.cols = cols;
  246. map->region.depths = depths;
  247. map->region.ew_res = ew_res;
  248. map->region.ns_res = ns_res;
  249. map->region.tb_res = tb_res;
  250. G3d_adjustRegion(&(map->region));
  251. map->tileX = tileX;
  252. map->tileY = tileY;
  253. map->tileZ = tileZ;
  254. map->tileXY = map->tileX * map->tileY;
  255. map->tileSize = map->tileXY * map->tileZ;
  256. map->nx = (map->region.cols - 1) / tileX + 1;
  257. map->ny = (map->region.rows - 1) / tileY + 1;
  258. map->nz = (map->region.depths - 1) / tileZ + 1;
  259. map->nxy = map->nx * map->ny;
  260. map->nTiles = map->nxy * map->nz;
  261. if ((map->region.cols) % map->tileX != 0)
  262. map->clipX = map->nx - 1;
  263. else
  264. map->clipX = -1;
  265. if ((map->region.rows) % map->tileY != 0)
  266. map->clipY = map->ny - 1;
  267. else
  268. map->clipY = -1;
  269. if ((map->region.depths) % map->tileZ != 0)
  270. map->clipZ = map->nz - 1;
  271. else
  272. map->clipZ = -1;
  273. if ((type != FCELL_TYPE) && (type != DCELL_TYPE))
  274. G3d_fatalError("G3d_fillHeader: invalid type");
  275. map->type = type;
  276. if ((typeIntern != FCELL_TYPE) && (typeIntern != DCELL_TYPE))
  277. G3d_fatalError("G3d_fillHeader: invalid type");
  278. map->typeIntern = typeIntern;
  279. if (!G3D_VALID_XDR_OPTION(useXdr))
  280. G3d_fatalError("G3d_fillHeader: invalid xdr option");
  281. map->useXdr = useXdr;
  282. map->offset = nofHeaderBytes;
  283. if ((map->fileEndPtr = lseek(map->data_fd, (long)0, SEEK_END)) == -1) {
  284. G3d_error("G3d_fillHeader: can't position file");
  285. return 0;
  286. }
  287. map->useCache = (cache != G3D_NO_CACHE);
  288. map->numLengthIntern = G3d_length(map->typeIntern);
  289. map->numLengthExtern = G3d_externLength(map->type);
  290. map->compression = compression;
  291. map->useRle = useRle;
  292. map->useLzw = useLzw;
  293. map->precision = precision;
  294. #define RLE_STATUS_BYTES 2
  295. if (map->compression != G3D_NO_COMPRESSION) {
  296. if (tmpCompress == NULL) {
  297. tmpCompressLength = map->tileSize *
  298. G3D_MAX(map->numLengthIntern, map->numLengthExtern) +
  299. RLE_STATUS_BYTES;
  300. tmpCompress = G3d_malloc(tmpCompressLength);
  301. if (tmpCompress == NULL) {
  302. G3d_error("G3d_fillHeader: error in G3d_malloc");
  303. return 0;
  304. }
  305. }
  306. else if (map->tileSize *
  307. G3D_MAX(map->numLengthIntern, map->numLengthExtern)
  308. + RLE_STATUS_BYTES > tmpCompressLength) {
  309. tmpCompressLength = map->tileSize *
  310. G3D_MAX(map->numLengthIntern, map->numLengthExtern) +
  311. RLE_STATUS_BYTES;
  312. tmpCompress = G3d_realloc(tmpCompress, tmpCompressLength);
  313. if (tmpCompress == NULL) {
  314. G3d_error("G3d_fillHeader: error in G3d_realloc");
  315. return 0;
  316. }
  317. }
  318. }
  319. #define XDR_MISUSE_BYTES 10
  320. if (!G3d_initFpXdr(map, XDR_MISUSE_BYTES)) {
  321. G3d_error("G3d_fillHeader: error in G3d_initFpXdr");
  322. return 0;
  323. }
  324. if ((!map->useCache) ||
  325. ((cache == G3D_USE_CACHE_DEFAULT) && (g3d_cache_default == 0))) {
  326. map->useCache = 0;
  327. map->cache = NULL;
  328. /* allocate one tile buffer */
  329. map->data = G3d_malloc(map->tileSize * map->numLengthIntern);
  330. if (map->data == NULL) {
  331. G3d_error("G3d_fillHeader: error in G3d_malloc");
  332. return 0;
  333. }
  334. map->currentIndex = -1;
  335. }
  336. else {
  337. if (!G3d_initCache(map,
  338. G3D_MAX(1,
  339. G3D_MIN(G3d__computeCacheSize(map, cache),
  340. g3d_cache_max /
  341. map->tileSize /
  342. map->numLengthIntern)))) {
  343. G3d_error("G3d_fillHeader: error in G3d_initCache");
  344. return 0;
  345. }
  346. }
  347. if (!G3d_initIndex(map, hasIndex)) {
  348. G3d_error("G3d_fillHeader: error in G3d_initIndex");
  349. return 0;
  350. }
  351. return 1;
  352. }