Exemple #1
0
static int ask_camera(char *prompt, char *camera)
{
    char buf[1024];

    while (1) {
	fprintf(stderr, "\n%s\n", prompt);
	fprintf(stderr, "Enter 'list' for a list of existing camera files\n");
	fprintf(stderr, "Enter 'list -f' for a verbose listing\n");
	fprintf(stderr, "Hit RETURN %s\n", G_get_ask_return_msg());
	fprintf(stderr, "> ");
	if (!G_gets(buf))
	    continue;

	G_squeeze(buf);
	fprintf(stderr, "<%s>\n", buf);
	if (*buf == 0)
	    return 0;

	if (strcmp(buf, "list") == 0)
	    I_list_cameras(0);
	else if (strcmp(buf, "list -f") == 0)
	    I_list_cameras(1);
	else if (G_legal_filename(buf) < 0)
	    fprintf(stderr, "\n** <%s> - illegal name **\n\n", buf);
	else
	    break;
    }
    strcpy(camera, buf);
    return 1;
}
Exemple #2
0
int print_label(long x)
{
    char *label;

    G_squeeze(label = Rast_get_c_cat((CELL *) &x, &cats));
    fprintf(stdout, "%ld%s%s\n", x, fs, label);

    return 0;
}
Exemple #3
0
int print_d_label(double x)
{
    char *label, tmp[40];
    DCELL dtmp;

    dtmp = x;
    G_squeeze(label = Rast_get_d_cat(&dtmp, &cats));
    sprintf(tmp, "%.10f", x);
    G_trim_decimal(tmp);
    fprintf(stdout, "%s%s%s\n", tmp, fs, label);

    return 0;
}
Exemple #4
0
static char *dxf_fgets(char *buf, int size, struct dxf_file *dxf)
{
    char *p;
    double perc;

    if ((p = fgets(buf, size, dxf->fp))) {
	dxf->pos += strlen(p);
	perc = (double) dxf->pos / dxf->size;
	G_percent((int) (perc * 100.0), 100, dxf->percent);
	G_squeeze(buf);
    }

    return p;
}
Exemple #5
0
/*
    Cleans a field string: removes leading and trailing whitespaces as well as any quotation
    marks that could be enclosing the field contents.
    Returns a newly allocated string. Caller must free it when done.
    Returns NULL on error or if input string was NULL.
*/
char *cleanString ( char *input ) 
{
    char *str;
    
    
    if ( input == NULL )
        return ( NULL );
	
    str = strdup ( input );
    
    G_strchg ( str, '\'', ' ' );
    
    if ( str != NULL )
        G_strchg ( str, '"', ' ' );  
    
    if ( str != NULL )
        G_squeeze ( str );
    
    return ( str );
}
Exemple #6
0
/*!
 *  \brief Extract a cell value from raster map (neighbor interpolation)
 *
 *  Extract a cell value from raster map at given northing and easting
 *  with a sampled 3x3 window using a neighbor interpolation.
 *
 *  \param fd file descriptor
 *  \param window region settings
 *  \param cats categories
 *  \param north northing position
 *  \param east easting position
 *  \param usedesc flag to scan category label
 *
 *  \return cell value at given position
 */
