GRASS GIS 8 Programmer's Manual  8.4.0dev(2024)-7413740dd8
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>"),
48  map->fileName);
49  }
50 
51  Rast3d_range_write(map);
52 
53  close(map->data_fd);
54 
55  /* finally move tempfile to data file */
57 #ifdef __MINGW32__
58  if (CopyFile(map->tempName, path, FALSE) == 0) {
59 #else
60  if (link(map->tempName, path) < 0) {
61 #endif
62  if (rename(map->tempName, path)) {
63  G_warning(
64  _("Unable to move temp raster map <%s> to 3D raster map <%s>"),
65  map->tempName, path);
66  return 0;
67  }
68  }
69  else
70  remove(map->tempName);
71 
72  return 1;
73 }
74 
75 static int close_cell_new(RASTER3D_Map *map)
76 {
77  long ltmp;
78 
79  if (map->useCache)
80  if (!Rast3d_flush_all_tiles(map)) {
81  G_warning(_("Unable to flush all tiles"));
82  return 0;
83  }
84 
85  if (!Rast3d_flush_index(map)) {
86  G_warning(_("Unable to flush index"));
87  return 0;
88  }
89 
90  /* write the header info which was filled with dummy values at the */
91  /* opening time */
92 
93  if (lseek(map->data_fd, (long)(map->offset - sizeof(int) - sizeof(long)),
94  SEEK_SET) == -1) {
95  G_warning(_("Unable to position file"));
96  return 0;
97  }
98 
99  if (!Rast3d_write_ints(map->data_fd, map->useXdr, &(map->indexNbytesUsed),
100  1)) {
101  G_warning(_("Unable to write header for 3D raster map <%s>"),
102  map->fileName);
103  return 0;
104  }
105 
106  Rast3d_long_encode(&(map->indexOffset), (unsigned char *)&ltmp, 1);
107  if (write(map->data_fd, &ltmp, sizeof(long)) != sizeof(long)) {
108  G_warning(_("Unable to write header for 3D raster map <%s>"),
109  map->fileName);
110  return 0;
111  }
112 
113  if (!close_new(map)) {
114  G_warning(_("Unable to create 3D raster map <%s>"), map->fileName);
115  return 0;
116  }
117 
118  return 1;
119 }
120 
121 static int close_old(RASTER3D_Map *map)
122 {
123  if (close(map->data_fd) != 0) {
124  G_warning(_("Unable to close 3D raster map <%s>"), map->fileName);
125  return 0;
126  }
127 
128  return 1;
129 }
130 
131 static int close_cell_old(RASTER3D_Map *map)
132 {
133  if (!close_old(map)) {
134  G_warning(_("Unable to close 3D raster map <%s>"), map->fileName);
135  return 0;
136  }
137 
138  return 1;
139 }
140 
141 /*!
142  \brief Close 3D raster map files
143 
144  Closes g3d-file. If <em>map</em> is new and cache-mode is used for
145  <em>map</em> then every tile which is not flushed before closing is
146  flushed.
147 
148  \param map pointer to RASTER3D_Map to be closed
149 
150  \return 1 success
151  \return 0 failure
152  */
154 {
155  if (map->operation == RASTER3D_WRITE_DATA) {
156  if (!close_cell_new(map)) {
157  G_warning(_("Unable to create 3D raster map <%s>"), map->fileName);
158  return 0;
159  }
160  }
161  else {
162  if (!close_cell_old(map)) {
163  G_warning(_("Unable to close 3D raster map <%s>"), map->fileName);
164  return 0;
165  }
166  }
167 
168  Rast3d_free(map->index);
169  Rast3d_free(map->tileLength);
170 
171  if (map->useCache) {
172  if (!Rast3d_dispose_cache(map)) {
173  G_warning(_("Error in cache"));
174  return 0;
175  }
176  }
177  else
178  Rast3d_free(map->data);
179 
180  if (map->operation == RASTER3D_WRITE_DATA)
181  if (!Rast3d_write_header(
182  map, map->region.proj, map->region.zone, map->region.north,
183  map->region.south, map->region.east, map->region.west,
184  map->region.top, map->region.bottom, map->region.rows,
185  map->region.cols, map->region.depths, map->region.ew_res,
186  map->region.ns_res, map->region.tb_res, map->tileX, map->tileY,
187  map->tileZ, map->type, map->compression, map->useRle,
188  map->useLzw, map->precision, map->offset, map->useXdr,
189  map->hasIndex, map->unit, map->vertical_unit, map->version)) {
190  G_warning(_("Unable to write header for 3D raster map <%s>"),
191  map->fileName);
192  return 0;
193  }
194 
195  Rast3d_free(map);
196 
197  return 1;
198 }
#define NULL
Definition: ccmath.h:32
void G_warning(const char *,...) __attribute__((format(printf
int Rast3d_flush_all_tiles(RASTER3D_Map *)
Definition: cache.c:283
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)
void Rast3d_filename(char *, const char *, const char *, const char *)
Definition: filename.c:9
void Rast3d_free(void *)
Same as free (ptr).
int Rast3d_flush_index(RASTER3D_Map *)
int Rast3d_dispose_cache(RASTER3D_Map *)
Definition: cache.c:249
int Rast3d_write_history(const char *, struct History *)
write raster3d History file
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:26
int Rast3d_write_ints(int, int, const int *, int)
Definition: intio.c:9
int Rast3d_range_write(RASTER3D_Map *)
Writes the range which is stored in the range structure of map. (This function is invoked automatical...
int Rast3d_long_encode(long *, unsigned char *, int)
Definition: long.c:5
int Rast3d_remove_color(const char *)
Removes the primary and/or secondary color file.
void Rast_free_cats(struct Categories *)
Free category structure memory.
Definition: raster/cats.c:1213
void Rast_init_cats(const char *, struct Categories *)
Initialize category structure.
Definition: raster/cats.c:1145
void Rast_short_history(const char *, const char *, struct History *)
Initialize history structure.
int Rast_command_history(struct History *)
Save command line to raster history structure.
#define GPATH_MAX
Definition: gis.h:194
#define FALSE
Definition: gis.h:83
#define _(str)
Definition: glocale.h:10
int Rast3d_close(RASTER3D_Map *map)
Close 3D raster map files.
#define RASTER3D_CELL_ELEMENT
Definition: raster3d.h:32
#define RASTER3D_WRITE_DATA
Raster history info (metadata)
Definition: raster.h:172
long * index
Definition: raster3d.h:142
int precision
Definition: raster3d.h:109
int compression
Definition: raster3d.h:111
char * fileName
Definition: raster3d.h:74
RASTER3D_Region region
Definition: raster3d.h:82
char * data
Definition: raster3d.h:153
char * tempName
Definition: raster3d.h:75
int version
Definition: raster3d.h:72
char * mapset
Definition: raster3d.h:76
int indexNbytesUsed
Definition: raster3d.h:130
char * unit
Definition: raster3d.h:91
int hasIndex
Definition: raster3d.h:136
int operation
Definition: raster3d.h:79
int vertical_unit
Definition: raster3d.h:92
int * tileLength
Definition: raster3d.h:145
int useCache
Definition: raster3d.h:160
long indexOffset
Definition: raster3d.h:123
double tb_res
Definition: raster3d.h:56
double north
Definition: raster3d.h:49
double south
Definition: raster3d.h:49
double east
Definition: raster3d.h:50
double ns_res
Definition: raster3d.h:56
double ew_res
Definition: raster3d.h:56
double bottom
Definition: raster3d.h:51
double top
Definition: raster3d.h:51
double west
Definition: raster3d.h:50
Definition: path.h:15