Browse Source

move north arrow code from d.barscale into its own module

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@57715 15284696-431f-4ddb-bdfa-cd5b030d7da7
Hamish Bowman 11 years ago
parent
commit
aa7cebb6ce

+ 1 - 0
display/Makefile

@@ -20,6 +20,7 @@ SUBDIRS = \
 	d.legend \
 	d.linegraph \
 	d.mon \
+	d.northarrow \
 	d.path \
 	d.profile \
 	d.rast \

+ 10 - 0
display/d.northarrow/Makefile

@@ -0,0 +1,10 @@
+MODULE_TOPDIR = ../..
+
+PGM = d.northarrow
+
+LIBES = $(DISPLAYLIB) $(SYMBLIB) $(GISLIB)
+DEPENDENCIES = $(DISPLAYDEP) $(SYMBDEP) $(GISDEP)
+
+include $(MODULE_TOPDIR)/include/Make/Module.make
+
+default: cmd

+ 29 - 0
display/d.northarrow/d.northarrow.html

@@ -0,0 +1,29 @@
+<h2>DESCRIPTION</h2>
+ 
+<em>d.northarrow</em> displays a north arrow symbol the graphics monitor at
+the given screen coordinates. If no coordinates are given it will draw the
+barscale in the bottom right of the display. It can draw the north arrow
+in a number of styles (see the
+ <a href="http://grasswiki.osgeo.org/wiki/Cartography#Display_monitors">wiki
+page</a> for details).
+
+
+<h2>SEE ALSO</h2>
+
+<em>
+<a href="d.barscale.html">d.barscale</a>,
+<a href="d.graph.html">d.graph</a>,
+<a href="d.grid.html">d.grid</a>,
+<a href="d.legend.html">d.legend</a>
+</em>
+
+
+<h2>AUTHOR</h2>
+
+Hamish Bowman<br> <i>
+Department of Geology<br>
+University of Otago<br>
+New Zealand</i><br>
+
+<p>
+<i>Last changed: $Date$</i>

+ 144 - 0
display/d.northarrow/draw_n_arrow.c

@@ -0,0 +1,144 @@
+/*
+ * draw_n_arrow() places a north arrow somewhere in the display frame
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/display.h>
+#include <grass/symbol.h>
+#include <grass/colors.h>
+#include <grass/glocale.h>
+#include "options.h"
+
+int draw_n_arrow(double east, double north, double fontsize, char *n_arrow_num)
+{
+    double x_pos, y_pos;
+    double t, b, l, r;
+    double tt, tb, tl, tr; /* text box*/
+
+    SYMBOL *Symb;
+    RGBA_Color *line_color, *fill_color;
+    int R, G, B;
+    double x0, y0;
+    char icon[64];
+    double symbol_size;
+
+
+    /* Establish text size */
+    if (fontsize > 0)
+	D_text_size(fontsize, fontsize);
+
+    D_setup_unity(0);
+    D_get_src(&t, &b, &l, &r);
+
+    x_pos = east * (r - l) / 100.;
+    y_pos = (100. - north) * (b - t) / 100.;
+
+    if (fontsize > 0) {
+    	/* draw the "N" */
+    	D_get_text_box("N", &tt, &tb, &tl, &tr);
+    	D_use_color(fg_color);
+
+    	/* positions manually tuned */
+    	switch (n_arrow_num[0]) {
+    	case '1':
+    	    D_pos_abs(x_pos - (tr + tl) / 2, y_pos - 45);
+    	    D_text("N");
+    	    break;
+    	case '3':
+    	    D_pos_abs(x_pos - (tr + tl) / 2, y_pos - 60);
+    	    D_text("N");
+    	    break;
+    	case '4':
+    	    D_pos_abs(x_pos - (tr + tl) / 2, y_pos - 45);
+    	    D_text("N");
+    	    break;
+    	case '7':
+    	    D_pos_abs(x_pos - (tr + tl) / 2, y_pos - 70);
+    	    D_text("N");
+    	    break;
+    	case '9':
+    	    D_pos_abs(x_pos - (tr + tl) / 2, y_pos - 55);
+    	    D_text("N");
+    	case '2':
+    	case '5':
+    	case '6':
+    	case '8':
+    	    break;
+    	default:
+    	    G_fatal_error(_("Could not parse symbol"));
+    	}
+    }
+
+    /* display the north arrow symbol */
+    line_color = G_malloc(sizeof(RGBA_Color));
+    fill_color = G_malloc(sizeof(RGBA_Color));
+
+    if (D_color_number_to_RGB(fg_color, &R, &G, &B) == 0)
+    	/* fall back to black on failure */
+    	G_str_to_color(DEFAULT_FG_COLOR, &R, &G, &B);
+    line_color->r = (unsigned char)R;
+    line_color->g = (unsigned char)G;
+    line_color->b = (unsigned char)B;
+    line_color->a = RGBA_COLOR_OPAQUE;
+
+    if (D_color_number_to_RGB(fg_color, &R, &G, &B) == 0)
+    	/* fall back to black on failure */
+    	G_str_to_color(DEFAULT_FG_COLOR, &R, &G, &B);
+    fill_color->r = (unsigned char)R;
+    fill_color->g = (unsigned char)G;
+    fill_color->b = (unsigned char)B;
+    fill_color->a = RGBA_COLOR_OPAQUE;
+
+    if (n_arrow_num[0] == '2' || n_arrow_num[0] == '9')
+    	fill_color->a = RGBA_COLOR_TRANSPARENT;
+
+    /* sizes manually tuned */
+    switch (n_arrow_num[0]) {
+    case '1':
+    	symbol_size = 35.;
+    	break;
+    case '2':
+    	symbol_size = 19.;
+    	break;
+    case '3':
+    	symbol_size = 20.;
+    	break;
+    case '4':
+    	symbol_size = 15.;
+    	break;
+    case '5':
+    case '6':
+    	symbol_size = 14.;
+    	break;
+    case '7':
+    	symbol_size = 23.;
+    	break;
+    case '8':
+    case '9':
+    	symbol_size = 17.;
+    	break;
+    default:
+    	G_fatal_error(_("Could not parse symbol"));
+    }
+
+    x0 = D_d_to_u_col(x_pos);
+    y0 = D_d_to_u_row(y_pos);
+
+    strcpy(icon, "n_arrows/n_arrow");
+    strncat(icon, n_arrow_num, 32);
+    Symb = S_read(icon);
+
+    if(!Symb)
+    	G_fatal_error(_("Could not read symbol \"%s\""), icon);
+
+    S_stroke(Symb, symbol_size, 0.0, 0);
+    D_symbol(Symb, x0, y0, line_color, fill_color);
+
+    G_free(Symb);
+    G_free(line_color);
+    G_free(fill_color);
+
+    return 0;
+}

