GRASS 8 Programmer's Manual  8.5.0dev(2025)-c070206eb1
retile.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 <grass/raster3d.h>
6 
7 /*---------------------------------------------------------------------------*/
8 
9 static void retileNocache(void *map, const char *nameOut, int tileX, int tileY,
10  int tileZ)
11 {
12  void *map2;
13  int x, y, z, saveType, nx, ny, nz;
14  int typeIntern;
15  void *data;
16  int tileXsave, tileYsave, tileZsave;
17  RASTER3D_Region region;
18 
19  saveType = Rast3d_get_file_type();
21  Rast3d_get_tile_dimension(&tileXsave, &tileYsave, &tileZsave);
22  Rast3d_set_tile_dimension(tileX, tileY, tileZ);
23  typeIntern = Rast3d_tile_type_map(map);
24  Rast3d_get_region_struct_map(map, &region);
25 
26  map2 =
27  Rast3d_open_cell_new(nameOut, typeIntern, RASTER3D_NO_CACHE, &region);
28 
29  if (map2 == NULL)
30  Rast3d_fatal_error("Rast3d_retile: error in Rast3d_open_cell_new");
31 
32  Rast3d_set_file_type(saveType);
33  Rast3d_set_tile_dimension(tileXsave, tileYsave, tileZsave);
34 
35  data = Rast3d_alloc_tiles(map2, 1);
36  if (data == NULL)
37  Rast3d_fatal_error("Rast3d_retile: error in Rast3d_alloc_tiles");
38 
39  Rast3d_get_nof_tiles_map(map2, &nx, &ny, &nz);
40 
41  for (z = 0; z < nz; z++) {
42  G_percent(z, nz, 1);
43  for (y = 0; y < ny; y++)
44  for (x = 0; x < nx; x++) {
45  Rast3d_get_block(map, x * tileX, y * tileY, z * tileZ, tileX,
46  tileY, tileZ, data, typeIntern);
47  if (!Rast3d_write_tile(map2,
48  Rast3d_tile2tile_index(map2, x, y, z),
49  data, typeIntern))
51  "Rast3d_retileNocache: error in Rast3d_write_tile");
52  }
53  }
54 
55  G_percent(1, 1, 1);
56 
57  Rast3d_free_tiles(data);
58  Rast3d_close(map2);
59 }
60 
61 /*---------------------------------------------------------------------------*/
62 
63 /*!
64  * \brief
65  *
66  * Makes a copy of <em>map</em> with name <em>nameOut</em> which has
67  * tile dimensions <em>tileX</em>, <em>tileY</em>, <em>tileZ</em>.
68  * The source code can be found in <em>retile.c</em>.
69  *
70  * \param map
71  * \param nameOut
72  * \param tileX
73  * \param tileY
74  * \param tileZ
75  * \return void
76  */
77 void Rast3d_retile(void *map, const char *nameOut, int tileX, int tileY,
78  int tileZ)
79 {
80  void *map2;
81  double value;
82  int x, y, z, saveType;
83  int rows, cols, depths, typeIntern;
84  int xTile, yTile, zTile;
85  int xOffs, yOffs, zOffs, prev;
86  int tileXsave, tileYsave, tileZsave;
87  RASTER3D_Region region;
88 
89  if (!Rast3d_tile_use_cache_map(map)) {
90  retileNocache(map, nameOut, tileX, tileY, tileZ);
91  return;
92  }
93 
94  saveType = Rast3d_get_file_type();
96  Rast3d_get_tile_dimension(&tileXsave, &tileYsave, &tileZsave);
97  Rast3d_set_tile_dimension(tileX, tileY, tileZ);
98 
99  typeIntern = Rast3d_tile_type_map(map);
100  Rast3d_get_region_struct_map(map, &region);
101 
102  map2 = Rast3d_open_cell_new(nameOut, typeIntern, RASTER3D_USE_CACHE_DEFAULT,
103  &region);
104  if (map2 == NULL)
105  Rast3d_fatal_error("Rast3d_retile: error in Rast3d_open_cell_new");
106 
107  Rast3d_set_file_type(saveType);
108  Rast3d_set_tile_dimension(tileXsave, tileYsave, tileZsave);
109 
110  Rast3d_coord2tile_coord(map2, 0, 0, 0, &xTile, &yTile, &zTile, &xOffs,
111  &yOffs, &zOffs);
112 
113  prev = zTile;
114 
115  x = 0;
116  y = 0;
117 
118  Rast3d_get_coords_map(map, &rows, &cols, &depths);
119 
120  for (z = 0; z < depths; z++) {
121  G_percent(z, depths, 1);
122  Rast3d_coord2tile_coord(map2, x, y, z, &xTile, &yTile, &zTile, &xOffs,
123  &yOffs, &zOffs);
124  if (zTile > prev) {
125  if (!Rast3d_flush_all_tiles(map2))
127  "Rast3d_retile: error in Rast3d_flush_all_tiles");
128  prev++;
129  }
130 
131  for (y = 0; y < rows; y++)
132  for (x = 0; x < cols; x++) {
133 
134  Rast3d_get_value_region(map, x, y, z, &value, typeIntern);
135  if (!Rast3d_put_value(map2, x, y, z, &value, typeIntern))
137  "Rast3d_retile: error in Rast3d_put_value");
138  }
139  }
140 
141  G_percent(1, 1, 1);
142  if (!Rast3d_flush_all_tiles(map2))
143  Rast3d_fatal_error("Rast3d_retile: error in Rast3d_flush_all_tiles");
144  if (!Rast3d_close(map2))
145  Rast3d_fatal_error("Rast3d_retile: error in Rast3d_close");
146 }
#define NULL
Definition: ccmath.h:32
void G_percent(long, long, int)
Print percent complete messages.
Definition: percent.c:61
int Rast3d_flush_all_tiles(RASTER3D_Map *)
Definition: cache.c:283
int Rast3d_file_type_map(RASTER3D_Map *)
Returns the type with which tiles of map are stored on file.
Definition: headerinfo.c:263
int Rast3d_tile_type_map(RASTER3D_Map *)
Returns the type in which tiles of map are stored in memory.
Definition: headerinfo.c:150
void Rast3d_get_nof_tiles_map(RASTER3D_Map *, int *, int *, int *)
Returns the dimensions of the tile-cube used to tile the region of map. These numbers include partial...
Definition: headerinfo.c:48
void Rast3d_get_tile_dimension(int *, int *, int *)
get Tile Dimension
Definition: defaults.c:252
void Rast3d_get_region_struct_map(RASTER3D_Map *, RASTER3D_Region *)
Returns in region the region of map.
Definition: headerinfo.c:108
void Rast3d_free_tiles(void *)
Is equivalent to Rast3d_free (tiles);
Definition: tilealloc.c:70
void Rast3d_set_tile_dimension(int, int, int)
set Tile Dimension
Definition: defaults.c:227
void Rast3d_get_coords_map(RASTER3D_Map *, int *, int *, int *)
Returns the size of the region of map in cells.
Definition: headerinfo.c:17
void Rast3d_get_block(RASTER3D_Map *, int, int, int, int, int, int, void *, int)
Copies the cells contained in the block (cube) with vertices (x0, y0, z0) and (x0 + nx - 1,...
Definition: getblock.c:101
int Rast3d_tile_use_cache_map(RASTER3D_Map *)
Returns 1 if map uses cache, returns 0 otherwise.
Definition: headerinfo.c:293
void Rast3d_set_file_type(int)
set G3d file type
Definition: defaults.c:197
int Rast3d_get_file_type(void)
get G3d file type
Definition: defaults.c:212
int Rast3d_put_value(RASTER3D_Map *, int, int, int, const void *, int)
After converting *value of type into the type specified at the initialization time (i....
Definition: putvalue.c:89
void * Rast3d_alloc_tiles(RASTER3D_Map *, int)
Is equivalent to Rast3d_alloc_tiles_type (map, nofTiles, Rast3d_file_type_map (map)).
Definition: tilealloc.c:47
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_get_value_region(RASTER3D_Map *, int, int, int, void *, int)
Returns in *value the cell-value of the cell with region-coordinate (x, y, z). The value returned is ...
Definition: getvalue.c:249
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
void * Rast3d_open_cell_new(const char *, int, int, RASTER3D_Region *)
Opens new g3d-file with name in the current mapset. Tiles are stored in memory with type which must b...
int Rast3d_write_tile(RASTER3D_Map *, int, const void *, int)
Writes tile with index tileIndex to the file corresponding to map. It is assumed that the cells in ti...
Definition: tilewrite.c:125
void Rast3d_fatal_error(const char *,...) __attribute__((format(printf
int Rast3d_close(RASTER3D_Map *)
Close 3D raster map files.
#define RASTER3D_USE_CACHE_DEFAULT
Definition: raster3d.h:19
#define RASTER3D_NO_CACHE
Definition: raster3d.h:18
void Rast3d_retile(void *map, const char *nameOut, int tileX, int tileY, int tileZ)
Makes a copy of map with name nameOut which has tile dimensions tileX, tileY, tileZ....
Definition: retile.c:77
#define x