[GRASS-SVN] r68826 - in grass-addons/grass7/display: . d.vect.legend
svn_grass at osgeo.org
svn_grass at osgeo.org
Fri Jul 1 07:06:24 PDT 2016
Author: lazaa
Date: 2016-07-01 07:06:24 -0700 (Fri, 01 Jul 2016)
New Revision: 68826
Added:
grass-addons/grass7/display/d.vect.legend/
grass-addons/grass7/display/d.vect.legend/Makefile
grass-addons/grass7/display/d.vect.legend/d.vect.legend.html
grass-addons/grass7/display/d.vect.legend/draw.c
grass-addons/grass7/display/d.vect.legend/local_proto.h
grass-addons/grass7/display/d.vect.legend/main.c
grass-addons/grass7/display/d.vect.legend/pipe.csv
Log:
New addons d.vect.legend, v1
Added: grass-addons/grass7/display/d.vect.legend/Makefile
===================================================================
--- grass-addons/grass7/display/d.vect.legend/Makefile (rev 0)
+++ grass-addons/grass7/display/d.vect.legend/Makefile 2016-07-01 14:06:24 UTC (rev 68826)
@@ -0,0 +1,12 @@
+MODULE_TOPDIR = ../..
+
+PGM = d.vect.legend
+
+LIBES = $(DISPLAYLIB) $(GISLIB) $(SYMBLIB)
+DEPENDENCIES = $(DISPLAYDEP) $(GISDEP) $(SYMBDEP)
+EXTRA_INC = $(VECT_INC)
+EXTRA_CFLAGS = $(VECT_CFLAGS)
+
+include $(MODULE_TOPDIR)/include/Make/Module.make
+
+default: cmd
Added: grass-addons/grass7/display/d.vect.legend/d.vect.legend.html
===================================================================
--- grass-addons/grass7/display/d.vect.legend/d.vect.legend.html (rev 0)
+++ grass-addons/grass7/display/d.vect.legend/d.vect.legend.html 2016-07-01 14:06:24 UTC (rev 68826)
@@ -0,0 +1,112 @@
+<h2>DESCRIPTION</h2>
+
+<em>d.vect.legend</em> draws vector legend.
+<p>
+TO DO
+Example taken from d.vect.thematic
+<h2>NOTES</h2>
+
+The <b>-l</b> flag instructs the module to print legend information
+(<i>class min | class max | number of observations in class |
+color</i>) to standard output for futher use in graphical software.
+When combined with the <b>-e</b> flag, the legend information will
+be extended with some additional statistical information. If the
+<b>-n</b> flag is set, the module will only print the legend
+information without drawing the map. If the user gives a
+<b>legendfile</b>, the module will
+write <em><a href="d.graph.html">d.graph</a></em> instructions for
+painting a legend into that file.
+
+
+<h2>EXAMPLES</h2>
+
+<h3>Thematic map with classes</h3>
+
+<div class="code"><pre>
+d.vect.thematic -l map=communes3 column=pop \
+ breaks=111393.250000,222785.500000,334177.750000 \
+ colors="255:0:0,0:255:0,0:0:255,0,0,0"
+</pre></div>
+
+<h3>Thematic map with calculated class breaks</h3>
+
+The following example uses a calculated attribute (<tt>density =
+pop/area</tt>) and the standard deviation algorithm to calculate class
+breaks for 5 classes:
+
+<div class="code"><pre>
+d.vect.thematic -l map=communes2 column=pop/area algorithm=std \
+ nbclasses=5 colors="0:0:255,50:100:255,255:100:50,255:0:0,156:0:0"
+</pre></div>
+
+To actually draw the legend for the polygons in the graphical
+display, the procedure is:
+
+<div class="code"><pre>
+# first save legend instructions to a file
+d.vect.thematic -n map=communes2 column=pop/area algorithm=std \
+ nbclasses=5 colors="0:0:255,50:100:255,255:100:50,255:0:0,156:0:0" \
+ legendfile=legend_pop_dens
+
+# open a monitor
+d.mon wx0
+
+# draw the map
+d.vect.thematic map=communes2 column=pop/area algorithm=std \
+ nbclasses=5 colors="0:0:255,50:100:255,255:100:50,255:0:0,156:0:0"
+
+# draw the legend
+d.graph input=legend_pop_dens
+</pre></div>
+
+In order to further enhance this legend, edit the legend file accordingly
+and rerun the process.
+
+<h3>Thematic map with calculated class breaks</h3>
+
+<!-- TODO: find better example -->
+
+Example for the North Carolina sample dataset, colorizing polygons by
+area size:
+
+<div class="code"><pre>
+# first save legend instructions to a file
+d.vect.thematic -n map=geology column=SHAPE_area algorithm=int nclasses=6 \
+ colors=215:48:39,252:141:89,254:224:139,217:239:139,145:207:96,26:152:80 \
+ legendfile=geology.leg
+
+# open the graphical display
+d.mon wx0
+# draw outline of polygons
+d.vect map=geology type=boundary
+# draw color fill
+d.vect.thematic map=geology column=SHAPE_area algorithm=int nclasses=6 \
+ colors="215:48:39,252:141:89,254:224:139,217:239:139,145:207:96,26:152:80" \
+# draw legend
+d.graph input=geology.leg
+</pre></div>
+
+<center>
+<img src="d_vect_thematic.png" alt="d_vect_thematic example" border=1><br>
+Thematic map of area sizes
+</center>
+
+<h2>SEE ALSO</h2>
+
+<em>
+<a href="v.class.html">v.class</a>,
+<a href="d.vect.html">d.vect</a>,
+<a href="d.graph.html">d.graph</a>,
+<a href="v.univar.html">v.univar</a>
+</em>
+
+<p>
+Check also Python module from
+AddOns: <em><a href="http://grass.osgeo.org/grass70/manuals/addons/d.vect.thematic2.html">d.vect.thematic2</a></em>
+
+<h2>AUTHOR</h2>
+
+Moritz Lennert
+
+<p>
+<i>Last changed: $Date: 2015-08-11 23:06:03 +0200 (Út, 11 srp 2015) $</i>
Added: grass-addons/grass7/display/d.vect.legend/draw.c
===================================================================
--- grass-addons/grass7/display/d.vect.legend/draw.c (rev 0)
+++ grass-addons/grass7/display/d.vect.legend/draw.c 2016-07-01 14:06:24 UTC (rev 68826)
@@ -0,0 +1,208 @@
+/* draw.c:
+ *
+ * With do_bg=1 compute position of all legend graphic item and draw only background.
+ * Eith do_bg=0 compute position of all legend graphic item and draw all.
+ *
+ * Copyright (C) 2016 by Adam Laza, GSoC 2016, and the GRASS Development Team*
+ * This program is free software under the GPL (>=v2)
+ * Read the COPYING file that comes with GRASS for details.
+ */
+
+#include <string.h>
+#include <grass/display.h>
+#include <grass/glocale.h>
+#include <grass/colors.h>
+#include "local_proto.h"
+
+void draw(char *file_name, char *sep, double LL, double LT, char *title, int bgcolor, int bcolor,
+ int do_bg, char* tit_font, int tit_size, char *sub_font, int sub_size, char *font,
+ int fontsize, int fontcolor)
+{
+ double db, dt, dl, dr;
+ double bb, bt, bl, br;
+ double x0, y0;
+ double offs_y, row_ind;
+ FILE *file_in;
+ char buf[512];
+ int got_new;
+ SYMBOL *Symb;
+ char *symb_name, *line_color_str, *fill_color_str, *label;
+ double size, line_width;
+ double row_w, row_h;
+ RGBA_Color *line_color, *fill_color;
+ int ret, R, G, B;
+ char *part, *sub_delim;
+ double maxlblw, sym_lbl_space;
+
+
+
+ D_get_src(&dt, &db, &dl, &dr);
+ x0 = dl + (int)((dr - dl) * LL / 100.);
+ y0 = dt + (int)((db - dt) * (100 - LT) / 100.);
+
+ maxlblw = 0;
+ offs_y = 0;
+
+ /* Draw title */
+ if (strlen(title) > 0) {
+ D_text_size(tit_size, tit_size);
+ D_font(tit_font);
+ D_get_text_box(title, &bb, &bt, &bl, &br);
+ row_h = bb - bt;
+ offs_y += row_h;
+ maxlblw = br - bl;
+ if (! do_bg) {
+ D_pos_abs(x0, y0 + offs_y);
+ D_use_color(fontcolor);
+ D_text(title);
+ }
+ }
+
+ file_in = fopen(file_name, "r");
+ if (!file_in)
+ G_fatal_error(_("Unable to open input file <%s>"), file_name);
+
+ row_ind = 5;
+ sym_lbl_space = 5;
+ sub_delim = G_malloc(strlen(buf)+1);
+ snprintf(sub_delim, sizeof(sub_delim), "%s%s%s%s%s", sep, sep, sep, sep, sep);
+
+ got_new = G_getl2(buf, sizeof(buf), file_in);
+ G_strip(buf);
+ while (got_new) {
+ if (strstr(buf, sub_delim) != NULL) {
+ label = G_malloc(strlen(buf)+1);
+ part = strtok(buf, sep);
+ sscanf(part, "%s", label);
+
+ D_text_size(sub_size, sub_size);
+ D_font(sub_font);
+ D_get_text_box(label, &bb, &bt, &bl, &br);
+ row_h = bb - bt;
+ row_w = br - bb;
+ offs_y += row_h + row_ind;
+ if (row_w > maxlblw)
+ maxlblw = row_w;
+
+ if (! do_bg) {
+ D_pos_abs(x0, y0 + offs_y);
+ D_use_color(fontcolor);
+ D_text(label);
+ }
+ }
+ else {
+ /* Parse the line */
+ symb_name = G_malloc(strlen(buf) + 1);
+ line_color_str = G_malloc(strlen(buf) + 1);
+ fill_color_str = G_malloc(strlen(buf) + 1);
+ label = G_malloc(strlen(buf)+1);
+ line_color = G_malloc(sizeof(RGBA_Color));
+ fill_color = G_malloc(sizeof(RGBA_Color));
+
+ part = strtok(buf, sep);
+ sscanf(part, "%s", symb_name);
+ part = strtok(NULL, sep);
+ sscanf(part, "%s", line_color_str);
+ part = strtok(NULL, sep);
+ sscanf(part, "%s", fill_color_str);
+ part = strtok(NULL, sep);
+ sscanf(part, "%lf", &size);
+ part = strtok(NULL, sep);
+ sscanf(part, "%lf", &line_width);
+ part = strtok(NULL, sep);
+ sscanf(part, "%s", label);
+
+ /* Symbol */
+ Symb = S_read(symb_name);
+ if (Symb == NULL)
+ G_warning(_("Cannot read symbol"));
+ else
+ S_stroke(Symb, size, 0, 0);
+ /* parse line color */
+ ret = G_str_to_color(line_color_str, &R, &G, &B);
+ line_color->r = (unsigned char)R;
+ line_color->g = (unsigned char)G;
+ line_color->b = (unsigned char)B;
+ if (ret == 1)
+ /* here alpha is only used as an on/off switch, otherwise unused by the display drivers */
+ line_color->a = RGBA_COLOR_OPAQUE;
+ else if (ret == 2)
+ line_color->a = RGBA_COLOR_NONE;
+ else
+ G_warning(_("[%s]: No such color"), line_color_str);
+ /* parse fill color */
+ ret = G_str_to_color(fill_color_str, &R, &G, &B);
+ fill_color->r = (unsigned char)R;
+ fill_color->g = (unsigned char)G;
+ fill_color->b = (unsigned char)B;
+ if (ret == 1)
+ fill_color->a = RGBA_COLOR_OPAQUE;
+ else if (ret == 2)
+ fill_color->a = RGBA_COLOR_NONE;
+ else
+ G_warning(_("[%s]: No such color"), fill_color_str);
+
+ /* Label */
+ D_text_size(fontsize, fontsize);
+ D_font(font);
+ D_get_text_box(label, &bb, &bt, &bl, &br);
+ row_h = bb - bt;
+ row_w = row_h + sym_lbl_space + br - bb;
+ offs_y += row_h + row_ind;
+ if (row_w > maxlblw)
+ maxlblw = row_w;
+
+ if (! do_bg) {
+ S_stroke(Symb, size, 0, 0);
+ D_symbol(Symb, x0 + row_h/2, y0 + offs_y - row_h/2, line_color, fill_color);
+
+ D_pos_abs(x0 + row_h + sym_lbl_space, y0 + offs_y);
+ D_use_color(fontcolor);
+ D_text(label);
+ }
+ } /* end of Parse the line */
+
+ got_new = G_getl2(buf, sizeof(buf), file_in);
+ G_strip(buf);
+ }
+
+ // // Pomocny ctverec
+ // D_begin();
+ // D_move_abs(x0, y0 + offs_y);
+ // D_cont_abs(x0 + row_h, y0 + offs_y);
+ // D_cont_abs(x0 + row_h, y0 + offs_y - row_h);
+ // D_cont_abs(x0, y0 + offs_y - row_h);
+ // D_close();
+ // D_end();
+ // D_stroke();
+ // // Pomocny ctverec
+
+ fclose(file_in);
+
+ /* Draw background */
+ if (do_bg) {
+ double x0bg, y0bg, x1bg, y1bg, bg_margin;
+ bg_margin = 10;
+ x0bg = x0 - bg_margin;
+ y0bg = y0 - bg_margin;
+ x1bg = x0 + maxlblw + bg_margin;
+ y1bg = y0 + offs_y + bg_margin;
+
+ if (bgcolor) {
+ D_use_color(bgcolor);
+ D_box_abs(x0bg, y0bg, x1bg, y1bg);
+ }
+
+ D_use_color(bcolor);
+ D_begin();
+ D_move_abs(x0bg, y0bg);
+ D_cont_abs(x0bg, y1bg);
+ D_cont_abs(x1bg, y1bg);
+ D_cont_abs(x1bg, y0bg);
+ D_close();
+ D_end();
+ D_stroke();
+ }
+
+ D_save_command(G_recreate_command());
+}
Added: grass-addons/grass7/display/d.vect.legend/local_proto.h
===================================================================
--- grass-addons/grass7/display/d.vect.legend/local_proto.h (rev 0)
+++ grass-addons/grass7/display/d.vect.legend/local_proto.h 2016-07-01 14:06:24 UTC (rev 68826)
@@ -0,0 +1,3 @@
+/* draw.c */
+void draw(char *, char *, double, double, char *, int, int,
+ int, char *, int, char *, int, char *, int, int);
Added: grass-addons/grass7/display/d.vect.legend/main.c
===================================================================
--- grass-addons/grass7/display/d.vect.legend/main.c (rev 0)
+++ grass-addons/grass7/display/d.vect.legend/main.c 2016-07-01 14:06:24 UTC (rev 68826)
@@ -0,0 +1,215 @@
+/*
+ ****************************************************************************
+ *
+ * MODULE: d.vect.legend
+ * AUTHOR(S): Adam Laza, CTU, GSoC 2016
+ * PURPOSE: Display a vector layer
+ * COPYRIGHT: (C) 2007-2016 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 <stdlib.h>
+#include <grass/display.h>
+#include <grass/glocale.h>
+#include "local_proto.h"
+
+
+int main(int argc, char **argv)
+{
+ struct GModule *module;
+ struct Option *opt_input, *opt_sep, *opt_at, *opt_font, *opt_fontsize, *opt_fontcolor,
+ *opt_title, *opt_tit_font, *opt_tit_fontsize, *opt_sub_font, *opt_sub_fontsize,
+ *opt_bcolor, *opt_bgcolor;
+ struct Flag *fl_bg;
+
+ double LL, LT;
+ char *title, *file_name;
+ int bcolor, bgcolor, do_bg;
+ char *sep;
+ int fontsize, fontcolor, tit_size, sub_size;
+ char *font, *tit_font, *sub_font;
+
+
+ /* Initialize the GIS calls */
+ G_gisinit(argv[0]);
+
+ module = G_define_module();
+ G_add_keyword(_("display"));
+ G_add_keyword(_("cartography"));
+ G_add_keyword(_("vector"));
+ G_add_keyword(_("legend"));
+ module->description =
+ _("Displays a vector legend "
+ "in the active graphics frame.");
+
+ opt_input = G_define_standard_option(G_OPT_F_INPUT);
+ opt_input->required = YES;
+ opt_input->key = "input";
+ opt_input->description = _("Input file");
+ opt_input->guisection = _("Input");
+
+ opt_sep = G_define_standard_option(G_OPT_F_SEP);
+ opt_sep->key = "delim";
+ opt_sep->description = _("Text separator");
+ opt_sep->guisection = _("Input");
+
+ opt_at = G_define_option();
+ opt_at->key = "at";
+ opt_at->key_desc = "left,top";
+ opt_at->type = TYPE_DOUBLE;
+ opt_at->options = "0-100";
+ opt_at->answer = "20,80";
+ opt_at->required = NO;
+ opt_at->description =
+ _("Screen position of legend to be drawn (percentage, [0,0] is lower left)");
+ opt_at->guisection = _("Position");
+
+ opt_title = G_define_option();
+ opt_title->key = "title";
+ opt_title->type = TYPE_STRING;
+ opt_title->required = NO;
+ opt_title->description = _("Legend title");
+ opt_title->guisection = _("Title");
+
+ opt_bcolor = G_define_standard_option(G_OPT_CN);
+ opt_bcolor->key = "border_color";
+ opt_bcolor->answer = "black";
+ opt_bcolor->label = _("Border color");
+ opt_bcolor->guisection = _("Background");
+
+ opt_bgcolor = G_define_standard_option(G_OPT_CN);
+ opt_bgcolor->key = "bgcolor";
+ opt_bgcolor->answer = "white";
+ opt_bgcolor->label = _("Background color");
+ opt_bgcolor->guisection = _("Background");
+
+ opt_tit_font = G_define_option();
+ opt_tit_font->key = "title_font";
+ opt_tit_font->type = TYPE_STRING;
+ opt_tit_font->required = NO;
+ opt_tit_font->description = _("Title font name");
+ opt_tit_font->guisection = _("Font settings");
+
+ opt_tit_fontsize = G_define_option();
+ opt_tit_fontsize->key = "title_fontsize";
+ opt_tit_fontsize->type = TYPE_DOUBLE;
+ opt_tit_fontsize->required = NO;
+ opt_tit_fontsize->options = "1-360";
+ opt_tit_fontsize->label = _("Title font size");
+ opt_tit_fontsize->description = _("Default: 18");
+ opt_tit_fontsize->guisection = _("Title");
+
+ opt_sub_font = G_define_option();
+ opt_sub_font->key = "sub_font";
+ opt_sub_font->type = TYPE_STRING;
+ opt_sub_font->required = NO;
+ opt_sub_font->description = _("Subtitle font name");
+ opt_sub_font->guisection = _("Font settings");
+
+ opt_sub_fontsize = G_define_option();
+ opt_sub_fontsize->key = "sub_fontsize";
+ opt_sub_fontsize->type = TYPE_DOUBLE;
+ opt_sub_fontsize->required = NO;
+ opt_sub_fontsize->options = "1-360";
+ opt_sub_fontsize->label = _("Subtitle font size");
+ opt_sub_fontsize->description = _("Default: 14");
+ opt_sub_fontsize->guisection = _("Font settings");
+
+ 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_fontsize = G_define_option();
+ opt_fontsize->key = "fontsize";
+ opt_fontsize->type = TYPE_DOUBLE;
+ opt_fontsize->required = NO;
+ opt_fontsize->options = "1-360";
+ opt_fontsize->label = _("Font size");
+ opt_fontsize->description = _("Default: 12");
+ opt_fontsize->guisection = _("Font settings");
+
+ opt_fontcolor = G_define_standard_option(G_OPT_CN);
+ opt_fontcolor->key = "fontcolor";
+ opt_fontcolor->answer = "black";
+ opt_fontcolor->label = _("font color");
+ opt_fontcolor->guisection = _("Font settings");
+
+ fl_bg = G_define_flag();
+ fl_bg->key = 'b';
+ fl_bg->description = _("Display legend background");
+ fl_bg->guisection = _("Background");
+
+
+ /* Check command line */
+ if (G_parser(argc, argv)) {
+ exit(EXIT_FAILURE);
+ }
+
+ D_open_driver();
+ D_setup_unity(0);
+
+ /* parse and check options and flags */
+ file_name = opt_input->answer;
+ sep = G_option_to_separator(opt_sep);
+
+ if (opt_at->answer) {
+ sscanf(opt_at->answers[0], "%lf", &LL);
+ sscanf(opt_at->answers[1], "%lf", <);
+ }
+
+ if (opt_title->answer)
+ title = opt_title->answer;
+ else
+ title = "";
+
+ /* Background */
+ do_bg = fl_bg->answer;
+ bcolor = D_parse_color(opt_bcolor->answer, TRUE);
+ bgcolor = D_parse_color(opt_bgcolor->answer, TRUE);
+
+ /* Font settings */
+ if (opt_font->answer)
+ font = opt_font->answer;
+ else
+ font = "sans";
+ if (opt_fontsize->answer != NULL)
+ sscanf(opt_fontsize->answer, "%d", &fontsize);
+ else
+ fontsize = 14;
+
+ if (opt_tit_font->answer)
+ tit_font = opt_tit_font->answer;
+ else
+ tit_font = font;
+ if (opt_tit_fontsize->answer)
+ sscanf(opt_tit_fontsize->answer, "%d", &tit_size);
+ else
+ tit_size = fontsize;
+
+ if (opt_sub_font->answer)
+ sub_font = opt_sub_font->answer;
+ else
+ sub_font = font;
+ if (opt_sub_fontsize->answer)
+ sscanf(opt_sub_fontsize->answer, "%d", &sub_size);
+ else
+ sub_size = fontsize;
+
+ fontcolor = D_parse_color(opt_fontcolor->answer, FALSE); /*default color: black */
+
+ if (do_bg)
+ draw(file_name, sep, LL, LT, title, bgcolor, bcolor, 1, tit_font, tit_size, sub_font, sub_size, font, fontsize, fontcolor);
+
+ draw(file_name, sep, LL, LT, title, bgcolor, bcolor, 0, tit_font, tit_size, sub_font, sub_size, font, fontsize, fontcolor);
+
+ D_close_driver();
+
+ exit(EXIT_SUCCESS);
+}
Added: grass-addons/grass7/display/d.vect.legend/pipe.csv
===================================================================
--- grass-addons/grass7/display/d.vect.legend/pipe.csv (rev 0)
+++ grass-addons/grass7/display/d.vect.legend/pipe.csv 2016-07-01 14:06:24 UTC (rev 68826)
@@ -0,0 +1,11 @@
+|||||buildings
+basic/box|red|grey|15|2|hospital
+basic/box|red|yellow|15|3|firefighters
+basic/box|blue|yellow|15|3|police
+|||||land_use
+basic/octagon|green|blue|15|20|water
+basic/octagon|yellow|brown|15|3|fields
+basic/octagon|black|gray|15|3|urban
+|||||transport
+geology/strike_parallel|green|none|7|1|lajna
+geology/strike_line|blue|none|7|1|znak4
More information about the grass-commit
mailing list