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