GRASS 8 Programmer's Manual 8.6.0dev(2026)-ddeab64dbf
Loading...
Searching...
No Matches
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 <string.h>
6#include <errno.h>
7
8#include <grass/gis.h>
9#include <grass/raster.h>
10#include <grass/glocale.h>
11
12#include "raster3d_intern.h"
13
14/*---------------------------------------------------------------------------*/
15
16static int Rast3d_tile2xdrTile(RASTER3D_Map *map, const void *tile, int rows,
17 int cols, int depths, int xRedundant,
19 int nofNum, int type)
20{
21 int y, z;
22
23 if (!Rast3d_init_copy_to_xdr(map, type)) {
24 Rast3d_error("Rast3d_tile2xdrTile: error in Rast3d_init_copy_to_xdr");
25 return 0;
26 }
27
28 if (nofNum == map->tileSize) {
29 if (!Rast3d_copy_to_xdr(tile, map->tileSize)) {
30 Rast3d_error("Rast3d_tile2xdrTile: error in Rast3d_copy_to_xdr");
31 return 0;
32 }
33 return 1;
34 }
35
36 if (xRedundant) {
37 for (z = 0; z < depths; z++) {
38 for (y = 0; y < rows; y++) {
39 if (!Rast3d_copy_to_xdr(tile, cols)) {
41 "Rast3d_tile2xdrTile: error in Rast3d_copy_to_xdr");
42 return 0;
43 }
45 }
46 if (yRedundant)
48 Rast3d_length(type));
49 }
50 return 1;
51 }
52
53 if (yRedundant) {
54 for (z = 0; z < depths; z++) {
55 if (!Rast3d_copy_to_xdr(tile, map->tileX * rows)) {
57 "Rast3d_tile2xdrTile: error in Rast3d_copy_to_xdr");
58 return 0;
59 }
61 }
62 return 1;
63 }
64
65 if (!Rast3d_copy_to_xdr(tile, map->tileXY * depths)) {
66 Rast3d_error("Rast3d_tile2xdrTile: error in Rast3d_copy_to_xdr");
67 return 0;
68 }
69 return 1;
70}
71
72/*---------------------------------------------------------------------------*/
73
74static int Rast3d_writeTileUncompressed(RASTER3D_Map *map, int nofNum)
75{
76 if (write(map->data_fd, xdr, map->numLengthExtern * nofNum) !=
77 map->numLengthExtern * nofNum) {
78 Rast3d_error("Rast3d_writeTileUncompressed: can't write file.");
79 return 0;
80 }
81
82 return 1;
83}
84
85/*---------------------------------------------------------------------------*/
86
87static int Rast3d_writeTileCompressed(RASTER3D_Map *map, int nofNum)
88{
91 map->type == FCELL_TYPE)) {
92 Rast3d_error("Rast3d_writeTileCompressed: error in "
93 "Rast3d_fpcompress_write_xdr_nums");
94 return 0;
95 }
96
97 return 1;
98}
99
100/*---------------------------------------------------------------------------*/
101
102/*---------------------------------------------------------------------------*/
103
104/* EXPORTED FUNCTIONS */
105
106/*---------------------------------------------------------------------------*/
107
108/*---------------------------------------------------------------------------*/
109
110/*!
111 * \brief
112 *
113 *
114 * Writes tile with index <em>tileIndex</em> to the file corresponding to
115 * <em>map</em>. It is assumed that the cells in <em>tile</em> are of
116 * <em>type</em> which must be one of FCELL_TYPE and DCELL_TYPE. The actual
117 * type used to write the tile depends on the type specified at the time when
118 * <em>map</em> is initialized. A tile can only be written once. Subsequent
119 * attempts to write the same tile are ignored.
120 *
121 * \param map
122 * \param tileIndex
123 * \param tile
124 * \param type
125 * \return 1 ... if successful,
126 * 2 ... if write request was ignored,
127 * 0 ... otherwise.
128 */
129int Rast3d_write_tile(RASTER3D_Map *map, int tileIndex, const void *tile,
130 int type)
131{
132 int rows, cols, depths, xRedundant, yRedundant, zRedundant, nofNum;
133
134 /* valid tileIndex ? */
135 if ((tileIndex > map->nTiles) || (tileIndex < 0))
136 Rast3d_fatal_error("Rast3d_write_tile: tileIndex out of range");
137
138 /* already written ? */
139 if (map->index[tileIndex] != -1)
140 return 2;
141
142 /* save the file position */
143 map->index[tileIndex] = lseek(map->data_fd, (long)0, SEEK_END);
144 if (map->index[tileIndex] == -1) {
145 Rast3d_error("Rast3d_write_tile: can't position file");
146 return 0;
147 }
148
150 &cols, &depths, &xRedundant,
152
153 Rast3d_range_update_from_tile(map, tile, rows, cols, depths, xRedundant,
155
156 if (!Rast3d_tile2xdrTile(map, tile, rows, cols, depths, xRedundant,
157 yRedundant, zRedundant, nofNum, type)) {
158 Rast3d_error("Rast3d_write_tile: error in Rast3d_tile2xdrTile");
159 return 0;
160 }
161
163 if (!Rast3d_writeTileUncompressed(map, nofNum)) {
165 "Rast3d_write_tile: error in Rast3d_writeTileUncompressed");
166 return 0;
167 }
168 }
169 else {
170 if (!Rast3d_writeTileCompressed(map, nofNum)) {
172 "Rast3d_write_tile: error in Rast3d_writeTileCompressed");
173 return 0;
174 }
175 }
176
177 /* compute the length */
178 map->tileLength[tileIndex] =
179 lseek(map->data_fd, (long)0, SEEK_END) - map->index[tileIndex];
180 if (map->tileLength[tileIndex] == -1) {
181 int err = errno;
182 G_fatal_error(_("File read/write operation failed: %s (%d)"),
183 strerror(err), err);
184 }
185
186 return 1;
187}
188
189/*---------------------------------------------------------------------------*/
190
191/*!
192 * \brief
193 *
194 * Is equivalent to <tt>Rast3d_write_tile (map, tileIndex, tile,
195 * FCELL_TYPE).</tt>
196 *
197 * \param map
198 * \param tileIndex
199 * \param tile
200 * \return int
201 */
203{
204 int status;
205
206 if ((status = Rast3d_write_tile(map, tileIndex, tile, FCELL_TYPE)))
207 return status;
208
209 Rast3d_error("Rast3d_write_tile_float: error in Rast3d_write_tile");
210 return 0;
211}
212
213/*---------------------------------------------------------------------------*/
214
215/*!
216 * \brief
217 *
218 * Is equivalent to <tt>Rast3d_write_tile (map, tileIndex, tile,
219 * DCELL_TYPE).</tt>
220 *
221 * \param map
222 * \param tileIndex
223 * \param tile
224 * \return int
225 */
227{
228 int status;
229
230 if ((status = Rast3d_write_tile(map, tileIndex, tile, DCELL_TYPE)))
231 return status;
232
233 Rast3d_error("Rast3d_write_tile_double: error in Rast3d_write_tile");
234 return 0;
235}
236
237/*---------------------------------------------------------------------------*/
238
239/* CACHE-MODE-ONLY FUNCTIONS */
240
241/*---------------------------------------------------------------------------*/
242
243/*!
244 * \brief
245 *
246 * Writes the tile with
247 * <em>tileIndex</em> to the file corresponding to <em>map</em> and removes the
248 * tile from the cache (in non-cache mode the buffer provided by the
249 * map-structure is written). If this tile has already been written before the
250 * write request is ignored. If the tile was never referred to before the
251 * invocation of Rast3d_flush_tile, a tile filled with NULL-values is written.
252 *
253 * \param map
254 * \param tileIndex
255 * \return 1 ... if successful,
256 * 0 ... otherwise.
257 */
259{
260 const void *tile;
261
263 if (tile == NULL) {
264 Rast3d_error("Rast3d_flush_tile: error in Rast3d_get_tile_ptr");
265 return 0;
266 }
267
268 if (!Rast3d_write_tile(map, tileIndex, tile, map->typeIntern)) {
269 Rast3d_error("Rast3d_flush_tile: error in Rast3d_write_tile");
270 return 0;
271 }
272
273 if (!Rast3d__remove_tile(map, tileIndex)) {
274 Rast3d_error("Rast3d_flush_tile: error in Rast3d__remove_tile");
275 return 0;
276 }
277
278 return 1;
279}
280
281/*---------------------------------------------------------------------------*/
282
283/*!
284 * \brief
285 *
286 * Writes the tiles with tile-coordinates
287 * contained in the axis-parallel cube with vertices <em>(xMin, yMin, zMin)</em>
288 * and <em>(xMax, yMax, zMax</em>). Tiles which are not stored in the cache are
289 * written as NULL-tiles. Write attempts for tiles which have already been
290 * written earlier are ignored.
291 *
292 * \param map
293 * \param xMin
294 * \param yMin
295 * \param zMin
296 * \param xMax
297 * \param yMax
298 * \param zMax
299 * \return 1 ... if successful,
300 * 0 ... otherwise.
301 */
303 int xMax, int yMax, int zMax)
304{
305 int x, y, z;
306
307 if (!map->useCache)
309 "Rast3d_flush_tile_cube: function invalid in non-cache mode");
310
311 for (x = xMin; x <= xMax; x++)
312 for (y = yMin; y <= yMax; y++)
313 for (z = zMin; z <= zMax; z++)
314 if (!Rast3d_flush_tile(map,
315 Rast3d_tile2tile_index(map, x, y, z))) {
317 "Rast3d_flush_tile_cube: error in Rast3d_flush_tile");
318 return 0;
319 }
320
321 return 1;
322}
323
324/*---------------------------------------------------------------------------*/
325
326/*!
327 * \brief
328 *
329 * Writes those tiles for which
330 * <em>every</em> cell has coordinate contained in the axis-parallel cube
331 * defined by the vertices with cell-coordinates <em>(xMin, yMin, zMin)</em>
332 * and <em>(xMax, yMax, zMax)</em>.
333 * Tiles which are not stored in the cache are written as NULL-tiles.
334 * Write attempts for tiles which have already been written earlier are
335 * ignored.
336 *
337 * \param map
338 * \param xMin
339 * \param yMin
340 * \param zMin
341 * \param xMax
342 * \param yMax
343 * \param zMax
344 * \return 1 ... if successful,
345 * 0 ... otherwise.
346 */
348 int xMax, int yMax, int zMax)
349{
351 int xOffs, yOffs, zOffs;
353
354 if (!map->useCache)
356 "Rast3d_flush_tiles_in_cube: function invalid in non-cache mode");
357 /*AV*/
358 /*BEGIN OF ORIGINAL CODE */
359 /*
360 * Rast3d_get_coords_map (map, &regionMaxX, &regionMaxY, &regionMaxZ);
361 */
362 /*AV*/
363 /* BEGIN OF MY CODE */
365 /* END OF MY CODE */
366
367 if ((xMin < 0) && (xMax < 0))
369 "Rast3d_flush_tiles_in_cube: coordinate out of Range");
370 if ((xMin >= regionMaxX) && (xMax >= regionMaxX))
372 "Rast3d_flush_tiles_in_cube: coordinate out of Range");
373
374 xMin = MIN(MAX(0, xMin), regionMaxX - 1);
375
376 if ((yMin < 0) && (yMax < 0))
378 "Rast3d_flush_tiles_in_cube: coordinate out of Range");
379 if ((yMin >= regionMaxY) && (yMax >= regionMaxY))
381 "Rast3d_flush_tiles_in_cube: coordinate out of Range");
382
383 yMin = MIN(MAX(0, yMin), regionMaxY - 1);
384
385 if ((zMin < 0) && (zMax < 0))
387 "Rast3d_flush_tiles_in_cube: coordinate out of Range");
388 if ((zMin >= regionMaxZ) && (zMax >= regionMaxZ))
390 "Rast3d_flush_tiles_in_cube: coordinate out of Range");
391
392 zMin = MIN(MAX(0, zMin), regionMaxZ - 1);
393
395 &zTileMin, &xOffs, &yOffs, &zOffs);
396
397 if (xOffs != 0)
398 xTileMin++;
399 if (yOffs != 0)
400 yTileMin++;
401 if (zOffs != 0)
402 zTileMin++;
403
404 Rast3d_coord2tile_coord(map, xMax + 1, yMax + 1, zMax + 1, &xTileMax,
406
407 xTileMax--;
408 yTileMax--;
409 zTileMax--;
410
412 yTileMax, zTileMax)) {
414 "Rast3d_flush_tiles_in_cube: error in Rast3d_flush_tile_cube");
415 return 0;
416 }
417
418 return 1;
419}
420
421#undef MIN
422#undef MAX
423
424/*---------------------------------------------------------------------------*/
#define NULL
Definition ccmath.h:32
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
#define G_incr_void_ptr(ptr, size)
Definition defs/gis.h:81
void * Rast3d_get_tile_ptr(RASTER3D_Map *, int)
This function returns a pointer to a tile which contains the data for the tile with index tileIndex....
Definition tileio.c:78
void Rast3d_get_coords_map(RASTER3D_Map *, int *, int *, int *)
Returns the size of the region of map in cells.
Definition headerinfo.c:17
int Rast3d_tile2tile_index(RASTER3D_Map *, int, int, int)
Returns tile-index corresponding to tile-coordinates (xTile, yTile, zTile).
Definition tilemath.c:47
void Rast3d_range_update_from_tile(RASTER3D_Map *, const void *, int, int, int, int, int, int, int, int)
void Rast3d_coord2tile_coord(RASTER3D_Map *, int, int, int, int *, int *, int *, int *, int *, int *)
Converts cell-coordinates (x, y, z) into tile-coordinates (xTile, yTile, zTile) and the coordinate of...
Definition tilemath.c:124
int Rast3d_length(int)
int Rast3d_copy_to_xdr(const void *, int)
Definition fpxdr.c:141
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__remove_tile(RASTER3D_Map *, int)
Definition tileio.c:133
void Rast3d_fatal_error(const char *,...) __attribute__((format(printf
int Rast3d_init_copy_to_xdr(RASTER3D_Map *, int)
Definition fpxdr.c:102
int Rast3d_fpcompress_write_xdr_nums(int, char *, int, int, char *, int)
Definition fpcompress.c:685
#define MIN(a, b)
Definition gis.h:153
#define UNUSED
A macro for an attribute, if attached to a variable, indicating that the variable is not used.
Definition gis.h:46
#define MAX(a, b)
Definition gis.h:148
#define _(str)
Definition glocale.h:10
void * xdr
void * tmpCompress
#define RASTER3D_NO_COMPRESSION
Definition raster3d.h:13
#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 compression
Definition raster3d.h:111
int * tileLength
Definition raster3d.h:145
SYMBOL * err(FILE *fp, SYMBOL *s, char *msg)
int Rast3d_write_tile_double(RASTER3D_Map *map, int tileIndex, const void *tile)
Is equivalent to Rast3d_write_tile (map, tileIndex, tile, DCELL_TYPE).
Definition tilewrite.c:226
int Rast3d_flush_tile_cube(RASTER3D_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:302
int Rast3d_write_tile_float(RASTER3D_Map *map, int tileIndex, const void *tile)
Is equivalent to Rast3d_write_tile (map, tileIndex, tile, FCELL_TYPE).
Definition tilewrite.c:202
int Rast3d_flush_tile(RASTER3D_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:258
int Rast3d_write_tile(RASTER3D_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:129
int Rast3d_flush_tiles_in_cube(RASTER3D_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:347
#define write
Definition unistd.h:6
#define x