Esempio n. 1
0
static int read_colors(const char *element, const char *name,
		       const char *mapset, struct Colors *colors)
{
    FILE *fd;
    int stat;
    char buf[1024];

    if (!(fd = G_fopen_old(element, name, mapset)))
	return -2;

    /*
     * first line in 4.0 color files is %
     * otherwise it is pre 4.0
     */
    if (fgets(buf, sizeof buf, fd) == NULL) {
	fclose(fd);
	return -1;
    }
    fseek(fd, 0L, 0);

    G_strip(buf);
    if (*buf == '%') {		/* 4.0 format */
	stat = read_new_colors(fd, colors);
	colors->version = 0;	/* 4.0 format */
    }
    else {
	stat = read_old_colors(fd, colors);
	colors->version = -1;	/* pre 4.0 format */
    }
    fclose(fd);
    return stat;
}
Esempio n. 2
0
/**
   \brief Open existing vector map

   Map->name and Map->mapset must be set before.

   \param Map poiter to vector map
   \param update non-zero for write mode, otherwise read-only

   \return 0 success
   \return -1 error
*/
int V1_open_old_nat(struct Map_info *Map, int update)
{
    char buf[1000];

    G_debug(1, "V1_open_old_nat(): name = %s mapset = %s", Map->name,
	    Map->mapset);

    sprintf(buf, "%s/%s", GRASS_VECT_DIRECTORY, Map->name);
    dig_file_init(&(Map->dig_fp));
    if (update)
	Map->dig_fp.file = G_fopen_modify(buf, GRASS_VECT_COOR_ELEMENT);
    else
	Map->dig_fp.file =
	    G_fopen_old(buf, GRASS_VECT_COOR_ELEMENT, Map->mapset);

    if (Map->dig_fp.file == NULL)
	return -1;

    if (!(dig__read_head(Map)))
	return (-1);
    check_coor(Map);

    /* set conversion matrices */
    dig_init_portable(&(Map->head.port), Map->head.port.byte_order);

    /* load to memory */
    if (!update)
	dig_file_load(&(Map->dig_fp));

    return (0);
}
Esempio n. 3
0
FILE *I_fopen_cam_file_old(char *camera)
{
    FILE *fd;

    fd = G_fopen_old("camera", camera, G_mapset());
    if (!fd)
	error(camera, "can't open ", "");
    return fd;
}
Esempio n. 4
0
/*!
   \brief Read the raster header

   The raster header for the raster map <i>name</i> in the specified
   <i>mapset</i> is read into the <i>cellhd</i> structure. If there is
   an error reading the raster header file, a diagnostic message is
   printed and -1 is returned. Otherwise, 0 is returned.

   <b>Note</b>:a warning message for errors encountered.

   Cell header files may contain either grid cell header information or
   reclass information. If it is a reclass file, it will specify the
   map and mapset names of the actual grid cell file being
   reclassed. Rast_get_cellhd(), upon reading reclass information will go
   read the cell header information for the referenced file. Only one
   reference is allowed.

   \param name name of map
   \param mapset mapset that map belongs to
   \param[out] cellhd structure to hold cell header info

   \return void
 */
void Rast_get_cellhd(const char *name, const char *mapset,
		     struct Cell_head *cellhd)
{
    FILE *fp;
    int is_reclass;
    char real_name[GNAME_MAX], real_mapset[GMAPSET_MAX];

    /*
       is_reclass = Rast_is_reclass (name, mapset, real_name, real_mapset);
       if (is_reclass < 0)
       {
       sprintf (buf,"Can't read header file for [%s in %s]\n", name, mapset);
       tail = buf + strlen(buf);
       strcpy (tail, "It is a reclass file, but with an invalid format");
       G_warning(buf);
       return -1;
       }
     */
    is_reclass = (Rast_is_reclass(name, mapset, real_name, real_mapset) > 0);
    if (is_reclass) {
	fp = G_fopen_old("cellhd", real_name, real_mapset);
	if (!fp)
	    G_fatal_error(_("Unable to read header file for raster map <%s@%s>. "
			    "It is a reclass of raster map <%s@%s> %s"),
			  name, mapset, real_name, real_mapset,
			  !G_find_raster(real_name, real_mapset)
			  ? _("which is missing.")
			  : _("whose header file can't be opened."));
    }
    else {
	fp = G_fopen_old("cellhd", name, mapset);
	if (!fp)
	    G_fatal_error(_("Unable to open header file for raster map <%s@%s>"),
			  name, mapset);
    }

    G__read_Cell_head(fp, cellhd, 1);
    fclose(fp);
}
Esempio n. 5
0
File: cindex.c Progetto: caomw/grass
/*!
  \brief Read category index from cidx file if exists
  
  \param Map pointer to Map_info structure
  \param head_only read only header of the file
  
  \return 0 on success 
  \return 1 if file does not exist
  \return -1 error, file exists but cannot be read
 */
