GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
raster3d/close.c
Go to the documentation of this file.
1 /*!
2  \file lib/raster3d/close.c
3 
4  \brief 3D Raster Library - Close 3D raster file
5 
6  (C) 1999-2009, 2011 by the GRASS Development Team
7 
8  This program is free software under the GNU General Public
9  License (>=v2). Read the file COPYING that comes with GRASS
10  for details.
11 
12  \author USACERL and many others
13 */
14 
15 #ifdef __MINGW32__
16 # include <windows.h>
17 #endif
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <sys/types.h>
21 #include <unistd.h>
22 
23 #include <grass/raster.h>
24 #include <grass/glocale.h>
25 
26 #include "raster3d_intern.h"
27 
28 static int close_new(RASTER3D_Map * map)
29 {
30  char path[GPATH_MAX];
31  struct Categories cats;
32  struct History hist;
33 
35 
36  /* create empty cats file */
37  Rast_init_cats(NULL, &cats);
38  Rast3d_write_cats(map->fileName, &cats);
39  Rast_free_cats(&cats);
40 
41  /*generate the history file, use the normal G_ functions */
42  Rast_short_history(map->fileName, "raster3d", &hist);
43  Rast_command_history(&hist);
44  /*Use the G3d function to write the history file,
45  * otherwise the path is wrong */
46  if (Rast3d_write_history(map->fileName, &hist) < 0) {
47  G_warning(_("Unable to write history for 3D raster map <%s>"), map->fileName);
48  }
49 
50  Rast3d_range_write(map);
51 
52  close(map->data_fd);
53 
54  /* finally move tempfile to data file */
56 #ifdef __MINGW32__
57  if (CopyFile(map->tempName, path, FALSE) == 0) {
58 #else
59  if (link(map->tempName, path) < 0) {
60 #endif
61  if (rename(map->tempName, path)) {
62  G_warning(_("Unable to move temp raster map <%s> to 3D raster map <%s>"),
63  map->tempName, path);
64  return 0;
65  }
66  }
67  else
68  remove(map->tempName);
69 
70  return 1;
71 }
72 
73 static int close_cell_new(RASTER3D_Map * map)
74 {
75  long ltmp;
76 
77  if (map->useCache)
78  if (!Rast3d_flush_all_tiles(map)) {
79  G_warning(_("Unable to flush all tiles"));
80  return 0;
81  }
82 
83  if (!Rast3d_flush_index(map)) {
84  G_warning(_("Unable to flush index"));
85  return 0;
86  }
87 
88  /* write the header info which was filled with dummy values at the */
89  /* opening time */
90 
91  if (lseek(map->data_fd,
92  (long)(map->offset - sizeof(int) - sizeof(long)),
93  SEEK_SET) == -1) {
94  G_warning(_("Unable to position file"));
95  return 0;
96  }
97 
98  if (!Rast3d_write_ints(map->data_fd, map->useXdr, &(map->indexNbytesUsed), 1)) {
99  G_warning(_("Unable to write header for 3D raster map <%s>"), map->fileName);
100  return 0;
101  }
102 
103  Rast3d_long_encode(&(map->indexOffset), (unsigned char *)&ltmp, 1);
104  if (write(map->data_fd, &ltmp, sizeof(long)) != sizeof(long)) {
105  G_warning(_("Unable to write header for 3D raster map <%s>"), map->fileName);
106  return 0;
107  }
108 
109  if (!close_new(map)) {
110  G_warning(_("Unable to create 3D raster map <%s>"), map->fileName);
111  return 0;
112  }
113 
114  return 1;
115 }
116 
117 static int close_old(RASTER3D_Map * map)
118 {
119  if (close(map->data_fd) != 0) {
120  G_warning(_("Unable to close 3D raster map <%s>"), map->fileName);
121  return 0;
122  }
123 
124  return 1;
125 }
126 
127 static int close_cell_old(RASTER3D_Map * map)
128 {
129  if (!close_old(map)) {
130  G_warning(_("Unable to close 3D raster map <%s>"), map->fileName);
131  return 0;
132  }
133 
134  return 1;
135 }
136 
137 /*!
138  \brief Close 3D raster map files
139 
140  Closes g3d-file. If <em>map</em> is new and cache-mode is used for
141  <em>map</em> then every tile which is not flushed before closing is
142  flushed.
143 
144  \param map pointer to RASTER3D_Map to be closed
145 
146  \return 1 success
147  \return 0 failure
148 */
150 {
151  if (map->operation == RASTER3D_WRITE_DATA) {
152  if (!close_cell_new(map)) {
153  G_warning(_("Unable to create 3D raster map <%s>"), map->fileName);
154  return 0;
155  }
156  }
157  else {
158  if (!close_cell_old(map)) {
159  G_warning(_("Unable to close 3D raster map <%s>"), map->fileName);
160  return 0;
161  }
162  }
163 
164  Rast3d_free(map->index);
165  Rast3d_free(map->tileLength);
166 
167  if (map->useCache) {
168  if (!Rast3d_dispose_cache(map)) {
169  G_warning(_("Error in cache"));
170  return 0;
171  }
172  }
173  else
174  Rast3d_free(map->data);
175 
176  if (map->operation == RASTER3D_WRITE_DATA)
177  if (!Rast3d_write_header(map,
178  map->region.proj, map->region.zone,
179  map->region.north, map->region.south,
180  map->region.east, map->region.west,
181  map->region.top, map->region.bottom,
182  map->region.rows, map->region.cols,
183  map->region.depths,
184  map->region.ew_res, map->region.ns_res,
185  map->region.tb_res,
186  map->tileX, map->tileY, map->tileZ,
187  map->type,
188  map->compression, map->useRle, map->useLzw,
189  map->precision, map->offset, map->useXdr,
190  map->hasIndex, map->unit, map->vertical_unit, map->version)) {
191  G_warning(_("Unable to write header for 3D raster map <%s>"), map->fileName);
192  return 0;
193  }
194 
195  Rast3d_free(map);
196 
197  return 1;
198 }
int Rast3d_range_write(RASTER3D_Map *)
Writes the range which is stored in the range structure of map. (This function is invoked automatical...
Definition: d/range.c:189
double east
Definition: raster3d.h:51
int Rast3d_write_cats(const char *, struct Categories *)
Writes the categories stored in the cats structure into the categories file for map name in the curre...
Definition: raster3d/cats.c:27
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 Rast3d_close(RASTER3D_Map *map)
Close 3D raster map files.
double bottom
Definition: raster3d.h:52
#define RASTER3D_WRITE_DATA
int hasIndex
Definition: raster3d.h:135
int Rast3d_dispose_cache(RASTER3D_Map *)
Definition: cache.c:245
int operation
Definition: raster3d.h:81
int Rast3d_write_header(RASTER3D_Map *, int, int, double, double, double, double, double, double, int, int, int, double, double, double, int, int, int, int, int, int, int, int, int, int, int, char *, int, int)
double ns_res
Definition: raster3d.h:57
int useCache
Definition: raster3d.h:159
int * tileLength
Definition: raster3d.h:144
int version
Definition: raster3d.h:74
int Rast3d_long_encode(long *, unsigned char *, int)
Definition: long.c:5
void Rast3d_filename(char *, const char *, const char *, const char *)
Definition: filename.c:10
#define NULL
Definition: ccmath.h:32
int Rast3d_remove_color(const char *)
Removes the primary and/or secondary color file.
double north
Definition: raster3d.h:50
char * tempName
Definition: raster3d.h:77
int Rast3d_write_history(const char *, struct History *)
write raster3d History file
Definition: d/history.c:97
#define RASTER3D_CELL_ELEMENT
Definition: raster3d.h:32
long * index
Definition: raster3d.h:141
double west
Definition: raster3d.h:51
char * data
Definition: raster3d.h:152
double ew_res
Definition: raster3d.h:57
#define FALSE
Definition: gis.h:63
int compression
Definition: raster3d.h:113
int vertical_unit
Definition: raster3d.h:94
Raster history info (metadata)
Definition: raster.h:180
char * unit
Definition: raster3d.h:93
int precision
Definition: raster3d.h:111
#define GPATH_MAX
Definition: gis.h:180
int Rast3d_flush_index(RASTER3D_Map *)
void Rast_init_cats(const char *, struct Categories *)
Initialize category structure.
Definition: raster/cats.c:1145
int indexNbytesUsed
Definition: raster3d.h:129
void Rast_short_history(const char *, const char *, struct History *)
Initialize history structure.
Definition: history.c:226
RASTER3D_Region region
Definition: raster3d.h:84
int Rast_command_history(struct History *)
Save command line to raster history structure.
Definition: history.c:272
int Rast3d_write_ints(int, int, const int *, int)
Definition: intio.c:9
void G_warning(const char *,...) __attribute__((format(printf
Definition: path.h:16
void Rast_free_cats(struct Categories *)
Free category structure memory.
Definition: raster/cats.c:1213
#define _(str)
Definition: glocale.h:10
int Rast3d_flush_all_tiles(RASTER3D_Map *)
Definition: cache.c:280
char * mapset
Definition: raster3d.h:78
double south
Definition: raster3d.h:50
void Rast3d_free(void *)
Same as free (ptr).