19 #define G3D_HEADER_TILEX "TileDimensionX"
20 #define G3D_HEADER_TILEY "TileDimensionY"
21 #define G3D_HEADER_TILEZ "TileDimensionZ"
22 #define G3D_HEADER_TYPE "CellType"
23 #define G3D_HEADER_COMPRESSION "useCompression"
24 #define G3D_HEADER_USERLE "useRle"
25 #define G3D_HEADER_USELZW "useLzw"
26 #define G3D_HEADER_PRECISION "Precision"
27 #define G3D_HEADER_DATA_OFFSET "nofHeaderBytes"
28 #define G3D_HEADER_USEXDR "useXdr"
29 #define G3D_HEADER_HASINDEX "hasIndex"
30 #define G3D_HEADER_UNIT "Units"
35 G3d_readWriteHeader(
struct Key_Value *headerKeys,
int doRead,
int *proj,
36 int *zone,
double *north,
double *south,
double *east,
37 double *west,
double *top,
double *bottom,
int *rows,
38 int *
cols,
int *depths,
double *ew_res,
double *ns_res,
39 double *tb_res,
int *tileX,
int *tileY,
int *tileZ,
41 int *precision,
int *dataOffset,
int *useXdr,
42 int *hasIndex,
char **
unit)
45 int (*headerInt) (), (*headerDouble) (), (*headerValue) ();
46 int (*headerString) ();
85 "double",
"float", DCELL_TYPE, FCELL_TYPE, type);
87 "0",
"1", 0, 1, compression);
89 "0",
"1", 0, 1, useRle);
91 "0",
"1", 0, 1, useLzw);
97 "0",
"1", 0, 1, useXdr);
99 "0",
"1", 0, 1, hasIndex);
105 G3d_error(
"G3d_readWriteHeader: error writing header");
113 double *south,
double *east,
double *west,
double *top,
114 double *bottom,
int *rows,
int *
cols,
int *depths,
115 double *ew_res,
double *ns_res,
double *tb_res,
int *tileX,
117 int *useRle,
int *useLzw,
int *precision,
int *dataOffset,
118 int *useXdr,
int *hasIndex,
char **
unit)
120 struct Key_Value *headerKeys;
121 char path[GPATH_MAX];
124 G3d_filename(path, G3D_HEADER_ELEMENT, map->fileName, map->mapset);
125 if (access(path,
R_OK) != 0) {
126 G3d_error(
"G3d_readHeader: unable to find [%s]", path);
132 G3d_error(
"G3d_readHeader: Unable to open %s", path);
136 if (!G3d_readWriteHeader(headerKeys, 1,
138 north, south, east, west, top, bottom,
140 ew_res, ns_res, tb_res,
142 type, compression, useRle, useLzw, precision,
143 dataOffset, useXdr, hasIndex, unit)) {
144 G3d_error(
"G3d_readHeader: error extracting header key(s) of file %s",
157 double east,
double west,
double top,
double bottom,
int rows,
158 int cols,
int depths,
double ew_res,
double ns_res,
159 double tb_res,
int tileX,
int tileY,
int tileZ,
int type,
160 int compression,
int useRle,
int useLzw,
int precision,
161 int dataOffset,
int useXdr,
int hasIndex,
char *
unit)
163 struct Key_Value *headerKeys;
164 char path[GPATH_MAX];
169 if (!G3d_readWriteHeader(headerKeys, 0,
171 &north, &south, &east, &west, &top, &bottom,
172 &rows, &cols, &depths,
173 &ew_res, &ns_res, &tb_res,
174 &tileX, &tileY, &tileZ,
175 &type, &compression, &useRle, &useLzw,
176 &precision, &dataOffset, &useXdr, &hasIndex,
178 G3d_error(
"G3d_writeHeader: error adding header key(s) for file %s",
183 G3d_filename(path, G3D_HEADER_ELEMENT, map->fileName, map->mapset);
192 G3d_error(
"G3d_writeHeader: error writing header file %s", path);
226 if (cacheCode >= G3D_NO_CACHE)
227 return cacheCode * n;
228 if (cacheCode == G3D_USE_CACHE_DEFAULT)
231 if (cacheCode < G3D_USE_CACHE_XYZ)
234 return n * (-10) + cacheCode;
243 if (cacheCode >= G3D_NO_CACHE)
245 if (cacheCode == G3D_USE_CACHE_DEFAULT)
248 n = -(cacheCode / 10);
250 cacheCode = -((-cacheCode) % 10);
252 if (cacheCode == G3D_USE_CACHE_X)
254 else if (cacheCode == G3D_USE_CACHE_Y)
256 else if (cacheCode == G3D_USE_CACHE_Z)
258 else if (cacheCode == G3D_USE_CACHE_XY)
260 else if (cacheCode == G3D_USE_CACHE_XZ)
261 size = map->nx * map->nz * n;
262 else if (cacheCode == G3D_USE_CACHE_YZ)
263 size = map->ny * map->nz * n;
264 else if (cacheCode == G3D_USE_CACHE_XYZ)
269 return G3D_MIN(size, map->nTiles);
280 int useLzw,
int type,
int precision,
int cache,
int hasIndex,
281 int useXdr,
int typeIntern,
int nofHeaderBytes,
int tileX,
282 int tileY,
int tileZ,
int proj,
int zone,
double north,
283 double south,
double east,
double west,
double top,
284 double bottom,
int rows,
int cols,
int depths,
double ew_res,
285 double ns_res,
double tb_res,
char *
unit)
290 map->operation = operation;
294 map->region.proj = proj;
295 map->region.zone = zone;
297 map->region.north = north;
298 map->region.south = south;
299 map->region.east = east;
300 map->region.west = west;
301 map->region.top = top;
302 map->region.bottom = bottom;
304 map->region.rows = rows;
305 map->region.cols =
cols;
306 map->region.depths = depths;
308 map->region.ew_res = ew_res;
309 map->region.ns_res = ns_res;
310 map->region.tb_res = tb_res;
317 map->tileXY = map->tileX * map->tileY;
318 map->tileSize = map->tileXY * map->tileZ;
320 map->nx = (map->region.cols - 1) / tileX + 1;
321 map->ny = (map->region.rows - 1) / tileY + 1;
322 map->nz = (map->region.depths - 1) / tileZ + 1;
323 map->nxy = map->nx * map->ny;
324 map->nTiles = map->nxy * map->nz;
326 if ((map->region.cols) % map->tileX != 0)
327 map->clipX = map->nx - 1;
330 if ((map->region.rows) % map->tileY != 0)
331 map->clipY = map->ny - 1;
334 if ((map->region.depths) % map->tileZ != 0)
335 map->clipZ = map->nz - 1;
339 if ((type != FCELL_TYPE) && (type != DCELL_TYPE))
343 if ((typeIntern != FCELL_TYPE) && (typeIntern != DCELL_TYPE))
345 map->typeIntern = typeIntern;
349 map->useXdr = useXdr;
351 map->offset = nofHeaderBytes;
353 if ((map->fileEndPtr = lseek(map->data_fd, (
long)0, SEEK_END)) == -1) {
354 G3d_error(
"G3d_fillHeader: can't position file");
358 map->useCache = (cache != G3D_NO_CACHE);
360 map->numLengthIntern =
G3d_length(map->typeIntern);
364 map->useRle = useRle;
365 map->useLzw = useLzw;
366 map->precision = precision;
368 #define RLE_STATUS_BYTES 2
370 if (map->compression != G3D_NO_COMPRESSION) {
373 G3D_MAX(map->numLengthIntern, map->numLengthExtern) +
377 G3d_error(
"G3d_fillHeader: error in G3d_malloc");
381 else if (map->tileSize *
382 G3D_MAX(map->numLengthIntern, map->numLengthExtern)
385 G3D_MAX(map->numLengthIntern, map->numLengthExtern) +
389 G3d_error(
"G3d_fillHeader: error in G3d_realloc");
395 #define XDR_MISUSE_BYTES 10
398 G3d_error(
"G3d_fillHeader: error in G3d_initFpXdr");
402 if ((!map->useCache) ||
407 map->data =
G3d_malloc(map->tileSize * map->numLengthIntern);
408 if (map->data ==
NULL) {
409 G3d_error(
"G3d_fillHeader: error in G3d_malloc");
412 map->currentIndex = -1;
420 map->numLengthIntern)))) {
421 G3d_error(
"G3d_fillHeader: error in G3d_initCache");
427 G3d_error(
"G3d_fillHeader: error in G3d_initIndex");