int Vect_cidx_open(struct Map_info *Map, int head_only)
{
    int ret;
    char file_path[GPATH_MAX], *path;
    struct gvfile fp;
    struct Plus_head *Plus;

    G_debug(2, "Vect_cidx_open(): name = %s mapset= %s", Map->name,
	    Map->mapset);

    Plus = &(Map->plus);

    path = Vect__get_path(Map);
    G_file_name(file_path, path, GV_CIDX_ELEMENT, Map->mapset);

    if (access(file_path, F_OK) != 0) {	/* does not exist */
        G_free(path);
	return 1;
    }

    dig_file_init(&fp);
    fp.file = G_fopen_old(path, GV_CIDX_ELEMENT, Map->mapset);
    G_free(path);
    
    if (fp.file == NULL) {	/* category index file is not available */
	G_warning(_("Unable to open category index file for vector map <%s>"),
		  Vect_get_full_name(Map));
	return -1;
    }

    /* load category index to memory */
    ret = dig_read_cidx(&fp, Plus, head_only);

    fclose(fp.file);

    if (ret == 1) {
	G_debug(3, "Cannot read cidx");
	return -1;
    }

    return 0;
}
Esempio n. 6
0
static void read_gdal_options(void)
{
    FILE *fp;
    struct Key_Value *key_val;
    const char *p;

    fp = G_fopen_old("", "GDAL", G_mapset());
    if (!fp)
	G_fatal_error(_("Unable to open GDAL file"));
    key_val = G_fread_key_value(fp);
    fclose(fp);

    p = G_find_key_value("directory", key_val);
    if (!p)
	p = "gdal";
    if (*p == '/') {
	st->opts.dir = G_store(p);
    }
    else {
	char path[GPATH_MAX];

	G_file_name(path, p, "", G_mapset());
	st->opts.dir = G_store(path);
	if (access(path, 0) != 0)
	    G_make_mapset_element(p);
    }

    p = G_find_key_value("extension", key_val);
    st->opts.ext = G_store(p ? p : "");

    p = G_find_key_value("format", key_val);
    st->opts.format = G_store(p ? p : "GTiff");

    p = G_find_key_value("options", key_val);
    st->opts.options = p ? G_tokenize(p, ",") : NULL;

    G_free_key_value(key_val);
}
Esempio n. 7
0
char *G_get_dig_title(const char *name, const char *mapset)
{
    FILE *fd;
    int stat = -1;
    char title[100];

    fd = G_fopen_old("dig_cats", name, mapset);
    if (fd) {
	stat = 1;
	if (!fgets(title, sizeof title, fd))	/* skip number of cats */
	    stat = -1;
	else if (!G_getl(title, sizeof title, fd))	/* read title */
	    stat = -1;

	fclose(fd);
    }

    if (stat < 0)
	*title = 0;
    else
	G_strip(title);

    return G_store(title);
}
Esempio n. 8
0
static CELL read_cats(const char *element,
		      const char *name,
		      const char *mapset, struct Categories *pcats, int full)
{
    FILE *fd;
    char buff[1024];
    CELL cat;
    DCELL val1, val2;
    int old = 0, fp_map;
    long num = -1;


    if (strncmp(element, "dig", 3) == 0)
	fp_map = 0;
    else
	fp_map = Rast_map_is_fp(name, mapset);

    if (!(fd = G_fopen_old(element, name, mapset)))
	return -2;

    /* Read the number of categories */
    if (G_getl(buff, sizeof buff, fd) == 0)
	goto error;

    if (sscanf(buff, "# %ld", &num) == 1)
	old = 0;
    else if (sscanf(buff, "%ld", &num) == 1)
	old = 1;

    if (!full) {
	fclose(fd);
	if (num < 0)
	    return 0;		/* coorect */
	return (CELL) num;
    }

    /* Read the title for the file */
    if (G_getl(buff, sizeof buff, fd) == 0)
	goto error;
    G_strip(buff);
    /*    G_ascii_check(buff) ; */

    Rast_init_cats(buff, pcats);
    if (num >= 0)
	pcats->num = num;

    if (!old) {
	char fmt[256];
	float m1, a1, m2, a2;

	if (G_getl(fmt, sizeof fmt, fd) == 0)
	    goto error;
	/* next line contains equation coefficients */
	if (G_getl(buff, sizeof buff, fd) == 0)
	    goto error;
	if (sscanf(buff, "%f %f %f %f", &m1, &a1, &m2, &a2) != 4)
	    goto error;
	Rast_set_cats_fmt(fmt, m1, a1, m2, a2, pcats);
    }

    /* Read all category names */
    for (cat = 0;; cat++) {
	char label[1024];

	if (G_getl(buff, sizeof buff, fd) == 0)
	    break;
	if (old)
	    Rast_set_c_cat(&cat, &cat, buff, pcats);
	else {
	    *label = 0;
	    if (sscanf(buff, "%1s", label) != 1)
		continue;
	    if (*label == '#')
		continue;
	    *label = 0;
	    /* for fp maps try to read a range of data */
	    if (fp_map
		&& sscanf(buff, "%lf:%lf:%[^\n]", &val1, &val2, label) == 3)
		Rast_set_cat(&val1, &val2, label, pcats, DCELL_TYPE);
	    else if (sscanf(buff, "%d:%[^\n]", &cat, label) >= 1)
		Rast_set_cat(&cat, &cat, label, pcats, CELL_TYPE);
	    else if (sscanf(buff, "%lf:%[^\n]", &val1, label) >= 1)
		Rast_set_cat(&val1, &val1, label, pcats, DCELL_TYPE);
	    else
		goto error;
	}
    }

    fclose(fd);
    return 0;
  error:
    fclose(fd);
    return -1;
}
Esempio n. 9
0
int G_put_cell_title (char *name, char *title)
{
    char *mapset;
    FILE *in, *out;
    char *tempfile;
    int line ;
    char buf[300];

    mapset = G_mapset() ;
    in = out = 0 ;
    in = G_fopen_old ("cats", name, mapset);
    if (!in)
    {
	sprintf (buf, "category information for [%s] in [%s] missing or invalid", name, mapset);
	G_warning (buf);
	return -1;
    }

    tempfile = G_tempfile();
    out = fopen (tempfile, "w");
    if (!out)
    {
	fclose (in);
        sprintf (buf, "G_put_title - can't create a temp file");
        G_warning (buf);
	return -1;
    }

    for (line = 0; G_getl (buf, sizeof buf, in); line++)
    {
	if (line == 1)
	{
	    strcpy (buf, title);
	    G_strip (buf);
	}
	fprintf (out, "%s\n", buf);
    }
    fclose (in);
    fclose (out);

/* must be #cats line, title line, and label for cat 0 */
    if (line < 3)
    {
	sprintf (buf, "category information for [%s] in [%s] invalid", name, mapset);
	G_warning (buf);
	return -1;
    }

    in = fopen (tempfile, "r");
    if (!in)
    {
	sprintf (buf, "G_put_title - can't reopen temp file");
	G_warning (buf);
	return -1;
    }

    out = G_fopen_new ("cats", name);
    if (!out)
    {
	fclose (in);
        sprintf (buf, "can't write category information for [%s] in [%s]", name, mapset);
        G_warning (buf);
	return -1;
    }

    while (fgets(buf, sizeof buf, in))
	fprintf (out, "%s", buf);

    fclose (in);
    fclose (out);

    return 1;
}
Esempio n. 10
0
/* 
 *  Read symbol specified by name.
 *  Name: group/name | group/name@mapset 
 *        (later add syntax to prefer symbol from GISBASE)
 *  S_read() searches first in mapsets (standard GRASS search) and
 *   then in GISBASE/etc/symbol/ 
 */
