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