GRASS GIS 7 Programmer's Manual  7.5.svn(2017)-r71793
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
raster3d/open.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <fcntl.h>
4 #include <sys/types.h>
5 #include <unistd.h>
6 #include <grass/raster3d.h>
7 #include <grass/glocale.h>
8 #include "raster3d_intern.h"
9 
10 /*---------------------------------------------------------------------------*/
11 
12 void *Rast3d_open_cell_old_no_header(const char *name, const char *mapset)
13 {
14  RASTER3D_Map *map;
15  char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
16 
18 
19  if (!Rast3d_mask_open_old()) {
20  Rast3d_error(_("Rast3d_open_cell_old_no_header: error in Rast3d_mask_open_old"));
21  return (void *)NULL;
22  }
23 
24  map = Rast3d_malloc(sizeof(RASTER3D_Map));
25  if (map == NULL) {
26  Rast3d_error(_("Rast3d_open_cell_old_no_header: error in Rast3d_malloc"));
27  return (void *)NULL;
28  }
29 
30  G_unqualified_name(name, mapset, xname, xmapset);
31 
32  map->fileName = G_store(xname);
33  map->mapset = G_store(xmapset);
34 
36  if (map->data_fd < 0) {
37  Rast3d_error(_("Rast3d_open_cell_old_no_header: error in G_open_old"));
38  return (void *)NULL;
39  }
40 
41  Rast3d_range_init(map);
42  Rast3d_mask_off(map);
43 
44  return map;
45 }
46 
47 /*---------------------------------------------------------------------------*/
48 
49 
50 /*!
51  * \brief
52  *
53  * Opens existing g3d-file <em>name</em> in <em>mapset</em>.
54  * Tiles are stored in memory with <em>type</em> which must be any of FCELL_TYPE,
55  * DCELL_TYPE, or RASTER3D_TILE_SAME_AS_FILE. <em>cache</em> specifies the
56  * cache-mode used and must be either RASTER3D_NO_CACHE, RASTER3D_USE_CACHE_DEFAULT,
57  * RASTER3D_USE_CACHE_X, RASTER3D_USE_CACHE_Y, RASTER3D_USE_CACHE_Z,
58  * RASTER3D_USE_CACHE_XY, RASTER3D_USE_CACHE_XZ, RASTER3D_USE_CACHE_YZ,
59  * RASTER3D_USE_CACHE_XYZ, the result of <tt>Rast3d_cache_size_encode ()</tt> (cf.{g3d:G3d.cacheSizeEncode}), or any positive integer which
60  * specifies the number of tiles buffered in the cache. <em>window</em> sets the
61  * window-region for the map. It is either a pointer to a window structure or
62  * RASTER3D_DEFAULT_WINDOW, which uses the window stored at initialization time or
63  * set via <tt>Rast3d_set_window ()</tt> (cf.{g3d:G3d.setWindow}).
64  * To modify the window for the map after it has already been opened use
65  * <tt>Rast3d_set_window_map ()</tt> (cf.{g3d:G3d.setWindowMap}).
66  * Returns a pointer to the cell structure ... if successful, NULL ...
67  * otherwise.
68  *
69  * \param name
70  * \param mapset
71  * \param window
72  * \param type
73  * \param cache
74  * \return void *
75  */
76 
77 void *Rast3d_open_cell_old(const char *name, const char *mapset,
78  RASTER3D_Region * window, int typeIntern, int cache)
79 {
80  RASTER3D_Map *map;
81  int proj, zone;
82  int compression, useRle, useLzw, type, tileX, tileY, tileZ;
83  int rows, cols, depths, precision;
84  double ew_res, ns_res, tb_res;
85  int nofHeaderBytes, dataOffset, useXdr, hasIndex;
86  char *ltmp, *unit;
87  int vertical_unit;
88  int version;
89  double north, south, east, west, top, bottom;
90 
91  map = Rast3d_open_cell_old_no_header(name, mapset);
92  if (map == NULL) {
93  Rast3d_error(_("Rast3d_open_cell_old: error in Rast3d_open_cell_old_no_header"));
94  return (void *)NULL;
95  }
96 
97  if (lseek(map->data_fd, (long)0, SEEK_SET) == -1) {
98  Rast3d_error(_("Rast3d_open_cell_old: can't rewind file"));
99  return (void *)NULL;
100  }
101 
102  if (!Rast3d_read_header(map,
103  &proj, &zone,
104  &north, &south, &east, &west, &top, &bottom,
105  &rows, &cols, &depths,
106  &ew_res, &ns_res, &tb_res,
107  &tileX, &tileY, &tileZ,
108  &type, &compression, &useRle, &useLzw,
109  &precision, &dataOffset, &useXdr, &hasIndex, &unit, &vertical_unit,
110  &version)) {
111  Rast3d_error(_("Rast3d_open_cell_old: error in Rast3d_read_header"));
112  return 0;
113  }
114 
115  if (window == RASTER3D_DEFAULT_WINDOW)
116  window = Rast3d_window_ptr();
117 
118  if (proj != window->proj) {
119  Rast3d_error(_("Rast3d_open_cell_old: projection does not match window projection"));
120  return (void *)NULL;
121  }
122  if (zone != window->zone) {
123  Rast3d_error(_("Rast3d_open_cell_old: zone does not match window zone"));
124  return (void *)NULL;
125  }
126 
127  map->useXdr = useXdr;
128 
129  if (hasIndex) {
130  /* see RASTER3D_openCell_new () for format of header */
131  if ((!Rast3d_read_ints(map->data_fd, map->useXdr,
132  &(map->indexLongNbytes), 1)) ||
133  (!Rast3d_read_ints(map->data_fd, map->useXdr,
134  &(map->indexNbytesUsed), 1))) {
135  Rast3d_error(_("Rast3d_open_cell_old: can't read header"));
136  return (void *)NULL;
137  }
138 
139  /* if our long is to short to store offsets we can't read the file */
140  if (map->indexNbytesUsed > sizeof(long))
141  Rast3d_fatal_error(_("Rast3d_open_cell_old: index does not fit into long"));
142 
143  ltmp = Rast3d_malloc(map->indexLongNbytes);
144  if (ltmp == NULL) {
145  Rast3d_error(_("Rast3d_open_cell_old: error in Rast3d_malloc"));
146  return (void *)NULL;
147  }
148 
149  /* convert file long to long */
150  if (read(map->data_fd, ltmp, map->indexLongNbytes) !=
151  map->indexLongNbytes) {
152  Rast3d_error(_("Rast3d_open_cell_old: can't read header"));
153  return (void *)NULL;
154  }
155  Rast3d_long_decode(ltmp, &(map->indexOffset), 1, map->indexLongNbytes);
156  Rast3d_free(ltmp);
157  }
158 
159  nofHeaderBytes = dataOffset;
160 
161  if (typeIntern == RASTER3D_TILE_SAME_AS_FILE)
162  typeIntern = type;
163 
164  if (!Rast3d_fill_header(map, RASTER3D_READ_DATA, compression, useRle, useLzw,
165  type, precision, cache,
166  hasIndex, map->useXdr, typeIntern,
167  nofHeaderBytes, tileX, tileY, tileZ,
168  proj, zone,
169  north, south, east, west, top, bottom,
170  rows, cols, depths, ew_res, ns_res, tb_res, unit, vertical_unit,
171  version)) {
172  Rast3d_error(_("Rast3d_open_cell_old: error in Rast3d_fill_header"));
173  return (void *)NULL;
174  }
175 
176  Rast3d_region_copy(&(map->window), window);
177  Rast3d_adjust_region(&(map->window));
179 
180  return map;
181 }
182 
183 /*---------------------------------------------------------------------------*/
184 
185 
186 /*!
187  * \brief
188  *
189  * Opens new g3d-file with <em>name</em> in the current mapset. Tiles
190  * are stored in memory with <em>type</em> which must be one of FCELL_TYPE,
191  * DCELL_TYPE, or RASTER3D_TILE_SAME_AS_FILE. <em>cache</em> specifies the
192  * cache-mode used and must be either RASTER3D_NO_CACHE, RASTER3D_USE_CACHE_DEFAULT,
193  * RASTER3D_USE_CACHE_X, RASTER3D_USE_CACHE_Y, RASTER3D_USE_CACHE_Z,
194  * RASTER3D_USE_CACHE_XY, RASTER3D_USE_CACHE_XZ, RASTER3D_USE_CACHE_YZ,
195  * RASTER3D_USE_CACHE_XYZ, the result of <tt>Rast3d_cache_size_encode ()</tt>
196  * (cf.{g3d:G3d.cacheSizeEncode}), or any positive integer which
197  * specifies the number of tiles buffered in the cache. <em>region</em> specifies
198  * the 3d region.
199  * Returns a pointer to the cell structure ... if successful,
200  * NULL ... otherwise.
201  *
202  * \param name
203  * \param type
204  * \param cache
205  * \param region
206  * \return void *
207  */
208 
209 void *Rast3d_open_cell_new(const char *name, int typeIntern, int cache,
210  RASTER3D_Region * region)
211 {
212  RASTER3D_Map *map;
213  int nofHeaderBytes, dummy = 0, compression, precision;
214  long ldummy = 0;
215  char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
216 
218  if (!Rast3d_mask_open_old()) {
219  Rast3d_error(_("Rast3d_open_cell_new: error in Rast3d_mask_open_old"));
220  return (void *)NULL;
221  }
222 
224  precision = g3d_precision;
225 
226  map = Rast3d_malloc(sizeof(RASTER3D_Map));
227  if (map == NULL) {
228  Rast3d_error(_("Rast3d_open_cell_new: error in Rast3d_malloc"));
229  return (void *)NULL;
230  }
231 
232  if (G_unqualified_name(name, G_mapset(), xname, xmapset) < 0) {
233  G_warning(_("map <%s> is not in the current mapset"), name);
234  return (void *)NULL;
235  }
236 
237  map->fileName = G_store(xname);
238  map->mapset = G_store(xmapset);
239 
240  map->tempName = G_tempfile();
241  map->data_fd = open(map->tempName, O_RDWR | O_CREAT | O_TRUNC, 0666);
242  if (map->data_fd < 0) {
243  Rast3d_error(_("Rast3d_open_cell_new: could not open file"));
244  return (void *)NULL;
245  }
246 
248 
249  /* XDR support has been removed */
250  map->useXdr = RASTER3D_NO_XDR;
251 
252  if (g3d_file_type == FCELL_TYPE) {
253  if (precision > 23)
254  precision = 23; /* 32 - 8 - 1 */
255  else if (precision < -1)
256  precision = 0;
257  }
258  else if (precision > 52)
259  precision = 52; /* 64 - 11 - 1 */
260  else if (precision < -1)
261  precision = 0;
262 
263  /* no need to write trailing zeros */
264  if ((typeIntern == FCELL_TYPE) && (g3d_file_type == DCELL_TYPE)) {
265  if (precision == -1)
266  precision = 23;
267  else
268  precision = RASTER3D_MIN(precision, 23);
269  }
270 
272  precision = RASTER3D_MAX_PRECISION;
273 
274  if (RASTER3D_HAS_INDEX) {
275  map->indexLongNbytes = sizeof(long);
276 
277  /* at the beginning of the file write */
278  /* nof bytes of "long" */
279  /* max nof bytes used for index */
280  /* position of index in file */
281  /* the index is appended at the end of the file at closing time. since */
282  /* we do not know this position yet we write dummy values */
283 
284  if ((!Rast3d_write_ints(map->data_fd, map->useXdr,
285  &(map->indexLongNbytes), 1)) ||
286  (!Rast3d_write_ints(map->data_fd, map->useXdr, &dummy, 1))) {
287  Rast3d_error(_("Rast3d_open_cell_new: can't write header"));
288  return (void *)NULL;
289  }
290  if (write(map->data_fd, &ldummy, map->indexLongNbytes) !=
291  map->indexLongNbytes) {
292  Rast3d_error(_("Rast3d_open_cell_new: can't write header"));
293  return (void *)NULL;
294  }
295  }
296 
297  /* can't use a constant since this depends on sizeof (long) */
298  nofHeaderBytes = lseek(map->data_fd, (long)0, SEEK_CUR);
299 
300  Rast3d_range_init(map);
301  Rast3d_adjust_region(region);
302 
304  g3d_file_type, precision, cache, RASTER3D_HAS_INDEX,
305  map->useXdr, typeIntern, nofHeaderBytes,
308  region->proj, region->zone,
309  region->north, region->south, region->east,
310  region->west, region->top, region->bottom,
311  region->rows, region->cols, region->depths,
312  region->ew_res, region->ns_res, region->tb_res,
314  Rast3d_error(_("Rast3d_open_cell_new: error in Rast3d_fill_header"));
315  return (void *)NULL;
316  }
317 
318  /*Set the map window to the map region */
319  Rast3d_region_copy(&(map->window), region);
320  /*Set the resampling function to nearest neighbor for data access */
322 
323  Rast3d_mask_off(map);
324 
325  return (void *)map;
326 }
double east
Definition: raster3d.h:51
unsigned short compression
Definition: gsd_img_tif.c:42
void Rast3d_adjust_region(RASTER3D_Region *region)
Computes an adjusts the resolutions in the region structure from the region boundaries and number of ...
Definition: region.c:151
#define GMAPSET_MAX
Definition: gis.h:149
#define RASTER3D_MAX_PRECISION
Definition: raster3d.h:17
double top
Definition: raster3d.h:52
long indexOffset
Definition: raster3d.h:122
double tb_res
Definition: raster3d.h:57
char * fileName
Definition: raster3d.h:76
int g3d_precision
Definition: defaults.c:66
double bottom
Definition: raster3d.h:52
#define RASTER3D_WRITE_DATA
void Rast3d_fatal_error(const char *msg,...)
Prints fatal error message.
double ns_res
Definition: raster3d.h:57
#define RASTER3D_DEFAULT_WINDOW
Definition: raster3d.h:29
char * G_store(const char *s)
Copy string to allocated memory.
Definition: strings.c:86
int g3d_do_compression
Definition: defaults.c:65
int Rast3d_fill_header(RASTER3D_Map *map, int operation, int compression, int useRle, int useLzw, int type, int precision, int cache, int hasIndex, int useXdr, int typeIntern, int nofHeaderBytes, int tileX, int tileY, int tileZ, int proj, int zone, double north, double south, double east, double west, double top, double bottom, int rows, int cols, int depths, double ew_res, double ns_res, double tb_res, char *unit, int vertical_unit, int version)
void Rast3d_get_nearest_neighbor_fun_ptr(void(**nnFunPtr)())
Returns in nnFunPtr a pointer to Rast3d_nearest_neighbor () (cf.{g3d:G3d.nearestNeighbor}).
Definition: resample.c:90
int G_unqualified_name(const char *name, const char *mapset, char *xname, char *xmapset)
Returns unqualified map name (without @ mapset)
Definition: nme_in_mps.c:134
char * G_tempfile(void)
Returns a temporary file name.
Definition: tempfile.c:62
#define NULL
Definition: ccmath.h:32
char * g3d_unit_default
Definition: defaults.c:73
int G_open_old_misc(const char *dir, const char *element, const char *name, const char *mapset)
open a database file for reading
Definition: open_misc.c:134
double north
Definition: raster3d.h:50
char * tempName
Definition: raster3d.h:77
void * Rast3d_open_cell_old_no_header(const char *name, const char *mapset)
Definition: raster3d/open.c:12
#define RASTER3D_CELL_ELEMENT
Definition: raster3d.h:32
#define DCELL_TYPE
Definition: raster.h:13
int Rast3d_mask_open_old(void)
Definition: mask.c:73
double west
Definition: raster3d.h:51
#define RASTER3D_NO_XDR
int indexLongNbytes
Definition: raster3d.h:125
void Rast3d_error(const char *msg,...)
void Rast3d_long_decode(unsigned char *source, long *dst, int nofNums, int longNbytes)
Definition: long.c:36
double ew_res
Definition: raster3d.h:57
#define RASTER3D_HAS_INDEX
int Rast3d_write_ints(int fd, int useXdr, const int *i, int nofNum)
Definition: intio.c:9
int Rast3d_read_header(RASTER3D_Map *map, int *proj, int *zone, double *north, double *south, double *east, double *west, double *top, double *bottom, int *rows, int *cols, int *depths, double *ew_res, double *ns_res, double *tb_res, int *tileX, int *tileY, int *tileZ, int *type, int *compression, int *useRle, int *useLzw, int *precision, int *dataOffset, int *useXdr, int *hasIndex, char **unit, int *vertical_unit, int *version)
RASTER3D_Region * Rast3d_window_ptr()
#define RASTER3D_MIN(a, b)
#define RASTER3D_NO_COMPRESSION
Definition: raster3d.h:14
#define RASTER3D_READ_DATA
void Rast3d_free(void *buf)
Same as free (ptr).
int indexNbytesUsed
Definition: raster3d.h:129
void * Rast3d_malloc(int nBytes)
Same as malloc (nBytes), except that in case of error Rast3d_error() is invoked.
void Rast3d_init_defaults(void)
Initializes the default values described in RASTER3D Defaults. Applications have to use this function...
Definition: defaults.c:305
#define RASTER3D_DIRECTORY
Definition: raster3d.h:31
int g3d_tile_dimension[3]
Definition: defaults.c:70
#define RASTER3D_TILE_SAME_AS_FILE
Definition: raster3d.h:12
#define GNAME_MAX
Definition: gis.h:148
int Rast3d_range_init(RASTER3D_Map *map)
Definition: d/range.c:206
int g3d_file_type
Definition: defaults.c:69
#define _(str)
Definition: glocale.h:13
int g3d_vertical_unit_default
Definition: defaults.c:74
#define FCELL_TYPE
Definition: raster.h:12
const char * G_mapset(void)
Get current mapset name.
Definition: gis/mapset.c:33
void * Rast3d_open_cell_new(const char *name, int typeIntern, int cache, RASTER3D_Region *region)
Opens new g3d-file with name in the current mapset. Tiles are stored in memory with type which must b...
const char * name
Definition: named_colr.c:7
int Rast3d_read_ints(int fd, int useXdr, int *i, int nofNum)
Definition: intio.c:51
char * mapset
Definition: raster3d.h:78
resample_fn * resampleFun
Definition: raster3d.h:90
#define RASTER3D_MAP_VERSION
Definition: raster3d.h:7
double south
Definition: raster3d.h:50
RASTER3D_Region window
Definition: raster3d.h:87
void * Rast3d_open_cell_old(const char *name, const char *mapset, RASTER3D_Region *window, int typeIntern, int cache)
Opens existing g3d-file name in mapset. Tiles are stored in memory with type which must be any of FCE...
Definition: raster3d/open.c:77
void G_warning(const char *msg,...)
Print a warning message to stderr.
Definition: gis/error.c:203
void Rast3d_mask_off(RASTER3D_Map *map)
Turns off the mask for map. This is the default. Do not invoke this function after the first tile has...
Definition: mask.c:349
void Rast3d_make_mapset_map_directory(const char *mapName)
void Rast3d_region_copy(RASTER3D_Region *regionDest, RASTER3D_Region *regionSrc)
Copies the values of regionSrc into regionDst.
Definition: region.c:207