SYMBOL *S_read(const char *sname)
{
    int i, j, k, l;
    FILE *fp;
    char group[500], name[500], buf[2001];
    const char *ms;
    char *c;
    double x, y, x2, y2, rad, ang1, ang2;
    int r, g, b;
    double fr, fg, fb;
    int ret;
    char clock;
    SYMBOL *symb;
    int current;		/* current part_type */
    SYMBPART *part;		/* current part */
    SYMBCHAIN *chain;		/* current chain */
    SYMBEL *elem;		/* current element */

    G_debug(3, "S_read(): sname = %s", sname);

    /* Find file */
    /* Get group and name */
    strcpy(group, sname);
    c = strchr(group, '/');
    if (c == NULL) {
	G_warning(_("Incorrect symbol name: '%s' (should be: group/name or group/name@mapset)"),
		  sname);
	return NULL;
    }
    c[0] = '\0';

    c++;
    strcpy(name, c);

    G_debug(3, "  group: '%s' name: '%s'", group, name);

    /* Search in mapsets */
    sprintf(buf, "symbol/%s", group);
    ms = G_find_file(buf, name, NULL);

    if (ms != NULL) {		/* Found in mapsets */
	fp = G_fopen_old(buf, name, ms);
    }
    else {			/* Search in GISBASE */
	sprintf(buf, "%s/etc/symbol/%s", G_gisbase(), sname);
	fp = fopen(buf, "r");
    }

    if (fp == NULL) {
	G_warning(_("Cannot find/open symbol: '%s'"), sname);
	return NULL;
    }

    /* create new symbol */
    symb = new_symbol();

    current = OBJ_NONE;		/* no part */

    /* read file */
    while (G_getl2(buf, 2000, fp) != 0) {
	G_chop(buf);
	G_debug(3, "  BUF: [%s]", buf);

	/* skip empty and comment lines */
	if ((buf[0] == '#') || (buf[0] == '\0'))
	    continue;

	get_key_data(buf);

	if (strcmp(key, "VERSION") == 0) {
	    if (strcmp(data, "1.0") != 0) {
		sprintf(buf, "Wrong symbol version: '%s'", data);
		return (err(fp, symb, buf));
	    }
	}
	else if (strcmp(key, "BOX") == 0) {
	    if (sscanf(data, "%lf %lf %lf %lf", &x, &y, &x2, &y2) != 4) {
		sprintf(buf, "Incorrect box definition: '%s'", data);
		return (err(fp, symb, buf));
	    }
	    symb->xscale = 1 / (x2 - x);
	    symb->yscale = 1 / (y2 - y);
	    if (x2 - x > y2 - y) {
		symb->scale = symb->xscale;
	    }
	    else {
		symb->scale = symb->yscale;
	    }
	}
	else if (strcmp(key, "STRING") == 0) {
	    G_debug(4, "  STRING >");
	    current = OBJ_STRING;
	    part = new_part(S_STRING);
	    add_part(symb, part);

	    chain = new_chain();
	    add_chain(part, chain);
	}
	else if (strcmp(key, "POLYGON") == 0) {
	    G_debug(4, "  POLYGON >");
	    current = OBJ_POLYGON;
	    part = new_part(S_POLYGON);
	    add_part(symb, part);

	}
	else if (strcmp(key, "RING") == 0) {
	    G_debug(4, "  RING >");
	    current = OBJ_RING;
	    chain = new_chain();
	    add_chain(part, chain);

	}
	else if (strcmp(key, "LINE") == 0) {
	    G_debug(4, "    LINE >");
	    elem = new_line();
	    add_element(chain, elem);
	    read_coor(fp, elem);

	}
	else if (strcmp(key, "ARC") == 0) {
	    G_debug(4, "    ARC");
	    ret =
		sscanf(data, "%lf %lf %lf %lf %lf %c", &x, &y, &rad, &ang1,
		       &ang2, &clock);
	    if (ret < 5) {
		sprintf(buf, "Incorrect arc definition: '%s'", buf);
		return (err(fp, symb, buf));
	    }
	    if (ret == 6 && (clock == 'c' || clock == 'C'))
		i = 1;
	    else
		i = 0;
	    elem = new_arc(x, y, rad, ang1, ang2, i);
	    add_element(chain, elem);

	}
	else if (strcmp(key, "END") == 0) {
	    switch (current) {
	    case OBJ_STRING:
		G_debug(4, "  STRING END");
		current = OBJ_NONE;
		break;
	    case OBJ_POLYGON:
		G_debug(4, "  POLYGON END");
		current = OBJ_NONE;
		break;
	    case OBJ_RING:
		G_debug(4, "  RING END");
		current = OBJ_POLYGON;
		break;
	    }
	}
	else if (strcmp(key, "COLOR") == 0) {
	    if (G_strcasecmp(data, "NONE") == 0) {
		part->color.color = S_COL_NONE;
	    }
	    else if (sscanf(data, "%d %d %d", &r, &g, &b) == 3) {
		if (r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255)
		    G_warning(_("Incorrect symbol color: '%s', using default."),
			      buf);
		else {
		    fr = r / 255.0;
		    fg = g / 255.0;
		    fb = b / 255.0;
		    part->color.color = S_COL_DEFINED;
		    part->color.r = r;
		    part->color.g = g;
		    part->color.b = b;
		    part->color.fr = fr;
		    part->color.fg = fg;
		    part->color.fb = fb;
		    G_debug(4, "  color [%d %d %d] = [%.3f %.3f %.3f]", r, g,
			    b, fr, fg, fb);
		}
	    }
	    else {
		G_warning(_("Incorrect symbol color: '%s', using default."),
			  buf);
	    }
	}
	else if (strcmp(key, "FCOLOR") == 0) {
	    if (G_strcasecmp(data, "NONE") == 0) {
		part->fcolor.color = S_COL_NONE;
	    }
	    else if (sscanf(data, "%d %d %d", &r, &g, &b) == 3) {
		if (r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255)
		    G_warning(_("Incorrect symbol color: '%s', using default."),
			      buf);
		else {
		    fr = r / 255.0;
		    fg = g / 255.0;
		    fb = b / 255.0;
		    part->fcolor.color = S_COL_DEFINED;
		    part->fcolor.r = r;
		    part->fcolor.g = g;
		    part->fcolor.b = b;
		    part->fcolor.fr = fr;
		    part->fcolor.fg = fg;
		    part->fcolor.fb = fb;
		    G_debug(4, "  color [%d %d %d] = [%.3f %.3f %.3f]", r, g,
			    b, fr, fg, fb);
		}
	    }
	    else {
		G_warning(_("Incorrect symbol color: '%s', using default."),
			  buf);
	    }
	}
	else {
	    sprintf(buf, "Unknown keyword in symbol: '%s'", buf);
	    return (err(fp, symb, buf));
	    break;
	}
    }

    /* Debug output */

    G_debug(3, "Number of parts: %d", symb->count);
    for (i = 0; i < symb->count; i++) {
	part = symb->part[i];
	G_debug(4, "  Part %d: type: %d number of chains: %d", i, part->type,
		part->count);
	G_debug(4, "           color: %d: fcolor: %d", part->color.color,
		part->fcolor.color);
	for (j = 0; j < part->count; j++) {
	    chain = part->chain[j];
	    G_debug(4, "    Chain %d: number of elements: %d", j,
		    chain->count);
	    for (k = 0; k < chain->count; k++) {
		elem = chain->elem[k];
		G_debug(4, "      Element %d: type: %d", k, elem->type);
		if (elem->type == S_LINE) {
		    G_debug(4, "        Number of points %d",
			    elem->coor.line.count);
		    for (l = 0; l < elem->coor.line.count; l++) {
			G_debug(4, "        x, y: %f %f",
				elem->coor.line.x[l], elem->coor.line.y[l]);
		    }
		}
		else {
		    G_debug(4, "        arc r = %f", elem->coor.arc.r);
		}
	    }
	}
    }

    fclose(fp);

    return symb;
}
Esempio n. 11
0
int G_get_3dview(const char *fname, const char *mapset, struct G_3dview *View)
{
    struct Cell_head curwin;
    FILE *fp;
    char buffer[80], keystring[24], boo[8], nbuf[128], ebuf[128];
    int lap, v_maj, v_min, wind_keys = 0, reqkeys = 0;
    int current = 0;		/* current version flag */

    mapset = G_find_file2("3d.view", fname, mapset);
    if (mapset != NULL) {
	if (NULL == (fp = G_fopen_old("3d.view", fname, mapset))) {
	    G_warning(_("Unable to open %s for reading"), fname);
	    return (-1);
	}

	G_get_set_window(&curwin);
	G_get_3dview_defaults(View, &curwin);

	if (NULL != fgets(buffer, 80, fp)) {
	    if (buffer[0] != '#') {	/* old d.3d format */
		rewind(fp);
		if (0 <= read_old_format(View, fp))
		    return (0);
		else
		    return (-1);
	    }
	    else {
		sscanf(buffer, "#%d.%d\n", &v_maj, &v_min);
		if (v_maj == vers_major && v_min == vers_minor)
		    current = 1;	/* same version */
	    }
	}

	while (NULL != fgets(buffer, 75, fp)) {
	    if (buffer[0] != '#') {

		sscanf(buffer, "%[^:]:", keystring);

		if (!strcmp(keystring, "PGM_ID")) {
		    sscanf(buffer, "%*s%s", (View->pgm_id));
		    continue;
		}
		if (!strcmp(keystring, "north")) {
		    sscanf(buffer, "%*s%lf", &(View->vwin.north));
		    ++wind_keys;
		    continue;
		}
		if (!strcmp(keystring, "south")) {
		    sscanf(buffer, "%*s%lf", &(View->vwin.south));
		    ++wind_keys;
		    continue;
		}
		if (!strcmp(keystring, "east")) {
		    sscanf(buffer, "%*s%lf", &(View->vwin.east));
		    ++wind_keys;
		    continue;
		}
		if (!strcmp(keystring, "west")) {
		    sscanf(buffer, "%*s%lf", &(View->vwin.west));
		    ++wind_keys;
		    continue;
		}
		if (!strcmp(keystring, "rows")) {
		    sscanf(buffer, "%*s%d", &(View->vwin.rows));
		    ++wind_keys;
		    continue;
		}
		if (!strcmp(keystring, "cols")) {
		    sscanf(buffer, "%*s%d", &(View->vwin.cols));
		    ++wind_keys;
		    continue;
		}
		if (!strcmp(keystring, "TO_EASTING")) {
		    sscanf(buffer, "%*s%f", &(View->from_to[1][0]));
		    ++reqkeys;
		    continue;
		}
		if (!strcmp(keystring, "TO_NORTHING")) {
		    sscanf(buffer, "%*s%f", &(View->from_to[1][1]));
		    ++reqkeys;
		    continue;
		}
		if (!strcmp(keystring, "TO_HEIGHT")) {
		    sscanf(buffer, "%*s%f", &(View->from_to[1][2]));
		    ++reqkeys;
		    continue;
		}
		if (!strcmp(keystring, "FROM_EASTING")) {
		    sscanf(buffer, "%*s%f", &(View->from_to[0][0]));
		    ++reqkeys;
		    continue;
		}
		if (!strcmp(keystring, "FROM_NORTHING")) {
		    sscanf(buffer, "%*s%f", &(View->from_to[0][1]));
		    ++reqkeys;
		    continue;
		}
		if (!strcmp(keystring, "FROM_HEIGHT")) {
		    sscanf(buffer, "%*s%f", &(View->from_to[0][2]));
		    ++reqkeys;
		    continue;
		}
		if (!strcmp(keystring, "Z_EXAG")) {
		    sscanf(buffer, "%*s%f", &(View->exag));
		    ++reqkeys;
		    continue;
		}
		if (!strcmp(keystring, "MESH_FREQ")) {
		    sscanf(buffer, "%*s%d", &(View->mesh_freq));
		    continue;
		}
		if (!strcmp(keystring, "POLY_RES")) {
		    sscanf(buffer, "%*s%d", &(View->poly_freq));
		    continue;
		}
		if (!strcmp(keystring, "DOAVG")) {
		    sscanf(buffer, "%*s%d", &(View->doavg));
		    continue;
		}
		if (!strcmp(keystring, "FIELD_VIEW")) {
		    sscanf(buffer, "%*s%f", &(View->fov));
		    ++reqkeys;
		    continue;
		}
		if (!strcmp(keystring, "TWIST")) {
		    sscanf(buffer, "%*s%f", &(View->twist));
		    continue;
		}
		if (!strcmp(keystring, "DISPLAY_TYPE")) {
		    sscanf(buffer, "%*s%d", &View->display_type);
		    continue;
		}
		if (!strcmp(keystring, "DOZERO")) {
		    sscanf(buffer, "%*s%s", boo);
		    View->dozero = get_bool(boo);
		    continue;
		}
		if (!strcmp(keystring, "COLORGRID")) {
		    sscanf(buffer, "%*s%s", boo);
		    View->colorgrid = get_bool(boo);
		    continue;
		}
		if (!strcmp(keystring, "FRINGE")) {
		    sscanf(buffer, "%*s%s", boo);
		    View->fringe = get_bool(boo);
		    continue;
		}
		if (!strcmp(keystring, "SHADING")) {
		    sscanf(buffer, "%*s%s", boo);
		    View->shading = get_bool(boo);
		    continue;
		}
		if (!strcmp(keystring, "BG_COL")) {
		    sscanf(buffer, "%*s%s", View->bg_col);
		    continue;
		}
		if (!strcmp(keystring, "GRID_COL")) {
		    sscanf(buffer, "%*s%s", View->grid_col);
		    continue;
		}
		if (!strcmp(keystring, "OTHER_COL")) {
		    sscanf(buffer, "%*s%s", View->other_col);
		    continue;
		}
		if (!strcmp(keystring, "SURFACEONLY")) {
		    sscanf(buffer, "%*s%s", boo);
		    View->surfonly = get_bool(boo);
		    continue;
		}
		if (!strcmp(keystring, "LIGHTS_ON")) {
		    sscanf(buffer, "%*s%s", boo);
		    View->lightson = get_bool(boo);
		    continue;
		}
		if (!strcmp(keystring, "LIGHTPOS")) {
		    sscanf(buffer, "%*s%f%f%f%f", &(View->lightpos[0]),
			   &(View->lightpos[1]), &(View->lightpos[2]),
			   &(View->lightpos[3]));
		    continue;
		}
		if (!strcmp(keystring, "LIGHTCOL")) {
		    sscanf(buffer, "%*s%f%f%f", &(View->lightcol[0]),
			   &(View->lightcol[1]), &(View->lightcol[2]));
		    continue;
		}
		if (!strcmp(keystring, "LIGHTAMBIENT")) {
		    sscanf(buffer, "%*s%f", &(View->ambient));
		    continue;
		}
		if (!strcmp(keystring, "SHINE")) {
		    sscanf(buffer, "%*s%f", &(View->shine));
		    continue;
		}
	    }
	}

	fclose(fp);

	if (reqkeys != REQ_KEYS)	/* required keys not found */
	    return (-1);

	/* fill rest of View->vwin */
	if (wind_keys == 6) {
	    View->vwin.ew_res = (View->vwin.east - View->vwin.west) /
		View->vwin.cols;
	    View->vwin.ns_res = (View->vwin.north - View->vwin.south) /
		View->vwin.rows;
	}
	else
	    return (0);		/* older format */

	if (!Suppress_warn) {
	    if (95 > (lap = compare_wind(&(View->vwin), &curwin))) {

		fprintf(stderr, _("GRASS window when view was saved:\n"));
		G_format_northing(View->vwin.north, nbuf, G_projection());
		fprintf(stderr, "north:   %s\n", nbuf);
		G_format_northing(View->vwin.south, nbuf, G_projection());
		fprintf(stderr, "south:   %s\n", nbuf);
		G_format_easting(View->vwin.east, ebuf, G_projection());
		fprintf(stderr, "east:    %s\n", ebuf);
		G_format_easting(View->vwin.west, ebuf, G_projection());
		fprintf(stderr, "west:    %s\n", ebuf);
		pr_winerr(lap, fname);
	    }
	}
    }
    else {
	G_warning(_("Unable to open %s for reading"), fname);
	return (-1);
    }

    if (current)
	return (2);

    return (1);
}
Esempio n. 12
0
File: header.c Progetto: caomw/grass
/*!
   \brief Reads head information from text file (GV_HEAD_ELEMENT) - for internal use only

   \param Map pointer to Map_info structure

   \return 0 on success
   \return -1 on error
 */
