void *Rast3d_open_cell_new(const char *name, int typeIntern, int cache, RASTER3D_Region * region) { RASTER3D_Map *map; int nofHeaderBytes, dummy = 0, compression, precision; long ldummy = 0; char xname[GNAME_MAX], xmapset[GMAPSET_MAX]; Rast3d_init_defaults(); if (!Rast3d_mask_open_old()) { Rast3d_error(_("Rast3d_open_cell_new: error in Rast3d_mask_open_old")); return (void *)NULL; } compression = g3d_do_compression; precision = g3d_precision; map = Rast3d_malloc(sizeof(RASTER3D_Map)); if (map == NULL) { Rast3d_error(_("Rast3d_open_cell_new: error in Rast3d_malloc")); return (void *)NULL; } if (G_unqualified_name(name, G_mapset(), xname, xmapset) < 0) { G_warning(_("map <%s> is not in the current mapset"), name); return (void *)NULL; } map->fileName = G_store(xname); map->mapset = G_store(xmapset); map->tempName = G_tempfile(); map->data_fd = open(map->tempName, O_RDWR | O_CREAT | O_TRUNC, 0666); if (map->data_fd < 0) { Rast3d_error(_("Rast3d_open_cell_new: could not open file")); return (void *)NULL; } Rast3d_make_mapset_map_directory(map->fileName); map->useXdr = RASTER3D_USE_XDR; if (g3d_file_type == FCELL_TYPE) { if (precision > 23) precision = 23; /* 32 - 8 - 1 */ else if (precision < -1) precision = 0; } else if (precision > 52) precision = 52; /* 64 - 11 - 1 */ else if (precision < -1) precision = 0; /* no need to write trailing zeros */ if ((typeIntern == FCELL_TYPE) && (g3d_file_type == DCELL_TYPE)) { if (precision == -1) precision = 23; else precision = RASTER3D_MIN(precision, 23); } if (compression == RASTER3D_NO_COMPRESSION) precision = RASTER3D_MAX_PRECISION; if (compression == RASTER3D_COMPRESSION) map->useXdr = RASTER3D_USE_XDR; if (RASTER3D_HAS_INDEX) { map->indexLongNbytes = sizeof(long); /* at the beginning of the file write */ /* nof bytes of "long" */ /* max nof bytes used for index */ /* position of index in file */ /* the index is appended at the end of the file at closing time. since */ /* we do not know this position yet we write dummy values */ if ((!Rast3d_write_ints(map->data_fd, map->useXdr, &(map->indexLongNbytes), 1)) || (!Rast3d_write_ints(map->data_fd, map->useXdr, &dummy, 1))) { Rast3d_error(_("Rast3d_open_cell_new: can't write header")); return (void *)NULL; } if (write(map->data_fd, &ldummy, map->indexLongNbytes) != map->indexLongNbytes) { Rast3d_error(_("Rast3d_open_cell_new: can't write header")); return (void *)NULL; } } /* can't use a constant since this depends on sizeof (long) */ nofHeaderBytes = lseek(map->data_fd, (long)0, SEEK_CUR); Rast3d_range_init(map); Rast3d_adjust_region(region); if (!Rast3d_fill_header(map, RASTER3D_WRITE_DATA, compression, g3d_do_rle_compression, g3d_do_lzw_compression, g3d_file_type, precision, cache, RASTER3D_HAS_INDEX, map->useXdr, typeIntern, nofHeaderBytes, g3d_tile_dimension[0], g3d_tile_dimension[1], g3d_tile_dimension[2], region->proj, region->zone, region->north, region->south, region->east, region->west, region->top, region->bottom, region->rows, region->cols, region->depths, region->ew_res, region->ns_res, region->tb_res, g3d_unit_default)) { Rast3d_error(_("Rast3d_open_cell_new: error in Rast3d_fill_header")); return (void *)NULL; } /*Set the map window to the map region */ Rast3d_region_copy(&(map->window), region); /*Set the resampling function to nearest neighbor for data access */ Rast3d_get_nearest_neighbor_fun_ptr(&(map->resampleFun)); Rast3d_mask_off(map); return (void *)map; }
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) != 0) { G_warning(_("Unable to create 3D raster map <%s>"), map->fileName); return 0; } return 1; }