Skip to content

Commit

Permalink
d.vect.thematic: Do not show decimal places for ints (OSGeo#3096)
Browse files Browse the repository at this point in the history
Number of minimal decimal places is fixed to 2 in the legend generated by d.vect.thematic. This adds logic which changes the number of decimal places to 0 when the column type is integer (DB_C_TYPE_INT).

This does not let user decide the number, but it covers more cases than the current code and it prepares a way for making it parametrized in the future.
  • Loading branch information
wenzeslaus authored Jan 30, 2024
1 parent 90f149c commit 8a85c75
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 37 deletions.
23 changes: 14 additions & 9 deletions display/d.vect.thematic/legend.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void write_into_legend_file(const char *legfile, const char *icon,
double stats_max, double *breakpoints, int nbreaks,
int size, struct color_rgb bcolor,
struct color_rgb *colors, int default_width,
int *frequencies, const char *topo)
int *frequencies, int c_type, const char *topo)
{
FILE *fd;
int i;
Expand All @@ -33,29 +33,34 @@ void write_into_legend_file(const char *legfile, const char *icon,
/* Title */
fprintf(fd, "||||||%s\n", title);

// Do not show decimal places for integers.
int n_places = c_type == DB_C_TYPE_INT ? 0 : 2;

/* First line */
if (stats_min > breakpoints[0]) {
fprintf(fd, "< %.2f|", breakpoints[0]);
fprintf(fd, "< %.*f|", n_places, breakpoints[0]);
}
else {
fprintf(fd, "%.2f - %.2f|", stats_min, breakpoints[0]);
fprintf(fd, "%.*f - %.*f|", n_places, stats_min, n_places,
breakpoints[0]);
}
fprintf(fd, "%s|%d|ps|%d:%d:%d|%d:%d:%d|%d|%s|%d\n", icon, size,
colors[0].r, colors[0].g, colors[0].b, bcolor.r, bcolor.g, bcolor.b,
default_width, topo, frequencies[0]);
/* Middle lines */
for (i = 1; i < nbreaks; i++) {
fprintf(fd, "%.2f - %.2f|%s|%d|ps|%d:%d:%d|%d:%d:%d|%d|%s|%d\n",
breakpoints[i - 1], breakpoints[i], icon, size, colors[i].r,
colors[i].g, colors[i].b, bcolor.r, bcolor.g, bcolor.b,
default_width, topo, frequencies[i]);
fprintf(fd, "%.*f - %.*f|%s|%d|ps|%d:%d:%d|%d:%d:%d|%d|%s|%d\n",
n_places, breakpoints[i - 1], n_places, breakpoints[i], icon,
size, colors[i].r, colors[i].g, colors[i].b, bcolor.r, bcolor.g,
bcolor.b, default_width, topo, frequencies[i]);
}
/* Last one */
if (stats_max < breakpoints[nbreaks - 1]) {
fprintf(fd, ">%.2f|", breakpoints[nbreaks - 1]);
fprintf(fd, ">%.*f|", n_places, breakpoints[nbreaks - 1]);
}
else {
fprintf(fd, "%.2f - %.2f|", breakpoints[nbreaks - 1], stats_max);
fprintf(fd, "%.*f - %.*f|", n_places, breakpoints[nbreaks - 1],
n_places, stats_max);
}
fprintf(fd, "%s|%d|ps|%d:%d:%d|%d:%d:%d|%d|%s|%d\n", icon, size,
colors[nbreaks].r, colors[nbreaks].g, colors[nbreaks].b, bcolor.r,
Expand Down
2 changes: 1 addition & 1 deletion display/d.vect.thematic/local_proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ int display_lines(struct Map_info *, struct cat_list *, int, const char *,
/* legend.c */
void write_into_legend_file(const char *, const char *, const char *, double,
double, double *, int, int, struct color_rgb,
struct color_rgb *, int, int *, const char *);
struct color_rgb *, int, int *, int, const char *);
54 changes: 27 additions & 27 deletions display/d.vect.thematic/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -533,26 +533,26 @@ int main(int argc, char **argv)
while (TRUE) {
nfeatures = Vect_get_num_primitives(&Map, GV_POINT);
if (nfeatures > 0) {
write_into_legend_file("stdout", icon_opt->answer, title,
stats.min, stats.max, breakpoints,
nbreaks, size, bcolor, colors,
default_width, frequencies, "point");
write_into_legend_file(
"stdout", icon_opt->answer, title, stats.min, stats.max,
breakpoints, nbreaks, size, bcolor, colors, default_width,
frequencies, ctype, "point");
break;
}
nfeatures = Vect_get_num_primitives(&Map, GV_LINE);
if (nfeatures > 0) {
write_into_legend_file("stdout", icon_line_opt->answer, title,
stats.min, stats.max, breakpoints,
nbreaks, size, bcolor, colors,
default_width, frequencies, "line");
write_into_legend_file(
"stdout", icon_line_opt->answer, title, stats.min,
stats.max, breakpoints, nbreaks, size, bcolor, colors,
default_width, frequencies, ctype, "line");
break;
}
nfeatures = Vect_get_num_primitives(&Map, GV_BOUNDARY);
if (nfeatures > 0) {
write_into_legend_file("stdout", icon_area_opt->answer, title,
stats.min, stats.max, breakpoints,
nbreaks, size, bcolor, colors,
default_width, frequencies, "area");
write_into_legend_file(
"stdout", icon_area_opt->answer, title, stats.min,
stats.max, breakpoints, nbreaks, size, bcolor, colors,
default_width, frequencies, ctype, "area");
break;
}
}
Expand All @@ -564,26 +564,26 @@ int main(int argc, char **argv)
while (TRUE) {
nfeatures = Vect_get_num_primitives(&Map, GV_POINT);
if (nfeatures > 0) {
write_into_legend_file(leg_file, icon_opt->answer, title,
stats.min, stats.max, breakpoints,
nbreaks, size, bcolor, colors,
default_width, frequencies, "point");
write_into_legend_file(
leg_file, icon_opt->answer, title, stats.min, stats.max,
breakpoints, nbreaks, size, bcolor, colors, default_width,
frequencies, ctype, "point");
break;
}
nfeatures = Vect_get_num_primitives(&Map, GV_LINE);
if (nfeatures > 0) {
write_into_legend_file(leg_file, icon_line_opt->answer, title,
stats.min, stats.max, breakpoints,
nbreaks, size, bcolor, colors,
default_width, frequencies, "line");
write_into_legend_file(
leg_file, icon_line_opt->answer, title, stats.min,
stats.max, breakpoints, nbreaks, size, bcolor, colors,
default_width, frequencies, ctype, "line");
break;
}
nfeatures = Vect_get_num_primitives(&Map, GV_BOUNDARY);
if (nfeatures > 0) {
write_into_legend_file(leg_file, icon_area_opt->answer, title,
stats.min, stats.max, breakpoints,
nbreaks, size, bcolor, colors,
default_width, frequencies, "area");
write_into_legend_file(
leg_file, icon_area_opt->answer, title, stats.min,
stats.max, breakpoints, nbreaks, size, bcolor, colors,
default_width, frequencies, ctype, "area");
break;
}
}
Expand All @@ -598,23 +598,23 @@ int main(int argc, char **argv)
write_into_legend_file(
legend_file_opt->answer, icon_opt->answer, title, stats.min,
stats.max, breakpoints, nbreaks, size, bcolor, colors,
default_width, frequencies, "point");
default_width, frequencies, ctype, "point");
break;
}
nfeatures = Vect_get_num_primitives(&Map, GV_LINE);
if (nfeatures > 0) {
write_into_legend_file(
legend_file_opt->answer, icon_line_opt->answer, title,
stats.min, stats.max, breakpoints, nbreaks, size, bcolor,
colors, default_width, frequencies, "line");
colors, default_width, frequencies, ctype, "line");
break;
}
nfeatures = Vect_get_num_primitives(&Map, GV_BOUNDARY);
if (nfeatures > 0) {
write_into_legend_file(
legend_file_opt->answer, icon_area_opt->answer, title,
stats.min, stats.max, breakpoints, nbreaks, size, bcolor,
colors, default_width, frequencies, "area");
colors, default_width, frequencies, ctype, "area");
break;
}
}
Expand Down

0 comments on commit 8a85c75

Please sign in to comment.