Esempio n. 1
0
/*!
  \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) != 0) {
	    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;
}
Esempio n. 2
0
int Rast3d_get_standard3d_params(int *useTypeDefault, int *type,
			    int *useCompressionDefault, int *doCompression,
			    int *usePrecisionDefault, int *precision,
			    int *useDimensionDefault, int *tileX, int *tileY,
			    int *tileZ)
{

    *useTypeDefault = *useCompressionDefault = 0;
    *usePrecisionDefault = *useDimensionDefault = 0;

    Rast3d_init_defaults();

    if (strcmp(param->type->answer, "double") == 0)
	*type = DCELL_TYPE;
    else if (strcmp(param->type->answer, "float") == 0)
	*type = FCELL_TYPE;
    else {
	*type = Rast3d_get_file_type();
	*useTypeDefault = 1;
    }

    Rast3d_get_compression_mode(doCompression, precision);

    if (strcmp(param->precision->answer, "default") != 0) {
	if (strcmp(param->precision->answer, "max") == 0)
	    *precision = -1;
	else if ((sscanf(param->precision->answer, "%d", precision) != 1) ||
		 (*precision < 0)) {
	    Rast3d_error(_("Rast3d_get_standard3d_params: precision value invalid"));
	    return 0;
	}
	}
	else
	*usePrecisionDefault = 1;


	if (strcmp(param->compression->answer, "default") != 0) {
 		if (strcmp(param->compression->answer, "zip") == 0)
			*doCompression = RASTER3D_COMPRESSION;
		else
			*doCompression = RASTER3D_NO_COMPRESSION;
	} else {
		*useCompressionDefault = 1;
	}

    Rast3d_get_tile_dimension(tileX, tileY, tileZ);
    if (strcmp(param->dimension->answer, "default") != 0) {
	if (sscanf(param->dimension->answer, "%dx%dx%d",
		   tileX, tileY, tileZ) != 3) {
	    Rast3d_error(_("Rast3d_get_standard3d_params: tile dimension value invalid"));
	    return 0;
	}
    }
    else
	*useDimensionDefault = 1;

    Rast3d_free(param);

    return 1;
}
Esempio n. 3
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;
}
Esempio n. 4
0
void Rast3d_cache_dispose(RASTER3D_cache * c)
{
    if (c == NULL)
	return;

    Rast3d_cache_hash_dispose(c->hash);

    if (c->elts != NULL)
	Rast3d_free(c->elts);
    if (c->names != NULL)
	Rast3d_free(c->names);
    if (c->locks != NULL)
	Rast3d_free(c->locks);
    if (c->next != NULL)
	Rast3d_free(c->next);
    if (c->prev != NULL)
	Rast3d_free(c->prev);

    Rast3d_free(c);
}
Esempio n. 5
0
static int Rast3d_readIndex(RASTER3D_Map * map)
{
    unsigned char *tmp, *tmp2;
    int dummy1, dummy2, indexLength, tileIndex;
    long indexLast;

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

    indexLength = indexLast - map->indexOffset;

    if (lseek(map->data_fd, map->indexOffset, SEEK_SET) == -1) {
	Rast3d_error("Rast3d_readIndex: can't position file");
	return 0;
    }

    tmp = Rast3d_malloc(map->indexLongNbytes * map->nTiles);

    if (tmp == NULL) {
	Rast3d_error("Rast3d_readIndex: error in Rast3d_malloc");
	return 0;
    }

    if (indexLength < map->indexLongNbytes * map->nTiles) {	/* RLE encoded? */

	if (indexLength > sizeof(long) * map->nTiles) {
						     /*->index large enough? */
	    tmp2 = Rast3d_malloc(indexLength);
	    if (tmp2 == NULL) {
		Rast3d_error("Rast3d_readIndex: error in Rast3d_malloc");
		return 0;
	    }
	}
	else			/* YES */
	    tmp2 = (unsigned char *)map->index;

	if (read(map->data_fd, tmp2, indexLength) != indexLength) {
	    Rast3d_error("Rast3d_readIndex: can't read file");
	    return 0;
	}

	Rast3d_rle_decode(tmp2, tmp, map->indexLongNbytes * map->nTiles, 1,
		     &dummy1, &dummy2);

	if (indexLength > sizeof(long) * map->nTiles)
	    Rast3d_free(tmp2);
    }
    else /* NO RLE */ if (read(map->data_fd, tmp, indexLength) != indexLength) {
	Rast3d_error("Rast3d_readIndex: can't read file");
	return 0;
    }

    Rast3d_long_decode(tmp, map->index, map->nTiles, map->indexLongNbytes);

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

    Rast3d_free(tmp);

    return 1;
}
Esempio n. 6
0
int Rast3d_init_index(RASTER3D_Map * map, int hasIndex)
{
    int tile;
    int i0, i1, i2, i3, i4, i5, offset, nofElts;
    int *offsetP;

    map->hasIndex = hasIndex;
    map->index = Rast3d_malloc(sizeof(long) * map->nTiles);
    map->tileLength = Rast3d_malloc(sizeof(int) * map->nTiles);

    if ((map->index == NULL) || (map->tileLength == NULL)) {
	Rast3d_error("Rast3d_init_index: error in Rast3d_malloc");
	return 0;
    }

    if (map->operation == RASTER3D_WRITE_DATA) {
	for (tile = 0; tile < map->nTiles; tile++)
	    map->index[tile] = -1;
	return 1;
    }

    if (!map->hasIndex) {
	offset = 0;
	for (tile = 0; tile < map->nTiles; tile++) {
	    map->index[tile] = offset * map->numLengthExtern + map->offset;
	    nofElts = Rast3d_compute_clipped_tile_dimensions
		(map, tile, &i0, &i1, &i2, &i3, &i4, &i5);
	    map->tileLength[tile] = nofElts * map->numLengthExtern;
	    offset += nofElts;
	}
	return 1;
    }

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

    offsetP = Rast3d_malloc(sizeof(int) * map->nTiles);
    if (offsetP == NULL) {
	Rast3d_error("Rast3d_init_index: error in Rast3d_malloc");
	return 0;
    }

    for (tile = 0; tile < map->nTiles; tile++)
	offsetP[tile] = tile;
    cmpIndex = map->index;
    qsort(offsetP, map->nTiles, sizeof(int), indexSortCompare);

    for (tile = 0; tile < map->nTiles - 1; tile++) {
	if (map->index[offsetP[tile]] == -1) {
	    map->tileLength[offsetP[tile]] = 0;
	    continue;
	}

	map->tileLength[offsetP[tile]] = map->index[offsetP[tile + 1]] -
	    map->index[offsetP[tile]];
    }

    if (map->index[offsetP[map->nTiles - 1]] == -1)
	map->tileLength[offsetP[map->nTiles - 1]] = 0;
    else
	map->tileLength[offsetP[map->nTiles - 1]] =
	    map->indexOffset - map->index[offsetP[map->nTiles - 1]];

    Rast3d_free(offsetP);

    return 1;
}
Esempio n. 7
0
void *Rast3d_open_cell_old(const char *name, const char *mapset,
		      RASTER3D_Region * window, int typeIntern, int cache)
{
    RASTER3D_Map *map;
    int proj, zone;
    int compression, useRle, useLzw, type, tileX, tileY, tileZ;
    int rows, cols, depths, precision;
    double ew_res, ns_res, tb_res;
    int nofHeaderBytes, dataOffset, useXdr, hasIndex;
    char *ltmp, *unit;
    double north, south, east, west, top, bottom;

    map = Rast3d_open_cell_old_no_header(name, mapset);
    if (map == NULL) {
	Rast3d_error(_("Rast3d_open_cell_old: error in Rast3d_open_cell_old_no_header"));
	return (void *)NULL;
    }

    if (lseek(map->data_fd, (long)0, SEEK_SET) == -1) {
	Rast3d_error(_("Rast3d_open_cell_old: can't rewind file"));
	return (void *)NULL;
    }

    if (!Rast3d_read_header(map,
			&proj, &zone,
			&north, &south, &east, &west, &top, &bottom,
			&rows, &cols, &depths,
			&ew_res, &ns_res, &tb_res,
			&tileX, &tileY, &tileZ,
			&type, &compression, &useRle, &useLzw,
			&precision, &dataOffset, &useXdr, &hasIndex, &unit)) {
	Rast3d_error(_("Rast3d_open_cell_old: error in Rast3d_read_header"));
	return 0;
    }

    if (window == RASTER3D_DEFAULT_WINDOW)
	window = Rast3d_window_ptr();

    if (proj != window->proj) {
	Rast3d_error(_("Rast3d_open_cell_old: projection does not match window projection"));
	return (void *)NULL;
    }
    if (zone != window->zone) {
	Rast3d_error(_("Rast3d_open_cell_old: zone does not match window zone"));
	return (void *)NULL;
    }

    map->useXdr = useXdr;

    if (hasIndex) {
	/* see RASTER3D_openCell_new () for format of header */
	if ((!Rast3d_read_ints(map->data_fd, map->useXdr,
			   &(map->indexLongNbytes), 1)) ||
	    (!Rast3d_read_ints(map->data_fd, map->useXdr,
			   &(map->indexNbytesUsed), 1))) {
	    Rast3d_error(_("Rast3d_open_cell_old: can't read header"));
	    return (void *)NULL;
	}

	/* if our long is to short to store offsets we can't read the file */
	if (map->indexNbytesUsed > sizeof(long))
	    Rast3d_fatal_error(_("Rast3d_open_cell_old: index does not fit into long"));

	ltmp = Rast3d_malloc(map->indexLongNbytes);
	if (ltmp == NULL) {
	    Rast3d_error(_("Rast3d_open_cell_old: error in Rast3d_malloc"));
	    return (void *)NULL;
	}

	/* convert file long to long */
	if (read(map->data_fd, ltmp, map->indexLongNbytes) !=
	    map->indexLongNbytes) {
	    Rast3d_error(_("Rast3d_open_cell_old: can't read header"));
	    return (void *)NULL;
	}
	Rast3d_long_decode(ltmp, &(map->indexOffset), 1, map->indexLongNbytes);
	Rast3d_free(ltmp);
    }

    nofHeaderBytes = dataOffset;

    if (typeIntern == RASTER3D_TILE_SAME_AS_FILE)
	typeIntern = type;

    if (!Rast3d_fill_header(map, RASTER3D_READ_DATA, compression, useRle, useLzw,
			type, precision, cache,
			hasIndex, map->useXdr, typeIntern,
			nofHeaderBytes, tileX, tileY, tileZ,
			proj, zone,
			north, south, east, west, top, bottom,
			rows, cols, depths, ew_res, ns_res, tb_res, unit)) {
	Rast3d_error(_("Rast3d_open_cell_old: error in Rast3d_fill_header"));
	return (void *)NULL;
    }

    Rast3d_region_copy(&(map->window), window);
    Rast3d_adjust_region(&(map->window));
    Rast3d_get_nearest_neighbor_fun_ptr(&(map->resampleFun));

    return map;
}
Esempio n. 8
0
void Rast3d_free_tiles(void *tiles)
{
    Rast3d_free(tiles);
}
Esempio n. 9
0
void Rast3d_set_unit(const char *unit)
{
    Rast3d_free(g3d_unit_default);
    g3d_unit_default = G_store(unit);
}