Esempio n. 1
0
/* the struct Con_Points */
int I_read_con_points(FILE * fd, struct Ortho_Control_Points *cp)
{
    char buf[300];
    double e1, e2, n1, n2, z1, z2;
    int status;

    cp->count = 0;

    /* read the control point lines. format is (on one line):
       photo_x        photo_y         -CFL 
       control_east control_north  control_elev  status(1=ok)
     */
    cp->e1 = NULL;
    cp->e2 = NULL;
    cp->n1 = NULL;
    cp->n2 = NULL;
    cp->z1 = NULL;
    cp->z2 = NULL;

    cp->status = NULL;

    while (G_getl(buf, sizeof(buf), fd)) {
	G_strip(buf);
	if (*buf == '#' || *buf == 0)
	    continue;
	if (sscanf(buf, "%lf%lf%lf%lf%lf%lf%d",
		   &e1, &n1, &z1, &e2, &n2, &z2, &status) == 7)
	    I_new_con_point(cp, e1, n1, z1, e2, n2, z2, status);
	else
	    return -4;
    }

    return 1;
}
Esempio n. 2
0
int I_read_ref_points(FILE * fd, struct Ortho_Photo_Points *cp)
{
    char buf[100];
    double e1, e2, n1, n2;
    int status;

    cp->count = 0;

    /* read the reference point lines. format is:
       image_east image_north  photo_x photo_y  status(1=ok)
     */
    cp->e1 = NULL;
    cp->e2 = NULL;
    cp->n1 = NULL;
    cp->n2 = NULL;
    cp->status = NULL;

    /*fprintf (stderr, "Try to read one point \n"); */
    while (G_getl(buf, sizeof(buf), fd)) {
	G_strip(buf);
	if (*buf == '#' || *buf == 0)
	    continue;
	if (sscanf(buf, "%lf%lf%lf%lf%d", &e1, &n1, &e2, &n2, &status) == 5)
	    I_new_ref_point(cp, e1, n1, e2, n2, status);
	else
	    return -4;
    }

    return 1;
}
Esempio n. 3
0
int I_list_cameras(int full)
{
    char *element;
    char buf[1024];
    char title[50];
    FILE *ls, *temp;
    int any;

    if (tempfile == NULL)
	tempfile = G_tempfile();

    element = "camera";
    G__make_mapset_element(element);

    temp = fopen(tempfile, "w");
    if (temp == NULL)
	G_fatal_error("can't open any temp files");
    fprintf(temp, "Available cameras\n");
    fprintf(temp, "---------------------------------\n");

    any = 0;
    strcpy(buf, "cd ");
    G_file_name(buf + strlen(buf), element, "", G_mapset());
    strcat(buf, ";ls");
    if (!full)
	strcat(buf, " -C");
    if (ls = popen(buf, "r")) {
	while (G_getl(buf, sizeof buf, ls)) {
	    any = 1;
	    fprintf(temp, "%s", buf);
	    if (full) {
		I_get_cam_title(buf, title, sizeof title);
		if (*title)
		    fprintf(temp, " (%s)", title);
		fprintf(temp, "\n");
	    }
	    else
		fprintf(temp, "\n");
	}
	pclose(ls);
    }
    if (!any)
	fprintf(temp, "no camera files available\n");
    fprintf(temp, "---------------------------------\n");
    fclose(temp);
    sprintf(buf, "$GRASS_PAGER %s", tempfile);
    G_system(buf);
    unlink(tempfile);
    fprintf(stderr, "hit RETURN to continue -->");
    G_gets(buf);

    return 0;
}
Esempio n. 4
0
int I_list_elev(int full)
{
    char *element;
    char buf[1024];
    FILE *ls, *temp;
    int any;

    if (tempfile == NULL)
	tempfile = G_tempfile();

    element = "cell";
    G__make_mapset_element(element);

    temp = fopen(tempfile, "w");
    if (temp == NULL)
	G_fatal_error("can't open any temp files");
    fprintf(temp, "Available raster maps:\n");
    fprintf(temp, "---------------------------------\n");

    any = 0;
    strcpy(buf, "cd ");
    G__file_name(buf + strlen(buf), element, " ", " ");
    strcat(buf, ";ls");
    strcat(buf, " -C");
    if ((ls = popen(buf, "r"))) {
	while (G_getl(buf, sizeof buf, ls)) {
	    any = 1;
	    fprintf(temp, "%s", buf);
	    fprintf(temp, "\n");
	}
	pclose(ls);
    }
    if (!any)
	fprintf(temp, "no raster maps available\n");
    fprintf(temp, "---------------------------------\n");
    fclose(temp);
    sprintf(buf, "$GRASS_PAGER %s", tempfile);
    G_system(buf);
    unlink(tempfile);
    fprintf(stderr, "hit RETURN to continue -->");
    G_gets(buf);

/******/
    G_list_element("cell", "cell", G_mapset(), NULL);


    return 0;
}
Esempio n. 5
0
char *G_myname(void)
{
    static char name[GNAME_MAX];
    char path[GPATH_MAX];
    FILE *fd;
    int ok;

    ok = 0;

    G__file_name(path, "", "MYNAME", "PERMANENT");
    if ((fd = fopen(path, "r"))) {
	ok = G_getl(name, sizeof name, fd);
	fclose(fd);
    }
    if (!ok)
	strcpy(name, _("This location has no description."));

    return name;
}
Esempio n. 6
0
File: la.c Progetto: caomw/grass
int G_matrix_read(FILE * fp, mat_struct * out)
{
    char buff[100];
    int rows, cols;
    int i, j, row;
    double val;

    /* skip comments */
    for (;;) {
	if (!G_getl(buff, sizeof(buff), fp))
	    return -1;
	if (buff[0] != '#')
	    break;
    }

    if (sscanf(buff, "Matrix: %d by %d", &rows, &cols) != 2) {
	G_warning(_("Input format error"));
	return -1;
    }

    G_matrix_set(out, rows, cols, rows);

    for (i = 0; i < rows; i++) {
	if (fscanf(fp, "row%d:", &row) != 1 || row != i) {
	    G_warning(_("Input format error"));
	    return -1;
	}
	for (j = 0; j < cols; j++) {
	    if (fscanf(fp, "%lf:", &val) != 1) {
		G_warning(_("Input format error"));
		return -1;
	    }

	    G_matrix_set_element(out, i, j, val);
	}
    }

    return 0;
}
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
int stats(void)
{
    char buf[1024];
    char mname[GNAME_MAX], rname[GMAPSET_MAX];
    const char *mmapset, *rmapset;
    int i, nl;
    size_t ns;
    FILE *fd;
    char **tokens;
    const char *argv[9];
    int argc = 0;

    strcpy(mname, maps[0]);
    mmapset = G_find_raster2(mname, "");
    if (mmapset == NULL)
	G_fatal_error(_("Raster map <%s> not found"), maps[0]);

    strcpy(rname, maps[1]);
    rmapset = G_find_raster2(rname, "");
    if (rmapset == NULL)
	G_fatal_error(_("Raster map <%s> not found"), maps[1]);

    stats_file = G_tempfile();

    argv[argc++] = "r.stats";

    argv[argc++] = "-cin";

    argv[argc++] = "fs=:";

    sprintf(buf, "input=%s,%s",
	    G_fully_qualified_name(maps[1], mmapset),
	    G_fully_qualified_name(maps[0], rmapset));
    argv[argc++] = buf;

    argv[argc++] = SF_REDIRECT_FILE;
    argv[argc++] = SF_STDOUT;
    argv[argc++] = SF_MODE_OUT;
    argv[argc++] = stats_file;

    argv[argc++] = NULL;

    if (G_vspawn_ex(argv[0], argv) != 0) {
	remove(stats_file);
	G_fatal_error("error running r.stats");
    }

    fd = fopen(stats_file, "r");
    if (fd == NULL) {
	unlink(stats_file);
	sprintf(buf, "Unable to open result file <%s>\n", stats_file);
    }

    while (G_getl(buf, sizeof buf, fd)) {
	tokens = G_tokenize(buf, ":");
	i = 0;
	ns = nstats++;
	Gstats = (GSTATS *) G_realloc(Gstats, nstats * sizeof(GSTATS));
	Gstats[ns].cats = (long *)G_calloc(nlayers, sizeof(long));
	for (nl = 0; nl < nlayers; nl++) {
	    if (sscanf(tokens[i++], "%ld", &Gstats[ns].cats[nl]) != 1)
		die();
	}
	if (sscanf(tokens[i++], "%ld", &Gstats[ns].count) != 1)
	    die();
	G_free_tokens(tokens);
    }
    fclose(fd);
    unlink(stats_file);

    return 0;
}
Esempio n. 11
0
static int
read_cats(const char *name, const char *mapset, struct Categories *pcats)
 /* adapted from G__read_cats */
{
    FILE *fd;
    char buff[1024];
    CELL cat;
    DCELL val1, val2;
    int old;
    long num = -1;

    fd = G_fopen_old_misc(RASTER3D_DIRECTORY, RASTER3D_CATS_ELEMENT, name, mapset);
    if (!fd)
	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;

    /* Read the title for the file */
    if (G_getl(buff, sizeof(buff), fd) == 0)
	goto error;
    G_strip(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;

	    /* try to read a range of data */
	    if (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;
}