GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-d6dec75dd4
writeascii.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/gis.h>
6 #include <grass/raster3d.h>
7 
8 /*!
9  * \brief
10  *
11  * Writes the cell-values of <em>map</em> in ascii format to file
12  * <em>fname</em>. The values are organized by horizontal slices.
13  *
14  * \param map
15  * \param fname
16  * \return void
17  */
18 
19 void Rast3d_write_ascii(void *map, const char *fname)
20 {
21  FILE *fp;
22  DCELL d1 = 0;
23  DCELL *d1p;
24  FCELL *f1p;
25  int x, y, z;
26  int rows, cols, depths, typeIntern;
27 
28  Rast3d_get_coords_map(map, &rows, &cols, &depths);
29  typeIntern = Rast3d_tile_type_map(map);
30 
31  d1p = &d1;
32  f1p = (FCELL *)&d1;
33 
34  if (fname == NULL)
35  fp = stdout;
36  else if ((fp = fopen(fname, "w")) == NULL)
37  Rast3d_fatal_error("Rast3d_write_ascii: can't open file to write\n");
38 
39  for (z = 0; z < depths; z++) {
40  for (y = 0; y < rows; y++) {
41  fprintf(fp, "z y x %d %d (%d - %d)\n", z, y, 0, cols - 1);
42  for (x = 0; x < cols; x++) {
43  Rast3d_get_value_region(map, x, y, z, d1p, typeIntern);
44 
45  if (typeIntern == FCELL_TYPE)
46  fprintf(fp, "%.18f ", *f1p);
47  else
48  fprintf(fp, "%.50f ", d1);
49  }
50  fprintf(fp, "\n");
51  }
52  }
53 
54  if (fp != stdout)
55  fclose(fp);
56 }
#define NULL
Definition: ccmath.h:32
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_coords_map(RASTER3D_Map *, int *, int *, int *)
Returns the size of the region of map in cells.
Definition: headerinfo.c:18
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_fatal_error(const char *,...) __attribute__((format(printf
float FCELL
Definition: gis.h:630
double DCELL
Definition: gis.h:629
#define FCELL_TYPE
Definition: raster.h:12
void Rast3d_write_ascii(void *map, const char *fname)
Writes the cell-values of map in ascii format to file fname. The values are organized by horizontal s...
Definition: writeascii.c:19
#define x