GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-36359e2344
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 
151 int Rast3d_read_tile(RASTER3D_Map *map, int tileIndex, void *tile, int type)
152 {
153  int nofNum, rows, cols, depths, xRedundant, yRedundant, zRedundant;
154 
155  if ((tileIndex >= map->nTiles) || (tileIndex < 0))
156  Rast3d_fatal_error("Rast3d_read_tile: tile index out of range");
157 
158  if (map->index[tileIndex] == -1) {
159  Rast3d_set_null_tile_type(map, tile, type);
160  return 1;
161  }
162 
163  nofNum = Rast3d_compute_clipped_tile_dimensions(map, tileIndex, &rows,
164  &cols, &depths, &xRedundant,
165  &yRedundant, &zRedundant);
166 
167  if (lseek(map->data_fd, map->index[tileIndex], SEEK_SET) == -1) {
168  Rast3d_error("Rast3d_read_tile: can't position file");
169  return 0;
170  }
171 
172  if (map->compression == RASTER3D_NO_COMPRESSION) {
173  if (!Rast3d_readTileUncompressed(map, tileIndex, nofNum)) {
174  Rast3d_error(
175  "Rast3d_read_tile: error in Rast3d_readTileUncompressed");
176  return 0;
177  }
178  }
179  else if (!Rast3d_readTileCompressed(map, tileIndex, nofNum)) {
180  Rast3d_error("Rast3d_read_tile: error in Rast3d_readTileCompressed");
181  return 0;
182  }
183 
184  if (!Rast3d_xdrTile2tile(map, tile, rows, cols, depths, xRedundant,
185  yRedundant, zRedundant, nofNum, type)) {
186  Rast3d_error("Rast3d_read_tile: error in Rast3d_xdrTile2tile");
187  return 0;
188  }
189 
190  if (Rast3d_mask_is_off(map))
191  return 1;
192 
193  Rast3d_mask_tile(map, tileIndex, tile, type);
194  return 1;
195 }
196 
197 /*---------------------------------------------------------------------------*/
198 
199 /*!
200  * \brief
201  *
202  * Is equivalent to Rast3d_read_tile (map, tileIndex, tile, FCELL_TYPE).
203  *
204  * \param map
205  * \param tileIndex
206  * \param tile
207  * \return int
208  */
209 
210 int Rast3d_read_tile_float(RASTER3D_Map *map, int tileIndex, void *tile)
211 {
212  if (!Rast3d_read_tile(map, tileIndex, tile, FCELL_TYPE)) {
213  Rast3d_error("Rast3d_read_tile_float: error in Rast3d_read_tile");
214  return 0;
215  }
216 
217  return 1;
218 }
219 
220 /*---------------------------------------------------------------------------*/
221 
222 /*!
223  * \brief
224  *
225  * Is equivalent to Rast3d_read_tile (map, tileIndex, tile, DCELL_TYPE).
226  *
227  * \param map
228  * \param tileIndex
229  * \param tile
230  * \return int
231  */
232 
233 int Rast3d_read_tile_double(RASTER3D_Map *map, int tileIndex, void *tile)
234 {
235  if (!Rast3d_read_tile(map, tileIndex, tile, DCELL_TYPE)) {
236  Rast3d_error("Rast3d_read_tile_double: error in Rast3d_read_tile");
237  return 0;
238  }
239 
240  return 1;
241 }
242 
243 /*---------------------------------------------------------------------------*/
244 
245 /* CACHE-MODE-ONLY FUNCTIONS */
246 
247 /*---------------------------------------------------------------------------*/
248 
249 /*!
250  * \brief
251  *
252  * Locks tile with <em>tileIndex</em> in cache. If after locking fewer than the
253  * minimum number of unlocked tiles are unlocked, the lock request is ignored.
254  *
255  * \param map
256  * \param tileIndex
257  * \return 1 ... if successful,
258  * -1 ... if request is ignored,
259  * 0 ... otherwise.
260  */
261 
262 int Rast3d_lock_tile(RASTER3D_Map *map, int tileIndex)
263 {
264  if (!map->useCache)
266  "Rast3d_lock_tile: function invalid in non-cache mode");
267 
268  if (!Rast3d_cache_lock(map->cache, tileIndex)) {
269  Rast3d_error("Rast3d_lock_tile: error in Rast3d_cache_lock");
270  return 0;
271  }
272 
273  return 1;
274 }
275 
276 /*---------------------------------------------------------------------------*/
277 
278 /*!
279  * \brief
280  *
281  * Unlocks tile with <em>tileIndex</em>.
282  *
283  * \param map
284  * \param tileIndex
285  * \return 1 ... if successful,
286  * 0 ... otherwise.
287  */
288 
289 int Rast3d_unlock_tile(RASTER3D_Map *map, int tileIndex)
290 {
291  if (!map->useCache)
293  "Rast3d_unlock_tile: function invalid in non-cache mode");
294 
295  if (!Rast3d_cache_unlock(map->cache, tileIndex)) {
296  Rast3d_error("Rast3d_unlock_tile: error in Rast3d_cache_unlock");
297  return 0;
298  }
299 
300  return 1;
301 }
302 
303 /*---------------------------------------------------------------------------*/
304 
305 /*!
306  * \brief
307  *
308  * Unlocks every tile in cache of <em>map</em>.
309  *
310  * \param map
311  * \return 1 ... if successful,
312  * 0 ... otherwise.
313  */
314 
316 {
317  if (!map->useCache)
319  "Rast3d_unlock_all: function invalid in non-cache mode");
320 
321  if (!Rast3d_cache_unlock_all(map->cache)) {
322  Rast3d_error("Rast3d_unlock_all: error in Rast3d_cache_unlock_all");
323  return 0;
324  }
325 
326  return 1;
327 }
328 
329 /*---------------------------------------------------------------------------*/
330 
331 /*!
332  * \brief
333  *
334  * Turns autolock mode on.
335  *
336  * \param map
337  * \return void
338  */
339 
341 {
342  if (!map->useCache)
344  "Rast3d_autoLockOn: function invalid in non-cache mode");
345 
347 }
348 
349 /*---------------------------------------------------------------------------*/
350 
351 /*!
352  * \brief
353  *
354  * Turns autolock mode Off.
355  *
356  * \param map
357  * \return void
358  */
359 
361 {
362  if (!map->useCache)
364  "Rast3d_autoLockOff: function invalid in non-cache mode");
365 
367 }
368 
369 /*---------------------------------------------------------------------------*/
370 
371 /*!
372  * \brief
373  *
374  * Sets the minimum
375  * number of unlocked tiles to <em>minUnlocked</em>. This function should be
376  * used in combination with <tt>Rast3d_unlock_all ()</tt> in order to avoid
377  * situations where the new minimum is larger than the actual number of unlocked
378  * tiles. <em>minUnlocked</em> must be one of RASTER3D_USE_CACHE_X,
379  * RASTER3D_USE_CACHE_Y, RASTER3D_USE_CACHE_Z, RASTER3D_USE_CACHE_XY,
380  * RASTER3D_USE_CACHE_XZ, RASTER3D_USE_CACHE_YZ, RASTER3D_USE_CACHE_XYZ, the
381  * result of Rast3d_cache_size_encode() (cf.{g3d:G3d.cacheSizeEncode}), or any
382  * positive integer which explicitly specifies the number of tiles.
383  *
384  * \param map
385  * \param minUnlocked
386  * \return void
387  */
388 
389 void Rast3d_min_unlocked(RASTER3D_Map *map, int minUnlocked)
390 {
391  if (!map->useCache)
393  "Rast3d_autoLockOff: function invalid in non-cache mode");
394 
396  Rast3d__compute_cache_size(map, minUnlocked));
397 }
398 
399 /*---------------------------------------------------------------------------*/
400 
401 /*!
402  * \brief
403  *
404  * Starts a new cycle.
405  *
406  * \param map
407  * \return 1 ... if successful,
408  * 0 ... otherwise.
409  */
410 
412 {
413  if (!Rast3d_unlock_all(map)) {
414  Rast3d_fatal_error("Rast3d_begin_cycle: error in Rast3d_unlock_all");
415  return 0;
416  }
417 
418  Rast3d_autolock_on(map);
419  return 1;
420 }
421 
422 /*---------------------------------------------------------------------------*/
423 
424 /*!
425  * \brief
426  *
427  * Ends a cycle.
428  *
429  * \param map
430  * \return 1 ... if successful,
431  * 0 ... otherwise.
432  */
433 
435 {
436  Rast3d_autolock_off(map);
437  return 1;
438 }
#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:374
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:22
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:270
void Rast3d_set_null_value(void *, int, int)
Fills the vector pointed to by c with nofElts NULL-values of type.
Definition: null.c:34
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:253
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
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:151
void Rast3d_autolock_off(RASTER3D_Map *map)
Turns autolock mode Off.
Definition: tileread.c:360
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:389
int Rast3d_begin_cycle(RASTER3D_Map *map)
Starts a new cycle.
Definition: tileread.c:411
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:233
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:210
int Rast3d_end_cycle(RASTER3D_Map *map)
Ends a cycle.
Definition: tileread.c:434
int Rast3d_unlock_all(RASTER3D_Map *map)
Unlocks every tile in cache of map.
Definition: tileread.c:315
int Rast3d_unlock_tile(RASTER3D_Map *map, int tileIndex)
Unlocks tile with tileIndex.
Definition: tileread.c:289
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:262
void Rast3d_autolock_on(RASTER3D_Map *map)
Turns autolock mode on.
Definition: tileread.c:340