DCELL Rast_get_sample_nearest(int fd,
			      const struct Cell_head * window,
			      struct Categories * cats,
			      double north, double east, int usedesc)
{
    int row, col;
    DCELL result;
    DCELL *maprow = Rast_allocate_d_buf();

    /* convert northing and easting to row and col, resp */
    row = (int)floor(Rast_northing_to_row(north, window));
    col = (int)floor(Rast_easting_to_col(east, window));

    if (row < 0 || row >= Rast_window_rows() ||
	col < 0 || col >= Rast_window_cols()) {
	Rast_set_d_null_value(&result, 1);
	goto done;
    }

    Rast_get_d_row(fd, maprow, row);

    if (Rast_is_d_null_value(&maprow[col])) {
	Rast_set_d_null_value(&result, 1);
	goto done;
    }

    if (usedesc) {
	char *buf = Rast_get_c_cat((CELL *) & (maprow[col]), cats);

	G_squeeze(buf);
	result = scancatlabel(buf);
    }
    else
	result = maprow[col];

  done:
    G_free(maprow);

    return result;
}
Exemple #7
0
int main(int argc, char **argv)
{
    struct GModule *module;
    struct
    {
	struct Option *text;
	struct Option *size;
	struct Option *fgcolor;
	struct Option *bgcolor;
	struct Option *line;
	struct Option *at;
	struct Option *rotation;
	struct Option *align;
	struct Option *linespacing;
	struct Option *font;
	struct Option *path;
	struct Option *charset;
	struct Option *input;
    } opt;
    struct
    {
	struct Flag *p;
	struct Flag *g;
	struct Flag *b;
	struct Flag *r;
	struct Flag *s;
    } flag;

    /* options and flags */
    char *text;
    double size;
    double x, y;
    int line;
    double rotation;
    char align[3];
    double linespacing;

    char bold;

    /* window info */
    struct rectinfo win;

    /* command file */
    FILE *cmd_fp;

    char buf[512];

    int first_text;
    int linefeed;
    int set_l;
    double orig_x, orig_y;
    double prev_x, prev_y;
    double set_x, set_y;
    double east, north;
    int do_background, fg_color, bg_color;

    /* initialize the GIS calls */
    G_gisinit(argv[0]);

    module = G_define_module();
    G_add_keyword(_("display"));
    G_add_keyword(_("cartography"));
    module->description =
	_("Draws text in the active display frame on the graphics monitor using the current font.");

    opt.text = G_define_option();
    opt.text->key = "text";
    opt.text->type = TYPE_STRING;
    opt.text->required = NO;
    opt.text->description = _("Text to display");
    opt.text->guisection = _("Input");

    opt.input = G_define_standard_option(G_OPT_F_INPUT);
    opt.input->required = NO;
    opt.input->description = _("Input file");
    opt.input->guisection = _("Input");

    opt.fgcolor = G_define_option();
    opt.fgcolor->key = "color";
    opt.fgcolor->type = TYPE_STRING;
    opt.fgcolor->answer = DEFAULT_COLOR;
    opt.fgcolor->required = NO;
    opt.fgcolor->description =
	_("Text color, either a standard GRASS color or R:G:B triplet");
    opt.fgcolor->gisprompt = "old_color,color,color";
    opt.fgcolor->guisection = _("Text");

    opt.bgcolor = G_define_option();
    opt.bgcolor->key = "bgcolor";
    opt.bgcolor->type = TYPE_STRING;
    opt.bgcolor->required = NO;
    opt.bgcolor->description =
        _("Text background color, either a standard GRASS color or R:G:B triplet");
    opt.bgcolor->gisprompt = "old_color,color,color";
    opt.bgcolor->guisection = _("Text");

    opt.rotation = G_define_option();
    opt.rotation->key = "rotation";
    opt.rotation->type = TYPE_DOUBLE;
    opt.rotation->required = NO;
    opt.rotation->answer = "0";
    opt.rotation->description =
    _("Rotation angle in degrees (counter-clockwise)");
    opt.rotation->guisection = _("Text");

    opt.linespacing = G_define_option();
    opt.linespacing->key = "linespacing";
    opt.linespacing->type = TYPE_DOUBLE;
    opt.linespacing->required = NO;
    opt.linespacing->answer = "1.25";
    opt.linespacing->description = _("Line spacing");
    opt.linespacing->guisection = _("Text");

    opt.at = G_define_option();
    opt.at->key = "at";
    opt.at->key_desc = "x,y";
    opt.at->type = TYPE_DOUBLE;
    opt.at->required = NO;
    opt.at->description =
	_("Screen position at which text will begin to be drawn (percentage, [0,0] is lower left)");
    opt.at->guisection = _("Position");

    opt.line = G_define_option();
    opt.line->key = "line";
    opt.line->required = NO;
    opt.line->type = TYPE_INTEGER;
    opt.line->options = "1-1000";
    opt.line->description =
	_("The screen line number on which text will begin to be drawn");
    opt.line->guisection = _("Position");

    opt.align = G_define_option();
    opt.align->key = "align";
    opt.align->type = TYPE_STRING;
    opt.align->required = NO;
    opt.align->answer = "ll";
    opt.align->options = "ll,lc,lr,cl,cc,cr,ul,uc,ur";
    opt.align->description = _("Text alignment");
    opt.align->guisection = _("Position");

    opt.font = G_define_option();
    opt.font->key = "font";
    opt.font->type = TYPE_STRING;
    opt.font->required = NO;
    opt.font->description = _("Font name");
    opt.font->guisection = _("Font settings");

    opt.size = G_define_option();
    opt.size->key = "size";
    opt.size->type = TYPE_DOUBLE;
    opt.size->required = NO;
    opt.size->answer = "5";
    opt.size->options = "0-100";
    opt.size->description =
    _("Height of letters in percentage of available frame height");
    opt.size->guisection = _("Font settings");

    opt.path = G_define_standard_option(G_OPT_F_INPUT);
    opt.path->key = "path";
    opt.path->required = NO;
    opt.path->description = _("Path to font file");
    opt.path->gisprompt = "old,font,file";
    opt.path->guisection = _("Font settings");

    opt.charset = G_define_option();
    opt.charset->key = "charset";
    opt.charset->type = TYPE_STRING;
    opt.charset->required = NO;
    opt.charset->description =
	_("Text encoding (only applicable to TrueType fonts)");
    opt.charset->guisection = _("Font settings");

    flag.p = G_define_flag();
    flag.p->key = 'p';
    flag.p->description = _("Screen position in pixels ([0,0] is top left)");
    flag.p->guisection = _("Position");

    flag.g = G_define_flag();
    flag.g->key = 'g';
    flag.g->description = _("Screen position in geographic coordinates");
    flag.g->guisection = _("Position");

    flag.b = G_define_flag();
    flag.b->key = 'b';
    flag.b->description = _("Use bold text");
    flag.b->guisection = _("Text");

    flag.r = G_define_flag();
    flag.r->key = 'r';
    flag.r->description = _("Use radians instead of degrees for rotation");
    flag.r->guisection = _("Text");

    flag.s = G_define_flag();
    flag.s->key = 's';
    flag.s->description = _("Font size is height in pixels");
    flag.s->guisection = _("Font settings");


    /* check command line */
    if (G_parser(argc, argv))
	exit(1);

    /* parse and check options and flags */

    if ((opt.line->answer && opt.at->answer) ||
	(flag.p->answer && flag.g->answer))
	G_fatal_error(_("Please choose only one placement method"));

    text = opt.text->answer;

    line = (opt.line->answer ? atoi(opt.line->answer) : 1);

    /* calculate rotation angle in radian */
    rotation = atof(opt.rotation->answer);
    if (!flag.r->answer)
	rotation *= M_PI / 180.0;
    rotation = fmod(rotation, 2.0 * M_PI);
    if (rotation < 0.0)
	rotation += 2.0 * M_PI;

    strncpy(align, opt.align->answer, 2);
    linespacing = atof(opt.linespacing->answer);

    bold = flag.b->answer;

    D_open_driver();
    
    if (opt.font->answer)
	D_font(opt.font->answer);
    else if (opt.path->answer)
	D_font(opt.path->answer);

    if (opt.charset->answer)
	D_encoding(opt.charset->answer);

    D_setup_unity(0);

    /* figure out where to put text */
    D_get_src(&win.t, &win.b, &win.l, &win.r);

    if (flag.s->answer)
	size = atof(opt.size->answer);
    else
#ifdef BACKWARD_COMPATIBILITY
	size = atof(opt.size->answer) / 100.0 * (win.b - win.t) / linespacing;
#else
	size = atof(opt.size->answer) / 100.0 * (win.b - win.t);
#endif

    fg_color = D_parse_color(opt.fgcolor->answer, TRUE);
    if (opt.bgcolor->answer) {
	do_background = 1;
	bg_color = D_parse_color(opt.bgcolor->answer, TRUE);
	if (bg_color == 0) /* ie color="none" */
	    do_background = 0;
    } else
	do_background = 0;
    set_color(opt.fgcolor->answer);

    orig_x = orig_y = 0;

    if (opt.at->answer) {
	if (get_coordinates(&x, &y, &east, &north,
			    win, opt.at->answers,
			    flag.p->answer, flag.g->answer))
	    G_fatal_error(_("Invalid coordinates"));
	orig_x = x;
	orig_y = y;
    }
    else {
	x = win.l + (size * linespacing + 0.5) - size;	/* d.text: +5 */
	y = win.t + line * (size * linespacing + 0.5);
    }

    prev_x = x;
    prev_y = y;

    D_text_size(size, size);
    D_text_rotation(rotation * 180.0 / M_PI);

    if (text) {
	double x2, y2;

	x2 = x;
	y2 = y;

	if (text[0])
	    draw_text(text, &x2, &y2, size, align, rotation, bold, do_background, fg_color, bg_color);

	/* reset */
	D_text_size(5, 5);
	D_text_rotation(0.0);

	D_save_command(G_recreate_command());
	D_close_driver();

	exit(EXIT_SUCCESS);
    }

    if (!opt.input->answer || strcmp(opt.input->answer, "-") == 0)
	cmd_fp = stdin;
    else {
	cmd_fp = fopen(opt.input->answer, "r");
	if (!cmd_fp)
	    G_fatal_error(_("Unable to open input file <%s>"), opt.input->answer);
    }

    if (isatty(fileno(cmd_fp)))
	fprintf(stderr,
		_("\nPlease enter text instructions.  Enter EOF (ctrl-d) on last line to quit\n"));

    set_x = set_y = set_l = 0;
    first_text = 1;
    linefeed = 1;
    /* do the plotting */
    while (fgets(buf, sizeof(buf), cmd_fp)) {
	int buf_len;
	char *buf_ptr, *ptr;

	buf_len = strlen(buf) - 1;
	for (; buf[buf_len] == '\r' || buf[buf_len] == '\n'; buf_len--) ;
	buf[buf_len + 1] = 0;

	if (buf[0] == '.' && buf[1] != '.') {
	    int i;
	    double d;

	    G_squeeze(buf);	/* added 6/91 DBS @ CWU */
	    for (buf_ptr = buf + 2; *buf_ptr == ' '; buf_ptr++) ;
	    buf_len = strlen(buf_ptr);

	    switch (buf[1] & 0x7f) {
	    case 'F':
		/* font */
		if ((ptr = strchr(buf_ptr, ':')))
		    *ptr = 0;
		D_font(buf_ptr);
		if (ptr)
		    D_encoding(ptr + 1);
		break;
	    case 'C':
		/* color */
		set_color(buf_ptr);
		fg_color = D_parse_color(buf_ptr, 1);
		break;
	    case 'G':
		/* background color */
		bg_color = D_parse_color(buf_ptr, 1);
		do_background = 1;
		break;
	    case 'S':
		/* size */
		i = 0;
		if (strchr("+-", buf_ptr[0]))
		    i = 1;
		d = atof(buf_ptr);
		if (buf_ptr[buf_len - 1] != 'p')
#ifdef BACKWARD_COMPATIBILITY
		    d *= (win.b - win.t) / 100.0 / linespacing;
#else
		    d *= (win.b - win.t) / 100.0;
#endif
		size = d + (i ? size : 0);
		D_text_size(size, size);
		break;
	    case 'B':
		/* bold */
		bold = (atoi(buf_ptr) ? 1 : 0);
		break;
	    case 'A':
		/* align */
		strncpy(align, buf_ptr, 2);
		break;
	    case 'R':
		/* rotation */
		i = 0;
		if (strchr("+-", buf_ptr[0]))
		    i = 1;
		d = atof(buf_ptr);
		if (buf_ptr[buf_len - 1] != 'r')
		    d *= M_PI / 180.0;
		d += (i ? rotation : 0.0);
		rotation = fmod(d, 2.0 * M_PI);
		if (rotation < 0.0)
		    rotation += 2.0 * M_PI;
		D_text_rotation(rotation * 180.0 / M_PI);
		break;
	    case 'I':
		/* linespacing */
		linespacing = atof(buf_ptr);
		break;
	    case 'X':
		/* x */
		set_l = 0;
		set_x = 1;
		i = 0;
		if (strchr("+-", buf_ptr[0]))
		    i = 1;
		d = atof(buf_ptr);
		if (buf_ptr[buf_len - 1] == '%')
		    /* percentage */
		    d *= (win.r - win.l) / 100.0;
		else if (buf_ptr[buf_len - 1] != 'p')
		    /* column */
		    d = (d - 1) * size * linespacing + 0.5;
		x = prev_x = d + (i ? x : orig_x);
		break;
	    case 'Y':
		/* y */
		set_l = 0;
		set_y = 1;
		i = 0;
		if (strchr("+-", buf_ptr[0]))
		    i = 1;
		d = atof(buf_ptr);
		if (buf_ptr[buf_len - 1] == '%')
		    /* percentage */
		    d = win.b - d * (win.b - win.t) / 100.0;
		else if (buf_ptr[buf_len - 1] != 'p')
		    /* row */
		    d *= size * linespacing + 0.5;
		y = prev_y = d + (i ? y : orig_y);
		break;
	    case 'L':
		/* linefeed */
		set_l = 1;
		linefeed = (atoi(buf_ptr) ? 1 : 0);
		break;
	    case 'E':
		i = 0;
		if (strchr("+-", buf_ptr[0]))
		    i = 1;
		d = atof(buf_ptr);
		if (buf_ptr[buf_len - 1] == '%')
		    d *= (win.r - win.l) / 100.0;
		else if (buf_ptr[buf_len - 1] != 'p')
		    d = D_u_to_d_col(d);
		x = prev_x = orig_x = d + (i ? orig_x : win.l);
		break;
	    case 'N':
		i = 0;
		if (strchr("+-", buf_ptr[0]))
		    i = 1;
		d = atof(buf_ptr);
		if (buf_ptr[buf_len - 1] == '%')
		    d *= (win.b - win.t) / 100.0;
		else if (buf_ptr[buf_len - 1] != 'p')
		    d = D_u_to_d_row(d);
		y = prev_y = orig_y = d + (i ? orig_y : win.t);
		break;
	    }
	}
	else {
	    buf_ptr = buf;
	    if (buf[0] == '.' && buf[1] == '.')
		buf_ptr++;

	    if (!first_text && (linefeed || set_l)) {
		/* if x is not given, increment x */
		if (!set_x)
		    x = prev_x +
			(size * linespacing + 0.5) * sin(rotation);
		/* if y is not given, increment y */
		if (!set_y)
		    y = prev_y +
			(size * linespacing + 0.5) * cos(rotation);
		prev_x = x;
		prev_y = y;
	    }
	    set_x = set_y = set_l = first_text = 0;

	    draw_text(buf_ptr, &x, &y, size, align, rotation, bold, do_background, fg_color, bg_color);
	}
    }

    if (cmd_fp != stdin)
	fclose(cmd_fp);

    /* reset */
    D_text_size(5, 5);
    D_text_rotation(0.0);

    D_close_driver();

    exit(EXIT_SUCCESS);
}
Exemple #8
0
/*!
 *  \brief Extract a cell value from raster map (cubic interpolation).
 *
 *  Extract a cell value from raster map at given northing and easting
 *  with a sampled 3x3 window using a cubic interpolation.
 *
 *  \param fd file descriptor
 *  \param window region settings
 *  \param cats categories
 *  \param north northing position
 *  \param east easting position
 *  \param usedesc flag to scan category label
 *
 *  \return cell value at given position
 */
