FILE *I_fopen_group_file_new(const char *group, const char *file) { FILE *fd; fd = G_fopen_new_misc("group", file, group); if (!fd) G_warning(_("Unable to create file [%s] of group [%s in %s]"), file, group, G_mapset()); return fd; }
/*! * \brief Write a line to a raster map metadata file * * Write (including overwrite) a string into a raster map's metadata file * found in in cell_misc/ in the current mapset. * * \param element metadata component filename * \param name * \param *str string containing data to be written */ static void misc_write_line(const char *elem, const char *name, const char *str) { FILE *fp; fp = G_fopen_new_misc("cell_misc", elem, name); if (!fp) G_fatal_error(_("Unable to create <%s> metadata file for raster map <%s@%s>"), elem, name, G_mapset()); fprintf(fp, "%s\n", str); if (fclose(fp) != 0) G_fatal_error(_("Error closing <%s> metadata file for raster map <%s@%s>"), elem, name, G_mapset()); }
/*! * \brief Write a line to a raster map metadata file * * Write (including overwrite) a string into a raster map's metadata file * found in in cell_misc/ in the current mapset. * * \param element metadata component filename * \param name * \param *str string containing data to be written * \return 0 on success * \return -1, EOF (fclose() result) on error */ int G__raster_misc_write_line(const char *elem, const char *name, const char *str) { FILE *fd; fd = G_fopen_new_misc("cell_misc", elem, name); if (fd == NULL) { G_warning(_("Can't create %s metadata file for [%s in %s]"), elem, name, G_mapset()); return -1; } fprintf(fd, "%s", str); return fclose(fd); }
int Rast3d_write_cats(const char *name, struct Categories *cats) /* adapted from Rast_write_cats */ { FILE *fd; int i; const char *descr; DCELL val1, val2; char str1[100], str2[100]; fd = G_fopen_new_misc(RASTER3D_DIRECTORY, RASTER3D_CATS_ELEMENT, name); if (!fd) return -1; /* write # cats - note # indicate 3.0 or later */ fprintf(fd, "# %ld categories\n", (long)cats->num); /* title */ fprintf(fd, "%s\n", cats->title != NULL ? cats->title : ""); /* write format and coefficients */ fprintf(fd, "%s\n", cats->fmt != NULL ? cats->fmt : ""); fprintf(fd, "%.2f %.2f %.2f %.2f\n", cats->m1, cats->a1, cats->m2, cats->a2); /* write the cat numbers:label */ for (i = 0; i < Rast_quant_nof_rules(&cats->q); i++) { descr = Rast_get_ith_d_cat(cats, i, &val1, &val2); if ((cats->fmt && cats->fmt[0]) || (descr && descr[0])) { if (val1 == val2) { sprintf(str1, "%.10f", val1); G_trim_decimal(str1); fprintf(fd, "%s:%s\n", str1, descr != NULL ? descr : ""); } else { sprintf(str1, "%.10f", val1); G_trim_decimal(str1); sprintf(str2, "%.10f", val2); G_trim_decimal(str2); fprintf(fd, "%s:%s:%s\n", str1, str2, descr != NULL ? descr : ""); } } } fclose(fd); return 1; }
FILE *I_fopen_subgroup_file_new(const char *group, const char *subgroup, const char *file) { FILE *fd; char element[GNAME_MAX * 2]; /* create subgroup directory */ sprintf(element, "%s/subgroup/%s", group, subgroup); G__make_mapset_element_misc("group", element); /* get subgroup element name */ sprintf(element, "subgroup/%s/%s", subgroup, file); fd = G_fopen_new_misc("group", element, group); if (!fd) G_warning(_("Unable to create file [%s] for subgroup [%s] of group [%s in %s]"), file, subgroup, group, G_mapset()); return fd; }
int Rast3d_write_colors(const char *name, const char *mapset, struct Colors *colors) /* adapted from Rast_write_colors */ { FILE *fd; if (strcmp(mapset, G_mapset()) != 0) { G_warning(_("mapset <%s> is not the current mapset"), mapset); return -1; } fd = G_fopen_new_misc(RASTER3D_DIRECTORY, RASTER3D_COLOR_ELEMENT, name); if (!fd) return -1; Rast__write_colors(fd, colors); fclose(fd); return 1; }
/*! * \brief Write raster range file * * This routine writes the range information for the raster map * <i>name</i> in the current mapset from the <i>range</i> structure. * A diagnostic message is printed and -1 is returned if there is an * error writing the range file. Otherwise, 0 is returned. * * This routine only writes 2 numbers (min,max) to the range * file, instead of the 4 (pmin,pmax,nmin,nmax) previously written. * If there is no defined min,max, an empty file is written. * * \param name map name * \param range pointer to Range structure which holds range info */ void Rast_write_range(const char *name, const struct Range *range) { FILE *fp; if (Rast_map_type(name, G_mapset()) != CELL_TYPE) { G_remove_misc("cell_misc", "range", name); /* remove the old file with this name */ G_fatal_error(_("Unable to write range file for <%s>"), name); } fp = G_fopen_new_misc("cell_misc", "range", name); if (!fp) { G_remove_misc("cell_misc", "range", name); /* remove the old file with this name */ G_fatal_error(_("Unable to write range file for <%s>"), name); } /* if range has been updated */ if (!range->first_time) fprintf(fp, "%ld %ld\n", (long)range->min, (long)range->max); fclose(fp); }
/*! \brief Create GDAL settings for given raster map \param name map name \param map_type map type (CELL, FCELL, DCELL) \return pointer to allocated GDAL_link structure \return NULL on error */ struct GDAL_link *Rast_create_gdal_link(const char *name, RASTER_MAP_TYPE map_type) { #ifdef GDAL_LINK char path[GPATH_MAX]; GDALDriverH driver; double transform[6]; struct GDAL_link *gdal; FILE *fp; struct Key_Value *key_val; char buf[32]; Rast__init_window(); Rast_init_gdal(); if (!G_is_initialized(&st->initialized)) { read_gdal_options(); st->projinfo = G_get_projinfo(); st->projunits = G_get_projunits(); #if 0 /* We cannot use GPJ_grass_to_wkt() here because that would create a circular dependency between libgis and libgproj */ if (st->projinfo && st->projunits) st->srswkt = GPJ_grass_to_wkt(st->projinfo, st->projunits); #endif G_initialize_done(&st->initialized); } gdal = G_calloc(1, sizeof(struct GDAL_link)); sprintf(path, "%s/%s%s", st->opts.dir, name, st->opts.ext); gdal->filename = G_store(path); gdal->band_num = 1; gdal->hflip = 0; gdal->vflip = 0; switch (map_type) { case CELL_TYPE: switch (R__.nbytes) { case 1: gdal->type = GDT_Byte; gdal->null_val = (DCELL) 0xFF; break; case 2: gdal->type = GDT_UInt16; gdal->null_val = (DCELL) 0xFFFF; break; case 3: case 4: gdal->type = GDT_Int32; gdal->null_val = (DCELL) 0x80000000U; break; } break; case FCELL_TYPE: gdal->type = GDT_Float32; Rast_set_d_null_value(&gdal->null_val, 1); break; case DCELL_TYPE: gdal->type = GDT_Float64; Rast_set_d_null_value(&gdal->null_val, 1); break; default: G_fatal_error(_("Invalid map type <%d>"), map_type); break; } driver = (*pGDALGetDriverByName) (st->opts.format); if (!driver) G_fatal_error(_("Unable to get <%s> driver"), st->opts.format); /* Does driver support GDALCreate ? */ if ((*pGDALGetMetadataItem) (driver, GDAL_DCAP_CREATE, NULL)) { gdal->data = (*pGDALCreate)(driver, gdal->filename, R__.wr_window.cols, R__.wr_window.rows, 1, gdal->type, st->opts.options); if (!gdal->data) G_fatal_error(_("Unable to create <%s> dataset using <%s> driver"), name, st->opts.format); } /* If not - create MEM driver for intermediate dataset. * Check if raster can be created at all (with GDALCreateCopy) */ else if ((*pGDALGetMetadataItem) (driver, GDAL_DCAP_CREATECOPY, NULL)) { GDALDriverH mem_driver; G_message(_("Driver <%s> does not support direct writing. " "Using MEM driver for intermediate dataset."), st->opts.format); mem_driver = (*pGDALGetDriverByName) ("MEM"); if (!mem_driver) G_fatal_error(_("Unable to get in-memory raster driver")); gdal->data = (*pGDALCreate)(mem_driver, "", R__.wr_window.cols, R__.wr_window.rows, 1, gdal->type, st->opts.options); if (!gdal->data) G_fatal_error(_("Unable to create <%s> dataset using memory driver"), name); } else G_fatal_error(_("Driver <%s> does not support creating rasters"), st->opts.format); gdal->band = (*pGDALGetRasterBand) (gdal->data, gdal->band_num); (*pGDALSetRasterNoDataValue) (gdal->band, gdal->null_val); /* Set Geo Transform */ transform[0] = R__.wr_window.west; transform[1] = R__.wr_window.ew_res; transform[2] = 0.0; transform[3] = R__.wr_window.north; transform[4] = 0.0; transform[5] = -R__.wr_window.ns_res; if ((*pGDALSetGeoTransform) (gdal->data, transform) >= CE_Failure) G_warning(_("Unable to set geo transform")); if (st->srswkt) if ((*pGDALSetProjection) (gdal->data, st->srswkt) == CE_Failure) G_warning(_("Unable to set projection")); fp = G_fopen_new_misc("cell_misc", "gdal", name); if (!fp) G_fatal_error(_("Unable to create cell_misc/%s/gdal file"), name); key_val = G_create_key_value(); G_set_key_value("file", gdal->filename, key_val); sprintf(buf, "%d", gdal->band_num); G_set_key_value("band", buf, key_val); sprintf(buf, "%.22g", gdal->null_val); G_set_key_value("null", buf, key_val); sprintf(buf, "%d", gdal->type); G_set_key_value("type", buf, key_val); if (G_fwrite_key_value(fp, key_val) < 0) G_fatal_error(_("Error writing cell_misc/%s/gdal file"), name); G_free_key_value(key_val); fclose(fp); return gdal; #else return NULL; #endif }