GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
Loading...
Searching...
No Matches
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 SPDX-FileCopyrightText: 1999-2009, 2011 GRASS Development Team
7 SPDX-License-Identifier: GPL-2.0-or-later
8
9 \author USACERL and many others
10 */
11
12#ifdef _WIN32
13#include <windows.h>
14#endif
15#include <stdio.h>
16#include <stdlib.h>
17#include <sys/types.h>
18#include <unistd.h>
19
20#include <grass/raster.h>
21#include <grass/glocale.h>
22
23#include "raster3d_intern.h"
24
25static int close_new(RASTER3D_Map *map)
26{
27 char path[GPATH_MAX];
28 struct Categories cats;
29 struct History hist;
30
32
33 /* create empty cats file */
34 Rast_init_cats(NULL, &cats);
35 Rast3d_write_cats(map->fileName, &cats);
36 Rast_free_cats(&cats);
37
38 /*generate the history file, use the normal G_ functions */
39 Rast_short_history(map->fileName, "raster3d", &hist);
41 /*Use the G3d function to write the history file,
42 * otherwise the path is wrong */
43 if (Rast3d_write_history(map->fileName, &hist) < 0) {
44 G_warning(_("Unable to write history for 3D raster map <%s>"),
45 map->fileName);
46 }
47
49
50 close(map->data_fd);
51
52 /* finally move tempfile to data file */
54#ifdef _WIN32
55 if (CopyFile(map->tempName, path, FALSE) == 0) {
56#else
57 if (link(map->tempName, path) < 0) {
58#endif
59 if (rename(map->tempName, path)) {
61 _("Unable to move temp raster map <%s> to 3D raster map <%s>"),
62 map->tempName, path);
63 return 0;
64 }
65 }
66 else
67 remove(map->tempName);
68
69 return 1;
70}
71
72static int close_cell_new(RASTER3D_Map *map)
73{
74 long ltmp;
75
76 if (map->useCache)
77 if (!Rast3d_flush_all_tiles(map)) {
78 G_warning(_("Unable to flush all tiles"));
79 return 0;
80 }
81
82 if (!Rast3d_flush_index(map)) {
83 G_warning(_("Unable to flush index"));
84 return 0;
85 }
86
87 /* write the header info which was filled with dummy values at the */
88 /* opening time */
89
90 if (lseek(map->data_fd, (long)(map->offset - sizeof(int) - sizeof(long)),
91 SEEK_SET) == -1) {
92 G_warning(_("Unable to position file"));
93 return 0;
94 }
95
96 if (!Rast3d_write_ints(map->data_fd, map->useXdr, &(map->indexNbytesUsed),
97 1)) {
98 G_warning(_("Unable to write header for 3D raster map <%s>"),
99 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>"),
106 map->fileName);
107 return 0;
108 }
109
110 if (!close_new(map)) {
111 G_warning(_("Unable to create 3D raster map <%s>"), map->fileName);
112 return 0;
113 }
114
115 return 1;
116}
117
118static int close_old(RASTER3D_Map *map)
119{
120 if (close(map->data_fd) != 0) {
121 G_warning(_("Unable to close 3D raster map <%s>"), map->fileName);
122 return 0;
123 }
124
125 return 1;
126}
127
128static int close_cell_old(RASTER3D_Map *map)
129{
130 if (!close_old(map)) {
131 G_warning(_("Unable to close 3D raster map <%s>"), map->fileName);
132 return 0;
133 }
134
135 return 1;
136}
137
138/*!
139 \brief Close 3D raster map files
140
141 Closes g3d-file. If <em>map</em> is new and cache-mode is used for
142 <em>map</em> then every tile which is not flushed before closing is
143 flushed.
144
145 \param map pointer to RASTER3D_Map to be closed
146
147 \return 1 success
148 \return 0 failure
149 */
151{
152 if (map->operation == RASTER3D_WRITE_DATA) {
153 if (!close_cell_new(map)) {
154 G_warning(_("Unable to create 3D raster map <%s>"), map->fileName);
155 return 0;
156 }
157 }
158 else {
159 if (!close_cell_old(map)) {
160 G_warning(_("Unable to close 3D raster map <%s>"), map->fileName);
161 return 0;
162 }
163 }
164
165 Rast3d_free(map->index);
167
168 if (map->useCache) {
169 if (!Rast3d_dispose_cache(map)) {
170 G_warning(_("Error in cache"));
171 return 0;
172 }
173 }
174 else
175 Rast3d_free(map->data);
176
177 if (map->operation == RASTER3D_WRITE_DATA)
179 map, map->region.proj, map->region.zone, map->region.north,
180 map->region.south, map->region.east, map->region.west,
181 map->region.top, map->region.bottom, map->region.rows,
182 map->region.cols, map->region.depths, map->region.ew_res,
183 map->region.ns_res, map->region.tb_res, map->tileX, map->tileY,
184 map->tileZ, map->type, map->compression, map->useRle,
185 map->useLzw, map->precision, map->offset, map->useXdr,
186 map->hasIndex, map->unit, map->vertical_unit, map->version)) {
187 G_warning(_("Unable to write header for 3D raster map <%s>"),
188 map->fileName);
189 return 0;
190 }
191
192 Rast3d_free(map);
193
194 return 1;
195}
#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...
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.
void Rast_init_cats(const char *, struct Categories *)
Initialize category structure.
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:196
#define FALSE
Definition gis.h:79
#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 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
char * mapset
Definition raster3d.h:76
int indexNbytesUsed
Definition raster3d.h:130
char * unit
Definition raster3d.h:91
int operation
Definition raster3d.h:79
int vertical_unit
Definition raster3d.h:92
int * tileLength
Definition raster3d.h:145
long indexOffset
Definition raster3d.h:123
double tb_res
Definition raster3d.h:56
double ns_res
Definition raster3d.h:56
double ew_res
Definition raster3d.h:56
double bottom
Definition raster3d.h:51
Definition path.h:15
#define close
Definition unistd.h:8
#define write
Definition unistd.h:6