GRASS GIS 8 Programmer's Manual  8.4.0dev(2024)-112dd97adf
raster3d/range.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 
6 #include <grass/gis.h>
7 #include <grass/raster.h>
8 #include <grass/glocale.h>
9 
10 #include "raster3d_intern.h"
11 
12 /*---------------------------------------------------------------------------*/
13 
14 void Rast3d_range_update_from_tile(RASTER3D_Map *map, const void *tile,
15  int rows, int cols, int depths,
16  int xRedundant, int yRedundant,
17  int zRedundant UNUSED, int nofNum, int type)
18 {
19  int y, z, cellType;
20  struct FPRange *range;
21 
22  range = &(map->range);
23  cellType = Rast3d_g3d_type2cell_type(type);
24 
25  if (nofNum == map->tileSize) {
26  Rast_row_update_fp_range(tile, map->tileSize, range, cellType);
27  return;
28  }
29 
30  if (xRedundant) {
31  for (z = 0; z < depths; z++) {
32  for (y = 0; y < rows; y++) {
33  Rast_row_update_fp_range(tile, cols, range, cellType);
34  tile = G_incr_void_ptr(tile, map->tileX * Rast3d_length(type));
35  }
36  if (yRedundant)
37  tile = G_incr_void_ptr(tile, map->tileX * yRedundant *
38  Rast3d_length(type));
39  }
40  return;
41  }
42 
43  if (yRedundant) {
44  for (z = 0; z < depths; z++) {
45  Rast_row_update_fp_range(tile, map->tileX * rows, range, cellType);
46  tile = G_incr_void_ptr(tile, map->tileXY * Rast3d_length(type));
47  }
48  return;
49  }
50 
51  Rast_row_update_fp_range(tile, map->tileXY * depths, range, cellType);
52 }
53 
54 /*---------------------------------------------------------------------------*/
55 
56 int Rast3d_read_range(const char *name, const char *mapset,
57  struct FPRange *drange)
58 /* adapted from Rast_read_fp_range */
59 {
60  int fd;
61  int bytes_read;
62  char xdr_buf[2 * RASTER3D_XDR_DOUBLE_LENGTH];
63  DCELL dcell1, dcell2;
64 
65  Rast_init_fp_range(drange);
66 
67  fd = -1;
68 
70  mapset);
71  if (fd < 0) {
72  G_warning(_("Unable to open range file for [%s in %s]"), name, mapset);
73  return -1;
74  }
75 
76  bytes_read = read(fd, xdr_buf, 2 * RASTER3D_XDR_DOUBLE_LENGTH);
77 
78  /* if the f_range file exists, but empty the range is NULL -> a NULL map */
79  if (bytes_read == 0) {
80  close(fd);
81  return 1;
82  }
83 
84  if (bytes_read != 2 * RASTER3D_XDR_DOUBLE_LENGTH) {
85  close(fd);
86  G_warning(_("Error reading range file for [%s in %s]"), name, mapset);
87  return 2;
88  }
89 
90  G_xdr_get_double(&dcell1, &xdr_buf[RASTER3D_XDR_DOUBLE_LENGTH * 0]);
91  G_xdr_get_double(&dcell2, &xdr_buf[RASTER3D_XDR_DOUBLE_LENGTH * 1]);
92 
93  Rast_update_fp_range(dcell1, drange);
94  Rast_update_fp_range(dcell2, drange);
95  close(fd);
96  return 1;
97 }
98 
99 /*---------------------------------------------------------------------------*/
100 
101 /*!
102  * \brief Loads the range into the range structure of <em>map</em>.
103  *
104  * \param map a pointer to a raster 3D map object
105  * \return 1 ... if successful
106  * 0 ... otherwise.
107  */
108 
110 {
111  if (map->operation == RASTER3D_WRITE_DATA)
112  return 1;
113  if (Rast3d_read_range(map->fileName, map->mapset, &(map->range)) == -1) {
114  return 0;
115  }
116 
117  return 1;
118 }
119 
120 /*---------------------------------------------------------------------------*/
121 
122 /*!
123  * \brief Returns in <em>min</em> and <em>max</em> the minimum and maximum
124  * values of the range.
125  *
126  * \param map a pointer to a raster 3D map object
127  * \param min a pointer to a double to store minumim
128  * \param max a pointer to a double to store maximum
129  */
130 
131 void Rast3d_range_min_max(RASTER3D_Map *map, double *min, double *max)
132 {
134 }
135 
136 /*-------------------------------------------------------------------------*/
137 
138 static int writeRange(const char *name, struct FPRange *range)
139 /* adapted from Rast_write_fp_range */
140 {
141  char xdr_buf[2 * RASTER3D_XDR_DOUBLE_LENGTH];
142  int fd;
143 
145  if (fd < 0) {
146  G_warning(_("Unable to open range file for <%s>"), name);
147  return -1;
148  }
149 
150  if (range->first_time) {
151  /* if range hasn't been updated, write empty file meaning NULLs */
152  close(fd);
153  return 0;
154  }
155 
156  G_xdr_put_double(&xdr_buf[RASTER3D_XDR_DOUBLE_LENGTH * 0], &range->min);
157  G_xdr_put_double(&xdr_buf[RASTER3D_XDR_DOUBLE_LENGTH * 1], &range->max);
158 
159  if (write(fd, xdr_buf, RASTER3D_XDR_DOUBLE_LENGTH * 2) !=
161  goto error;
162 
163  close(fd);
164  return 0;
165 
166 error:
167  close(fd);
169  name); /* remove the old file with this name */
170  G_warning("can't write range file for [%s in %s]", name, G_mapset());
171  return -1;
172 }
173 
174 /*---------------------------------------------------------------------------*/
175 
176 /*!
177  * \brief
178  *
179  * Writes the range which is stored in the range structure of <em>map</em>.
180  * (This function is invoked automatically when a new file is closed).
181  *
182  * \param map
183  * \return 1 ... if successful
184  * 0 ... otherwise.
185  */
187 {
188  char path[GPATH_MAX];
189 
191  remove(path);
192 
193  if (writeRange(map->fileName, &(map->range)) == -1) {
194  Rast3d_error("Rast3d_closeCellNew: error in writeRange");
195  return 0;
196  }
197 
198  return 1;
199 }
200 
201 /*---------------------------------------------------------------------------*/
202 
204 {
205  Rast_init_fp_range(&(map->range));
206  return 0;
207 }
void G_warning(const char *,...) __attribute__((format(printf
int G_open_old_misc(const char *, const char *, const char *, const char *)
open a database misc file for reading
Definition: open_misc.c:132
const char * G_mapset(void)
Get current mapset name.
Definition: gis/mapset.c:33
int G_remove_misc(const char *, const char *, const char *)
Remove a database misc file.
Definition: remove.c:65
void G_xdr_put_double(void *, const double *)
Definition: gis/xdr.c:94
void G_xdr_get_double(double *, const void *)
Definition: gis/xdr.c:89
#define G_incr_void_ptr(ptr, size)
Definition: defs/gis.h:81
int G_open_new_misc(const char *, const char *, const char *)
open a new database misc file
Definition: open_misc.c:111
int Rast3d_g3d_type2cell_type(int)
Definition: raster3d/misc.c:13
void Rast3d_filename(char *, const char *, const char *, const char *)
Definition: filename.c:9
int Rast3d_length(int)
Definition: raster3d/misc.c:75
void Rast3d_error(const char *,...) __attribute__((format(printf
void Rast_get_fp_range_min_max(const struct FPRange *, DCELL *, DCELL *)
Get minimum and maximum value from fp range.
Definition: raster/range.c:768
void Rast_init_fp_range(struct FPRange *)
Initialize fp range.
Definition: raster/range.c:746
void Rast_row_update_fp_range(const void *, int, struct FPRange *, RASTER_MAP_TYPE)
Update range structure based on raster row (floating-point)
Definition: raster/range.c:635
void Rast_update_fp_range(DCELL, struct FPRange *)
Update range structure (floating-point)
Definition: raster/range.c:548
#define min(x, y)
Definition: draw2.c:29
#define max(x, y)
Definition: draw2.c:30
#define GPATH_MAX
Definition: gis.h:194
double DCELL
Definition: gis.h:626
#define UNUSED
A macro for an attribute, if attached to a variable, indicating that the variable is not used.
Definition: gis.h:47
#define _(str)
Definition: glocale.h:10
const char * name
Definition: named_colr.c:6
void Rast3d_range_min_max(RASTER3D_Map *map, double *min, double *max)
Returns in min and max the minimum and maximum values of the range.
int Rast3d_range_init(RASTER3D_Map *map)
int Rast3d_range_load(RASTER3D_Map *map)
Loads the range into the range structure of map.
int Rast3d_read_range(const char *name, const char *mapset, struct FPRange *drange)
void Rast3d_range_update_from_tile(RASTER3D_Map *map, const void *tile, int rows, int cols, int depths, int xRedundant, int yRedundant, int zRedundant UNUSED, int nofNum, int type)
int Rast3d_range_write(RASTER3D_Map *map)
Writes the range which is stored in the range structure of map. (This function is invoked automatical...
#define RASTER3D_DIRECTORY
Definition: raster3d.h:31
#define RASTER3D_RANGE_ELEMENT
Definition: raster3d.h:34
#define RASTER3D_XDR_DOUBLE_LENGTH
#define RASTER3D_WRITE_DATA
DCELL min
Definition: raster.h:219
int first_time
Definition: raster.h:221
DCELL max
Definition: raster.h:220
char * fileName
Definition: raster3d.h:74
struct FPRange range
Definition: raster3d.h:168
char * mapset
Definition: raster3d.h:76
int operation
Definition: raster3d.h:79
int tileSize
Definition: raster3d.h:180
Definition: path.h:15