int Vect__read_head(struct Map_info *Map)
{
    FILE *head_fp;
    char buff[2000];
    char *path, *ptr;

    /* Reset / init */
    Vect__init_head(Map);
    
    G_debug(1, "Vect__read_head(): vector = %s@%s", Map->name, Map->mapset);
    path = Vect__get_path(Map);
    head_fp = G_fopen_old(path, GV_HEAD_ELEMENT, Map->mapset);
    G_free(path);
    if (head_fp == NULL) {
	G_warning(_("Unable to open header file of vector <%s>"),
		  Vect_get_full_name(Map));
	return -1;
    }

    while (G_getl2(buff, 2000, head_fp)) {

	if (!(ptr = strchr(buff, ':'))) {
	    G_warning(_("Corrupted row in head: %s"), buff);
	    continue;
	}

	ptr++;			/* Search for the start of text */
	while (*ptr == ' ')
	    ptr++;

	if (strncmp(buff, "ORGANIZATION:", sizeof(char) * 12) == 0)
	    Vect_set_organization(Map, ptr);
	else if (strncmp(buff, "DIGIT DATE:", sizeof(char) * 11) == 0)
	    Vect_set_date(Map, ptr);
	else if (strncmp(buff, "DIGIT NAME:", sizeof(char) * 11) == 0)
	    Vect_set_person(Map, ptr);
	else if (strncmp(buff, "MAP NAME:", sizeof(char) * 9) == 0)
	    Vect_set_map_name(Map, ptr);
	else if (strncmp(buff, "MAP DATE:", sizeof(char) * 9) == 0)
	    Vect_set_map_date(Map, ptr);
	else if (strncmp(buff, "MAP SCALE:", sizeof(char) * 10) == 0)
	    Vect_set_scale(Map, atoi(ptr));
	else if (strncmp(buff, "OTHER INFO:", sizeof(char) * 11) == 0)
	    Vect_set_comment(Map, ptr);
	else if (strncmp(buff, "PROJ:", sizeof(char) * 5) == 0)
	    Vect_set_proj(Map, atoi(ptr));
	else if (strncmp(buff, "ZONE:", sizeof(char) * 5) == 0 ||
		 strncmp(buff, "UTM ZONE:", sizeof(char) * 9) == 0)
	    Vect_set_zone(Map, atoi(ptr));
	else if (strncmp(buff, "WEST EDGE:", sizeof(char) * 10) == 0) {
	}
	else if (strncmp(buff, "EAST EDGE:", sizeof(char) * 10) == 0) {
	}
	else if (strncmp(buff, "SOUTH EDGE:", sizeof(char) * 11) == 0) {
	}
	else if (strncmp(buff, "NORTH EDGE:", sizeof(char) * 11) == 0) {
	}
	else if (strncmp(buff, "MAP THRESH:", sizeof(char) * 11) == 0)
	    Vect_set_thresh(Map, atof(ptr));
	else
	    G_warning(_("Unknown keyword '%s' in vector head"), buff);
    }

    fclose(head_fp);

    return 0;
}
Esempio n. 13
0
int main( int argc, char *argv[] )
{
	Shypothesis **samples; /* Data to be combined */
	unsigned int i;
	FILE *kb;
	char **groups;
	int norm = 1; /* turn on normalisation of evidence by default */
	/* these are for time keeping in the logfile */	
	time_t systime;
	clock_t proctime;
	unsigned long timeused;
	unsigned int days, hours, mins, secs;
	
	xmlDocPtr dstXMLFile;
	Sfp_struct* file_pointers;	
	
	G_gisinit ( argv[0] );		
	
	module = G_define_module ();
	module->description = "Combines evidences from a DST knowledge base";
		
	parm.file = G_define_option ();
	parm.file->key = "file";
	parm.file->type = TYPE_STRING;
	parm.file->required = YES;
	parm.file->description = "Name of the knowledge base that contains the evidence";
	
	parm.groups = G_define_option ();
	parm.groups->key = "sources";
	parm.groups->type = TYPE_STRING;
	parm.groups->required = NO;
	parm.groups->multiple = YES;
	parm.groups->description = "Evidences to be combined (default: all)";
			
	parm.type = G_define_option ();
	parm.type->key = "type";
	parm.type->type = TYPE_STRING;
	parm.type->required = NO;
	parm.type->options = "const,rast,vect";
	parm.type->answer = "rast";
	parm.type->description = "Type(s) of evidences to combine";	
	
	parm.output = G_define_option ();
	parm.output->key = "output";
	parm.output->type = TYPE_STRING;
	parm.output->required = NO;
	parm.output->answer = G_location ();
	parm.output->description = "Prefix for result maps (dflt: location name)";

	parm.vals = G_define_option ();
	parm.vals->key = "values";
	parm.vals->type = TYPE_STRING;
	parm.vals->required = NO;
	parm.vals->multiple = YES;
	parm.vals->options = "bel,pl,doubt,common,bint,woc,maxbpa,minbpa,maxsrc,minsrc";
	parm.vals->answer = "bel";
	parm.vals->description = "Dempster-Shafer values to map";

	parm.hyps = G_define_option ();
	parm.hyps->key = "hypotheses";
	parm.hyps->type = TYPE_STRING;
	parm.hyps->required = NO;
	parm.hyps->multiple = NO;
	parm.hyps->description = "Hypotheses to map (default: all)";

	parm.logfile = G_define_option ();
	parm.logfile->key = "logfile";
	parm.logfile->type = TYPE_STRING;
	parm.logfile->required = NO;
	parm.logfile->description = "Name of logfile";

	/* TODO: not implemented yet
	parm.warnings = G_define_option ();
	parm.warnings->key = "warnings";
	parm.warnings->type = TYPE_STRING;
	parm.warnings->required = NO;
	parm.warnings->description = "Name of site list to store locations of warnings." ;
	*/

	flag.norm = G_define_flag ();
	flag.norm->key = 'n';
	flag.norm->description = "Turn off normalisation";

	flag.quiet = G_define_flag ();
	flag.quiet->key = 'q';
	flag.quiet->description = "Quiet operation: no progress diplay";

	/* append output to existing logfile ? */
	flag.append = G_define_flag ();
	flag.append->key = 'a';
	flag.append->description = "Append log output to existing file";
	
	no_assigns = 0;
	
	/* INIT GLOBAL VARS */
	WOC_MIN = 0;
	WOC_MAX = 0;
	
	/* do not pause after a warning message was displayed */
	G_sleep_on_error (0);
	
	/* parse command line */
	if (G_parser (argc, argv))
	{
		exit (-1);
	}			
	
	/* check if given parameters are valid */
	if (G_legal_filename (parm.file->answer) == -1) {
		G_fatal_error ("Please provide the name of an existing DST knowledge base.\n");
	}
	
	if (G_find_file ("DST",parm.file->answer,G_mapset()) == NULL) {
		G_fatal_error ("Knowledge base does not exist in user's MAPSET!\n");
	}
	
	/* check logfile */
	if (parm.logfile->answer != NULL) {		
		if ( !G_legal_filename (parm.logfile->answer) ) {
			G_fatal_error ("Please specify a legal filename for the logfile.\n");
		}
		/* attempt to write to logfile */
		if (flag.append->answer) {
			if (fopen (parm.logfile->answer, "r") == NULL) {
				lp = fopen (parm.logfile->answer, "w+");
				if (lp == NULL) {
					G_fatal_error ("Logfile error: %s\n", strerror (errno));
				}				
			} else {
				lp = fopen (parm.logfile->answer, "a");
				if (lp == NULL) {
					G_fatal_error ("Logfile error: %s\n", strerror (errno));
				}
				fprintf (lp,"\n\n * * * * * \n\n");
			}
		} else {
			if ( (lp = fopen ( parm.logfile->answer, "w+" ) ) == NULL ) {
				G_fatal_error ("Logfile error: %s\n", strerror (errno));
			}
		}
		/* we want unbuffered output for the logfile */
		setvbuf (lp,NULL,_IONBF,0);
	} else {		
		/* log output to stderr by default */
		lp = stderr;
	}
		
	/* setup coordinate file storage, if desired */
	/* try to create a sites file to store coordinates */
	/* set 'warn' to point to the user-defined sites file */	
	/*
	warn = NULL;
	if ( parm.warnings != NULL ) {
	}
	*/
			
	/* check if we have read/write access to knowledge base */
	kb = G_fopen_old ("DST",parm.file->answer,G_mapset());
	if ( kb == NULL ) {
		G_fatal_error ("Cannot open knowledge base file for reading and writing!\n");
	}
	fclose(kb);
	
	/* start logfile */
	if ( parm.logfile->answer != NULL) {
		fprintf (lp,"This is %s, version %.2f\n",argv[0],PROGVERSION);
		systime = time (NULL);
		fprintf (lp,"Calculation started on %s\n",ctime(&systime));
	}		
		
	/* open DST file and get basic evidence group information */
	dstXMLFile = stat_XML ( parm.file->answer, &NO_SINGLETONS, &N );
	groups = get_groups_XML ( N, dstXMLFile );
	
	if ( NO_SINGLETONS == 1 ) {
		G_fatal_error ("Knowledge base does not contain any user-supplied hypotheses.\n");
	}

	if ( parm.groups->answer != NULL ) {
		/* user specified a subset of groups */
		N = check_groups ( groups );
	}
		
	if ( N < 2 ) {
		G_fatal_error ("At least two groups of evidences must be present in the knowledge base.\n");
	}				

	/* allocate memory for all samples 
	 a sample holds one double for bel, pl and bpn for each
	 piece of evidence. The number of pieces of evidence is
	 the number of possible subsets in Theta 
	 = 2^NO_SINGLETONS ! 
	
	 The number of samples is = number of groups in the XML
	 knowledge base file (N) !
	*/
	samples = (Shypothesis**) G_malloc ((N * sizeof(Shypothesis*)));
	for ( i=0; i < N; i ++ ) {
		samples[i] = (Shypothesis*) G_malloc (sizeof(Shypothesis));
	}
	
	
	/* turn off normalisation if user wants it so */
	if ( flag.norm->answer == 1 ) {
		norm = 0;
	}	
			
	/* do some type-dependant checking */
	/* and open file pointers for all the maps to read! */
	file_pointers = NULL;
	if ( !strcmp (parm.type->answer,"rast") ) {
		if ( parm.groups->answer != NULL ) {
			/* check only user-specified groups */
			file_pointers = test_rast_XML ( parm.groups->answers, dstXMLFile );			
		} else {
			/* check all groups */
			file_pointers = test_rast_XML ( groups, dstXMLFile );
		}
	}	
	
	/* read in all samples in a type-dependant manner */
	if ( parm.groups->answer != NULL ) {
		/* read only user-specified groups */
		if ( !strcmp (parm.type->answer,"const") ) {
			if ( strcmp (parm.output->answer,G_location ()) != 0) {
				G_warning ("Ignoring parameter 'output='.\n");
			}
			if ( strcmp (parm.vals->answer,"bel") !=0 ) {
				G_warning ("Ignoring parameter 'values='.\n");
			}
			if ( parm.hyps->answer != NULL ) {
				G_warning ("Ignoring parameter 'hypotheses='.\n");
			}
			do_calculations_const (samples,parm.groups->answers, norm, dstXMLFile);
		}
		if ( !strcmp (parm.type->answer,"rast") ) {
			do_calculations_rast (samples,parm.groups->answers, norm,
								  parm.output->answer, parm.vals->answers, 
								  parm.hyps->answer, flag.quiet->answer,
							      parm.logfile->answer, dstXMLFile, file_pointers );
		}			
	} else {
		/* read all groups */
		if ( !strcmp (parm.type->answer,"const") ) {
			if ( strcmp (parm.output->answer,G_location ()) != 0) {
				G_warning ("Ignoring parameter 'output='.\n");
			}
			if ( strcmp (parm.vals->answer,"bel") !=0 ) {
				G_warning ("Ignoring parameter 'values='.\n");
			}
			if ( parm.hyps->answer != NULL ) {
				G_warning ("Ignoring parameter 'hypotheses='.\n");
			}
			do_calculations_const (samples,groups, norm, dstXMLFile);
		}
		if ( !strcmp (parm.type->answer,"rast") ) {
			do_calculations_rast (samples,groups, norm,
								  parm.output->answer, parm.vals->answers, 
							      parm.hyps->answer, flag.quiet->answer,
								  parm.logfile->answer, dstXMLFile, file_pointers );
		}			
	}	
		
	/* close logfile */
	/* write processing time to logfile */
	proctime = clock ();
	timeused = (unsigned long) proctime / CLOCKS_PER_SEC;
	days = timeused / 86400;
	hours = (timeused - (days * 86400)) / 3600;
	mins = (timeused - (days * 86400) - (hours * 3600)) / 60;		
	secs = (timeused - (days * 86400) - (hours * 3600) - (mins * 60));
	systime = time (NULL);
	
	if ( parm.logfile->answer != NULL ) {
		fprintf (lp,"\nCalculation finished on %s",ctime(&systime));		
		fprintf (lp,"Processing time: %id, %ih, %im, %is\n",
				days, hours, mins, secs );
		fflush (lp);
	}
	
	for (i=0; i<N; i++) {
		G_free (groups[i]);		
	}
	G_free (groups);
	
	return (EXIT_SUCCESS);
}
Esempio n. 14
0
FILE *G_oldsites_open_old(const char *name, const char *mapset)
{
    return G_fopen_old("site_lists", name, mapset);
}
Esempio n. 15
0
File: field.c Progetto: caomw/grass
static int read_dblinks_nat(struct Map_info *Map)
{
    FILE *fd;
    char file[1024], buf[2001];
    char tab[1024], col[1024], db[1024], drv[1024], fldstr[1024], *fldname;
    int fld;
    char *c, *path;
    int row, rule;
    struct dblinks *dbl;
    char **tokens;
    int ntok, i;

    dbl = Map->dblnk;

    /* Read dblink for native format */
    path = Vect__get_path(Map);
    fd = G_fopen_old(path, GV_DBLN_ELEMENT, Map->mapset);
    G_free(path);
    if (fd == NULL) {		/* This may be correct, no tables defined */
	G_debug(1, "Cannot open vector database definition file");
	return -1;
    }

    row = 0;
    rule = 0;
    while (G_getl2(buf, 2000, fd)) {
	row++;
	G_chop(buf);
	G_debug(1, "dbln: %s", buf);

	c = (char *)strchr(buf, '#');
	if (c != NULL)
	    *c = '\0';

	if (strlen(buf) == 0)
	    continue;

#ifdef NOT_ABLE_TO_READ_GRASS_6
	int ndef;
	ndef = sscanf(buf, "%s|%s|%s|%s|%s", fldstr, tab, col, db, drv);

        if (ndef < 2 || (ndef < 5 && rule < 1)) {
            G_warning(_("Error in rule on row %d in <%s>"), row, file);
            continue;
        }
#else
	tokens = G_tokenize(buf, " |");
	ntok = G_number_of_tokens(tokens);

	if (ntok < 2 || (ntok < 5 && rule < 1)) {
	    G_warning(_("Error in rule on row %d in <%s>"), row, file);
	    continue;
	}

	strcpy(fldstr, tokens[0]);
	strcpy(tab, tokens[1]);
	if (ntok > 2) {
	    strcpy(col, tokens[2]);
	    if (ntok > 3) {
		strcpy(db, tokens[3]);
		/* allow for spaces in path names */
		for (i=4; i < ntok-1; i++) {
		    strcat(db, " ");
		    strcat(db, tokens[i]);
		}

		strcpy(drv, tokens[ntok-1]);
	    }
	}
	G_free_tokens(tokens);
#endif

	/* get field and field name */
	fldname = strchr(fldstr, '/');
	if (fldname != NULL) {	/* field has name */
	    fldname[0] = 0;
	    fldname++;
	}
	fld = atoi(fldstr);

	Vect_add_dblink(dbl, fld, fldname, tab, col, db, drv);

	G_debug(1,
		"field = %d name = %s, table = %s, key = %s, database = %s, driver = %s",
		fld, fldname, tab, col, db, drv);

	rule++;
    }
    fclose(fd);

    G_debug(1, "Dblinks read");
    
    return rule;
}
Esempio n. 16
0
/*!
  \brief Open feature index file
  
  \param[in,out] Map pointer to Map_info struct
  \param[out] offset pointer to Format_info_offset (OGR or PG)
  
  \return 0 on success
  \return -1 on error
*/
int Vect_open_fidx(struct Map_info *Map, struct Format_info_offset *offset)
{
    char elem[GPATH_MAX];
    char buf[5];		/* used for format version */
    long length;
    int Version_Major, Version_Minor, Back_Major, Back_Minor, byte_order;
    
    struct gvfile fp;
    struct Port_info port;
    
    G_debug(1, "Vect_open_fidx(): name = %s mapset = %s format = %d",
	    Map->name, Map->mapset, Map->format);
    
    sprintf(elem, "%s/%s", GV_DIRECTORY, Map->name);
    dig_file_init(&fp);
    fp.file = G_fopen_old(elem, GV_FIDX_ELEMENT, Map->mapset);
    if (fp.file == NULL) {
        G_debug(1, "unable to open fidx file for vector map <%s>",
                Vect_get_full_name(Map));
	return -1;
    }

    /* Header */
    if (0 >= dig__fread_port_C(buf, 5, &fp))
	return -1;
    Version_Major = buf[0];
    Version_Minor = buf[1];
    Back_Major    = buf[2];
    Back_Minor    = buf[3];
    byte_order    = buf[4];
    
    /* check version numbers */
    if (Version_Major > 5 || Version_Minor > 0) {
	if (Back_Major > 5 || Back_Minor > 0) {
	    G_fatal_error(_("Feature index format version %d.%d is not supported by this release."
			   " Try to rebuild topology or upgrade GRASS."),
			  Version_Major, Version_Minor);
	    return -1;
	}
	G_warning(_("Your GRASS version does not fully support feature index format %d.%d of the vector."
		   " Consider to rebuild topology or upgrade GRASS."),
		  Version_Major, Version_Minor);
    }

    dig_init_portable(&port, byte_order);
    dig_set_cur_port(&port);

    /* Body */
    /* bytes 6 - 9 : header size */
    if (0 >= dig__fread_port_L(&length, 1, &fp))
	return -1;
    G_debug(4, "  header size %ld", length);

    G_fseek(fp.file, length, SEEK_SET);

    /* number of records  */
    if (0 >= dig__fread_port_I(&(offset->array_num), 1, &fp))
	return -1;
    
    /* alloc space */
    offset->array = (int *) G_malloc(offset->array_num * sizeof(int));
    offset->array_alloc = offset->array_num;
    
    /* offsets */
    if (0 >= dig__fread_port_I(offset->array,
			       offset->array_num, &fp))
	return -1;

    fclose(fp.file);

    G_debug(3, "%d records read from fidx", offset->array_num);

    return 0;
}