GRASS 8 Programmer's Manual  8.5.0dev(2025)-e5e60895a1
tileread.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 
6 #include <grass/raster.h>
7 #include "raster3d_intern.h"
8 
9 static int Rast3d_xdrTile2tile(RASTER3D_Map *map, void *tile, int rows,
10  int cols, int depths, int xRedundant,
11  int yRedundant, int zRedundant, int nofNum,
12  int type)
13 {
14  int y, z, xLength, yLength, length;
15 
16  if (!Rast3d_init_copy_from_xdr(map, type)) {
17  Rast3d_error("Rast3d_xdrTile2tile: error in Rast3d_init_copy_from_xdr");
18  return 0;
19  }
20 
21  if (nofNum == map->tileSize) {
22  if (!Rast3d_copy_from_xdr(map->tileSize, tile)) {
23  Rast3d_error("Rast3d_xdrTile2tile: error in Rast3d_copy_from_xdr");
24  return 0;
25  }
26  return 1;
27  }
28 
29  length = Rast3d_length(type);
30  xLength = xRedundant * length;
31  yLength = map->tileX * yRedundant * length;
32 
33  if (xRedundant) {
34  for (z = 0; z < depths; z++) {
35  for (y = 0; y < rows; y++) {
36  if (!Rast3d_copy_from_xdr(cols, tile)) {
38  "Rast3d_xdrTile2tile: error in Rast3d_copy_from_xdr");
39  return 0;
40  }
41  tile = G_incr_void_ptr(tile, cols * length);
42  Rast3d_set_null_value(tile, xRedundant, type);
43  tile = G_incr_void_ptr(tile, xLength);
44  }
45  if (yRedundant) {
46  Rast3d_set_null_value(tile, map->tileX * yRedundant, type);
47  tile = G_incr_void_ptr(tile, yLength);
48  }
49  }
50  if (!zRedundant)
51  return 1;
52 
53  Rast3d_set_null_value(tile, map->tileXY * zRedundant, type);
54  return 1;
55  }
56 
57  if (yRedundant) {
58  for (z = 0; z < depths; z++) {
59  if (!Rast3d_copy_from_xdr(map->tileX * rows, tile)) {
61  "Rast3d_xdrTile2tile: error in Rast3d_copy_from_xdr");
62  return 0;
63  }
64  tile = G_incr_void_ptr(tile, map->tileX * rows * length);
65  Rast3d_set_null_value(tile, map->tileX * yRedundant, type);
66  tile = G_incr_void_ptr(tile, yLength);
67  }
68  if (!zRedundant)
69  return 1;
70 
71  Rast3d_set_null_value(tile, map->tileXY * zRedundant, type);
72  return 1;
73  }
74 
75  if (!Rast3d_copy_from_xdr(map->tileXY * depths, tile)) {
76  Rast3d_error("Rast3d_xdrTile2tile: error in Rast3d_copy_from_xdr");
77  return 0;
78  }
79 
80  if (!zRedundant)
81  return 1;
82 
83  tile = G_incr_void_ptr(tile, map->tileXY * depths * length);
84  Rast3d_set_null_value(tile, map->tileXY * zRedundant, type);
85 
86  return 1;
87 }
88 
89 /*---------------------------------------------------------------------------*/
90 
91 static int Rast3d_readTileUncompressed(RASTER3D_Map *map, int tileIndex,
92  int nofNum)
93 {
94  size_t nofBytes;
95  ssize_t res;
96 
97  nofBytes = (size_t)nofNum * map->numLengthExtern;
98  nofBytes =
99  RASTER3D_MIN(nofBytes, (size_t)map->fileEndPtr - map->index[tileIndex]);
100 
101  if ((res = read(map->data_fd, xdr, nofBytes)) < 0 ||
102  (size_t)res != nofBytes) {
103  Rast3d_error("Rast3d_readTileUncompressed: can't read file");
104  return 0;
105  }
106 
107  return 1;
108 }
109 
110 /*---------------------------------------------------------------------------*/
111 
112 static int Rast3d_readTileCompressed(RASTER3D_Map *map, int tileIndex,
113  int nofNum)
114 {
116  map->data_fd, xdr, nofNum, map->tileLength[tileIndex],
117  map->precision, tmpCompress, map->type == FCELL_TYPE)) {
118  Rast3d_error("Rast3d_readTileCompressed: error in "
119  "Rast3d_fpcompress_read_xdr_nums");
120  return 0;
121  }
122 
123  return 1;
124 }
125 
126 /*---------------------------------------------------------------------------*/
127 
128 /*---------------------------------------------------------------------------*/
129 /* EXPORTED FUNCTIONS */
130 
131 /*---------------------------------------------------------------------------*/
132 
133 /*!
134  * \brief
135  *
136  *
137  * Reads tile with index <em>tileIndex</em> into the <em>tile</em> buffer. The
138  * cells are stored with type <em>type</em> which must be one of FCELL_TYPE and
139  * DCELL_TYPE. If the tile with <em>tileIndex</em> is not stored on the file
140  * corresponding to <em>map</em>, and <em>tileIndex</em> is a valid index
141  * <em>tile</em> is filled with NULL-values.
142  *
143  * \param map
144  * \param tileIndex
145  * \param tile
146  * \param type
147  * \return 1 ... if successful,
148  * 0 ... otherwise
149  */
150 int Rast3d_read_tile(RASTER3D_Map *map, int tileIndex, void *tile, int type)
151 {
152  int nofNum, rows, cols, depths, xRedundant, yRedundant, zRedundant;
153 
154  if ((tileIndex >= map->nTiles) || (tileIndex < 0))
155  Rast3d_fatal_error("Rast3d_read_tile: tile index out of range");
156 
157  if (map->index[tileIndex] == -1) {
158  Rast3d_set_null_tile_type(map, tile, type);
159  return 1;
160  }
161 
162  nofNum = Rast3d_compute_clipped_tile_dimensions(map, tileIndex, &rows,
163  &cols, &depths, &xRedundant,
164  &yRedundant, &zRedundant);
165 
166  if (lseek(map->data_fd, map->index[tileIndex], SEEK_SET) == -1) {
167  Rast3d_error("Rast3d_read_tile: can't position file");
168  return 0;
169  }
170 
171  if (map->compression == RASTER3D_NO_COMPRESSION) {
172  if (!Rast3d_readTileUncompressed(map, tileIndex, nofNum)) {
173  Rast3d_error(
174  "Rast3d_read_tile: error in Rast3d_readTileUncompressed");
175  return 0;
176  }
177  }
178  else if (!Rast3d_readTileCompressed(map, tileIndex, nofNum)) {
179  Rast3d_error("Rast3d_read_tile: error in Rast3d_readTileCompressed");
180  return 0;
181  }
182 
183  if (!Rast3d_xdrTile2tile(map, tile, rows, cols, depths, xRedundant,
184  yRedundant, zRedundant, nofNum, type)) {
185  Rast3d_error("Rast3d_read_tile: error in Rast3d_xdrTile2tile");
186  return 0;
187  }
188 
189  if (Rast3d_mask_is_off(map))
190  return 1;
191 
192  Rast3d_mask_tile(map, tileIndex, tile, type);
193  return 1;
194 }
195 
196 /*---------------------------------------------------------------------------*/
197 
198 /*!
199  * \brief
200  *
201  * Is equivalent to Rast3d_read_tile (map, tileIndex, tile, FCELL_TYPE).
202  *
203  * \param map
204  * \param tileIndex
205  * \param tile
206  * \return int
207  */
208 int Rast3d_read_tile_float(RASTER3D_Map *map, int tileIndex, void *tile)
209 {
210  if (!Rast3d_read_tile(map, tileIndex, tile, FCELL_TYPE)) {
211  Rast3d_error("Rast3d_read_tile_float: error in Rast3d_read_tile");
212  return 0;
213  }
214 
215  return 1;
216 }
217 
218 /*---------------------------------------------------------------------------*/
219 
220 /*!
221  * \brief
222  *
223  * Is equivalent to Rast3d_read_tile (map, tileIndex, tile, DCELL_TYPE).
224  *
225  * \param map
226  * \param tileIndex
227  * \param tile
228  * \return int
229  */
230 int Rast3d_read_tile_double(RASTER3D_Map *map, int tileIndex, void *tile)
231 {
232  if (!Rast3d_read_tile(map, tileIndex, tile, DCELL_TYPE)) {
233  Rast3d_error("Rast3d_read_tile_double: error in Rast3d_read_tile");
234  return 0;
235  }
236 
237  return 1;
238 }
239 
240 /*---------------------------------------------------------------------------*/
241 
242 /* CACHE-MODE-ONLY FUNCTIONS */
243 
244 /*---------------------------------------------------------------------------*/
245 
246 /*!
247  * \brief
248  *
249  * Locks tile with <em>tileIndex</em> in cache. If after locking fewer than the
250  * minimum number of unlocked tiles are unlocked, the lock request is ignored.
251  *
252  * \param map
253  * \param tileIndex
254  * \return 1 ... if successful,
255  * -1 ... if request is ignored,
256  * 0 ... otherwise.
257  */
258 int Rast3d_lock_tile(RASTER3D_Map *map, int tileIndex)
259 {
260  if (!map->useCache)
262  "Rast3d_lock_tile: function invalid in non-cache mode");
263 
264  if (!Rast3d_cache_lock(map->cache, tileIndex)) {
265  Rast3d_error("Rast3d_lock_tile: error in Rast3d_cache_lock");
266  return 0;
267  }
268 
269  return 1;
270 }
271 
272 /*---------------------------------------------------------------------------*/
273 
274 /*!
275  * \brief
276  *
277  * Unlocks tile with <em>tileIndex</em>.
278  *
279  * \param map
280  * \param tileIndex
281  * \return 1 ... if successful,
282  * 0 ... otherwise.
283  */
284 int Rast3d_unlock_tile(RASTER3D_Map *map, int tileIndex)
285 {
286  if (!map->useCache)
288  "Rast3d_unlock_tile: function invalid in non-cache mode");
289 
290  if (!Rast3d_cache_unlock(map->cache, tileIndex)) {
291  Rast3d_error("Rast3d_unlock_tile: error in Rast3d_cache_unlock");
292  return 0;
293  }
294 
295  return 1;
296 }
297 
298 /*---------------------------------------------------------------------------*/
299 
300 /*!
301  * \brief
302  *
303  * Unlocks every tile in cache of <em>map</em>.
304  *
305  * \param map
306  * \return 1 ... if successful,
307  * 0 ... otherwise.
308  */
310 {
311  if (!map->useCache)
313  "Rast3d_unlock_all: function invalid in non-cache mode");
314 
315  if (!Rast3d_cache_unlock_all(map->cache)) {
316  Rast3d_error("Rast3d_unlock_all: error in Rast3d_cache_unlock_all");
317  return 0;
318  }
319 
320  return 1;
321 }
322 
323 /*---------------------------------------------------------------------------*/
324 
325 /*!
326  * \brief
327  *
328  * Turns autolock mode on.
329  *
330  * \param map
331  * \return void
332  */
334 {
335  if (!map->useCache)
337  "Rast3d_autoLockOn: function invalid in non-cache mode");
338 
340 }
341 
342 /*---------------------------------------------------------------------------*/
343 
344 /*!
345  * \brief
346  *
347  * Turns autolock mode Off.
348  *
349  * \param map
350  * \return void
351  */
353 {
354  if (!map->useCache)
356  "Rast3d_autoLockOff: function invalid in non-cache mode");
357 
359 }
360 
361 /*---------------------------------------------------------------------------*/
362 
363 /*!
364  * \brief
365  *
366  * Sets the minimum
367  * number of unlocked tiles to <em>minUnlocked</em>. This function should be
368  * used in combination with <tt>Rast3d_unlock_all ()</tt> in order to avoid
369  * situations where the new minimum is larger than the actual number of unlocked
370  * tiles. <em>minUnlocked</em> must be one of RASTER3D_USE_CACHE_X,
371  * RASTER3D_USE_CACHE_Y, RASTER3D_USE_CACHE_Z, RASTER3D_USE_CACHE_XY,
372  * RASTER3D_USE_CACHE_XZ, RASTER3D_USE_CACHE_YZ, RASTER3D_USE_CACHE_XYZ, the
373  * result of Rast3d_cache_size_encode() (cf.{g3d:G3d.cacheSizeEncode}), or any
374  * positive integer which explicitly specifies the number of tiles.
375  *
376  * \param map
377  * \param minUnlocked
378  * \return void
379  */
380 void Rast3d_min_unlocked(RASTER3D_Map *map, int minUnlocked)
381 {
382  if (!map->useCache)
384  "Rast3d_autoLockOff: function invalid in non-cache mode");
385 
387  Rast3d__compute_cache_size(map, minUnlocked));
388 }
389 
390 /*---------------------------------------------------------------------------*/
391 
392 /*!
393  * \brief
394  *
395  * Starts a new cycle.
396  *
397  * \param map
398  * \return 1 ... if successful,
399  * 0 ... otherwise.
400  */
402 {
403  if (!Rast3d_unlock_all(map)) {
404  Rast3d_fatal_error("Rast3d_begin_cycle: error in Rast3d_unlock_all");
405  return 0;
406  }
407 
408  Rast3d_autolock_on(map);
409  return 1;
410 }
411 
412 /*---------------------------------------------------------------------------*/
413 
414 /*!
415  * \brief
416  *
417  * Ends a cycle.
418  *
419  * \param map
420  * \return 1 ... if successful,
421  * 0 ... otherwise.
422  */
424 {
425  Rast3d_autolock_off(map);
426  return 1;
427 }
#define G_incr_void_ptr(ptr, size)
Definition: defs/gis.h:81
int Rast3d_cache_lock(RASTER3D_cache *, int)
Definition: cache1.c:267
int Rast3d_mask_is_off(RASTER3D_Map *)
Returns 1 if the mask for map is turned off. Returns 0 otherwise.
Definition: mask.c:366
int Rast3d_init_copy_from_xdr(RASTER3D_Map *, int)
Definition: fpxdr.c:184
void Rast3d_cache_autolock_off(RASTER3D_cache *)
Definition: cache1.c:361
void Rast3d_set_null_tile_type(RASTER3D_Map *, void *, int)
Assumes that tile is a tile with the same dimensions as the tiles of map. Fills tile with NULL-values...
Definition: tilenull.c:21
void Rast3d_mask_tile(RASTER3D_Map *, int, void *, int)
Replaces the values stored in tile (with tileIndex) for which Rast3d_is_masked returns 1 with NULL-va...
Definition: mask.c:266
void Rast3d_set_null_value(void *, int, int)
Fills the vector pointed to by c with nofElts NULL-values of type.
Definition: null.c:33
int Rast3d__compute_cache_size(RASTER3D_Map *, int)
void Rast3d_cache_autolock_on(RASTER3D_cache *)
Definition: cache1.c:354
void Rast3d_cache_set_min_unlock(RASTER3D_cache *, int)
Definition: cache1.c:368
int Rast3d_length(int)
Definition: raster3d/misc.c:75
int Rast3d_cache_unlock_all(RASTER3D_cache *)
Definition: cache1.c:324
int Rast3d_cache_unlock(RASTER3D_cache *, int)
Definition: cache1.c:303
int Rast3d_copy_from_xdr(int, void *)
Definition: fpxdr.c:223
int Rast3d_compute_clipped_tile_dimensions(RASTER3D_Map *, int, int *, int *, int *, int *, int *, int *)
Computes the dimensions of the tile when clipped to fit the region of map. The clipped dimensions are...
Definition: tilemath.c:243
void Rast3d_error(const char *,...) __attribute__((format(printf
int Rast3d_fpcompress_read_xdr_nums(int, char *, int, int, int, char *, int)
Definition: fpcompress.c:716
void Rast3d_fatal_error(const char *,...) __attribute__((format(printf
void * xdr
void * tmpCompress
#define RASTER3D_NO_COMPRESSION
Definition: raster3d.h:13
#define RASTER3D_MIN(a, b)
#define FCELL_TYPE
Definition: raster.h:12
#define DCELL_TYPE
Definition: raster.h:13
SSIZE_T ssize_t
Definition: stdio.h:9
long * index
Definition: raster3d.h:142
int numLengthExtern
Definition: raster3d.h:173
int precision
Definition: raster3d.h:109
int compression
Definition: raster3d.h:111
void * cache
Definition: raster3d.h:161
int tileSize
Definition: raster3d.h:180
int fileEndPtr
Definition: raster3d.h:133
int * tileLength
Definition: raster3d.h:145
int useCache
Definition: raster3d.h:160
int Rast3d_read_tile(RASTER3D_Map *map, int tileIndex, void *tile, int type)
Reads tile with index tileIndex into the tile buffer. The cells are stored with type type which must ...
Definition: tileread.c:150
void Rast3d_autolock_off(RASTER3D_Map *map)
Turns autolock mode Off.
Definition: tileread.c:352
void Rast3d_min_unlocked(RASTER3D_Map *map, int minUnlocked)
Sets the minimum number of unlocked tiles to minUnlocked. This function should be used in combination...
Definition: tileread.c:380
int Rast3d_begin_cycle(RASTER3D_Map *map)
Starts a new cycle.
Definition: tileread.c:401
int Rast3d_read_tile_double(RASTER3D_Map *map, int tileIndex, void *tile)
Is equivalent to Rast3d_read_tile (map, tileIndex, tile, DCELL_TYPE).
Definition: tileread.c:230
int Rast3d_read_tile_float(RASTER3D_Map *map, int tileIndex, void *tile)
Is equivalent to Rast3d_read_tile (map, tileIndex, tile, FCELL_TYPE).
Definition: tileread.c:208
int Rast3d_end_cycle(RASTER3D_Map *map)
Ends a cycle.
Definition: tileread.c:423
int Rast3d_unlock_all(RASTER3D_Map *map)
Unlocks every tile in cache of map.
Definition: tileread.c:309
int Rast3d_unlock_tile(RASTER3D_Map *map, int tileIndex)
Unlocks tile with tileIndex.
Definition: tileread.c:284
int Rast3d_lock_tile(RASTER3D_Map *map, int tileIndex)
Locks tile with tileIndex in cache. If after locking fewer than the minimum number of unlocked tiles ...
Definition: tileread.c:258
void Rast3d_autolock_on(RASTER3D_Map *map)
Turns autolock mode on.
Definition: tileread.c:333
#define read
Definition: unistd.h:5