/*! * \brief Read the first line of a file in cell_misc/ * * Read the first line of data from a cell_misc/ meta-data file. * * \param element metadata component filename * \param name * \param mapset * \param *str string to be populated with data * \return dynamically-allocated string on success * \return NULL on error */ static char *misc_read_line(const char *elem, const char *name, const char *mapset) { char buff[GNAME_MAX]; FILE *fp; buff[0] = '\0'; if (G_find_file2_misc("cell_misc", elem, name, mapset) == NULL) return NULL; fp = G_fopen_old_misc("cell_misc", elem, name, mapset); if (!fp) { G_warning(_("Unable to read <%s> for raster map <%s@%s>"), elem, name, mapset); return NULL; } if (G_getl2(buff, sizeof(buff) - 1, fp) == 0) { /* file is empty */ *buff = '\0'; } if (fclose(fp) != 0) G_fatal_error(_("Error closing <%s> metadata file for raster map <%s@%s>"), elem, name, mapset); return *buff ? G_store(buff) : NULL; }
/*! * \brief Read the first line of a file in cell_misc/ * * Read the first line of data from a cell_misc/ meta-data file. * * \param element metadata component filename * \param name * \param mapset * \param *str string to be populated with data * \return 0 on success * \return -1, EOF (fclose() result) on error */ int G__raster_misc_read_line(const char *elem, const char *name, const char *mapset, char *str) { FILE *fd; char buff[GNAME_MAX]; buff[0] = '\0'; if (G_find_file2_misc("cell_misc", elem, name, mapset) == NULL) return -1; fd = G_fopen_old_misc("cell_misc", elem, name, mapset); if (!fd) { G_warning(_("Can't read %s for [%s in %s]"), elem, name, mapset); return -1; } if (G_getl2(buff, sizeof(buff) - 1, fd) == 0) { /* file is empty */ return fclose(fd); } strcpy(str, buff); return fclose(fd); }
static int G__open_misc(const char *dir, const char *element, const char *name, const char *mapset, int mode) { char path[GPATH_MAX]; char xname[GNAME_MAX], xmapset[GMAPSET_MAX]; G__check_gisinit(); /* READ */ if (mode == 0) { if (G_name_is_fully_qualified(name, xname, xmapset)) { if (*mapset && strcmp(xmapset, mapset) != 0) { G_warning(_("G__open_misc(read): mapset <%s> doesn't match xmapset <%s>"), mapset, xmapset); return -1; } name = xname; mapset = xmapset; } else if (!*mapset) mapset = G_find_file2_misc(dir, element, name, mapset); if (!mapset) return -1; G_file_name_misc(path, dir, element, name, mapset); return open(path, 0); } /* WRITE */ if (mode == 1 || mode == 2) { mapset = G_mapset(); if (G_name_is_fully_qualified(name, xname, xmapset)) { if (strcmp(xmapset, mapset) != 0) { G_warning(_("G__open_misc(write): xmapset <%s> != G_mapset() <%s>"), xmapset, mapset); return -1; } name = xname; } if (G_legal_filename(name) == -1) return -1; G_file_name_misc(path, dir, element, name, mapset); if (mode == 1 || access(path, 0) != 0) { G__make_mapset_element_misc(dir, name); close(creat(path, 0666)); } return open(path, mode); } return -1; }
/*! * \brief Read floating-point range * * Read the floating point range file <i>drange</i>. This file is * written in binary using XDR format. * * An empty range file indicates that the min, max are undefined. This * is a valid case, and the result should be an initialized range * struct with no defined min/max. If the range file is missing and * the map is a floating-point map, this function will create a * default range by calling G_construct_default_range(). * * \param name map name * \param mapset mapset name * \param drange pointer to FPRange structure which holds fp range * * \return 1 on success * \return 2 range is empty * \return -1 on error */ int Rast_read_fp_range(const char *name, const char *mapset, struct FPRange *drange) { struct Range range; int fd; char xdr_buf[2][XDR_DOUBLE_NBYTES]; DCELL dcell1, dcell2; Rast_init(); Rast_init_fp_range(drange); if (Rast_map_type(name, mapset) == CELL_TYPE) { /* if map is integer read integer range and convert it to double */ if (Rast_read_range(name, mapset, &range) >= 0) { /* if the integer range is empty */ if (range.first_time) return 2; Rast_update_fp_range((DCELL) range.min, drange); Rast_update_fp_range((DCELL) range.max, drange); return 1; } return -1; } fd = -1; if (G_find_file2_misc("cell_misc", "f_range", name, mapset)) { fd = G_open_old_misc("cell_misc", "f_range", name, mapset); if (fd < 0) { G_warning(_("Unable to read fp range file for <%s>"), G_fully_qualified_name(name, mapset)); return -1; } if (read(fd, xdr_buf, sizeof(xdr_buf)) != sizeof(xdr_buf)) { /* if the f_range file exists, but empty file, meaning Nulls */ close(fd); G_debug(1, "Empty fp range file meaning Nulls for <%s>", G_fully_qualified_name(name, mapset)); return 2; } G_xdr_get_double(&dcell1, xdr_buf[0]); G_xdr_get_double(&dcell2, xdr_buf[1]); Rast_update_fp_range(dcell1, drange); Rast_update_fp_range(dcell2, drange); close(fd); } return 1; }
/*! * \brief Read raster range (CELL) * * This routine reads the range information for the raster map * <i>name</i> in <i>mapset</i> into the <i>range</i> structure. * * A diagnostic message is printed and -1 is returned if there is an error * reading the range file. Otherwise, 0 is returned. * * Old range file (those with 4 numbers) should treat zeros in this * file as NULL-values. New range files (those with just 2 numbers) * should treat these numbers as real data (zeros are real data in * this case). An empty range file indicates that the min, max are * undefined. This is a valid case, and the result should be an * initialized range struct with no defined min/max. If the range file * is missing and the map is a floating-point map, this function will * create a default range by calling G_construct_default_range(). * * \param name map name * \param mapset mapset name * \param[out] range pointer to Range structure which holds range info * * \return -1 on error * \return 1 on success * \return 2 if range is empty * \return 3 if raster map is floating-point, get range from quant rules */ int Rast_read_range(const char *name, const char *mapset, struct Range *range) { FILE *fd; CELL x[4]; char buf[200]; int n, count; struct Quant quant; struct FPRange drange; Rast_init_range(range); fd = NULL; /* if map is not integer, read quant rules, and get limits */ if (Rast_map_type(name, mapset) != CELL_TYPE) { DCELL dmin, dmax; if (Rast_read_quant(name, mapset, &quant) < 0) { G_warning(_("Unable to read quant rules for raster map <%s>"), G_fully_qualified_name(name, mapset)); return -1; } if (Rast_quant_is_truncate(&quant) || Rast_quant_is_round(&quant)) { if (Rast_read_fp_range(name, mapset, &drange) >= 0) { Rast_get_fp_range_min_max(&drange, &dmin, &dmax); if (Rast_quant_is_truncate(&quant)) { x[0] = (CELL) dmin; x[1] = (CELL) dmax; } else { /* round */ if (dmin > 0) x[0] = (CELL) (dmin + .5); else x[0] = (CELL) (dmin - .5); if (dmax > 0) x[1] = (CELL) (dmax + .5); else x[1] = (CELL) (dmax - .5); } } else return -1; } else Rast_quant_get_limits(&quant, &dmin, &dmax, &x[0], &x[1]); Rast_update_range(x[0], range); Rast_update_range(x[1], range); return 3; } if (G_find_file2_misc("cell_misc", "range", name, mapset)) { fd = G_fopen_old_misc("cell_misc", "range", name, mapset); if (!fd) { G_warning(_("Unable to read range file for <%s>"), G_fully_qualified_name(name, mapset)); return -1; } /* if range file exists but empty */ if (!fgets(buf, sizeof buf, fd)) { if (fd) fclose(fd); return 2; } x[0] = x[1] = x[2] = x[3] = 0; count = sscanf(buf, "%d%d%d%d", &x[0], &x[1], &x[2], &x[3]); /* if wrong format */ if (count <= 0) { if (fd) fclose(fd); G_warning(_("Unable to read range file for <%s>"), G_fully_qualified_name(name, mapset)); return -1; } for (n = 0; n < count; n++) { /* if count==4, the range file is old (4.1) and 0's in it have to be ignored */ if (count < 4 || x[n]) Rast_update_range((CELL) x[n], range); } fclose(fd); } return 1; }
/*! \brief Search for a 3D raster map in current search path or in a specified mapset. Note: rejects all names that begin with '.' \param name map name \param mapset mapset to search. ("" for search path) \return pointer to a string with name of mapset where the map was found \return NULL if not found */ const char *G_find_raster3d(const char *name, const char *mapset) { return G_find_file2_misc("grid3", "cell", name, mapset); }