GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-36359e2344
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 
78 void Rast3d_retile(void *map, const char *nameOut, int tileX, int tileY,
79  int tileZ)
80 {
81  void *map2;
82  double value;
83  int x, y, z, saveType;
84  int rows, cols, depths, typeIntern;
85  int xTile, yTile, zTile;
86  int xOffs, yOffs, zOffs, prev;
87  int tileXsave, tileYsave, tileZsave;
88  RASTER3D_Region region;
89 
90  if (!Rast3d_tile_use_cache_map(map)) {
91  retileNocache(map, nameOut, tileX, tileY, tileZ);
92  return;
93  }
94 
95  saveType = Rast3d_get_file_type();
97  Rast3d_get_tile_dimension(&tileXsave, &tileYsave, &tileZsave);
98  Rast3d_set_tile_dimension(tileX, tileY, tileZ);
99 
100  typeIntern = Rast3d_tile_type_map(map);
101  Rast3d_get_region_struct_map(map, &region);
102 
103  map2 = Rast3d_open_cell_new(nameOut, typeIntern, RASTER3D_USE_CACHE_DEFAULT,
104  &region);
105  if (map2 == NULL)
106  Rast3d_fatal_error("Rast3d_retile: error in Rast3d_open_cell_new");
107 
108  Rast3d_set_file_type(saveType);
109  Rast3d_set_tile_dimension(tileXsave, tileYsave, tileZsave);
110 
111  Rast3d_coord2tile_coord(map2, 0, 0, 0, &xTile, &yTile, &zTile, &xOffs,
112  &yOffs, &zOffs);
113 
114  prev = zTile;
115 
116  x = 0;
117  y = 0;
118 
119  Rast3d_get_coords_map(map, &rows, &cols, &depths);
120 
121  for (z = 0; z < depths; z++) {
122  G_percent(z, depths, 1);
123  Rast3d_coord2tile_coord(map2, x, y, z, &xTile, &yTile, &zTile, &xOffs,
124  &yOffs, &zOffs);
125  if (zTile > prev) {
126  if (!Rast3d_flush_all_tiles(map2))
128  "Rast3d_retile: error in Rast3d_flush_all_tiles");
129  prev++;
130  }
131 
132  for (y = 0; y < rows; y++)
133  for (x = 0; x < cols; x++) {
134 
135  Rast3d_get_value_region(map, x, y, z, &value, typeIntern);
136  if (!Rast3d_put_value(map2, x, y, z, &value, typeIntern))
138  "Rast3d_retile: error in Rast3d_put_value");
139  }
140  }
141 
142  G_percent(1, 1, 1);
143  if (!Rast3d_flush_all_tiles(map2))
144  Rast3d_fatal_error("Rast3d_retile: error in Rast3d_flush_all_tiles");
145  if (!Rast3d_close(map2))
146  Rast3d_fatal_error("Rast3d_retile: error in Rast3d_close");
147 }
#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:276
int Rast3d_tile_type_map(RASTER3D_Map *)
Returns the type in which tiles of map are stored in memory.
Definition: headerinfo.c:156
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:50
void Rast3d_get_tile_dimension(int *, int *, int *)
get Tile Dimension
Definition: defaults.c:262
void Rast3d_get_region_struct_map(RASTER3D_Map *, RASTER3D_Region *)
Returns in region the region of map.
Definition: headerinfo.c:112
void Rast3d_free_tiles(void *)
Is equivalent to Rast3d_free (tiles);
Definition: tilealloc.c:73
void Rast3d_set_tile_dimension(int, int, int)
set Tile Dimension
Definition: defaults.c:236
void Rast3d_get_coords_map(RASTER3D_Map *, int *, int *, int *)
Returns the size of the region of map in cells.
Definition: headerinfo.c:18
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:102
int Rast3d_tile_use_cache_map(RASTER3D_Map *)
Returns 1 if map uses cache, returns 0 otherwise.
Definition: headerinfo.c:308
void Rast3d_set_file_type(int)
set G3d file type
Definition: defaults.c:204
int Rast3d_get_file_type(void)
get G3d file type
Definition: defaults.c:220
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:92
void * Rast3d_alloc_tiles(RASTER3D_Map *, int)
Is equivalent to Rast3d_alloc_tiles_type (map, nofTiles, Rast3d_file_type_map (map)).
Definition: tilealloc.c:49
int Rast3d_tile2tile_index(RASTER3D_Map *, int, int, int)
Returns tile-index corresponding to tile-coordinates (xTile, yTile, zTile).
Definition: tilemath.c:49
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:257
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:129
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:126
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:78
#define x