+ 115 - 0
display/d.northarrow/main.c

@@ -0,0 +1,115 @@
+/****************************************************************************
+ *
+ * MODULE:       d.northarrow
+ *
+ * AUTHOR(S):    Hamish Bowman, Dunedin, NZ <hamish_b yahoo.com>
+ *
+ * PURPOSE:      Displays a north arrow on graphics monitor
+ *
+ * COPYRIGHT:    (C) 2013 by the GRASS Development Team
+ *
+ *               This program is free software under the GNU General Public
+ *               License (>=v2). Read the file COPYING that comes with GRASS
+ *               for details.
+ *
+ *****************************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/display.h>
+#include <grass/glocale.h>
+#include "options.h"
+
+int fg_color, bg_color;
+int do_background = TRUE;
+
+int main(int argc, char **argv)
+{
+    struct GModule *module;
+    struct Option *bg_color_opt, *fg_color_opt, *coords, *n_arrow, *fsize;
+    struct Flag *no_text;
+    double east, north;
+    double fontsize;
+
+    /* Initialize the GIS calls */
+    G_gisinit(argv[0]);
+
+    module = G_define_module();
+    G_add_keyword(_("display"));
+    G_add_keyword(_("cartography"));
+    module->description = _("Displays a north arrow on the graphics monitor.");
+
+    n_arrow = G_define_option();
+    n_arrow->key = "style";
+    n_arrow->description = _("North arrow style (used only with the -n flag)");
+    n_arrow->options = "1a,1b,2,3,4,5,6,7a,7b,8a,8b,9";
+    n_arrow->answer = "1a";
+    n_arrow->guisection = _("Style");
+
+    coords = G_define_option();
+    coords->key = "at";
+    coords->key_desc = "x,y";
+    coords->type = TYPE_DOUBLE;
+    coords->answer = "85.0,15.0";
+    coords->options = "0-100";
+    coords->label =
+	_("Screen coordinates of the rectangle's top-left corner");
+    coords->description = _("(0,0) is lower-left of the display frame");
+
+    fg_color_opt = G_define_standard_option(G_OPT_C_FG);
+    fg_color_opt->label = _("North arrow color");
+    fg_color_opt->guisection = _("Colors");
+
+    bg_color_opt = G_define_standard_option(G_OPT_C_BG);
+    bg_color_opt->label = _("Background color");
+    bg_color_opt->guisection = _("Colors");
+
+    fsize = G_define_option();
+    fsize->key = "fontsize";
+    fsize->type = TYPE_DOUBLE;
+    fsize->required = NO;
+    fsize->answer = "12";
+    fsize->options = "1-360";
+    fsize->description = _("Font size");
+
+    no_text = G_define_flag();
+    no_text->key = 't';
+    no_text->description = _("Draw the symbol without text");
+
+    if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+
+
+    sscanf(coords->answers[0], "%lf", &east);
+    sscanf(coords->answers[1], "%lf", &north);
+
+    fontsize = atof(fsize->answer);
+    if (no_text->answer)
+	fontsize = -1;
+
+    /* Parse and select foreground color */
+    fg_color = D_parse_color(fg_color_opt->answer, 0);
+
+    /* Parse and select background color */
+    bg_color = D_parse_color(bg_color_opt->answer, 1);
+    if (bg_color == 0)
+	do_background = FALSE;
+
+
+    if (D_open_driver() != 0)
+	G_fatal_error(_("No graphics device selected. "
+			"Use d.mon to select graphics device."));
+    D_setup(0);
+
+
+    draw_n_arrow(east, north, fontsize, n_arrow->answer);
+
+
+    D_save_command(G_recreate_command());
+    D_close_driver();
+
+    exit(EXIT_SUCCESS);
+}

+ 8 - 0
display/d.northarrow/options.h

@@ -0,0 +1,8 @@
+
+/* globals */
+extern int fg_color;
+extern int bg_color;
+extern int do_background;
+
+/* draw_n_arrow.c */
+int draw_n_arrow(double, double, double, char *);