DCELL Rast_get_sample_cubic(int fd,
			    const struct Cell_head * window,
			    struct Categories * cats,
			    double north, double east, int usedesc)
{
    int i, j, row, col;
    double grid[4][4];
    DCELL *rows[4];
    double frow, fcol, trow, tcol;
    DCELL result;

    for (i = 0; i < 4; i++)
	rows[i] = Rast_allocate_d_buf();

    frow = Rast_northing_to_row(north, window);
    fcol = Rast_easting_to_col(east, window);

    /* convert northing and easting to row and col, resp */
    row = (int)floor(frow - 1.5);
    col = (int)floor(fcol - 1.5);

    trow = frow - row - 1.5;
    tcol = fcol - col - 1.5;

    if (row < 0 || row + 3 >= Rast_window_rows() ||
	col < 0 || col + 3 >= Rast_window_cols()) {
	Rast_set_d_null_value(&result, 1);
	goto done;
    }

    for (i = 0; i < 4; i++)
	Rast_get_d_row(fd, rows[i], row + i);

    for (i = 0; i < 4; i++)
	for (j = 0; j < 4; j++)
	    if (Rast_is_d_null_value(&rows[i][col + j])) {
		Rast_set_d_null_value(&result, 1);
		goto done;
	    }

    /*
     * now were ready to do cubic interpolation over
     * arow[col], arow[col+1], arow[col+2], arow[col+3],
     * brow[col], brow[col+1], brow[col+2], brow[col+3],
     * crow[col], crow[col+1], crow[col+2], crow[col+3],
     * drow[col], drow[col+1], drow[col+2], drow[col+3],
     */

    if (usedesc) {
	char *buf;

	for (i = 0; i < 4; i++) {
	    for (j = 0; j < 4; j++) {
		G_squeeze(buf =
			  Rast_get_c_cat((CELL *) & (rows[i][col + j]),
					 cats));
		grid[i][j] = scancatlabel(buf);
	    }
	}
    }
    else {
	for (i = 0; i < 4; i++)
	    for (j = 0; j < 4; j++)
		grid[i][j] = rows[i][col + j];
    }

    result = Rast_interp_bicubic(tcol, trow,
				 grid[0][0], grid[0][1], grid[0][2],
				 grid[0][3], grid[1][0], grid[1][1],
				 grid[1][2], grid[1][3], grid[2][0],
				 grid[2][1], grid[2][2], grid[2][3],
				 grid[3][0], grid[3][1], grid[3][2],
				 grid[3][3]);

  done:
    for (i = 0; i < 4; i++)
	G_free(rows[i]);

    return result;
}
Exemple #9
0
/*!
 *  \brief Extract a cell value from raster map (bilinear interpolation).
 *
 *  Extract a cell value from raster map at given northing and easting
 *  with a sampled 3x3 window using a bilinear interpolation.
 *
 *  \param fd file descriptor
 *  \param window region settings
 *  \param cats categories
 *  \param north northing position
 *  \param east easting position
 *  \param usedesc flag to scan category label
 *
 *  \return cell value at given position
 */
