Ejemplo n.º 1
0
int Rast3d_flush_index(RASTER3D_Map * map)
{
    int sizeCompressed, indexLength, tileIndex;
    unsigned char *tmp;
    long ldummy;

    if (!map->hasIndex)
	return 1;

    map->indexOffset = lseek(map->data_fd, (long)0, SEEK_END);
    if (map->indexOffset == -1) {
	Rast3d_error("Rast3d_flush_index: can't rewind file");
	return 0;
    }

    map->indexNbytesUsed = Rast3d_long_encode(&(map->indexOffset),
					  (unsigned char *)&ldummy, 1);

    tmp = Rast3d_malloc(sizeof(long) * map->nTiles);
    if (tmp == NULL) {
	Rast3d_error("Rast3d_flush_index: error in Rast3d_malloc");
	return 0;
    }

    for (tileIndex = 0; tileIndex < map->nTiles; tileIndex++)
	if (map->index[tileIndex] == -1)
	    map->index[tileIndex] = 0;

    (void)Rast3d_long_encode(map->index, tmp, map->nTiles);

    sizeCompressed = Rast3d_rle_count_only(tmp, sizeof(long) * map->nTiles, 1);

    if (sizeCompressed >= map->nTiles * sizeof(long)) {
	indexLength = map->nTiles * sizeof(long);
	if (write(map->data_fd, tmp, indexLength) != indexLength) {
	    Rast3d_error("Rast3d_flush_index: can't write file");
	    return 0;
	}
    }
    else {
	indexLength = sizeCompressed;
	Rast3d_rle_encode(tmp, (char *)map->index, sizeof(long) * map->nTiles, 1);
	if (write(map->data_fd, map->index, sizeCompressed) != sizeCompressed) {
	    Rast3d_error("Rast3d_flush_index: can't write file");
	    return 0;
	}
    }

    Rast3d_free(tmp);
    if (!Rast3d_readIndex(map)) {
	Rast3d_error("Rast3d_flush_index: error in Rast3d_readIndex");
	return 0;
    }

    return 1;
}
Ejemplo n.º 2
0
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 *)&ltmp, 1);
    if (write(map->data_fd, &ltmp, 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;
}