[GRASS-SVN] r68753 - grass/trunk/display/d.legend
svn_grass at osgeo.org
svn_grass at osgeo.org
Thu Jun 23 17:31:43 PDT 2016
Author: annakrat
Date: 2016-06-23 17:31:43 -0700 (Thu, 23 Jun 2016)
New Revision: 68753
Modified:
grass/trunk/display/d.legend/draw.c
grass/trunk/display/d.legend/local_proto.h
grass/trunk/display/d.legend/main.c
Log:
d.legend: logarithmic legend and fixes to background size, author Adam Laza
Modified: grass/trunk/display/d.legend/draw.c
===================================================================
--- grass/trunk/display/d.legend/draw.c 2016-06-23 22:03:32 UTC (rev 68752)
+++ grass/trunk/display/d.legend/draw.c 2016-06-24 00:31:43 UTC (rev 68753)
@@ -30,7 +30,7 @@
struct Option *opt_at, struct Option *opt_fontsize,
struct Option *opt_ticks, struct Option *opt_tstep,
struct Option *opt_range, struct Flag *histo, struct Flag *hidestr,
- int draw)
+ int log_sc, int draw, int digits)
{
char buff[512];
int black, white;
@@ -67,6 +67,9 @@
int dx, dy;
double coef;
double ppl;
+ double bb,bt,bl,br;
+ char MaxLabel[512];
+ double num;
if (draw) {
@@ -316,6 +319,13 @@
}
}
+ /* In case of log. scale raster doesn't contain negative or zero values */
+ if (log_sc)
+ if ((dmin<=0) || (dmax<=0))
+ G_fatal_error(_("Range [%.3f, %.3f] out of the logarithm domain."),
+ dmin, dmax);
+
+
if (use_catlist) {
for (i = 0; i < catlistCount; i++) {
if ((catlist[i] < dmin) || (catlist[i] > dmax)) {
@@ -329,6 +339,9 @@
do_cats = 0; /* if only to get rid of the compiler warning */
cats_num = 0; /* if only to get rid of the compiler warning */
/* determine how many significant digits to display based on range */
+ if (digits != -1) /* number of digits given by user */
+ sprintf(DispFormat, "%%.%df", digits);
+ else {/* automatic calculation */
if (0 == (dmax - dmin)) /* trap divide by 0 for single value rasters */
sprintf(DispFormat, "%%f");
else {
@@ -340,6 +353,7 @@
else
sprintf(DispFormat, "%%.2g"); /* eg 4.2e-9 */
}
+ }
} /* end of is fp */
if (use_catlist) {
@@ -372,6 +386,25 @@
/* Draw the legend bar */
if (draw) {
for (k = 0; k < lleg; k++) {
+ if (log_sc) { /* logarithmic scale */
+ num = k / lleg;
+ val = dmin * pow(dmax/dmin, num);
+ D_d_color(val, &colors);
+ if (!flip) {
+ if (horiz)
+ D_box_abs(x0 + k, y0, x0 + k + 1, y0 + dy);
+ else
+ D_box_abs(x0, y0 + k, x0 + dx, y0 + k + 1);
+ }
+ else {
+ if (horiz)
+ D_box_abs(x1 - k, y0, x1 - k - 1, y0 + dy);
+ else
+ D_box_abs(x0, y1 - k, x0 + dx, y1 - k - 1);
+ }
+
+ } /* linear scale */
+ else{
if (!fp) {
if (!flip)
tcell =
@@ -385,9 +418,9 @@
}
else {
if (!flip)
- val = dmin + k * (dmax - dmin) / lleg;
+ val = dmin + k / lleg * (dmax - dmin);
else
- val = dmax - k * (dmax - dmin) / lleg;
+ val = dmax - k / lleg * (dmax - dmin);
D_d_color(val, &colors);
}
@@ -399,6 +432,7 @@
y0 + k + (dy ? -dy : 1));
}
}
+ }
/* Format text */
if (!fp) { /* cut down labelnum so they don't repeat */
@@ -475,42 +509,91 @@
if (hide_catnum)
buff[0] = 0; /* no text */
else {
+ if (log_sc) {
+ num = log10(dmax) - k * ((log10(dmax) - log10(dmin)) / (steps - 1));
+ val = pow(10,num);
+ }
+ else{
if (!flip)
val = dmin + k * (dmax - dmin) / (steps - 1);
else
val = dmax - k * (dmax - dmin) / (steps - 1);
-
+ }
sprintf(buff, DispFormat, val);
-
}
}
if (draw) {
- ppl = (lleg) / (steps * 1.0 - 1);
if (!hide_catnum) {
+ D_text_size(txsiz, txsiz);
+ D_get_text_box(buff, &bb, &bt, &bl, &br);
if (!horiz) {
+ if (log_sc) {
+ coef = (log10(val) - log10(dmin)) / (log10(dmax) - log10(dmin));
+ if (flip)
D_pos_abs(x1 + label_indent,
- y0 + ppl * k + txsiz / 2);
- if (show_ticks)
- D_line_abs(x1, y0 + ppl * k, x1 + 6,
- y0 + ppl * k);
+ y1 - coef * lleg + (bb - bt) / 2);
+ else
+ D_pos_abs(x1 + label_indent,
+ y0 + coef * lleg + (bb - bt) / 2);
}
else {
- /* text width is 0.81 of text height? so even though we set width
- to txsiz with D_text_size(), we still have to reduce.. hmmm */
- D_pos_abs(x0 + ppl * k -
- (strlen(buff) * txsiz * .81 / 2),
+ ppl = (lleg) / (steps * 1.0 - 1);
+ D_pos_abs(x1 + label_indent,
+ y0 + ppl * k + (bb - bt) / 2);
+ }
+ if (show_ticks) {
+ D_use_color(black);
+ if (log_sc)
+ if (flip)
+ D_line_abs(x1, y1 - coef * lleg,
+ x1 + 6, y1 - coef * lleg);
+ else
+ D_line_abs(x1, y0 + coef * lleg,
+ x1 + 6, y0 + coef * lleg);
+ else
+ D_line_abs(x1, y0 + ppl * k,
+ x1 + 6, y0 + ppl * k);
+ }
+ }
+ else {
+ if (log_sc) {
+ coef = (log10(val) - log10(dmin)) / (log10(dmax) - log10(dmin));
+ if (flip)
+ D_pos_abs(x1 - coef * wleg - ((br - bl) / 2),
y1 + label_indent + txsiz);
- if (show_ticks)
- D_line_abs(x0 + ppl * k, y1, x0 + ppl * k,
- y1 + 6);
+ else
+ D_pos_abs(x0 + coef * wleg - ((br - bl) / 2),
+ y1 + label_indent + txsiz);
}
+ else {
+ ppl = (wleg) / (steps * 1.0 - 1);
+ D_pos_abs(x0 + ppl * k - ((br - bl) / 2),
+ y1 + label_indent + txsiz);
+ }
+ if (show_ticks) {
+ D_use_color(black);
+ if (log_sc)
+ if (flip)
+ D_line_abs(x1 - coef * wleg, y1,
+ x1 - coef * wleg, y1 + 6);
+ else
+ D_line_abs(x0 + coef * wleg, y1,
+ x0 + coef * wleg, y1 + 6);
+ else
+ D_line_abs(x0 + ppl * k, y1,
+ x0 + ppl * k, y1 + 6);
+ }
+ }
if (color)
+ D_use_color(color);
D_text(buff);
}
}
- if (strlen(buff) > MaxLabelLen)
+ if (strlen(buff) > MaxLabelLen) {
MaxLabelLen = strlen(buff);
+ sprintf(MaxLabel, DispFormat, val);
+ }
} /* for */
}
@@ -523,20 +606,29 @@
}
sprintf(buff, DispFormat, tick_values[i]);
+ if (log_sc) {
+ coef = (log10(tick_values[i]) - log10(dmin)) / (log10(dmax) - log10(dmin));
+ }
+ else
coef = (tick_values[i] - dmin) / ((dmax - dmin) * 1.0);
+
if (draw) {
if (!flip) {
if (!horiz) {
- if (show_ticks)
+ if (show_ticks) {
+ D_use_color(black);
D_line_abs(x1, y0 + coef * lleg,
x1 + 6, y0 + coef * lleg);
+ }
D_pos_abs(x1 + label_indent,
y0 + coef * lleg + txsiz / 2);
}
else {
- if (show_ticks)
+ if (show_ticks) {
+ D_use_color(black);
D_line_abs(x0 + coef * wleg, y1,
x0 + coef * wleg, y1 + 6);
+ }
D_pos_abs(x0 + coef * wleg -
(strlen(buff) * txsiz * .81 / 2),
y1 + label_indent + txsiz);
@@ -544,31 +636,95 @@
}
else {
if (!horiz) {
- if (show_ticks)
+ if (show_ticks) {
+ D_use_color(black);
D_line_abs(x1, y1 - coef * lleg,
x1 + 6, y1 - coef * lleg);
+ }
D_pos_abs(x1 + label_indent,
y1 - coef * lleg + txsiz / 2);
}
else {
- if (show_ticks)
+ if (show_ticks) {
+ D_use_color(black);
D_line_abs(x1 - coef * wleg, y1,
x1 - coef * wleg, y1 + 6);
+ }
D_pos_abs(x1 - coef * wleg -
(strlen(buff) * txsiz * .81 / 2),
y1 + label_indent + txsiz);
}
}
+ D_use_color(color);
D_text(buff);
}
- if (strlen(buff) > MaxLabelLen)
+ if (strlen(buff) > MaxLabelLen) {
MaxLabelLen = strlen(buff);
+ sprintf(MaxLabel, DispFormat, tick_values[i]);
}
}
+ }
/* LABEL_STEP OPTION */
if (opt_tstep->answer) {
+ if (log_sc) { /* logarithmic */
+ t_start=0;
+ while (log10(dmin) + t_start < log10(dmax)){
+ num = ceil(log10(dmin)) + t_start;
+ val = pow(10,num);
+ sprintf(buff, DispFormat, val);
+ coef = (log10(val) - log10(dmin)) / (log10(dmax) - log10(dmin));
+ if (draw){
+ if (!flip){
+ if (!horiz){
+ if (show_ticks) {
+ D_use_color(black);
+ D_line_abs(x1, y0 + coef * lleg,
+ x1 + 6, y0 + coef * lleg);
+ }
+ D_pos_abs(x1 + label_indent,
+ y0 + coef * lleg + txsiz / 2);
+ }
+ else{
+ if (show_ticks) {
+ D_use_color(black);
+ D_line_abs(x0 + coef * wleg, y1,
+ x0 + coef * wleg, y1 + 6);
+ }
+ D_pos_abs(x0 + coef * wleg -
+ (strlen(buff) * txsiz * .81 / 2),
+ y1 + label_indent + txsiz);
+ }
+ }
+ else{
+ if (!horiz){
+ if (show_ticks) {
+ D_use_color(black);
+ D_line_abs(x1, y1 - coef * lleg,
+ x1 + 6, y1 - coef * lleg);
+ }
+ D_pos_abs(x1 + label_indent,
+ y1 - coef * lleg + txsiz / 2);
+ }
+ else{
+ if (show_ticks){
+ D_use_color(black);
+ D_line_abs(x1 - coef * wleg, y1,
+ x1 - coef * wleg, y1 + 6);
+ }
+ D_pos_abs(x1 - coef * wleg -
+ (strlen(buff) * txsiz * .81 / 2),
+ y1 + label_indent + txsiz);
+ }
+ }
+ D_use_color(color);
+ D_text(buff);
+ }
+ t_start += t_step;
+ }
+ }
+ else { /* linear */
t_start = ceil(dmin / t_step) * t_step;
if (t_start == -0)
t_start = 0;
@@ -577,16 +733,20 @@
if (!horiz)
while (t_start < dmax) {
sprintf(buff, DispFormat, t_start);
- if (strlen(buff) > MaxLabelLen)
+ if (strlen(buff) > MaxLabelLen) {
MaxLabelLen = strlen(buff);
-
+ sprintf(MaxLabel, DispFormat, t_start);
+ }
if (draw) {
coef = (t_start - dmin) / ((dmax - dmin) * 1.0);
- if (show_ticks)
+ if (show_ticks) {
+ D_use_color(black);
D_line_abs(x1, y0 + coef * lleg,
x1 + 6, y0 + coef * lleg);
+ }
D_pos_abs(x1 + label_indent,
y0 + coef * lleg + txsiz / 2);
+ D_use_color(color);
D_text(buff);
}
t_start += t_step;
@@ -594,17 +754,22 @@
else
while (t_start < dmax) {
sprintf(buff, DispFormat, t_start);
- if (strlen(buff) > MaxLabelLen)
+ if (strlen(buff) > MaxLabelLen) {
MaxLabelLen = strlen(buff);
+ sprintf(MaxLabel, DispFormat, t_start);
+ }
if (draw) {
coef = (t_start - dmin) / ((dmax - dmin) * 1.0);
- if (show_ticks)
+ if (show_ticks) {
+ D_use_color(black);
D_line_abs(x0 + coef * wleg, y1,
x0 + coef * wleg, y1 + 6);
+ }
D_pos_abs(x0 + coef * wleg -
(strlen(buff) * txsiz * .81 / 2),
y1 + label_indent + txsiz);
+ D_use_color(color);
D_text(buff);
}
t_start += t_step;
@@ -614,16 +779,21 @@
if (!horiz)
while (t_start < dmax) {
sprintf(buff, DispFormat, t_start);
- if (strlen(buff) > MaxLabelLen)
+ if (strlen(buff) > MaxLabelLen) {
MaxLabelLen = strlen(buff);
+ sprintf(MaxLabel, DispFormat, t_start);
+ }
if (draw) {
coef = (t_start - dmin) / ((dmax - dmin) * 1.0);
- if (show_ticks)
+ if (show_ticks) {
+ D_use_color(black);
D_line_abs(x1, y1 - coef * lleg,
x1 + 6, y1 - coef * lleg);
+ }
D_pos_abs(x1 + label_indent,
y1 - coef * lleg + txsiz / 2);
+ D_use_color(color);
D_text(buff);
}
t_start += t_step;
@@ -631,23 +801,29 @@
else
while (t_start < dmax) {
sprintf(buff, DispFormat, t_start);
- if (strlen(buff) > MaxLabelLen)
+ if (strlen(buff) > MaxLabelLen) {
MaxLabelLen = strlen(buff);
+ sprintf(MaxLabel, DispFormat, t_start);
+ }
if (draw) {
coef = (t_start - dmin) / ((dmax - dmin) * 1.0);
- if (show_ticks)
+ if (show_ticks){
+ D_use_color(black);
D_line_abs(x1 - coef * wleg, y1,
x1 - coef * wleg, y1 + 6);
+ }
D_pos_abs(x1 - coef * wleg -
(strlen(buff) * txsiz * .81 / 2),
y1 + label_indent + txsiz);
+ D_use_color(color);
D_text(buff);
}
t_start += t_step;
}
}
}
+ }
if (draw) {
/* Draw boxes outside of legend bar */
@@ -697,17 +873,20 @@
/* display title or units */
if (strlen(title) > 0) {
+ D_text_size(titsiz, titsiz);
+ D_get_text_box(title, &bb, &bt, &bl, &br);
/* title */
if (horiz) {
- x_tit = (x0 + x1) / 2. - (strlen(title) * titsiz * 0.81) / 2;
+ x_tit = (x0 + x1) / 2. - (br - bl) / 2;
y_tit = y0 - (titsiz) - max_hist;
}
else {
x_tit = x0;
y_tit = y0 - (titsiz);
}
- x1_tit = x_tit + strlen(title) * titsiz * 0.81;
+ x1_tit = x_tit + (br - bl);
+
if (draw) {
D_use_color(color);
/* use tit_fontsize */
@@ -733,15 +912,17 @@
units = "";
if (strlen(units)) {
+ D_text_size(titsiz, titsiz);
+ D_get_text_box(title, &bb, &bt, &bl, &br);
if (horiz) {
x_tit =
- (x0 + x1) / 2. - (strlen(units) * txsiz * 0.81) / 2;
+ (x0 + x1) / 2. - (br - bl) / 2;
y_tit = y1 + (txsiz * 2.75);
}
else {
x_tit = x0;
}
- x1_tit = x_tit + strlen(units) * titsiz * 0.81;
+ x1_tit = x_tit + (br - bl);
if (draw) {
D_use_color(color);
@@ -753,11 +934,11 @@
if (!draw) {
/* Draw background */
+ D_text_size(txsiz, txsiz);
+ D_get_text_box(MaxLabel, &bb, &bt, &bl, &br);
if (!horiz) {
x0bg = x0 - max_hist - txsiz;
- x1bg =
- x0 + wleg + label_indent + (MaxLabelLen * txsiz * 0.81) +
- txsiz;
+ x1bg = x0 + wleg + label_indent + (br - bl) + txsiz;
if (x1bg < x1_tit)
x1bg = x1_tit + txsiz;
y1bg = y0 + lleg + txsiz;
@@ -767,8 +948,8 @@
y0bg = y0 - titsiz;
}
else {
- x0bg = x0 - (MaxLabelLen * txsiz * 0.81) / 2 - txsiz;
- x1bg = x0 + wleg + (MaxLabelLen * txsiz * 0.81) / 2 + txsiz;
+ x0bg = x0 - (br - bl) / 2 - txsiz;
+ x1bg = x0 + wleg + (br - bl) / 2 + txsiz;
if (x1bg < x1_tit) {
x0bg = x_tit - txsiz;
x1bg = x1_tit + txsiz;
@@ -971,8 +1152,10 @@
catlist[catlistCount - i - 1]);
}
}
- if (strlen(buff) > MaxLabelLen)
+ if (strlen(buff) > MaxLabelLen) {
MaxLabelLen = strlen(buff);
+ sprintf(MaxLabel, DispFormat, catlist[catlistCount - i - 1]);
+ }
if (draw) {
D_pos_abs((l + 3 + dots_per_line), (cur_dot_row) - 3);
@@ -988,8 +1171,11 @@
if (strlen(title) > 0) {
x_tit = x0;
y_tit = y0;
- x1_tit = x_tit + strlen(title) * titsiz * 0.81;
+ D_text_size(titsiz, titsiz);
+ D_get_text_box(title, &bb, &bt, &bl, &br);
+ x1_tit = x_tit + (br - bl);
+
if (draw) {
D_use_color(color);
/* use tit_fontsize */
@@ -1007,8 +1193,10 @@
/* sprintf(buff, "%d of %d categories\n", (j-1), cats_num); */
sprintf(buff, "%d of %d categories\n", k, cats_num);
- if (strlen(buff) > MaxLabelLen)
+ if (strlen(buff) > MaxLabelLen) {
MaxLabelLen = strlen(buff);
+ sprintf(MaxLabel, "%d of %d categories\n", k, cats_num);
+ }
if (draw) {
if (opt_fontsize->answer != NULL)
@@ -1023,9 +1211,10 @@
if (!draw) {
/* Draw background */
+ D_get_text_box(MaxLabel, &bb, &bt, &bl, &br);
x0bg = x0 - txsiz;
x1bg =
- x0 + dots_per_line + 3 + (MaxLabelLen * txsiz * 0.81) + txsiz;
+ x0 + dots_per_line + 3 + (br - bl) + txsiz;
if (x1bg < x1_tit)
x1bg = x1_tit + txsiz;
y1bg = cur_dot_row + txsiz;
Modified: grass/trunk/display/d.legend/local_proto.h
===================================================================
--- grass/trunk/display/d.legend/local_proto.h 2016-06-23 22:03:32 UTC (rev 68752)
+++ grass/trunk/display/d.legend/local_proto.h 2016-06-24 00:31:43 UTC (rev 68753)
@@ -36,19 +36,10 @@
void get_stats(const char *, struct stat_list *, int, int);
void run_stats(const char *, int, const char *, int);
-/* background.c */
-void background(const char *, int, int, int, int, int, int, int, int, int,
- int, int, int, struct Categories, struct Colors, double,
- double, double, double, int, int, double, double, double *,
- int, int, int, double, double, const char *, double *, double,
- int, int, struct Option *, struct Option *, struct Option *,
- struct Option *, struct Option *, struct Option *,
- struct Flag *, struct Flag *);
-
/* draw.c */
void draw(const char *, int, int, int, int, int, int, int, int, int, int, int,
int, struct Categories, struct Colors, double, double, double,
double, int, int, double, double, double *, int, int, int, double,
double, const char *, double *, double, int, int, struct Option *,
struct Option *, struct Option *, struct Option *, struct Option *,
- struct Option *, struct Flag *, struct Flag *, int);
+ struct Option *, struct Flag *, struct Flag *, int, int, int);
Modified: grass/trunk/display/d.legend/main.c
===================================================================
--- grass/trunk/display/d.legend/main.c 2016-06-23 22:03:32 UTC (rev 68752)
+++ grass/trunk/display/d.legend/main.c 2016-06-24 00:31:43 UTC (rev 68753)
@@ -54,9 +54,9 @@
*opt_thin, *opt_labelnum, *opt_at, *opt_use, *opt_range,
*opt_font, *opt_path, *opt_charset, *opt_fontsize, *opt_title,
*opt_ticks, *opt_tstep, *opt_brdcolor, *opt_bgcolor,
- *opt_tit_fontsize;
+ *opt_tit_fontsize, *opt_digits;
struct Flag *hidestr, *hidenum, *hidenodata, *smooth, *flipit, *histo,
- *showtick, *showbg;
+ *showtick, *showbg, *log_sc;
double X0, X1, Y0, Y1;
int flip, UserRange;
double UserRangeMin, UserRangeMax, UserRangeTemp;
@@ -68,6 +68,7 @@
double t_step;
int colorb, colorbg;
double tit_fontsize;
+ int log_scale, digits;
/* Initialize the GIS calls */
G_gisinit(argv[0]);
@@ -148,9 +149,16 @@
opt_tstep->type = TYPE_DOUBLE;
opt_tstep->required = NO;
opt_tstep->description = _("Display label every step");
- opt_tstep->required = NO;
opt_tstep->guisection = _("Gradient");
+ opt_digits = G_define_option();
+ opt_digits->key = "digits";
+ opt_digits->type = TYPE_INTEGER;
+ opt_digits->required = NO;
+ opt_digits->description = _("Number of digits after decimal point");
+ opt_digits->guisection = _("Advanced");
+ opt_digits->answer = NULL;
+ opt_digits->options = "0-6";
opt_at = G_define_option();
opt_at->key = "at";
@@ -270,6 +278,11 @@
showbg->description = _("Show background");
showbg->guisection = _("Background");
+ log_sc = G_define_flag();
+ log_sc->key = 'l';
+ log_sc->description = _("Use logarithmic scale");
+ log_sc->guisection = _("Advanced");
+
G_option_required(opt_rast2d, opt_rast3d, NULL);
G_option_exclusive(opt_rast2d, opt_rast3d, NULL);
G_option_exclusive(hidenum, opt_ticks, NULL);
@@ -300,6 +313,7 @@
do_smooth = smooth->answer;
flip = flipit->answer;
show_bg = showbg->answer;
+ log_scale = log_sc->answer;
if (showtick->answer) {
label_indent = 12;
@@ -307,6 +321,11 @@
else
label_indent = 6;
+ if (opt_digits->answer != NULL)
+ sscanf(opt_digits->answer, "%d", &digits);
+ else
+ digits = -1;
+
color = D_parse_color(opt_color->answer, TRUE);
if (opt_lines->answer != NULL)
@@ -442,7 +461,7 @@
UserRangeMax, catlist, catlistCount, use_catlist, ticksCount,
fontsize, tit_fontsize, title, tick_values, t_step, colorb,
colorbg, opt_use, opt_at, opt_fontsize, opt_ticks, opt_tstep,
- opt_range, histo, hidestr, 0);
+ opt_range, histo, hidestr, log_scale, 0, digits);
draw(map_name, maptype, color, thin, lines, steps, fp, label_indent,
hide_catnum, hide_catstr, show_ticks, hide_nodata, do_smooth, cats,
@@ -450,7 +469,7 @@
catlist, catlistCount, use_catlist, ticksCount, fontsize,
tit_fontsize, title, tick_values, t_step, colorb, colorbg, opt_use,
opt_at, opt_fontsize, opt_ticks, opt_tstep, opt_range, histo,
- hidestr, 1);
+ hidestr, log_scale, 1, digits);
D_close_driver();
More information about the grass-commit
mailing list