GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
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
10 retileNocache(void *map, const char *nameOut, int tileX, int tileY, 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 = Rast3d_open_cell_new(nameOut, typeIntern, RASTER3D_NO_CACHE, &region);
27 
28  if (map2 == NULL)
29  Rast3d_fatal_error("Rast3d_retile: error in Rast3d_open_cell_new");
30 
31  Rast3d_set_file_type(saveType);
32  Rast3d_set_tile_dimension(tileXsave, tileYsave, tileZsave);
33 
34  data = Rast3d_alloc_tiles(map2, 1);
35  if (data == NULL)
36  Rast3d_fatal_error("Rast3d_retile: error in Rast3d_alloc_tiles");
37 
38  Rast3d_get_nof_tiles_map(map2, &nx, &ny, &nz);
39 
40  for (z = 0; z < nz; z++) {
41  G_percent(z, nz, 1);
42  for (y = 0; y < ny; y++)
43  for (x = 0; x < nx; x++) {
44  Rast3d_get_block(map, x * tileX, y * tileY, z * tileZ,
45  tileX, tileY, tileZ, data, typeIntern);
47  (map2, Rast3d_tile2tile_index(map2, x, y, z), data,
48  typeIntern))
50  ("Rast3d_retileNocache: error in Rast3d_write_tile");
51  }
52  }
53 
54  G_percent(1, 1, 1);
55 
56  Rast3d_free_tiles(data);
57  Rast3d_close(map2);
58 }
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
79 Rast3d_retile(void *map, const char *nameOut, int tileX, int tileY, 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 =
104  Rast3d_open_cell_new(nameOut, typeIntern, RASTER3D_USE_CACHE_DEFAULT, &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,
112  &xTile, &yTile, &zTile, &xOffs, &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,
124  &xOffs, &yOffs, &zOffs);
125  if (zTile > prev) {
126  if (!Rast3d_flush_all_tiles(map2))
127  Rast3d_fatal_error("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))
136  Rast3d_fatal_error("Rast3d_retile: error in Rast3d_put_value");
137  }
138  }
139 
140  G_percent(1, 1, 1);
141  if (!Rast3d_flush_all_tiles(map2))
142  Rast3d_fatal_error("Rast3d_retile: error in Rast3d_flush_all_tiles");
143  if (!Rast3d_close(map2))
144  Rast3d_fatal_error("Rast3d_retile: error in Rast3d_close");
145 }
#define RASTER3D_USE_CACHE_DEFAULT
Definition: raster3d.h:20
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:96
void Rast3d_get_region_struct_map(RASTER3D_Map *, RASTER3D_Region *)
Returns in region the region of map.
Definition: headerinfo.c:115
void Rast3d_set_tile_dimension(int, int, int)
set Tile Dimension
Definition: defaults.c:244
void Rast3d_get_coords_map(RASTER3D_Map *, int *, int *, int *)
Returns the size of the region of map in cells.
Definition: headerinfo.c:19
int Rast3d_file_type_map(RASTER3D_Map *)
Returns the type with which tiles of map are stored on file.
Definition: headerinfo.c:281
int Rast3d_tile_use_cache_map(RASTER3D_Map *)
Returns 1 if map uses cache, returns 0 otherwise.
Definition: headerinfo.c:315
int Rast3d_close(RASTER3D_Map *)
Close 3D raster map files.
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...
#define NULL
Definition: ccmath.h:32
#define x
void Rast3d_free_tiles(void *)
Is equivalent to Rast3d_free (tiles);
Definition: tilealloc.c:75
void Rast3d_fatal_error(const char *,...) __attribute__((format(printf
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:105
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. The source code can be found in retile.c.
Definition: retile.c:79
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:256
void G_percent(long, long, int)
Print percent complete messages.
Definition: percent.c:62
int Rast3d_get_file_type(void)
get G3d file type
Definition: defaults.c:227
void * Rast3d_alloc_tiles(RASTER3D_Map *, int)
Is equivalent to Rast3d_alloc_tiles_type (map, nofTiles, Rast3d_file_type_map (map)).
Definition: tilealloc.c:50
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:128
void Rast3d_get_tile_dimension(int *, int *, int *)
get Tile Dimension
Definition: defaults.c:271
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:136
int Rast3d_flush_all_tiles(RASTER3D_Map *)
Definition: cache.c:280
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:51
#define RASTER3D_NO_CACHE
Definition: raster3d.h:19
int Rast3d_tile_type_map(RASTER3D_Map *)
Returns the type in which tiles of map are stored in memory.
Definition: headerinfo.c:161
void Rast3d_set_file_type(int)
set G3d file type
Definition: defaults.c:210
int Rast3d_tile2tile_index(RASTER3D_Map *, int, int, int)
Returns tile-index corresponding to tile-coordinates (xTile, yTile, zTile).
Definition: tilemath.c:52