DCELL Rast_get_sample_bilinear(int fd,
			       const struct Cell_head * window,
			       struct Categories * cats,
			       double north, double east, int usedesc)
{
    int row, col;
    double grid[2][2];
    DCELL *arow = Rast_allocate_d_buf();
    DCELL *brow = Rast_allocate_d_buf();
    double frow, fcol, trow, tcol;
    DCELL result;

    frow = Rast_northing_to_row(north, window);
    fcol = Rast_easting_to_col(east, window);

    /* convert northing and easting to row and col, resp */
    row = (int)floor(frow - 0.5);
    col = (int)floor(fcol - 0.5);

    trow = frow - row - 0.5;
    tcol = fcol - col - 0.5;

    if (row < 0 || row + 1 >= Rast_window_rows() ||
	col < 0 || col + 1 >= Rast_window_cols()) {
	Rast_set_d_null_value(&result, 1);
	goto done;
    }

    Rast_get_d_row(fd, arow, row);
    Rast_get_d_row(fd, brow, row + 1);

    if (Rast_is_d_null_value(&arow[col]) ||
	Rast_is_d_null_value(&arow[col + 1]) ||
	Rast_is_d_null_value(&brow[col]) ||
	Rast_is_d_null_value(&brow[col + 1])) {
	Rast_set_d_null_value(&result, 1);
	goto done;
    }

    /*-
     * now were ready to do bilinear interpolation over
     * arow[col], arow[col+1],
     * brow[col], brow[col+1]
     */

    if (usedesc) {
	char *buf;

	G_squeeze(buf = Rast_get_c_cat((int *)&(arow[col]), cats));
	grid[0][0] = scancatlabel(buf);
	G_squeeze(buf = Rast_get_c_cat((CELL *) & (arow[col + 1]), cats));
	grid[0][1] = scancatlabel(buf);
	G_squeeze(buf = Rast_get_c_cat((CELL *) & (brow[col]), cats));
	grid[1][0] = scancatlabel(buf);
	G_squeeze(buf = Rast_get_c_cat((CELL *) & (brow[col + 1]), cats));
	grid[1][1] = scancatlabel(buf);
    }
    else {
	grid[0][0] = arow[col];
	grid[0][1] = arow[col + 1];
	grid[1][0] = brow[col];
	grid[1][1] = brow[col + 1];
    }

    result = Rast_interp_bilinear(tcol, trow,
				  grid[0][0], grid[0][1], grid[1][0],
				  grid[1][1]);

  done:
    G_free(arow);
    G_free(brow);

    return result;
}
Exemple #10
0
/* 
    Read ASCII file with point IDs and measurements.
    Store ID and X/Y/Z coordinates in the global double array 'pointTable'.
    Sort the array into a tree-like structure: first by skeleton ID, then all points
    of the same skeleton ID by bone ID, so the points are always "skull to toe" for
    each coherent skeleton.
    Any line the parser can make no sense of will be read as an invalid point.
*/
void createTable()
{
    int num_lines = 0;
    FILE *fp;
    char *line, *copy;
    int i, j;
    char *token;
    int num_tokens;
    char *id = NULL;
    char *id_token;
    char *clean_token;
    int skipped;
    
    
    fp = fopen ( file->answer, "r" );
    if ( fp == NULL ) {
        G_fatal_error ("Unable to open ASCII input file for reading");
    }
    
    line = G_malloc ( sizeof (char) * MAXSTR );
    
    while ( fgets ( line, MAXSTR, fp ) ) {
        num_lines ++;
    }
    
    if ( num_lines < 2 ) {
        G_fatal_error ("Input file too short (need at least two points)");
    }
    
    rewind ( fp );
    
    /* now that we know how many points to expect, 
       we can allocate the array to store the points */
    pointTable = G_malloc ( sizeof ( Point ) * num_lines );
    for ( i = 0; i < num_lines; i ++ ) {
        pointTable[i].ID = NULL;
        pointTable[i].invalid = 0;
    }
    
    G_message (_("Importing %i lines from input file:"), num_lines );
        
    /* split input lines into tokens and save data */
    i = -1;
    while ( fgets ( line, MAXSTR, fp ) ) {    	
        i ++;
    	num_tokens = 0;
	j = 1;
	G_squeeze ( line );
	/* skip empty lines and commented lines */
	if ( strlen ( line  ) > 0 && line[0] != '#' ) {
    	    token = strtok ( line, delimiter );
	    skipped = 0;	    
	} else {
	    token = NULL;
	    skipped = 1;
	}
	while ( token != NULL ) {
	    num_tokens ++;
	    /* point ID ? */
	    if ( j == ID_COL_POS ) { 
	    	if ( id != NULL ) {
		    free ( id );
		}
		clean_token = cleanString ( token );
		if ( clean_token == NULL ) {
                    G_warning (_("Invalid ID field on line %i"), i + 1);
                    pointTable[i].invalid = 1;		
		} else {
	    	    pointTable[i].ID = strdup ( clean_token );
		    G_free ( clean_token );
		}
	    }
	    /* X coordinate (Easting) ? */
	    if ( j == X_COL_POS ) {
	        clean_token = cleanString ( token );
		if ( clean_token == NULL ) {
                    G_warning ("Invalid X coordinate on line %i", i + 1);
                    pointTable[i].invalid = 1;		
		} else {
	            pointTable[i].X = atof ( clean_token );
		    G_free ( clean_token );
		    if ( pointTable[i].X == 0.0 ) {
                        G_warning (_("Invalid X coordinate (0.0) on line %i"), i + 1);
                        pointTable[i].invalid = 1;
                    }	    
		}
	    }
	    /* Y coordinate (Northing) ? */
	    if ( j == Y_COL_POS ) {
	        clean_token = cleanString ( token );
		if ( clean_token == NULL ) {
                    G_warning (_("Invalid Y coordinate on line %i"), i + 1);
                    pointTable[i].invalid = 1;		
		} else {
	            pointTable[i].Y = atof ( clean_token );
		    G_free ( clean_token );
		    if ( pointTable[i].Y == 0.0 ) {
                        G_warning (_("Invalid Y coordinate (0.0) on line %i"), i + 1);
                        pointTable[i].invalid = 1;
                    }
		} 
	    }
	    /* Z coordinate (Elevation) ? */
	    if ( j == Z_COL_POS ) {
	        clean_token = cleanString ( token );
		if ( clean_token == NULL ) {
                    G_warning (_("Invalid Z coordinate on line %i"), i + 1);
                    pointTable[i].invalid = 1;				
		} else {
	            pointTable[i].Z = atof ( clean_token );
		    G_free ( clean_token );
		    if ( pointTable[i].Z == 0.0 ) {
                        G_warning (_("Invalid Z coordinate (0.0) on line %i"), i + 1);
                        pointTable[i].invalid = 1;
                    }
		}
	    }	    
	    /* next field */
	    token = strtok ( NULL, delimiter );	    
	    j ++;
	}
	/* we need EXACTLY four fields in pre-defined order */
	if ( num_tokens < 4 ) {
	    if ( skipped == 0 ) {
	        G_warning (_("Wrong field count (needed 4, got %i) on line %i"), num_tokens, i + 1);
	    }
	    pointTable[i].invalid = 1;
	}
        G_free ( line );
        line = G_malloc ( sizeof (char) * MAXSTR );
	
	G_percent ( i, num_lines - 1, 10 );
    }
    numPoints = num_lines;

    /* split ID into BONE_ID and SKEL_ID */
    
    for ( i = 0; i < numPoints; i ++ ) {
        if ( pointTable[i].invalid == 0 ) {
            id = strdup ( pointTable[i].ID );
	    id_token = strtok ( id, "." );
	    if ( id_token == NULL ) {
	        G_warning (_("Invalid point ID on line %i"),i + 1);
	        pointTable[i].invalid = 1;
	    }
	    pointTable[i].SKEL_ID = atoi ( id_token );
	    if ( pointTable[i].SKEL_ID < 0 ) {
	        G_warning (_("Invalid (negative) point ID on line %i"),i + 1);
	        pointTable[i].invalid = 1;	
	    }
	    id_token = strtok ( NULL, "." );
	    if ( id_token == NULL ) {
	        G_warning (_("Invalid point ID on line %i"),i + 1);
	        pointTable[i].invalid = 1;
	    }
	    pointTable[i].BONE_ID = atoi ( id_token );
	    if ( pointTable[i].BONE_ID < 0 ) {
	        G_warning (_("Invalid (negative) point ID on line %i"),i + 1);
	        pointTable[i].invalid = 1;	
	    }	
	    id_token = strtok ( NULL, "." );
	    if ( id_token != NULL ) {
	        G_warning (_("Invalid point ID on line %i"),i + 1);
	        pointTable[i].invalid = 1;
	    }
            G_free ( id );
        }
    }

    if ( DEBUG ) {
        fprintf ( stderr, "createTable() DONE.\n" );
	dumpPts();
    }    
    
    fclose ( fp );
    
    G_message ("Sorting points and deleting duplicates:");
    
    /* prune invalid point records */
    pointTable = cleanPts();

    G_percent ( 1, 4, 1 );
    
    if ( DEBUG ) {
        fprintf ( stderr, "Prune invalid input file records DONE.\n" );
	dumpPts();
    }    
    
    /* sort global points array to make it a tree-like structure */
    qsort ( pointTable, numPoints, sizeof ( Point ), comparePts );

    G_percent ( 2, 4, 1 );

    if ( DEBUG ) {
        fprintf ( stderr, "Sort points DONE.\n" );
	dumpPts();
    }    

    /* prune invalid point records */
    pointTable = cleanPts();
    if ( DEBUG ) {
        fprintf ( stderr, "Pruning duplicate points DONE.\n" );
	dumpPts();
    }
    
    G_percent ( 3, 4, 1 );
    
    /* get max bone ID from remaining valid points */
    if ( numPoints > 0 ) {
        MAX_BONE_ID = pointTable[0].BONE_ID;
        for ( i = 0; i < numPoints; i ++ ) {
	    if ( pointTable[i].BONE_ID > MAX_BONE_ID ) {
	        MAX_BONE_ID = pointTable[i].BONE_ID;
	    }
        }
	MAX_BONE_ID = MAX_BONE_ID / 2;	
        /* map bone possible bone IDs to RGB values */ 
	if ( MAX_BONE_ID > 0 ) {
	    RGB_MAPPER_COLOUR = G_malloc ( sizeof ( int ) * MAX_BONE_ID );
            for ( i = 0; i < MAX_BONE_ID; i ++ ) {
		RGB_MAPPER_COLOUR[i] = RGBNUM_BONE;
		RGBNUM_BONE ++;
		if ( RGBNUM_BONE == RGBMAX ) {
		    RGBNUM_BONE = 0;
		}
            }	    	    
	}
    }
    
    G_percent ( 4, 4, 1 );

}
Exemple #11
0
int E_edit_cellhd(struct Cell_head *cellhd, int type)
{
    char ll_north[20];
    char ll_south[20];
    char ll_east[20];
    char ll_west[20];
    char ll_nsres[20];
    char ll_ewres[20];
    char ll_def_north[20];
    char ll_def_south[20];
    char ll_def_east[20];
    char ll_def_west[20];
    char ll_def_ewres[20];
    char ll_def_nsres[20];
    char projection[80];
    char **screen;

    struct Cell_head def_wind;
    double north, south, east, west;
    double nsres, ewres;
    char buf[64], buf2[30], *p;
    short ok;
    int line;
    char *prj;
    char *err;

    if (type == AS_CELLHD && (cellhd->rows <= 0 || cellhd->cols <= 0)) {
	G_message("E_edit_cellhd() - programmer error");
	G_message("  ** rows and cols must be positive **");
	return -1;
    }
    if (type != AS_DEF_WINDOW) {
	if (G_get_default_window(&def_wind) != 1)
	    return -1;

	if (cellhd->proj < 0) {
	    cellhd->proj = def_wind.proj;
	    cellhd->zone = def_wind.zone;
	}
	else if (cellhd->zone < 0)
	    cellhd->zone = def_wind.zone;
    }

    prj = G__projection_name(cellhd->proj);
    if (!prj)
	prj = "** unknown **";
    sprintf(projection, "%d (%s)", cellhd->proj, prj);

    if (type != AS_DEF_WINDOW) {
	if (cellhd->west >= cellhd->east || cellhd->south >= cellhd->north) {
	    cellhd->north = def_wind.north;
	    cellhd->south = def_wind.south;
	    cellhd->west = def_wind.west;
	    cellhd->east = def_wind.east;

	    if (type != AS_CELLHD) {
		cellhd->ew_res = def_wind.ew_res;
		cellhd->ns_res = def_wind.ns_res;
		cellhd->rows = def_wind.rows;
		cellhd->cols = def_wind.cols;
	    }
	}

	if (cellhd->proj != def_wind.proj) {
	    if (type == AS_CELLHD)
		G_message
		    ("header projection %d differs from default projection %d",
		     cellhd->proj, def_wind.proj);
	    else
		G_message
		    ("region projection %d differs from default projection %d",
		     cellhd->proj, def_wind.proj);

	    if (!G_yes("do you want to make them match? ", 1))
		return -1;

	    cellhd->proj = def_wind.proj;
	    cellhd->zone = def_wind.zone;
	}

	if (cellhd->zone != def_wind.zone) {
	    if (type == AS_CELLHD)
		G_message("header zone %d differs from default zone %d",
			  cellhd->zone, def_wind.zone);
	    else
		G_message("region zone %d differs from default zone %d",
			  cellhd->zone, def_wind.zone);

	    if (!G_yes("do you want to make them match? ", 1))
		return -1;

	    cellhd->zone = def_wind.zone;
	}

	*ll_def_north = 0;
	*ll_def_south = 0;
	*ll_def_east = 0;
	*ll_def_west = 0;
	*ll_def_ewres = 0;
	*ll_def_nsres = 0;
	format_northing(def_wind.north, ll_def_north, def_wind.proj);
	format_northing(def_wind.south, ll_def_south, def_wind.proj);
	format_easting(def_wind.east, ll_def_east, def_wind.proj);
	format_easting(def_wind.west, ll_def_west, def_wind.proj);
	format_resolution(def_wind.ew_res, ll_def_ewres, def_wind.proj);
	format_resolution(def_wind.ns_res, ll_def_nsres, def_wind.proj);
    }

    *ll_north = 0;
    *ll_south = 0;
    *ll_east = 0;
    *ll_west = 0;
    *ll_ewres = 0;
    *ll_nsres = 0;
    format_northing(cellhd->north, ll_north, cellhd->proj);
    format_northing(cellhd->south, ll_south, cellhd->proj);
    format_easting(cellhd->east, ll_east, cellhd->proj);
    format_easting(cellhd->west, ll_west, cellhd->proj);
    format_resolution(cellhd->ew_res, ll_ewres, cellhd->proj);
    format_resolution(cellhd->ns_res, ll_nsres, cellhd->proj);

    while (1) {
	ok = 1;

	/* List window options on the screen for the user to answer */
	switch (type) {
	case AS_CELLHD:
	    screen = cellhd_screen;
	    break;
	case AS_DEF_WINDOW:
	    screen = def_window_screen;
	    break;
	default:
	    screen = window_screen;
	    break;
	}

	V_clear();
	line = 0;
	while (*screen)
	    V_line(line++, *screen++);

	/* V_ques ( variable, type, row, col, length) ; */
	V_ques(ll_north, 's', 6, 36, 10);
	V_ques(ll_south, 's', 10, 36, 10);
	V_ques(ll_west, 's', 9, 12, 10);
	V_ques(ll_east, 's', 9, 52, 10);

	if (type != AS_CELLHD) {
	    V_ques(ll_ewres, 's', 18, 48, 10);
	    V_ques(ll_nsres, 's', 19, 48, 10);
	}

	if (type != AS_DEF_WINDOW) {
	    V_const(ll_def_north, 's', 3, 36, 10);
	    V_const(ll_def_south, 's', 13, 36, 10);
	    V_const(ll_def_west, 's', 9, 1, 10);
	    V_const(ll_def_east, 's', 9, 65, 10);

	    if (type != AS_CELLHD) {
		V_const(ll_def_ewres, 's', 18, 21, 10);
		V_const(ll_def_nsres, 's', 19, 21, 10);
	    }
	}

	V_const(projection, 's', 15, 23, (int)strlen(projection));
	V_const(&cellhd->zone, 'i', 15, 60, 3);

	V_intrpt_ok();
	if (!V_call())
	    return -1;

	G_squeeze(ll_north);
	G_squeeze(ll_south);
	G_squeeze(ll_east);
	G_squeeze(ll_west);

	if (type != AS_CELLHD) {
	    G_squeeze(ll_ewres);
	    G_squeeze(ll_nsres);
	}

	if (!G_scan_northing(ll_north, &cellhd->north, cellhd->proj)) {
	    G_warning("Illegal value for north: %s", ll_north);
	    ok = 0;
	}

	if (!G_scan_northing(ll_south, &cellhd->south, cellhd->proj)) {
	    G_warning("Illegal value for south: %s", ll_south);
	    ok = 0;
	}

	if (!G_scan_easting(ll_east, &cellhd->east, cellhd->proj)) {
	    G_warning("Illegal value for east: %s", ll_east);
	    ok = 0;
	}

	if (!G_scan_easting(ll_west, &cellhd->west, cellhd->proj)) {
	    G_warning("Illegal value for west: %s", ll_west);
	    ok = 0;
	}

	if (type != AS_CELLHD) {
	    if (!G_scan_resolution(ll_ewres, &cellhd->ew_res, cellhd->proj)) {
		G_warning("Illegal east-west resolution: %s", ll_ewres);
		ok = 0;
	    }

	    if (!G_scan_resolution(ll_nsres, &cellhd->ns_res, cellhd->proj)) {
		G_warning("Illegal north-south resolution: %s", ll_nsres);
		ok = 0;
	    }
	}

	if (!ok) {
	    hitreturn();
	    continue;
	}

	/* Adjust and complete the cell header */
	north = cellhd->north;
	south = cellhd->south;
	east = cellhd->east;
	west = cellhd->west;
	nsres = cellhd->ns_res;
	ewres = cellhd->ew_res;

	if ((err =
	     G_adjust_Cell_head(cellhd, type == AS_CELLHD,
				type == AS_CELLHD))) {
	    G_message("%s", err);
	    hitreturn();
	    continue;
	}

	if (type == AS_CELLHD) {
	    nsres = cellhd->ns_res;
	    ewres = cellhd->ew_res;
	}

      SHOW:
	fprintf(stderr, "\n\n");
	G_message("  projection:   %s", projection);
	G_message("  zone:         %d", cellhd->zone);

	G_format_northing(cellhd->north, buf, cellhd->proj);
	G_format_northing(north, buf2, cellhd->proj);
	fprintf(stderr, "  north:       %s", buf);

	if (strcmp(buf, buf2) != 0) {
	    ok = 0;
	    fprintf(stderr, "  (Changed to match resolution)");
	}
	fprintf(stderr, "\n");

	G_format_northing(cellhd->south, buf, cellhd->proj);
	G_format_northing(south, buf2, cellhd->proj);
	fprintf(stderr, "  south:       %s", buf);
	if (strcmp(buf, buf2) != 0) {
	    ok = 0;
	    fprintf(stderr, "  (Changed to match resolution)");
	}
	fprintf(stderr, "\n");

	G_format_easting(cellhd->east, buf, cellhd->proj);
	G_format_easting(east, buf2, cellhd->proj);
	fprintf(stderr, "  east:        %s", buf);
	if (strcmp(buf, buf2) != 0) {
	    ok = 0;
	    fprintf(stderr, "  (Changed to match resolution)");
	}
	fprintf(stderr, "\n");

	G_format_easting(cellhd->west, buf, cellhd->proj);
	G_format_easting(west, buf2, cellhd->proj);
	fprintf(stderr, "  west:        %s", buf);
	if (strcmp(buf, buf2) != 0) {
	    ok = 0;
	    fprintf(stderr, "  (Changed to match resolution)");
	}
	fprintf(stderr, "\n\n");

	G_format_resolution(cellhd->ew_res, buf, cellhd->proj);
	G_format_resolution(ewres, buf2, cellhd->proj);
	fprintf(stderr, "  e-w res:     %s", buf);
	if (strcmp(buf, buf2) != 0) {
	    ok = 0;
	    fprintf(stderr, "  (Changed to conform to grid)");
	}
	fprintf(stderr, "\n");

	G_format_resolution(cellhd->ns_res, buf, cellhd->proj);
	G_format_resolution(nsres, buf2, cellhd->proj);
	fprintf(stderr, "  n-s res:     %s", buf);
	if (strcmp(buf, buf2) != 0) {
	    ok = 0;
	    fprintf(stderr, "  (Changed to conform to grid)");
	}
	fprintf(stderr, "\n\n");

	G_message("  total rows:  %15d", cellhd->rows);
	G_message("  total cols:  %15d", cellhd->cols);

	sprintf(buf, "%lf", (double)cellhd->rows * cellhd->cols);
	*(p = strchr(buf, '.')) = 0;
	G_insert_commas(buf);
	G_message("  total cells: %15s", buf);
	fprintf(stderr, "\n");

	if (type != AS_DEF_WINDOW) {
	    if (cellhd->north > def_wind.north) {
		G_warning("north falls outside the default region");
		ok = 0;
	    }

	    if (cellhd->south < def_wind.south) {
		G_warning("south falls outside the default region");
		ok = 0;
	    }

	    if (cellhd->proj != PROJECTION_LL) {
		if (cellhd->east > def_wind.east) {
		    G_warning("east falls outside the default region");
		    ok = 0;
		}

		if (cellhd->west < def_wind.west) {
		    G_warning("west falls outside the default region");
		    ok = 0;
		}
	    }
	}

      ASK:
	fflush(stdin);
	if (type == AS_CELLHD)
	    fprintf(stderr, "\nDo you accept this header? (y/n) [%s] > ",
		    ok ? "y" : "n");
	else
	    fprintf(stderr, "\nDo you accept this region? (y/n) [%s] > ",
		    ok ? "y" : "n");

	if (!G_gets(buf))
	    goto SHOW;

	G_strip(buf);
	switch (*buf) {
	case 0:
	    break;
	case 'y':
	case 'Y':
	    ok = 1;
	    break;
	case 'n':
	case 'N':
	    ok = 0;
	    break;
	default:
	    goto ASK;
	}

	if (ok)
	    return 0;
    }
}