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