GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
tilewrite.c
Go to the documentation of this file.
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 "G3d_intern.h"
8 
9 
10 /*---------------------------------------------------------------------------*/
11 
12 static int
13 G3d_tile2xdrTile(G3D_Map * map, const void *tile, int rows, int cols,
14  int depths, int xRedundant, int yRedundant, int zRedundant,
15  int nofNum, int type)
16 {
17  int y, z;
18 
19  if (!G3d_initCopyToXdr(map, type)) {
20  G3d_error("G3d_tile2xdrTile: error in G3d_initCopyToXdr");
21  return 0;
22  }
23 
24 
25  if (nofNum == map->tileSize) {
26  if (!G3d_copyToXdr(tile, map->tileSize)) {
27  G3d_error("G3d_tile2xdrTile: error in G3d_copyToXdr");
28  return 0;
29  }
30  return 1;
31  }
32 
33  if (xRedundant) {
34  for (z = 0; z < depths; z++) {
35  for (y = 0; y < rows; y++) {
36  if (!G3d_copyToXdr(tile, cols)) {
37  G3d_error("G3d_tile2xdrTile: error in G3d_copyToXdr");
38  return 0;
39  }
40  tile = G_incr_void_ptr(tile, map->tileX * G3d_length(type));
41  }
42  if (yRedundant)
43  tile =
44  G_incr_void_ptr(tile,
45  map->tileX * yRedundant *
46  G3d_length(type));
47  }
48  return 1;
49  }
50 
51  if (yRedundant) {
52  for (z = 0; z < depths; z++) {
53  if (!G3d_copyToXdr(tile, map->tileX * rows)) {
54  G3d_error("G3d_tile2xdrTile: error in G3d_copyToXdr");
55  return 0;
56  }
57  tile = G_incr_void_ptr(tile, map->tileXY * G3d_length(type));
58  }
59  return 1;
60  }
61 
62  if (!G3d_copyToXdr(tile, map->tileXY * depths)) {
63  G3d_error("G3d_tile2xdrTile: error in G3d_copyToXdr");
64  return 0;
65  }
66  return 1;
67 }
68 
69 /*---------------------------------------------------------------------------*/
70 
71 static int G3d_writeTileUncompressed(G3D_Map * map, int nofNum)
72 {
73  if (write(map->data_fd, xdr, map->numLengthExtern * nofNum) !=
74  map->numLengthExtern * nofNum) {
75  G3d_error("G3d_writeTileUncompressed: can't write file.");
76  return 0;
77  }
78 
79  return 1;
80 }
81 
82 /*---------------------------------------------------------------------------*/
83 
84 static int G3d_writeTileCompressed(G3D_Map * map, int nofNum)
85 {
86  if (!G_fpcompress_writeXdrNums(map->data_fd, xdr, nofNum, map->precision,
87  tmpCompress, map->type == FCELL_TYPE,
88  map->useRle, map->useLzw)) {
89  G3d_error
90  ("G3d_writeTileCompressed: error in G_fpcompress_writeXdrNums");
91  return 0;
92  }
93 
94  return 1;
95 }
96 
97 /*---------------------------------------------------------------------------*/
98 
99 /*---------------------------------------------------------------------------*/
100 
101  /* EXPORTED FUNCTIONS */
102 
103 /*---------------------------------------------------------------------------*/
104 
105 /*---------------------------------------------------------------------------*/
106 
107 
128 int G3d_writeTile(G3D_Map * map, int tileIndex, const void *tile, int type)
129 {
130  int rows, cols, depths, xRedundant, yRedundant, zRedundant, nofNum;
131 
132  /* valid tileIndex ? */
133  if ((tileIndex >= map->nTiles) || (tileIndex < 0))
134  G3d_fatalError("G3d_writeTile: tileIndex out of range");
135 
136  /* already written ? */
137  if (map->index[tileIndex] != -1)
138  return 2;
139 
140  /* save the file position */
141  map->index[tileIndex] = lseek(map->data_fd, (long)0, SEEK_END);
142  if (map->index[tileIndex] == -1) {
143  G3d_error("G3d_writeTile: can't position file");
144  return 0;
145  }
146 
147  nofNum = G3d_computeClippedTileDimensions(map, tileIndex,
148  &rows, &cols, &depths,
149  &xRedundant, &yRedundant,
150  &zRedundant);
151 
152  G3d_range_updateFromTile(map, tile, rows, cols, depths,
153  xRedundant, yRedundant, zRedundant, nofNum,
154  type);
155 
156  if (!G3d_tile2xdrTile(map, tile, rows, cols, depths,
157  xRedundant, yRedundant, zRedundant, nofNum, type)) {
158  G3d_error("G3d_writeTileCompressed: error in G3d_tile2xdrTile");
159  return 0;
160  }
161 
162  if (map->compression == G3D_NO_COMPRESSION) {
163  if (!G3d_writeTileUncompressed(map, nofNum)) {
164  G3d_error("G3d_writeTile: error in G3d_writeTileUncompressed");
165  return 0;
166  }
167  }
168  else if (!G3d_writeTileCompressed(map, nofNum)) {
169  G3d_error("G3d_writeTile: error in G3d_writeTileCompressed");
170  return 0;
171  }
172 
173  /* compute the length */
174  map->tileLength[tileIndex] = lseek(map->data_fd, (long)0, SEEK_END) -
175  map->index[tileIndex];
176 
177  return 1;
178 }
179 
180 /*---------------------------------------------------------------------------*/
181 
182 
194 int G3d_writeTileFloat(G3D_Map * map, int tileIndex, const void *tile)
195 {
196  int status;
197 
198  if ((status = G3d_writeTile(map, tileIndex, tile, FCELL_TYPE)))
199  return status;
200 
201  G3d_error("G3d_writeTileFloat: error in G3d_writeTile");
202  return 0;
203 }
204 
205 /*---------------------------------------------------------------------------*/
206 
207 
219 int G3d_writeTileDouble(G3D_Map * map, int tileIndex, const void *tile)
220 {
221  int status;
222 
223  if ((status = G3d_writeTile(map, tileIndex, tile, DCELL_TYPE)))
224  return status;
225 
226  G3d_error("G3d_writeTileDouble: error in G3d_writeTile");
227  return 0;
228 }
229 
230 /*---------------------------------------------------------------------------*/
231 
232  /* CACHE-MODE-ONLY FUNCTIONS */
233 
234 /*---------------------------------------------------------------------------*/
235 
236 
254 int G3d_flushTile(G3D_Map * map, int tileIndex)
255 {
256  const void *tile;
257 
258  tile = G3d_getTilePtr(map, tileIndex);
259  if (tile == NULL) {
260  G3d_error("G3d_flushTile: error in G3d_getTilePtr");
261  return 0;
262  }
263 
264  if (!G3d_writeTile(map, tileIndex, tile, map->typeIntern)) {
265  G3d_error("G3d_flushTile: error in G3d_writeTile");
266  return 0;
267  }
268 
269  if (!G3d__removeTile(map, tileIndex)) {
270  G3d_error("G3d_flushTile: error in G3d__removeTile");
271  return 0;
272  }
273 
274  return 1;
275 }
276 
277 /*---------------------------------------------------------------------------*/
278 
279 #ifndef MIN
280 #define MIN(a,b) (a < b ? a : b)
281 #define MAX(a,b) (a > b ? a : b)
282 #endif
283 
284 
305 int
306 G3d_flushTileCube(G3D_Map * map, int xMin, int yMin, int zMin, int xMax,
307  int yMax, int zMax)
308 {
309  int x, y, z;
310 
311  if (!map->useCache)
313  ("G3d_flushTileCube: function invalid in non-cache mode");
314 
315  for (x = xMin; x <= xMax; x++)
316  for (y = yMin; y <= yMax; y++)
317  for (z = zMin; z <= zMax; z++)
318  if (!G3d_flushTile(map, G3d_tile2tileIndex(map, x, y, z))) {
319  G3d_error("G3d_flushTileCube: error in G3d_flushTile");
320  return 0;
321  }
322 
323  return 1;
324 }
325 
326 /*---------------------------------------------------------------------------*/
327 
328 
351 int
352 G3d_flushTilesInCube(G3D_Map * map, int xMin, int yMin, int zMin, int xMax,
353  int yMax, int zMax)
354 {
355  int xTileMin, yTileMin, zTileMin, xTileMax, yTileMax, zTileMax;
356  int xOffs, yOffs, zOffs;
357  int regionMaxX, regionMaxY, regionMaxZ;
358 
359  if (!map->useCache)
361  ("G3d_flushTilesInCube: function invalid in non-cache mode");
362  /*AV*/
363  /*BEGIN OF ORIGINAL CODE */
364  /*
365  * G3d_getCoordsMap (map, &regionMaxX, &regionMaxY, &regionMaxZ);
366  */
367  /*AV*/
368  /* BEGIN OF MY CODE */
369  G3d_getCoordsMap(map, &regionMaxY, &regionMaxX, &regionMaxZ);
370  /* END OF MY CODE */
371 
372  if ((xMin < 0) && (xMax < 0))
373  G3d_fatalError("G3d_flushTilesInCube: coordinate out of Range");
374  if ((xMin >= regionMaxX) && (xMax >= regionMaxX))
375  G3d_fatalError("G3d_flushTilesInCube: coordinate out of Range");
376 
377  xMin = MIN(MAX(0, xMin), regionMaxX - 1);
378 
379  if ((yMin < 0) && (yMax < 0))
380  G3d_fatalError("G3d_flushTilesInCube: coordinate out of Range");
381  if ((yMin >= regionMaxY) && (yMax >= regionMaxY))
382  G3d_fatalError("G3d_flushTilesInCube: coordinate out of Range");
383 
384  yMin = MIN(MAX(0, yMin), regionMaxY - 1);
385 
386  if ((zMin < 0) && (zMax < 0))
387  G3d_fatalError("G3d_flushTilesInCube: coordinate out of Range");
388  if ((zMin >= regionMaxZ) && (zMax >= regionMaxZ))
389  G3d_fatalError("G3d_flushTilesInCube: coordinate out of Range");
390 
391  zMin = MIN(MAX(0, zMin), regionMaxZ - 1);
392 
393  G3d_coord2tileCoord(map, xMin, yMin, zMin,
394  &xTileMin, &yTileMin, &zTileMin,
395  &xOffs, &yOffs, &zOffs);
396 
397  if (xOffs != 0)
398  xTileMin++;
399  if (yOffs != 0)
400  yTileMin++;
401  if (zOffs != 0)
402  zTileMin++;
403 
404  G3d_coord2tileCoord(map, xMax + 1, yMax + 1, zMax + 1,
405  &xTileMax, &yTileMax, &zTileMax,
406  &xOffs, &yOffs, &zOffs);
407 
408  xTileMax--;
409  yTileMax--;
410  zTileMax--;
411 
412  if (!G3d_flushTileCube(map, xTileMin, yTileMin, zTileMin,
413  xTileMax, yTileMax, zTileMax)) {
414  G3d_error("G3d_flushTilesInCube: error in G3d_flushTileCube");
415  return 0;
416  }
417 
418  return 1;
419 }
420 
421 #undef MIN
422 #undef MAX
423 
424 /*---------------------------------------------------------------------------*/
425 
426 
440 int G3d_putDouble();
441 
442 
456 int G3d_putFloat(G3D_Map * map, int x, int y, int z, float value)
457 {
458  int tileIndex, offs;
459  float *tile;
460 
461  if (map->typeIntern == DCELL_TYPE) {
462  if (!G3d_putDouble(map, x, y, z, (double)value)) {
463  G3d_error("G3d_putFloat: error in G3d_putDouble");
464  return 0;
465  }
466  return 1;
467  }
468 
469  G3d_coord2tileIndex(map, x, y, z, &tileIndex, &offs);
470  tile = (float *)G3d_getTilePtr(map, tileIndex);
471  if (tile == NULL) {
472  G3d_error("G3d_putFloat: error in G3d_getTilePtr");
473  return 0;
474  }
475 
476  tile[offs] = value;
477  return 1;
478 }
479 
480 /*---------------------------------------------------------------------------*/
481 
482 
496 int G3d_putDouble(G3D_Map * map, int x, int y, int z, double value)
497 {
498  int tileIndex, offs;
499  double *tile;
500 
501  if (map->typeIntern == FCELL_TYPE) {
502  if (!G3d_putFloat(map, x, y, z, (float)value)) {
503  G3d_error("G3d_putDouble: error in G3d_putFloat");
504  return 0;
505  }
506  return 1;
507  }
508 
509  G3d_coord2tileIndex(map, x, y, z, &tileIndex, &offs);
510  tile = (double *)G3d_getTilePtr(map, tileIndex);
511  if (tile == NULL) {
512  G3d_error("G3d_putDouble: error in G3d_getTilePtr");
513  return 0;
514  }
515 
516  tile[offs] = value;
517  return 1;
518 }
519 
520 /*---------------------------------------------------------------------------*/
521 
539 int
540 G3d_putValue(G3D_Map * map, int x, int y, int z, const void *value, int type)
541 {
542  if (type == FCELL_TYPE) {
543  if (!G3d_putFloat(map, x, y, z, *((float *)value))) {
544  G3d_error("G3d_putValue: error in G3d_putFloat");
545  return 0;
546  }
547  return 1;
548  }
549 
550  if (!G3d_putDouble(map, x, y, z, *((double *)value))) {
551  G3d_error("G3d_putValue: error in G3d_putDouble");
552  return 0;
553  }
554  return 1;
555 }
int G3d_flushTilesInCube(G3D_Map *map, int xMin, int yMin, int zMin, int xMax, int yMax, int zMax)
Writes those tiles for which every cell has coordinate contained in the axis-parallel cube defined by...
Definition: tilewrite.c:352
int G3d__removeTile(G3D_Map *map, int tileIndex)
Definition: tileio.c:127
int G3d_writeTile(G3D_Map *map, int tileIndex, const void *tile, int type)
Writes tile with index tileIndex to the file corresponding to map. It is assumed that the cells in ti...
Definition: tilewrite.c:128
void G3d_error(const char *msg,...)
Definition: g3derror.c:75
int y
Definition: plot.c:34
void * G_incr_void_ptr(const void *ptr, const size_t size)
Advance void pointer.
Definition: gis/raster.c:33
void * tmpCompress
Definition: g3d/header.c:12
void * G3d_getTilePtr(G3D_Map *map, int tileIndex)
This function returns a pointer to a tile which contains the data for the tile with index tileIndex...
Definition: tileio.c:71
int G3d_copyToXdr(const void *src, int nofNum)
Definition: g3dfpxdr.c:147
#define MAX(a, b)
Definition: tilewrite.c:281
char * value
Definition: env.c:30
int G3d_flushTile(G3D_Map *map, int tileIndex)
Writes the tile with tileIndex to the file corresponding to map and removes the tile from the cache (...
Definition: tilewrite.c:254
int G3d_putDouble()
Is equivalent to G3d_putValue (map, x, y, z, &amp;value, DCELL_TYPE).
#define MIN(a, b)
Definition: tilewrite.c:280
int G3d_computeClippedTileDimensions(G3D_Map *map, int tileIndex, int *rows, int *cols, int *depths, int *xRedundant, int *yRedundant, int *zRedundant)
Computes the dimensions of the tile when clipped to fit the region of map. The clipped dimensions are...
Definition: tilemath.c:267
return NULL
Definition: dbfopen.c:1394
tuple cols
int G3d_length(int t)
Definition: g3dmisc.c:77
int G3d_writeTileDouble(G3D_Map *map, int tileIndex, const void *tile)
Is equivalent to G3d_writeTile (map, tileIndex, tile, DCELL_TYPE).
Definition: tilewrite.c:219
int G3d_putFloat(G3D_Map *map, int x, int y, int z, float value)
Is equivalent to G3d_putValue (map, x, y, z, &amp;value, FCELL_TYPE).
Definition: tilewrite.c:456
int G_fpcompress_writeXdrNums(int fd, char *src, int nofNum, int precision, char *compressBuf, int isFloat, int useRle, int useLzw)
Definition: fpcompress.c:698
void * xdr
Definition: g3d/header.c:14
void G3d_coord2tileCoord(G3D_Map *map, int x, int y, int z, int *xTile, int *yTile, int *zTile, int *xOffs, int *yOffs, int *zOffs)
Converts cell-coordinates (x, y, z) into tile-coordinates (xTile, yTile, zTile) and the coordinate of...
Definition: tilemath.c:136
int G3d_tile2tileIndex(G3D_Map *map, int xTile, int yTile, int zTile)
Returns tile-index corresponding to tile-coordinates (xTile, yTile, zTile).
Definition: tilemath.c:52
void G3d_coord2tileIndex(G3D_Map *map, int x, int y, int z, int *tileIndex, int *offset)
Converts cell-coordinates (x, y, z) into tileIndex and the offset of the cell within the tile...
Definition: tilemath.c:167
void G3d_fatalError(const char *,...)
This function prints the error message msg, and terminates the program with an error status...
Definition: g3derror.c:58
void G3d_range_updateFromTile(G3D_Map *map, const void *tile, int rows, int cols, int depths, int xRedundant, int yRedundant, int zRedundant, int nofNum, int type)
Definition: g3drange.c:13
void G3d_getCoordsMap(G3D_Map *map, int *rows, int *cols, int *depths)
Returns the size of the region of map in cells.
Definition: headerinfo.c:19
int G3d_writeTileFloat(G3D_Map *map, int tileIndex, const void *tile)
Is equivalent to G3d_writeTile (map, tileIndex, tile, FCELL_TYPE).
Definition: tilewrite.c:194
int G3d_putValue(G3D_Map *map, int x, int y, int z, const void *value, int type)
After converting *value of type into the type specified at the initialization time (i...
Definition: tilewrite.c:540
int G3d_flushTileCube(G3D_Map *map, int xMin, int yMin, int zMin, int xMax, int yMax, int zMax)
Writes the tiles with tile-coordinates contained in the axis-parallel cube with vertices (xMin...
Definition: tilewrite.c:306
int G3d_initCopyToXdr(G3D_Map *map, int sType)
Definition: g3dfpxdr.c:118