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