Beispiel #1
0
void Rast3d_change_precision(void *map, int precision, const char *nameOut)
{
    void *map2;
    int x, y, z, savePrecision, saveCompression;
    char *data;
    RASTER3D_Region region;
    int typeIntern;
    int nx, ny, nz;
    int tileXsave, tileYsave, tileZsave, tileX, tileY, tileZ, saveType;

    saveType = Rast3d_get_file_type();
    /*   Rast3d_set_file_type (Rast3d_file_type_map (map)); */
    Rast3d_get_compression_mode(&saveCompression, &savePrecision);
    Rast3d_set_compression_mode(RASTER3D_COMPRESSION, precision);
    Rast3d_get_tile_dimension(&tileXsave, &tileYsave, &tileZsave);
    Rast3d_get_tile_dimensions_map(map, &tileX, &tileY, &tileZ);
    Rast3d_set_tile_dimension(tileX, tileY, tileZ);

    typeIntern = Rast3d_tile_type_map(map);
    Rast3d_get_region_struct_map(map, &region);

    map2 =
	Rast3d_open_cell_new(nameOut, typeIntern, RASTER3D_USE_CACHE_DEFAULT, &region);
    if (map2 == NULL)
	Rast3d_fatal_error("Rast3d_change_precision: error in Rast3d_open_cell_new");

    Rast3d_set_file_type(saveType);
    Rast3d_set_compression_mode(saveCompression, savePrecision);
    Rast3d_set_tile_dimension(tileXsave, tileYsave, tileZsave);

    data = Rast3d_alloc_tiles(map, 1);
    if (data == NULL)
	Rast3d_fatal_error("Rast3d_change_precision: error in Rast3d_alloc_tiles");
    Rast3d_get_nof_tiles_map(map2, &nx, &ny, &nz);

    for (z = 0; z < nz; z++)
	for (y = 0; y < ny; y++)
	    for (x = 0; x < nx; x++) {
		if (!Rast3d_read_tile(map, Rast3d_tile2tile_index(map, x, y, z), data,
				  typeIntern))
		    Rast3d_fatal_error
			("Rast3d_change_precision: error in Rast3d_read_tile");
		if (!Rast3d_write_tile
		    (map2, Rast3d_tile2tile_index(map2, x, y, z), data,
		     typeIntern))
		    Rast3d_fatal_error
			("Rast3d_change_precision: error in Rast3d_write_tile");
	    }

    Rast3d_free_tiles(data);
    if (!Rast3d_close(map2))
	Rast3d_fatal_error("Rast3d_change_precision: error in Rast3d_close");
}
Beispiel #2
0
static void makeMask(char *name, d_Mask * maskRules)
{
    void *map, *mask;
    RASTER3D_Region region;
    int tileX, tileY, tileZ, x, y, z, cacheSize;
    double value;
    float floatNull;

    cacheSize = Rast3d_cache_size_encode(RASTER3D_USE_CACHE_XY, 1);

    if (NULL == G_find_raster3d(name, ""))
	Rast3d_fatal_error(_("3D raster map <%s> not found"), name);

    map = Rast3d_open_cell_old(name, G_mapset(), RASTER3D_DEFAULT_WINDOW,
			  DCELL_TYPE, cacheSize);

    if (map == NULL)
	Rast3d_fatal_error(_("Unable to open 3D raster map <%s>"), name);

    Rast3d_get_region_struct_map(map, &region);

    Rast3d_get_tile_dimensions_map(map, &tileX, &tileY, &tileZ);

    mask = Rast3d_open_new_param(Rast3d_mask_file(), FCELL_TYPE, cacheSize,
			    &region, FCELL_TYPE, RASTER3D_COMPRESSION, 0,
			    tileX, tileY, tileZ);

    if (mask == NULL)
	Rast3d_fatal_error(_("Unable to open 3D raster mask file"));

    Rast3d_min_unlocked(map, RASTER3D_USE_CACHE_X);
    Rast3d_autolock_on(map);
    Rast3d_unlock_all(map);
    Rast3d_min_unlocked(mask, RASTER3D_USE_CACHE_X);
    Rast3d_autolock_on(mask);
    Rast3d_unlock_all(mask);

    Rast3d_set_null_value(&floatNull, 1, FCELL_TYPE);

    for (z = 0; z < region.depths; z++) {
	if ((z % tileZ) == 0) {
	    Rast3d_unlock_all(map);
	    Rast3d_unlock_all(mask);
	}
    
	for (y = 0; y < region.rows; y++)	/* We count from north to south in the cube coordinate system */
	    for (x = 0; x < region.cols; x++) {
		value = Rast3d_get_double_region(map, x, y, z);
		if (Rast3d_mask_d_select((DCELL *) & value, maskRules))
		    Rast3d_put_float(mask, x, y, z, (float)floatNull);	/* mask-out value */
		else
		    Rast3d_put_float(mask, x, y, z, (float)0.0);	/* not mask-out value */
	    }
	if ((z % tileZ) == 0) {
	    if (!Rast3d_flush_tiles_in_cube
		(mask, 0, 0, MAX(0, z - tileZ), region.rows - 1,
		 region.cols - 1, z))
		Rast3d_fatal_error(_("makeMask: error flushing tiles in cube"));
	}
    }

    if (!Rast3d_flush_all_tiles(mask))
	Rast3d_fatal_error(_("makeMask: error flushing all tiles"));

    Rast3d_autolock_off(map);
    Rast3d_unlock_all(map);
    Rast3d_autolock_off(mask);
    Rast3d_unlock_all(mask);

    if (!Rast3d_close(mask))
	Rast3d_fatal_error(_("Unable to close 3D raster mask file"));
    if (!Rast3d_close(map))
	Rast3d_fatal_error(_("Unable to close raster map <%s>"), name);
}
Beispiel #3
0
void
asciiToG3d(FILE * fp, RASTER3D_Region * region, int convertNull, char *nullValue)
{
    int x, y, z;
    int col, row, depth;
    double value;
    char buff[256];
    int tileX, tileY, tileZ;

    Rast3d_get_tile_dimensions_map(map, &tileX, &tileY, &tileZ);
    Rast3d_min_unlocked(map, RASTER3D_USE_CACHE_X);

    Rast3d_autolock_on(map);
    Rast3d_unlock_all(map);
    G_message(_("Loading data ...  (%dx%dx%d)"), region->cols, region->rows,
              region->depths);

    G_debug(3,
            "asciiToG3d: writing the 3D raster map, with rows %i cols %i depths %i",
            region->rows, region->cols, region->depths);

    for (z = 0; z < region->depths; z++) {
        G_percent(z, region->depths, 1);

        if ((z % tileZ) == 0)
            Rast3d_unlock_all(map);

        for (y = 0; y < region->rows; y++) /* go south to north */
            for (x = 0; x < region->cols; x++) {

                /* From west to east */
                col = x;
                /* The default is to read rows from north to south */
                row = y;
                /* From bottom to the top */
                depth = z;

                /* Read rows as from south to north */
                if (rowOrder == ROW_ORDER_SOUTH_TO_NORTH)
                    row = region->rows - y - 1;

                /* Read XY layer from top to bottom */
                if (depthOrder == DEPTH_ORDER_TOP_TO_BOTTOM)
                    depth = region->depths - z - 1;

                if (fscanf(fp, "%s", buff) != 1) {
                    if (feof(fp))
                        G_warning(_("End of file reached while still loading data."));
                    G_debug(3,
                            "missing data at col=%d row=%d depth=%d last_value=[%.4f]",
                            x + 1, y + 1, z + 1, value);
                    fatalError("asciiToG3d: read failed");
                }

                /* Check for null value */
                if (convertNull && strncmp(buff, nullValue, strlen(nullValue)) == 0) {
                    Rast3d_set_null_value(&value, 1, DCELL_TYPE);
                } else {
                    if (sscanf(buff, "%lf", &value) != 1) {
                        G_warning(_("Invalid value detected"));
                        G_debug(1, "invalid value at col=%d row=%d depth=%d last_value=[%s]",
                                x + 1, y + 1, z + 1, buff);
                        fatalError("asciiToG3d: read failed");
                    }
                }
                /* Write the data */
                Rast3d_put_double(map, col, row, depth, value);
            }
    }

    if (fscanf(fp, "%lf", &value) == 1) {
        G_warning(_("Data exists in input file after fully importing "
                    "expected data.  [%.4f ...]"), value);
    }

    if (!Rast3d_flush_all_tiles(map))
        fatalError("asciiTog3d: error flushing tiles");

    Rast3d_autolock_off(map);
    Rast3d_unlock_all(map);

    G_percent(1, 1, 1);
}
Beispiel #4
0
static void bin_to_raster3d(char *null, int map_type, int is_integer,
		int is_signed, int bytes, int byte_swap, int row_swap, int depth_swap) {
	int x, y, z;
	int col, row, depth;
	DCELL value;
	FCELL fvalue;
	DCELL null_value;
	int tileX, tileY, tileZ;

	if (null)
		null_value = atof(null);

	Rast3d_get_tile_dimensions_map(map, &tileX, &tileY, &tileZ);
	Rast3d_min_unlocked(map, RASTER3D_USE_CACHE_X);

	Rast3d_autolock_on(map);
	Rast3d_unlock_all(map);
	G_message(_("Loading %s data with %i  bytes ...  (%dx%dx%d)"),
			(is_integer? "integer":"floating point "), bytes, region.cols,
			region.rows, region.depths);

	for (z = 0; z < region.depths; z++) {
		G_percent(z, region.depths, 1);

		if ((z % tileZ) == 0)
			Rast3d_unlock_all(map);

		for (y = 0; y < region.rows; y++) {/* go south to north */
			for (x = 0; x < region.cols; x++) {

				/* From west to east */
				col = x;
				/* The default is to read rows from north to south */
				row = y;
				/* From bottom to the top */
				depth = z;

				/* Read rows as from south to north */
				if (row_swap)
					row = region.rows - y - 1;

				/* Read XY layer from top to bottom */
				if (depth_swap)
					depth = region.depths - z - 1;

				/* Read value from binary file */
				read_cell(&value, is_integer, is_signed, bytes, byte_swap);

				/* Write value to the 3D raster map */
				if (map_type == DCELL_TYPE) {
					if (null && value == null_value)
						Rast3d_set_null_value(&value, 1, DCELL_TYPE);
					Rast3d_put_double(map, col, row, depth, value);
				} else {
					fvalue = (FCELL) value;
					if (null && value == null_value)
						Rast3d_set_null_value(&fvalue, 1, FCELL_TYPE);
					Rast3d_put_double(map, col, row, depth, fvalue);

				}
			}
		}
	}

	if (!Rast3d_flush_all_tiles(map))
		G_fatal_error(_("Error flushing tiles"));

	Rast3d_autolock_off(map);
	Rast3d_unlock_all(map);

	G_percent(1, 1, 1);
}
Beispiel #5
0
static void
modifyNull(char *name, d_Mask * maskRules, int changeNull, double newNullVal)
{
    void *map, *mapOut;
    RASTER3D_Region region;
    int tileX, tileY, tileZ, x, y, z;
    double value;
    int doCompress, doLzw, doRle, precision;
    int cacheSize;

    cacheSize = Rast3d_cache_size_encode(RASTER3D_USE_CACHE_XY, 1);

    if (NULL == G_find_raster3d(name, ""))
	Rast3d_fatal_error(_("3D raster map <%s> not found"), name);

    fprintf(stderr, "name %s Mapset %s \n", name, G_mapset());
    map = Rast3d_open_cell_old(name, G_mapset(), RASTER3D_DEFAULT_WINDOW,
			  DCELL_TYPE, cacheSize);

    if (map == NULL)
	Rast3d_fatal_error(_("Unable to open 3D raster map <%s>"), name);

    Rast3d_get_region_struct_map(map, &region);
    Rast3d_get_tile_dimensions_map(map, &tileX, &tileY, &tileZ);

    Rast3d_get_compression_mode(&doCompress, &doLzw, &doRle, &precision);

    mapOut = Rast3d_open_new_param(name, DCELL_TYPE, RASTER3D_USE_CACHE_XY,
			      &region, Rast3d_file_type_map(map),
			      doLzw, doRle, Rast3d_tile_precision_map(map), tileX,
			      tileY, tileZ);
    if (mapOut == NULL)
	Rast3d_fatal_error(_("modifyNull: error opening tmp file"));

    Rast3d_min_unlocked(map, RASTER3D_USE_CACHE_X);
    Rast3d_autolock_on(map);
    Rast3d_unlock_all(map);
    Rast3d_min_unlocked(mapOut, RASTER3D_USE_CACHE_X);
    Rast3d_autolock_on(mapOut);
    Rast3d_unlock_all(mapOut);

	for (z = 0; z < region.depths; z++) {
	if ((z % tileZ) == 0) {
	    Rast3d_unlock_all(map);
	    Rast3d_unlock_all(mapOut);
	}
	for (y = 0; y < region.rows; y++)
	    for (x = 0; x < region.cols; x++) {

		value = Rast3d_get_double_region(map, x, y, z);

		if (Rast3d_is_null_value_num(&value, DCELL_TYPE)) {
		    if (changeNull) {
			value = newNullVal;
		    }
		}
		else if (Rast3d_mask_d_select((DCELL *) & value, maskRules)) {
		    Rast3d_set_null_value(&value, 1, DCELL_TYPE);
		}

		Rast3d_put_double(mapOut, x, y, z, value);
	    }
	if ((z % tileZ) == 0) {
	    if (!Rast3d_flush_tiles_in_cube
		(mapOut, 0, 0, MAX(0, z - tileZ), region.rows - 1,
		 region.cols - 1, z))
		Rast3d_fatal_error(_("modifyNull: error flushing tiles in cube"));
	}
    }

    if (!Rast3d_flush_all_tiles(mapOut))
	Rast3d_fatal_error(_("modifyNull: error flushing all tiles"));

    Rast3d_autolock_off(map);
    Rast3d_unlock_all(map);
    Rast3d_autolock_off(mapOut);
    Rast3d_unlock_all(mapOut);

    if (!Rast3d_close(map))
	Rast3d_fatal_error(_("Unable to close raster map"));
    if (!Rast3d_close(mapOut))
	Rast3d_fatal_error(_("modifyNull: Unable to close tmp file"));
}