g3dopen.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <fcntl.h>
  4. #include <sys/types.h>
  5. #include <unistd.h>
  6. #include <grass/G3d.h>
  7. #include <grass/glocale.h>
  8. #include "G3d_intern.h"
  9. /*---------------------------------------------------------------------------*/
  10. void *G3d_openCellOldNoHeader(const char *name, const char *mapset)
  11. {
  12. G3D_Map *map;
  13. char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
  14. G3d_initDefaults();
  15. if (!G3d_maskOpenOld()) {
  16. G3d_error(_("G3d_openCellOldNoHeader: error in G3d_maskOpenOld"));
  17. return (void *)NULL;
  18. }
  19. map = G3d_malloc(sizeof(G3D_Map));
  20. if (map == NULL) {
  21. G3d_error(_("G3d_openCellOldNoHeader: error in G3d_malloc"));
  22. return (void *)NULL;
  23. }
  24. G_unqualified_name(name, mapset, xname, xmapset);
  25. map->fileName = G_store(xname);
  26. map->mapset = G_store(xmapset);
  27. map->data_fd = G_open_old_misc(G3D_DIRECTORY, G3D_CELL_ELEMENT, xname, xmapset);
  28. if (map->data_fd < 0) {
  29. G3d_error(_("G3d_openCellOldNoHeader: error in G_open_old"));
  30. return (void *)NULL;
  31. }
  32. G3d_range_init(map);
  33. G3d_maskOff(map);
  34. return map;
  35. }
  36. /*---------------------------------------------------------------------------*/
  37. /*!
  38. * \brief
  39. *
  40. * Opens existing g3d-file <em>name</em> in <em>mapset</em>.
  41. * Tiles are stored in memory with <em>type</em> which must be any of FCELL_TYPE,
  42. * DCELL_TYPE, or G3D_TILE_SAME_AS_FILE. <em>cache</em> specifies the
  43. * cache-mode used and must be either G3D_NO_CACHE, G3D_USE_CACHE_DEFAULT,
  44. * G3D_USE_CACHE_X, G3D_USE_CACHE_Y, G3D_USE_CACHE_Z,
  45. * G3D_USE_CACHE_XY, G3D_USE_CACHE_XZ, G3D_USE_CACHE_YZ,
  46. * G3D_USE_CACHE_XYZ, the result of <tt>G3d_cacheSizeEncode ()</tt> (cf.{g3d:G3d.cacheSizeEncode}), or any positive integer which
  47. * specifies the number of tiles buffered in the cache. <em>window</em> sets the
  48. * window-region for the map. It is either a pointer to a window structure or
  49. * G3D_DEFAULT_WINDOW, which uses the window stored at initialization time or
  50. * set via <tt>G3d_setWindow ()</tt> (cf.{g3d:G3d.setWindow}).
  51. * To modify the window for the map after it has already been opened use
  52. * <tt>G3d_setWindowMap ()</tt> (cf.{g3d:G3d.setWindowMap}).
  53. * Returns a pointer to the cell structure ... if successful, NULL ...
  54. * otherwise.
  55. *
  56. * \param name
  57. * \param mapset
  58. * \param window
  59. * \param type
  60. * \param cache
  61. * \return void *
  62. */
  63. void *G3d_openCellOld(const char *name, const char *mapset,
  64. G3D_Region * window, int typeIntern, int cache)
  65. {
  66. G3D_Map *map;
  67. int proj, zone;
  68. int compression, useRle, useLzw, type, tileX, tileY, tileZ;
  69. int rows, cols, depths, precision;
  70. double ew_res, ns_res, tb_res;
  71. int nofHeaderBytes, dataOffset, useXdr, hasIndex;
  72. char *ltmp, *unit;
  73. double north, south, east, west, top, bottom;
  74. map = G3d_openCellOldNoHeader(name, mapset);
  75. if (map == NULL) {
  76. G3d_error(_("G3d_openCellOld: error in G3d_openCellOldNoHeader"));
  77. return (void *)NULL;
  78. }
  79. if (lseek(map->data_fd, (long)0, SEEK_SET) == -1) {
  80. G3d_error(_("G3d_openCellOld: can't rewind file"));
  81. return (void *)NULL;
  82. }
  83. if (!G3d_readHeader(map,
  84. &proj, &zone,
  85. &north, &south, &east, &west, &top, &bottom,
  86. &rows, &cols, &depths,
  87. &ew_res, &ns_res, &tb_res,
  88. &tileX, &tileY, &tileZ,
  89. &type, &compression, &useRle, &useLzw,
  90. &precision, &dataOffset, &useXdr, &hasIndex, &unit)) {
  91. G3d_error(_("G3d_openCellOld: error in G3d_readHeader"));
  92. return 0;
  93. }
  94. if (window == G3D_DEFAULT_WINDOW)
  95. window = G3d_windowPtr();
  96. if (proj != window->proj) {
  97. G3d_error(_("G3d_openCellOld: projection does not match window projection"));
  98. return (void *)NULL;
  99. }
  100. if (zone != window->zone) {
  101. G3d_error(_("G3d_openCellOld: zone does not match window zone"));
  102. return (void *)NULL;
  103. }
  104. map->useXdr = useXdr;
  105. if (hasIndex) {
  106. /* see G3D_openCell_new () for format of header */
  107. if ((!G3d_readInts(map->data_fd, map->useXdr,
  108. &(map->indexLongNbytes), 1)) ||
  109. (!G3d_readInts(map->data_fd, map->useXdr,
  110. &(map->indexNbytesUsed), 1))) {
  111. G3d_error(_("G3d_openCellOld: can't read header"));
  112. return (void *)NULL;
  113. }
  114. /* if our long is to short to store offsets we can't read the file */
  115. if (map->indexNbytesUsed > sizeof(long))
  116. G3d_fatalError(_("G3d_openCellOld: index does not fit into long"));
  117. ltmp = G3d_malloc(map->indexLongNbytes);
  118. if (ltmp == NULL) {
  119. G3d_error(_("G3d_openCellOld: error in G3d_malloc"));
  120. return (void *)NULL;
  121. }
  122. /* convert file long to long */
  123. if (read(map->data_fd, ltmp, map->indexLongNbytes) !=
  124. map->indexLongNbytes) {
  125. G3d_error(_("G3d_openCellOld: can't read header"));
  126. return (void *)NULL;
  127. }
  128. G3d_longDecode(ltmp, &(map->indexOffset), 1, map->indexLongNbytes);
  129. G3d_free(ltmp);
  130. }
  131. nofHeaderBytes = dataOffset;
  132. if (typeIntern == G3D_TILE_SAME_AS_FILE)
  133. typeIntern = type;
  134. if (!G3d_fillHeader(map, G3D_READ_DATA, compression, useRle, useLzw,
  135. type, precision, cache,
  136. hasIndex, map->useXdr, typeIntern,
  137. nofHeaderBytes, tileX, tileY, tileZ,
  138. proj, zone,
  139. north, south, east, west, top, bottom,
  140. rows, cols, depths, ew_res, ns_res, tb_res, unit)) {
  141. G3d_error(_("G3d_openCellOld: error in G3d_fillHeader"));
  142. return (void *)NULL;
  143. }
  144. G3d_regionCopy(&(map->window), window);
  145. G3d_adjustRegion(&(map->window));
  146. G3d_getNearestNeighborFunPtr(&(map->resampleFun));
  147. return map;
  148. }
  149. /*---------------------------------------------------------------------------*/
  150. /*!
  151. * \brief
  152. *
  153. * Opens new g3d-file with <em>name</em> in the current mapset. Tiles
  154. * are stored in memory with <em>type</em> which must be one of FCELL_TYPE,
  155. * DCELL_TYPE, or G3D_TILE_SAME_AS_FILE. <em>cache</em> specifies the
  156. * cache-mode used and must be either G3D_NO_CACHE, G3D_USE_CACHE_DEFAULT,
  157. * G3D_USE_CACHE_X, G3D_USE_CACHE_Y, G3D_USE_CACHE_Z,
  158. * G3D_USE_CACHE_XY, G3D_USE_CACHE_XZ, G3D_USE_CACHE_YZ,
  159. * G3D_USE_CACHE_XYZ, the result of <tt>G3d_cacheSizeEncode ()</tt>
  160. * (cf.{g3d:G3d.cacheSizeEncode}), or any positive integer which
  161. * specifies the number of tiles buffered in the cache. <em>region</em> specifies
  162. * the 3d region.
  163. * Returns a pointer to the cell structure ... if successful,
  164. * NULL ... otherwise.
  165. *
  166. * \param name
  167. * \param type
  168. * \param cache
  169. * \param region
  170. * \return void *
  171. */
  172. void *G3d_openCellNew(const char *name, int typeIntern, int cache,
  173. G3D_Region * region)
  174. {
  175. G3D_Map *map;
  176. int nofHeaderBytes, dummy = 0, compression, precision;
  177. long ldummy = 0;
  178. char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
  179. G3d_initDefaults();
  180. if (!G3d_maskOpenOld()) {
  181. G3d_error(_("G3d_openCellNew: error in G3d_maskOpenOld"));
  182. return (void *)NULL;
  183. }
  184. compression = g3d_do_compression;
  185. precision = g3d_precision;
  186. map = G3d_malloc(sizeof(G3D_Map));
  187. if (map == NULL) {
  188. G3d_error(_("G3d_openCellNew: error in G3d_malloc"));
  189. return (void *)NULL;
  190. }
  191. if (G_unqualified_name(name, G_mapset(), xname, xmapset) < 0) {
  192. G_warning(_("map <%s> is not in the current mapset"), name);
  193. return (void *)NULL;
  194. }
  195. map->fileName = G_store(xname);
  196. map->mapset = G_store(xmapset);
  197. map->tempName = G_tempfile();
  198. map->data_fd = open(map->tempName, O_RDWR | O_CREAT | O_TRUNC, 0666);
  199. if (map->data_fd < 0) {
  200. G3d_error(_("G3d_openCellNew: could not open file"));
  201. return (void *)NULL;
  202. }
  203. G3d_makeMapsetMapDirectory(map->fileName);
  204. map->useXdr = G3D_USE_XDR;
  205. if (g3d_file_type == FCELL_TYPE) {
  206. if (precision > 23)
  207. precision = 23; /* 32 - 8 - 1 */
  208. else if (precision < -1)
  209. precision = 0;
  210. }
  211. else if (precision > 52)
  212. precision = 52; /* 64 - 11 - 1 */
  213. else if (precision < -1)
  214. precision = 0;
  215. /* no need to write trailing zeros */
  216. if ((typeIntern == FCELL_TYPE) && (g3d_file_type == DCELL_TYPE)) {
  217. if (precision == -1)
  218. precision = 23;
  219. else
  220. precision = G3D_MIN(precision, 23);
  221. }
  222. if (compression == G3D_NO_COMPRESSION)
  223. precision = G3D_MAX_PRECISION;
  224. if (compression == G3D_COMPRESSION)
  225. map->useXdr = G3D_USE_XDR;
  226. if (G3D_HAS_INDEX) {
  227. map->indexLongNbytes = sizeof(long);
  228. /* at the beginning of the file write */
  229. /* nof bytes of "long" */
  230. /* max nof bytes used for index */
  231. /* position of index in file */
  232. /* the index is appended at the end of the file at closing time. since */
  233. /* we do not know this position yet we write dummy values */
  234. if ((!G3d_writeInts(map->data_fd, map->useXdr,
  235. &(map->indexLongNbytes), 1)) ||
  236. (!G3d_writeInts(map->data_fd, map->useXdr, &dummy, 1))) {
  237. G3d_error(_("G3d_openCellNew: can't write header"));
  238. return (void *)NULL;
  239. }
  240. if (write(map->data_fd, &ldummy, map->indexLongNbytes) !=
  241. map->indexLongNbytes) {
  242. G3d_error(_("G3d_openCellNew: can't write header"));
  243. return (void *)NULL;
  244. }
  245. }
  246. /* can't use a constant since this depends on sizeof (long) */
  247. nofHeaderBytes = lseek(map->data_fd, (long)0, SEEK_CUR);
  248. G3d_range_init(map);
  249. G3d_adjustRegion(region);
  250. if (!G3d_fillHeader(map, G3D_WRITE_DATA, compression,
  251. g3d_do_rle_compression, g3d_do_lzw_compression,
  252. g3d_file_type, precision, cache, G3D_HAS_INDEX,
  253. map->useXdr, typeIntern, nofHeaderBytes,
  254. g3d_tile_dimension[0], g3d_tile_dimension[1],
  255. g3d_tile_dimension[2],
  256. region->proj, region->zone,
  257. region->north, region->south, region->east,
  258. region->west, region->top, region->bottom,
  259. region->rows, region->cols, region->depths,
  260. region->ew_res, region->ns_res, region->tb_res,
  261. g3d_unit_default)) {
  262. G3d_error(_("G3d_openCellNew: error in G3d_fillHeader"));
  263. return (void *)NULL;
  264. }
  265. /*Set the map window to the map region */
  266. G3d_regionCopy(&(map->window), region);
  267. /*Set the resampling function to nearest neighbor for data access */
  268. G3d_getNearestNeighborFunPtr(&(map->resampleFun));
  269. G3d_maskOff(map);
  270. return (void *)map;
  271. }