GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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/G3d.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  G3D_Region region;
18 
19  saveType = G3d_getFileType();
21  G3d_getTileDimension(&tileXsave, &tileYsave, &tileZsave);
22  G3d_setTileDimension(tileX, tileY, tileZ);
23  typeIntern = G3d_tileTypeMap(map);
24  G3d_getRegionStructMap(map, &region);
25 
26  map2 = G3d_openCellNew(nameOut, typeIntern, G3D_NO_CACHE, &region);
27 
28  if (map2 == NULL)
29  G3d_fatalError("G3d_retile: error in G3d_openCellNew");
30 
31  G3d_setFileType(saveType);
32  G3d_setTileDimension(tileXsave, tileYsave, tileZsave);
33 
34  data = G3d_allocTiles(map2, 1);
35  if (data == NULL)
36  G3d_fatalError("G3d_retile: error in G3d_allocTiles");
37 
38  G3d_getNofTilesMap(map2, &nx, &ny, &nz);
39 
40  for (z = 0; z < nz; z++)
41  for (y = 0; y < ny; y++)
42  for (x = 0; x < nx; x++) {
43 
44  G3d_getBlock(map, x * tileX, y * tileY, z * tileZ,
45  tileX, tileY, tileZ, data, typeIntern);
46  if (!G3d_writeTile
47  (map2, G3d_tile2tileIndex(map2, x, y, z), data,
48  typeIntern))
50  ("G3d_retileNocache: error in G3d_writeTile");
51  }
52 
53  G3d_freeTiles(data);
54  G3d_closeCell(map2);
55 }
56 
57 /*---------------------------------------------------------------------------*/
58 
59 
75 void
76 G3d_retile(void *map, const char *nameOut, int tileX, int tileY, int tileZ)
77 {
78  void *map2;
79  double value;
80  int x, y, z, saveType;
81  int rows, cols, depths, typeIntern;
82  int xTile, yTile, zTile;
83  int xOffs, yOffs, zOffs, prev;
84  int tileXsave, tileYsave, tileZsave;
85  G3D_Region region;
86 
87  if (!G3d_tileUseCacheMap(map)) {
88  retileNocache(map, nameOut, tileX, tileY, tileZ);
89  return;
90  }
91 
92  saveType = G3d_getFileType();
94  G3d_getTileDimension(&tileXsave, &tileYsave, &tileZsave);
95  G3d_setTileDimension(tileX, tileY, tileZ);
96 
97  typeIntern = G3d_tileTypeMap(map);
98  G3d_getRegionStructMap(map, &region);
99 
100  map2 =
101  G3d_openCellNew(nameOut, typeIntern, G3D_USE_CACHE_DEFAULT, &region);
102  if (map2 == NULL)
103  G3d_fatalError("G3d_retile: error in G3d_openCellNew");
104 
105  G3d_setFileType(saveType);
106  G3d_setTileDimension(tileXsave, tileYsave, tileZsave);
107 
108  G3d_coord2tileCoord(map2, 0, 0, 0,
109  &xTile, &yTile, &zTile, &xOffs, &yOffs, &zOffs);
110 
111  prev = zTile;
112 
113  x = 0;
114  y = 0;
115 
116  G3d_getCoordsMap(map, &rows, &cols, &depths);
117 
118  for (z = 0; z < depths; z++) {
119  G3d_coord2tileCoord(map2, x, y, z, &xTile, &yTile, &zTile,
120  &xOffs, &yOffs, &zOffs);
121  if (zTile > prev) {
122  if (!G3d_flushAllTiles(map2))
123  G3d_fatalError("G3d_retile: error in G3d_flushAllTiles");
124  prev++;
125  }
126 
127  for (y = 0; y < rows; y++)
128  for (x = 0; x < cols; x++) {
129 
130  G3d_getValueRegion(map, x, y, z, &value, typeIntern);
131  if (!G3d_putValue(map2, x, y, z, &value, typeIntern))
132  G3d_fatalError("G3d_retile: error in G3d_putValue");
133  }
134  }
135 
136  if (!G3d_flushAllTiles(map2))
137  G3d_fatalError("G3d_retile: error in G3d_flushAllTiles");
138  if (!G3d_closeCell(map2))
139  G3d_fatalError("G3d_retile: error in G3d_closeCell");
140 }
int G3d_writeTile(G3D_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:128
void G3d_getValueRegion(G3D_Map *map, int x, int y, int z, void *value, int type)
Returns in *value the cell-value of the cell with region-coordinate (x, y, z). The value returned is ...
Definition: tileio.c:244
void G3d_getRegionStructMap(G3D_Map *map, G3D_Region *region)
Returns in region the region of map.
Definition: headerinfo.c:115
int G3d_flushAllTiles(G3D_Map *map)
Definition: g3dcache.c:278
void * G3d_allocTiles(G3D_Map *map, int nofTiles)
Is equivalent to G3d_allocTilesType (map, nofTiles, G3d_fileTypeMap (map)).
Definition: tilealloc.c:52
int y
Definition: plot.c:34
void G3d_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:76
void G3d_setFileType(int type)
set G3d file type
Definition: g3ddefaults.c:223
void G3d_getNofTilesMap(G3D_Map *map, int *nx, int *ny, int *nz)
Returns the dimensions of the tile-cube used to tile the region of map. These numbers include partial...
Definition: headerinfo.c:51
tuple data
void G3d_getTileDimension(int *tileX, int *tileY, int *tileZ)
get Tile Dimension
Definition: g3ddefaults.c:291
def region
Returns the output from running &quot;g.region -gu&quot;, as a dictionary.
Definition: core.py:576
int G3d_tileUseCacheMap(G3D_Map *map)
Returns 1 if map uses cache, returns 0 otherwise.
Definition: headerinfo.c:212
char * value
Definition: env.c:30
int G3d_closeCell(G3D_Map *map)
Closes g3d-file. If map is new and cache-mode is used for map then every tile which is not flushed be...
Definition: g3dclose.c:144
return NULL
Definition: dbfopen.c:1394
tuple cols
void G3d_freeTiles(void *tiles)
Is equivalent to G3d_free (tiles);
Definition: tilealloc.c:77
void G3d_setTileDimension(int tileX, int tileY, int tileZ)
set Tile Dimension
Definition: g3ddefaults.c:262
int G3d_fileTypeMap(G3D_Map *map)
Returns the type with which tiles of map are stored on file.
Definition: headerinfo.c:178
void G3d_coord2tileCoord(G3D_Map *map, int x, int y, int z, int *xTile, int *yTile, int *zTile, int *xOffs, int *yOffs, int *zOffs)
Converts cell-coordinates (x, y, z) into tile-coordinates (xTile, yTile, zTile) and the coordinate of...
Definition: tilemath.c:136
void G3d_getBlock(G3D_Map *map, int x0, int y0, int z0, int nx, int ny, int nz, void *block, int type)
Copies the cells contained in the block (cube) with vertices (x0, y0, z0) and (x0 + nx - 1...
Definition: getblock.c:103
int G3d_tile2tileIndex(G3D_Map *map, int xTile, int yTile, int zTile)
Returns tile-index corresponding to tile-coordinates (xTile, yTile, zTile).
Definition: tilemath.c:52
int G3d_tileTypeMap(G3D_Map *map)
Returns the type in which tiles of map are stored in memory.
Definition: headerinfo.c:161
void G3d_fatalError(const char *,...)
This function prints the error message msg, and terminates the program with an error status...
Definition: g3derror.c:58
void G3d_getCoordsMap(G3D_Map *map, int *rows, int *cols, int *depths)
Returns the size of the region of map in cells.
Definition: headerinfo.c:19
int G3d_putValue(G3D_Map *map, int x, int y, int z, const void *value, int type)
After converting *value of type into the type specified at the initialization time (i...
Definition: tilewrite.c:540
void * G3d_openCellNew(const char *name, int typeIntern, int cache, G3D_Region *region)
Opens new g3d-file with name in the current mapset. Tiles are stored in memory with type which must b...
Definition: g3dopen.c:213
int G3d_getFileType()
get G3d file type
Definition: g3ddefaults.c:243