[GRASS-SVN] r56841 - grass/trunk/display/d.barscale
svn_grass at osgeo.org
svn_grass at osgeo.org
Thu Jun 20 08:18:34 PDT 2013
Author: hamish
Date: 2013-06-20 08:18:33 -0700 (Thu, 20 Jun 2013)
New Revision: 56841
Modified:
grass/trunk/display/d.barscale/Makefile
grass/trunk/display/d.barscale/d.barscale.html
grass/trunk/display/d.barscale/draw_scale.c
grass/trunk/display/d.barscale/main.c
grass/trunk/display/d.barscale/options.h
Log:
major rewrite and expansion
Modified: grass/trunk/display/d.barscale/Makefile
===================================================================
--- grass/trunk/display/d.barscale/Makefile 2013-06-20 15:02:57 UTC (rev 56840)
+++ grass/trunk/display/d.barscale/Makefile 2013-06-20 15:18:33 UTC (rev 56841)
@@ -2,8 +2,8 @@
PGM = d.barscale
-LIBES = $(DISPLAYLIB) $(GISLIB)
-DEPENDENCIES = $(DISPLAYDEP) $(GISDEP)
+LIBES = $(DISPLAYLIB) $(SYMBLIB) $(GISLIB)
+DEPENDENCIES = $(DISPLAYDEP) $(SYMBDEP) $(GISDEP)
include $(MODULE_TOPDIR)/include/Make/Module.make
Modified: grass/trunk/display/d.barscale/d.barscale.html
===================================================================
--- grass/trunk/display/d.barscale/d.barscale.html 2013-06-20 15:02:57 UTC (rev 56840)
+++ grass/trunk/display/d.barscale/d.barscale.html 2013-06-20 15:18:33 UTC (rev 56841)
@@ -1,8 +1,9 @@
<h2>DESCRIPTION</h2>
-<em>d.barscale</em> displays a barscale on the graphics monitor using either
-the mouse or given screen coordinates. It can draw the scale in a couple of
-styles or simply draw a north arrow.
+<em>d.barscale</em> displays a barscale on the graphics monitor
+at the given screen coordinates. If no coordinates are given it
+will draw the barscale in the bottom left of the display.
+It can draw the scale or north arrow in a number of styles.
<h2>NOTE</h2>
@@ -10,9 +11,6 @@
<em>d.barscale</em> will not work in Lat/Lon locations as the horizontal
scale distance changes with latitude. Try <em>d.grid</em> instead.
-<p>The -l flag will always draw a north arrow <em>and</em> a line scale
-together; it is currently not possible to draw a line scale without a north
-arrow. Instead, the -s flag can be used to draw a barscale only.
<h2>SEE ALSO</h2>
@@ -20,13 +18,15 @@
<a href="d.graph.html">d.graph</a><br>
<a href="d.grid.html">d.grid</a><br>
<a href="d.legend.html">d.legend</a><br>
-<a href="d.measure.html">d.measure</a><br>
-<a href="d.where.html">d.where</a><br>
<a href="g.region.html">g.region</a><br>
</em>
<h2>AUTHOR</h2>
-unknown.
-<p><i>Last changed: $Date$</i>
+unknown, but from USACE/CERL.<br>
+Major rewrite for GRASS 7 by Hamish Bowman
+
+
+<p>
+<i>Last changed: $Date$</i>
Modified: grass/trunk/display/d.barscale/draw_scale.c
===================================================================
--- grass/trunk/display/d.barscale/draw_scale.c 2013-06-20 15:02:57 UTC (rev 56840)
+++ grass/trunk/display/d.barscale/draw_scale.c 2013-06-20 15:18:33 UTC (rev 56841)
@@ -1,11 +1,14 @@
/*
- * draw_scale places a scale in the upper left hand corner of a map image
+ * draw_scale() places a scalebar or 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"
#define NUMSCALES 16
@@ -37,7 +40,7 @@
{"5000 km", 5000000., 20000000., 5},
{"10000 km", 10000000., 70000000., 10}
},
- {/* feet/miles */
+ { /* feet/miles */
{"", 0.000, 1., 10},
{"1 foot", 0.305, 2., 10},
{"5 feet", 1.524, 10., 5},
@@ -57,70 +60,141 @@
}
};
-int draw_scale(int toptext, int size)
+int draw_scale(double east, double north, int style, int text_posn,
+ double fontsize, char *n_arrow_num)
{
double meters;
double line_len;
- int incr;
- int x_pos, y_pos;
+ int i, incr;
+ double x_pos, y_pos;
double t, b, l, r;
- int pt, pb, pl, pr;
- int i;
+ double pt, pb, pl, pr; /* background box */
+ double tt, tb, tl, tr; /* text box*/
double xarr[5], yarr[5];
double seg_len;
const struct scale *scales = all_scales[use_feet];
+ SYMBOL *Symb;
+ RGBA_Color *line_color, *fill_color;
+ int R, G, B;
+ double x0, y0;
+ char icon[64];
+ double symbol_size;
/* Establish text size */
- D_text_size(size, size);
+ if (fontsize > 0)
+ D_text_size(fontsize, fontsize);
D_setup_unity(0);
D_get_src(&t, &b, &l, &r);
- x_pos = (int)(east * (r - l) / 100.);
- y_pos = (int)((100. - north) * (b - t) / 100.);
+ x_pos = east * (r - l) / 100.;
+ y_pos = (100. - north) * (b - t) / 100.;
- if (draw == 1) {
- int w, h;
- w = 30;
- h = 17 + 2 * w;
+ if (style == STYLE_NONE) {
+ /* draw north arrow only */
- pl = x_pos;
- pt = y_pos;
- pr = x_pos + w + 2; /* 1 pixel margin for both sides */
- pb = y_pos + h + 2; /* 1 pixel margin for both sides */
+ if (fontsize > 0) {
+ /* draw the "N" */
+ D_get_text_box("N", &tt, &tb, &tl, &tr);
+ D_use_color(fg_color);
- if (do_background) {
- D_use_color(color1);
- D_box_abs(pl, pt, pr, pb);
+ /* 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 '2':
+ case '5':
+ case '6':
+ case '8':
+ break;
+ default:
+ G_fatal_error(_("Could not parse symbol"));
+ }
}
- /* Draw legend */
- D_use_color(color2);
- D_begin();
+ /* display the symbol */
+ line_color = G_malloc(sizeof(RGBA_Color));
+ fill_color = G_malloc(sizeof(RGBA_Color));
- D_move_abs(pl + w / 2 + 1, pt + 17 + 1);
- D_cont_rel(-0.5 * w, 2.0 * w);
- D_cont_rel( 0.5 * w, -0.5 * w);
- D_close();
+ 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;
- D_move_abs(pl + w / 2 + 1, pt + 17 + 1);
- D_cont_rel( 0.5 * w, 2.0 * w);
- D_cont_rel(-0.5 * w, -0.5 * w);
- D_close();
+ 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;
- D_end();
- D_stroke();
+ if (n_arrow_num[0] == '2')
+ fill_color->a = RGBA_COLOR_TRANSPARENT;
- /* actual text width is 81% of size? from d.legend */
- D_pos_abs(pl + w / 2 - 7 * .81, pt + 14);
- D_text("N");
+ /* 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':
+ 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);
+ S_stroke(Symb, symbol_size, 0.0, 0);
+ D_symbol(Symb, x0, y0, line_color, fill_color);
+
+ G_free(line_color);
+ G_free(fill_color);
+
return 0;
}
- D_setup(0);
+ D_setup(0); /* back to regular coordinate settings */
meters = D_get_u_east() - D_get_u_west();
meters *= G_database_units_to_meters_factor();
@@ -131,48 +205,119 @@
}
if (!incr)
- return (-1);
+ return -1; /* use a G_fatal_error() here? */
line_len = D_get_u_to_d_xconv() * scales[incr].size
/ G_database_units_to_meters_factor();
seg_len = line_len / scales[incr].seg;
/* work around round off */
- line_len = ((int)seg_len) * scales[incr].seg;
+ line_len = seg_len * scales[incr].seg;
D_setup_unity(0);
- /* Blank out area with background color */
- if (toptext) {
- pr = x_pos + 35 + (int)line_len;
- pt = y_pos - 15;
+ if (do_background) {
+ /* Blank out area with background color */
+ D_get_text_box(scales[incr].name, &tt, &tb, &tl, &tr);
+
+ if (text_posn == TEXT_OVER) {
+ pr = x_pos + 35 + line_len;
+ pl = x_pos + 0;
+ pt = y_pos + tb - 5;
+ pb = y_pos + 30;
+ if (style != STYLE_CLASSIC_BAR && style != STYLE_THIN_WITH_ENDS)
+ pl += 15;
+ if (style == STYLE_TICKS_DOWN)
+ pb += 12;
+ }
+ if (text_posn == TEXT_UNDER) {
+ pr = x_pos + 35 + line_len;
+ pl = x_pos + 0;
+ pt = y_pos + 0;
+ pb = y_pos + 30 - tb + 5;
+ if (style != STYLE_CLASSIC_BAR && style != STYLE_THIN_WITH_ENDS)
+ pl += 15;
+ if (style == STYLE_TICKS_UP)
+ pt -= 12;
+ }
+ else if (text_posn == TEXT_RIGHT){
+ pr = x_pos + 35 + line_len + tr + 5;
+ pl = x_pos + 0;
+ pt = y_pos + 0;
+ pb = y_pos + 30;
+ if (style == STYLE_TICKS_UP) {
+ pt -= 12;
+ pl += 15;
+ }
+ if (style == STYLE_TICKS_DOWN) {
+ pb += 12;
+ pl += 15;
+ }
+ }
+ else if (text_posn == TEXT_LEFT){
+ pr = x_pos + 35 + line_len;
+ pl = x_pos - tr - 13;
+ pt = y_pos + 0;
+ pb = y_pos + 30;
+ if (style == STYLE_TICKS_UP)
+ pt -= 12;
+ if (style == STYLE_TICKS_DOWN)
+ pb += 11;
+ }
+
+ if (fontsize < 0) { /* no text */
+ switch (style) {
+ case STYLE_CLASSIC_BAR:
+ case STYLE_THIN_WITH_ENDS:
+ pr = x_pos + 35 + line_len;
+ pl = x_pos + 0;
+ pt = y_pos + 0;
+ pb = y_pos + 30;
+ break;
+ case STYLE_PART_CHECKER:
+ case STYLE_FULL_CHECKER:
+ case STYLE_SOLID_BAR:
+ case STYLE_HOLLOW_BAR:
+ case STYLE_TICKS_BOTH:
+ case STYLE_ARROW_ENDS:
+ pr = x_pos + 35 + line_len;
+ pl = x_pos + 15;
+ pt = y_pos + 0;
+ pb = y_pos + 30;
+ break;
+ case STYLE_TICKS_UP:
+ pr = x_pos + 35 + line_len;
+ pl = x_pos + 15;
+ pt = y_pos - 12;
+ pb = y_pos + 25;
+ break;
+ case STYLE_TICKS_DOWN:
+ pr = x_pos + 35 + line_len;
+ pl = x_pos + 15;
+ pt = y_pos + 3;
+ pb = y_pos + 40;
+ break;
+ default:
+ G_fatal_error(_("Programmer error"));
+ }
+ }
+
+ /* keep it on the screen */
if (pt < t)
pt = t;
- }
- else {
- pr = x_pos + 35 + (int)line_len + size * strlen(scales[incr].name);
- pt = y_pos + 0;
- if (pt < t)
- pt = t;
- }
- pb = y_pos + 30;
- if (pb > b)
- pb = b;
- pl = x_pos + 0;
- if (pl < l)
- pl = l;
- pr = pr;
- if (pr > r)
- pr = r;
+ if (pb > b)
+ pb = b;
+ if (pl < l)
+ pl = l;
+ if (pr > r)
+ pr = r;
- if (do_background) {
- D_use_color(color1);
+ D_use_color(bg_color);
D_box_abs(pl, pt, pr, pb);
}
- /* Draw legend */
- D_use_color(color2);
-
- if (draw != 2) {
+ /* Draw the small N with an arrow through it on the left side */
+ D_use_color(fg_color);
+ if (style == STYLE_CLASSIC_BAR || style == STYLE_THIN_WITH_ENDS) {
D_begin();
D_move_abs(x_pos + 5, y_pos + 20);
D_cont_rel(0, -10);
@@ -180,44 +325,24 @@
D_cont_rel(0, -10);
D_move_rel(-5, 14);
D_cont_rel(0, -17);
- D_cont_rel(-2, -0);
- D_cont_rel(2, -2);
- D_cont_rel(2, 2);
- D_cont_rel(-2, -0);
+ D_cont_rel(-2.5, -0);
+ D_cont_rel(2.5, -4);
+ D_cont_rel(2.5, 4);
+ D_cont_rel(-2.5, -0);
+ D_close();
D_end();
D_stroke();
}
- if (draw == 2) {
+ /* The end points of the center-line are (x_pos + 25, y_pos + 15)
+ and (x_pos + 25 + line_len, y_pos + 15) */
+ if (style == STYLE_CLASSIC_BAR) {
D_begin();
- D_move_abs(x_pos + 25 - draw * 10, y_pos + 17);
- /* actual width is line_len-1+1=line_len and height is 7+1=8 */
- D_cont_rel(line_len - 1, 0);
- D_cont_rel(0, -7);
- D_cont_rel(line_len * -1 + 1, 0);
- D_cont_rel(0, 7);
- D_end();
- D_stroke();
-
- D_pos_rel(0, 1 - 4);
- for (i = 1; i <= scales[incr].seg; i++) {
- xarr[0] = 0; yarr[0] = 0;
- xarr[1] = (int)seg_len; yarr[1] = 0;
- xarr[2] = 0; yarr[2] = (i % 2 ? -4 : 4);
- xarr[3] = -(int)seg_len; yarr[3] = 0;
- xarr[4] = 0; yarr[4] = (i % 2 ? 4 : -4);
- /* width is seg_len and height is 4 */
- D_polygon_rel(xarr, yarr, 5);
- D_pos_rel((int)seg_len, 0);
- }
- }
- else if (do_bar) {
- D_begin();
D_move_abs(x_pos + 25, y_pos + 17);
/* actual width is line_len-1+1=line_len and height is 4+1=5 */
- D_cont_rel((int)line_len - 1, 0);
+ D_cont_rel(line_len - 1, 0);
D_cont_rel(0, -4);
- D_cont_rel((int)(line_len * -1 + 1), 0);
+ D_cont_rel(-line_len + 1, 0);
D_cont_rel(0, 4);
D_end();
D_stroke();
@@ -225,11 +350,12 @@
D_pos_rel(0, 1);
for (i = 1; i <= scales[incr].seg; i += 2) {
/* width is seg_len and height is 5 */
- D_box_rel((int)seg_len, -5);
- D_pos_rel((int)seg_len * 2, 0);
+ D_box_rel(seg_len, -5);
+ D_pos_rel(seg_len * 2, 0);
}
}
- else { /* draw simple line scale */
+ else if (style == STYLE_THIN_WITH_ENDS) {
+ /* draw simple line scale */
D_begin();
D_move_abs(x_pos + 25, y_pos + 5);
D_cont_abs(x_pos + 25, y_pos + 25);
@@ -237,21 +363,142 @@
D_cont_abs(x_pos + 25 + line_len, y_pos + 15);
D_move_abs(x_pos + 25 + line_len, y_pos + 5);
D_cont_abs(x_pos + 25 + line_len, y_pos + 25);
- D_end();
+ D_close();
+ D_end(); /* no-op? */
+ }
+ else if (style == STYLE_SOLID_BAR) {
+ /* draw simple solid-bar scale */
+ xarr[0] = 0;
+ yarr[0] = +8;
+ xarr[1] = line_len;
+ yarr[1] = 0;
+ xarr[2] = 0;
+ yarr[2] = -8;
+ xarr[3] = -line_len;
+ yarr[3] = 0;
+ xarr[4] = 0;
+ yarr[4] = +8;
+
+ D_move_abs(x_pos + 25, y_pos + 15 - 4);
+ D_polygon_rel(xarr, yarr, 5);
+ }
+ else if (style == STYLE_HOLLOW_BAR) {
+ /* draw hollow-bar scale */
+ D_use_color(fg_color);
+ D_begin();
+ D_move_abs(x_pos + 25, y_pos + 15 - 4);
+ D_cont_rel(0, +8);
+ D_cont_rel(line_len, 0);
+ D_cont_rel(0, -8);
+ D_cont_rel(-line_len, 0);
+ D_cont_rel(0, +8);
+ D_close();
+ D_end(); /* no-op? */
+ }
+ else if (style == STYLE_FULL_CHECKER) {
+ D_begin();
+ D_move_abs(x_pos + 25, y_pos + 15 + 6);
+ /* actual width is line_len-1+1=line_len and height is 7+1=8 */
+ D_cont_rel(line_len, 0);
+ D_cont_rel(0, -12);
+ D_cont_rel(-line_len, 0);
+ D_cont_rel(0, +12);
+ D_close();
+ D_end(); /* no-op? */
D_stroke();
+
+ D_pos_rel(0, -6);
+ for (i = 1; i <= scales[incr].seg; i++) {
+ xarr[0] = 0; yarr[0] = 0;
+ xarr[1] = seg_len; yarr[1] = 0;
+ xarr[2] = 0; yarr[2] = (i % 2 ? -6 : 6);
+ xarr[3] = -seg_len; yarr[3] = 0;
+ xarr[4] = 0; yarr[4] = (i % 2 ? 6 : -6);
+ /* width is seg_len and height is 4 */
+ D_polygon_rel(xarr, yarr, 5);
+ D_pos_rel(seg_len, 0);
+ }
}
+ else if (style == STYLE_TICKS_BOTH) {
+ /* draw simple line scale with corssing ticks */
+ D_move_abs(x_pos + 25, y_pos + 5);
+ D_cont_abs(x_pos + 25, y_pos + 25);
+ D_move_abs(x_pos + 25, y_pos + 15);
+ D_cont_abs(x_pos + 25 + line_len, y_pos + 15);
+ D_move_abs(x_pos + 25 + line_len, y_pos + 5);
+ D_cont_abs(x_pos + 25 + line_len, y_pos + 25);
- if (toptext) {
- D_pos_abs(x_pos + 25 - draw * 10 + line_len / 2.
- - strlen(scales[incr].name) * size * 0.81 / 2,
+ D_move_abs(x_pos + 25, y_pos + 15);
+ D_move_rel(0, +6);
+ for (i = 0; i <= scales[incr].seg - 2; i++) {
+ D_move_rel(seg_len, 0);
+ D_cont_rel(0, -11); /* 5 above, on px on line, and 5 below */
+ D_move_rel(0, +11);
+ }
+ }
+ else if (style == STYLE_TICKS_UP) {
+ /* draw simple line scale with up facing ticks */
+ D_begin();
+ D_move_abs(x_pos + 25, y_pos - 2);
+ D_cont_abs(x_pos + 25, y_pos + 15);
+ D_cont_abs(x_pos + 25 + line_len, y_pos + 15);
+ D_move_abs(x_pos + 25 + line_len, y_pos - 2);
+ D_cont_abs(x_pos + 25 + line_len, y_pos + 15);
+ D_end(); /* no-op? */
+ D_close();
+
+ D_move_abs(x_pos + 25, y_pos + 15);
+ for (i = 0; i <= scales[incr].seg - 2; i++) {
+ D_move_rel(seg_len, 0);
+ D_cont_rel(0, -7); /* 5 above, on px on line, and 5 below */
+ D_move_rel(0, +7);
+ }
+ }
+ else if (style == STYLE_TICKS_DOWN) {
+ /* draw simple line scale with down facing ticks */
+ D_begin();
+ D_move_abs(x_pos + 25, y_pos + 15 + 17);
+ D_cont_abs(x_pos + 25, y_pos + 15);
+ D_cont_abs(x_pos + 25 + line_len, y_pos + 15);
+ D_move_abs(x_pos + 25 + line_len, y_pos +15 + 17);
+ D_cont_abs(x_pos + 25 + line_len, y_pos + 15);
+ D_end(); /* no-op? */
+ D_close();
+
+ D_move_abs(x_pos + 25, y_pos + 15);
+ for (i = 0; i <= scales[incr].seg - 2; i++) {
+ D_move_rel(seg_len, 0);
+ D_cont_rel(0, +7); /* 5 above, on px on line, and 5 below */
+ D_move_rel(0, -7);
+ }
+ }
+ D_stroke();
+
+ if (fontsize < 0)
+ return 0;
+
+ /* draw the distance + units text */
+ if (text_posn == TEXT_OVER) {
+ D_pos_abs(x_pos + 25 + line_len / 2.
+ - strlen(scales[incr].name) * fontsize * 0.81 / 2,
y_pos);
D_text(scales[incr].name);
}
- else {
- D_pos_abs(x_pos + 35 - draw * 10 + line_len, y_pos + 20);
+ if (text_posn == TEXT_UNDER) {
+ D_pos_abs(x_pos + 25 + line_len / 2.
+ - strlen(scales[incr].name) * fontsize * 0.81 / 2,
+ y_pos + 43);
D_text(scales[incr].name);
}
+ else if (text_posn == TEXT_RIGHT) {
+ D_pos_abs(x_pos + 35 + line_len, y_pos + 20);
+ D_text(scales[incr].name);
+ }
+ else if (text_posn == TEXT_LEFT) {
+ D_pos_abs(x_pos - 24 -
+ strlen(scales[incr].name) * fontsize/2, y_pos + 20);
+ D_text(scales[incr].name);
+ }
- return (0);
+ return 0;
}
-
Modified: grass/trunk/display/d.barscale/main.c
===================================================================
--- grass/trunk/display/d.barscale/main.c 2013-06-20 15:02:57 UTC (rev 56840)
+++ grass/trunk/display/d.barscale/main.c 2013-06-20 15:18:33 UTC (rev 56841)
@@ -1,7 +1,7 @@
-
/****************************************************************************
*
* MODULE: d.barscale
+ *
* AUTHOR(S): unknown but from CERL code (original contributor)
* Markus Neteler <neteler itc.it>,
* Bernhard Reiter <bernhard intevation.de>,
@@ -11,40 +11,41 @@
* Glynn Clements <glynn gclements.plus.com>,
* Hamish Bowman <hamish_b yahoo.com>,
* Jan-Oliver Wagner <jan intevation.de>
- * PURPOSE: displays a barscale on graphics monitor
- * COPYRIGHT: (C) 1999-2011 by the GRASS Development Team
+ * Major rewrite for GRASS 7 by Hamish Bowman, June 2013
*
+ * PURPOSE: Displays a barscale or north arrow on graphics monitor
+ *
+ * COPYRIGHT: (C) 1999-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"
-#include <grass/glocale.h>
-int color1;
-int color2;
-double east;
-double north;
+int fg_color, bg_color;
int use_feet;
-int do_background = 1;
-int do_bar = 1;
-int draw = 0;
+int do_background = TRUE;
int main(int argc, char **argv)
{
struct GModule *module;
- struct Option *opt1, *opt2, *opt3, *fsize;
- struct Flag *feet, *top, *linescale, *northarrow, *scalebar;
+ struct Option *bg_color_opt, *fg_color_opt, *coords, *fsize,
+ *barstyle, *text_placement, *n_arrow;
+ struct Flag *feet, *northarrow, *no_text;
struct Cell_head W;
- int fontsize;
+ double east, north;
+ double fontsize;
+ int bar_style, text_position;
/* Initialize the GIS calls */
G_gisinit(argv[0]);
@@ -52,53 +53,69 @@
module = G_define_module();
G_add_keyword(_("display"));
G_add_keyword(_("cartography"));
- module->description = _("Displays a barscale on the graphics monitor.");
+ module->description = _("Displays a barscale or north arrow on the graphics monitor.");
feet = G_define_flag();
feet->key = 'f';
feet->description = _("Use feet/miles instead of meters");
- linescale = G_define_flag();
- linescale->key = 'l';
- linescale->description = _("Draw a line scale instead of a bar scale");
-
- top = G_define_flag();
- top->key = 't';
- top->description = _("Write text on top of the scale, not to the right");
-
northarrow = G_define_flag();
northarrow->key = 'n';
northarrow->description = _("Draw a north arrow only");
- scalebar = G_define_flag();
- scalebar->key = 's';
- scalebar->description = _("Draw a scale bar only");
+ no_text = G_define_flag();
+ no_text->key = 't';
+ no_text->description = _("Draw the scale bar without text");
- opt1 = G_define_standard_option(G_OPT_C_BG);
- opt1->key = "bcolor";
+ barstyle = G_define_option();
+ barstyle->key = "style";
+ barstyle->description = _("Type of barscale to draw");
+/* TODO: barstyle->options: part_checker (left side twice the freq of the right) and |<--arrow_ends-->| */
+ barstyle->options =
+ "classic,line,solid,hollow,full_checker,up_ticks,down_ticks,both_ticks";
+ barstyle->answer = "classic";
- opt2 = G_define_standard_option(G_OPT_C_FG);
- opt2->key = "tcolor";
- opt2->label = _("Text color");
-
- opt3 = G_define_option();
- opt3->key = "at";
- opt3->key_desc = "x,y";
- opt3->type = TYPE_DOUBLE;
- opt3->answer = "0.0,5.0";
- opt3->options = "0-100";
- opt3->required = NO;
- opt3->description =
- _("The screen coordinates for top-left corner of label ([0,0] is lower-left of frame)");
+ coords = G_define_option();
+ coords->key = "at";
+ coords->key_desc = "x,y";
+ coords->type = TYPE_DOUBLE;
+ coords->answer = "0.0,5.0";
+ coords->options = "0-100";
+ coords->required = NO;
+ coords->label =
+ _("The screen coordinates for top-left corner of label");
+ coords->description = _("(0,0) is lower-left of frame");
+ fg_color_opt = G_define_standard_option(G_OPT_C_FG);
+ fg_color_opt->key = "color";
+ fg_color_opt->label = _("Bar scale, text, and north arrow color");
+
+ bg_color_opt = G_define_standard_option(G_OPT_C_BG);
+ bg_color_opt->key = "background_color";
+ bg_color_opt->label = _("Background color (drawn behind the bar)");
+
+ text_placement = G_define_option();
+ text_placement->key = "text_position";
+ text_placement->description = _("Text position");
+ text_placement->options = "under,over,left,right";
+ text_placement->answer = "right";
+
fsize = G_define_option();
fsize->key = "fontsize";
- fsize->type = TYPE_INTEGER;
+ fsize->type = TYPE_DOUBLE;
fsize->required = NO;
fsize->answer = "14";
fsize->options = "1-72";
fsize->description = _("Font size");
+ n_arrow = G_define_option();
+ n_arrow->key = "north_arrow";
+ n_arrow->description = _("Only used when drawing a north arrow only");
+ n_arrow->label = _("North arrow style");
+ n_arrow->options = "1a,1b,2,3,4,5,6,7a,7b,8";
+ n_arrow->answer = "1a";
+
+
if (G_parser(argc, argv))
exit(EXIT_FAILURE);
@@ -108,39 +125,91 @@
G_fatal_error(_("%s does not work with a latitude-longitude location"),
argv[0]);
- if (linescale->answer)
- do_bar = 0;
+ use_feet = feet->answer ? TRUE : FALSE;
- use_feet = feet->answer ? 1 : 0;
- if (northarrow->answer && scalebar->answer)
- G_fatal_error(_("Choose either -n or -s flag"));
+ switch (barstyle->answer[0]) {
+ case 'c':
+ bar_style = STYLE_CLASSIC_BAR;
+ break;
+ case 'p':
+ bar_style = STYLE_PART_CHECKER;
+ break;
+ case 'f':
+ bar_style = STYLE_FULL_CHECKER;
+ break;
+ case 'l':
+ bar_style = STYLE_THIN_WITH_ENDS;
+ break;
+ case 's':
+ bar_style = STYLE_SOLID_BAR;
+ break;
+ case 'h':
+ bar_style = STYLE_HOLLOW_BAR;
+ break;
+ case 'u':
+ bar_style = STYLE_TICKS_UP;
+ break;
+ case 'd':
+ bar_style = STYLE_TICKS_DOWN;
+ break;
+ case 'b':
+ bar_style = STYLE_TICKS_BOTH;
+ break;
+ case 'a':
+ bar_style = STYLE_ARROW_ENDS;
+ break;
+ default:
+ G_fatal_error(_("Programmer error"));
+ }
if (northarrow->answer)
- draw = 1;
- else if (scalebar->answer)
- draw = 2;
+ bar_style = STYLE_NONE;
- sscanf(opt3->answers[0], "%lf", &east);
- sscanf(opt3->answers[1], "%lf", &north);
+
+ switch (text_placement->answer[0]) {
+ case 'u':
+ text_position = TEXT_UNDER;
+ break;
+ case 'o':
+ text_position = TEXT_OVER;
+ break;
+ case 'l':
+ text_position = TEXT_LEFT;
+ break;
+ case 'r':
+ text_position = TEXT_RIGHT;
+ break;
+ default:
+ G_fatal_error(_("Programmer error"));
+ }
+
+ sscanf(coords->answers[0], "%lf", &east);
+ sscanf(coords->answers[1], "%lf", &north);
+
fontsize = atoi(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);
- /* Parse and select background color */
- color1 = D_parse_color(opt1->answer, 1);
- if (color1 == 0)
- do_background = 0;
- /* Parse and select foreground color */
- color2 = D_parse_color(opt2->answer, 0);
-
/* Draw the scale */
- draw_scale(top->answer, fontsize);
+ draw_scale(east, north, bar_style, text_position, fontsize,
+ n_arrow->answer);
+
D_save_command(G_recreate_command());
D_close_driver();
Modified: grass/trunk/display/d.barscale/options.h
===================================================================
--- grass/trunk/display/d.barscale/options.h 2013-06-20 15:02:57 UTC (rev 56840)
+++ grass/trunk/display/d.barscale/options.h 2013-06-20 15:18:33 UTC (rev 56841)
@@ -1,12 +1,28 @@
-extern int color1;
-extern int color2;
-extern double east;
-extern double north;
+/* barscale types */
+#define STYLE_NONE 0
+#define STYLE_CLASSIC_BAR 1
+#define STYLE_PART_CHECKER 2
+#define STYLE_FULL_CHECKER 3
+#define STYLE_THIN_WITH_ENDS 4
+#define STYLE_SOLID_BAR 5
+#define STYLE_HOLLOW_BAR 6
+#define STYLE_TICKS_UP 7
+#define STYLE_TICKS_DOWN 8
+#define STYLE_TICKS_BOTH 9
+#define STYLE_ARROW_ENDS 10
+
+/* text placement */
+#define TEXT_UNDER 0
+#define TEXT_OVER 1
+#define TEXT_LEFT 2
+#define TEXT_RIGHT 3
+
+/* globals */
+extern int fg_color;
+extern int bg_color;
extern int use_feet;
extern int do_background;
-extern int do_bar;
-extern int draw;
/* draw_scale.c */
-int draw_scale(int toptext, int fontsize);
+int draw_scale(double, double, int, int, double, char *);
More information about the grass-commit
mailing list