GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
g3drange.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 <rpc/types.h>
6 #include <rpc/xdr.h>
7 #include <grass/gis.h>
8 #include "G3d_intern.h"
9 
10 /*---------------------------------------------------------------------------*/
11 
12 void
13 G3d_range_updateFromTile(G3D_Map * map, const void *tile, int rows, int cols,
14  int depths, int xRedundant, int yRedundant,
15  int zRedundant, int nofNum, int type)
16 {
17  int y, z, cellType;
18  struct FPRange *range;
19 
20  range = &(map->range);
21  cellType = G3d_g3dType2cellType(type);
22 
23  if (nofNum == map->tileSize) {
24  G_row_update_fp_range(tile, map->tileSize, range, cellType);
25  return;
26  }
27 
28  if (xRedundant) {
29  for (z = 0; z < depths; z++) {
30  for (y = 0; y < rows; y++) {
31  G_row_update_fp_range(tile, cols, range, cellType);
32  tile = G_incr_void_ptr(tile, map->tileX * G3d_length(type));
33  }
34  if (yRedundant)
35  tile =
36  G_incr_void_ptr(tile,
37  map->tileX * yRedundant *
38  G3d_length(type));
39  }
40  return;
41  }
42 
43  if (yRedundant) {
44  for (z = 0; z < depths; z++) {
45  G_row_update_fp_range(tile, map->tileX * rows, range, cellType);
46  tile = G_incr_void_ptr(tile, map->tileXY * G3d_length(type));
47  }
48  return;
49  }
50 
51  G_row_update_fp_range(tile, map->tileXY * depths, range, cellType);
52 }
53 
54 /*---------------------------------------------------------------------------*/
55 
56 int
57 G3d_readRange(const char *name, const char *mapset, struct FPRange *drange)
58  /* adapted from G_read_fp_range */
59 {
60  int fd;
61  char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
62  char buf[GNAME_MAX + sizeof(G3D_DIRECTORY) + 2],
63  buf2[GMAPSET_MAX + sizeof(G3D_RANGE_ELEMENT) + 2];
64  char xdr_buf[100];
65  DCELL dcell1, dcell2;
66  XDR xdr_str;
67 
68  G_init_fp_range(drange);
69 
70  fd = -1;
71 
72  if (G__name_is_fully_qualified(name, xname, xmapset)) {
73  sprintf(buf, "%s/%s", G3D_DIRECTORY, xname);
74  sprintf(buf2, "%s@%s", G3D_RANGE_ELEMENT, xmapset); /* == range@mapset */
75  }
76  else {
77  sprintf(buf, "%s/%s", G3D_DIRECTORY, name);
78  sprintf(buf2, "%s", G3D_RANGE_ELEMENT);
79  }
80 
81  if (G_find_file2(buf, buf2, mapset)) {
82  fd = G_open_old(buf, buf2, mapset);
83  if (fd < 0)
84  goto error;
85 
86  if (read(fd, xdr_buf, 2 * G3D_XDR_DOUBLE_LENGTH) !=
88  return 2;
89 
90  xdrmem_create(&xdr_str, xdr_buf, (u_int) G3D_XDR_DOUBLE_LENGTH * 2,
91  XDR_DECODE);
92 
93  /* if the f_range file exists, but empty */
94  if (!xdr_double(&xdr_str, &dcell1) || !xdr_double(&xdr_str, &dcell2))
95  goto error;
96 
97  G_update_fp_range(dcell1, drange);
98  G_update_fp_range(dcell2, drange);
99  close(fd);
100  return 1;
101  }
102 
103  error:
104  if (fd > 0)
105  close(fd);
106  G_warning("can't read range file for [%s in %s]", name, mapset);
107  return -1;
108 }
109 
110 /*---------------------------------------------------------------------------*/
111 
112 
123 int G3d_range_load(G3D_Map * map)
124 {
125  if (map->operation == G3D_WRITE_DATA)
126  return 1;
127  if (G3d_readRange(map->fileName, map->mapset, &(map->range)) == -1) {
128  return 0;
129  }
130 
131  return 1;
132 }
133 
134 /*---------------------------------------------------------------------------*/
135 
136 
149 void G3d_range_min_max(G3D_Map * map, double *min, double *max)
150 {
151  G_get_fp_range_min_max(&(map->range), min, max);
152 }
153 
154 /*-------------------------------------------------------------------------*/
155 
156 static int writeRange(const char *name, struct FPRange *range)
157  /* adapted from G_write_fp_range */
158 {
159  char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
160  char buf[GNAME_MAX + sizeof(G3D_DIRECTORY) + 2],
161  buf2[GMAPSET_MAX + sizeof(G3D_RANGE_ELEMENT) + 2];
162  char xdr_buf[100];
163  int fd;
164  XDR xdr_str;
165 
166  if (G__name_is_fully_qualified(name, xname, xmapset)) {
167  sprintf(buf, "%s/%s", G3D_DIRECTORY, xname);
168  sprintf(buf2, "%s@%s", G3D_RANGE_ELEMENT, xmapset); /* == range@mapset */
169  }
170  else {
171  sprintf(buf, "%s/%s", G3D_DIRECTORY, name);
172  sprintf(buf2, "%s", G3D_RANGE_ELEMENT);
173  }
174 
175  fd = G_open_new(buf, buf2);
176  if (fd < 0)
177  goto error;
178 
179  if (range->first_time) {
180  /* if range hasn't been updated, write empty file meaning NULLs */
181  close(fd);
182  return 0;
183  }
184 
185  xdrmem_create(&xdr_str, xdr_buf, (u_int) G3D_XDR_DOUBLE_LENGTH * 2,
186  XDR_ENCODE);
187 
188  if (!xdr_double(&xdr_str, &(range->min)))
189  goto error;
190  if (!xdr_double(&xdr_str, &(range->max)))
191  goto error;
192 
193  write(fd, xdr_buf, G3D_XDR_DOUBLE_LENGTH * 2);
194  close(fd);
195  return 0;
196 
197  error:
198  G_remove(buf, buf2); /* remove the old file with this name */
199  sprintf(buf, "can't write range file for [%s in %s]", name, G_mapset());
200  G_warning(buf);
201  return -1;
202 }
203 
204 /*---------------------------------------------------------------------------*/
205 
206 
218 int G3d_range_write(G3D_Map * map)
219 {
220  char path[GPATH_MAX];
221 
222  G3d_filename(path, G3D_RANGE_ELEMENT, map->fileName, map->mapset);
223  remove(path);
224 
225  if (writeRange(map->fileName, &(map->range)) == -1) {
226  G3d_error("G3d_closeCellNew: error in writeRange");
227  return 0;
228  }
229 
230  return 1;
231 }
232 
233 /*---------------------------------------------------------------------------*/
234 
235 int G3d_range_init(G3D_Map * map)
236 {
237  return G_init_fp_range(&(map->range));
238 }
char * G_mapset(void)
current mapset name
Definition: mapset.c:31
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
int G3d_range_init(G3D_Map *map)
Definition: g3drange.c:235
int G3d_g3dType2cellType(int g3dType)
Definition: g3dmisc.c:12
char xmapset[512]
Definition: g3dcats.c:89
void G3d_error(const char *msg,...)
Definition: g3derror.c:75
FILE * fd
Definition: g3dcolor.c:368
string name
Definition: render.py:1314
#define min(x, y)
Definition: draw2.c:68
DCELL dcell1
Definition: g3drange.c:65
int G3d_range_write(G3D_Map *map)
Writes the range which is stored in the range structure of map. (This function is invoked automatical...
Definition: g3drange.c:218
char xdr_buf[100]
Definition: g3drange.c:64
#define G3D_WRITE_DATA
Definition: G3d_intern.h:19
void G3d_filename(char *path, const char *elementName, const char *mapName, const char *mapset)
Definition: filename.c:10
def error
Display an error message using g.message -e
Definition: core.py:370
int y
Definition: plot.c:34
#define max(x, y)
Definition: draw2.c:69
void * G_incr_void_ptr(const void *ptr, const size_t size)
Advance void pointer.
Definition: gis/raster.c:33
int G_row_update_fp_range(const void *rast, int n, struct FPRange *range, RASTER_MAP_TYPE data_type)
Definition: range.c:524
int G_update_fp_range(DCELL val, struct FPRange *range)
Definition: range.c:456
void G3d_range_min_max(G3D_Map *map, double *min, double *max)
Returns in min and max the minimum and maximum values of the range.
Definition: g3drange.c:149
int G3d_range_load(G3D_Map *map)
Loads the range into the range structure of map.
Definition: g3drange.c:123
XDR xdr_str
Definition: g3drange.c:66
int G_remove(const char *element, const char *name)
Remove a database file.
Definition: remove.c:47
int G_open_old(const char *element, const char *name, const char *mapset)
Open a database file for reading.
Definition: gis/open.c:147
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
char * G_find_file2(const char *element, const char *name, const char *mapset)
searches for a file from the mapset search list or in a specified mapset. (look but don&#39;t touch) retu...
Definition: find_file.c:191
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
int G_get_fp_range_min_max(const struct FPRange *range, DCELL *min, DCELL *max)
Extract the min/max from the range structure r. If the range structure has no defined min/max (first!...
Definition: range.c:667
DCELL dcell2
Definition: g3drange.c:65
tuple cols
int G_open_new(const char *element, const char *name)
Open a new database file.
Definition: gis/open.c:125
char buf2[200]
Definition: g3dcats.c:89
int G3d_length(int t)
Definition: g3dmisc.c:77
G_init_fp_range(drange)
struct FPRange drange
Definition: g3dcolor.c:52
tuple range
Definition: tools.py:1406
#define G3D_XDR_DOUBLE_LENGTH
Definition: G3d_intern.h:14
char xname[512]
Definition: g3dcats.c:89
void G3d_range_updateFromTile(G3D_Map *map, const void *tile, int rows, int cols, int depths, int xRedundant, int yRedundant, int zRedundant, int nofNum, int type)
Definition: g3drange.c:13
int G__name_is_fully_qualified(const char *fullname, char *name, char *mapset)
Check if map name is fully qualified (map @ mapset)
Definition: nme_in_mps.c:57