[GRASS-SVN] r69091 - grass-addons/grass7/display/d.legend.vect

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Aug 5 05:51:10 PDT 2016


Author: lazaa
Date: 2016-08-05 05:51:10 -0700 (Fri, 05 Aug 2016)
New Revision: 69091

Modified:
   grass-addons/grass7/display/d.legend.vect/d.legend.vect.html
   grass-addons/grass7/display/d.legend.vect/main.c
Log:
Input/Output file

Modified: grass-addons/grass7/display/d.legend.vect/d.legend.vect.html
===================================================================
--- grass-addons/grass7/display/d.legend.vect/d.legend.vect.html	2016-08-05 12:40:26 UTC (rev 69090)
+++ grass-addons/grass7/display/d.legend.vect/d.legend.vect.html	2016-08-05 12:51:10 UTC (rev 69091)
@@ -15,6 +15,8 @@
 Flag <b>-b</b> shows the background and user can specify its properties with <b>border_color, bgcolor, border_width</b>
 <p>
 <b>symbol_size</b> defines the size of line and area symbols. The size of point symbols is defined in legend file via <em><a href="d.vect.html">d.vect</a></em> command.
+<p>
+<b>output</b> defines path to file where the internal legend file ['GRASS_LEGEND_FILE'] will be saved to. <b>input</b> defines the input file which the vector legend will be based on. This input file must have correct format.
 
 
 <h2>EXAMPLES</h2>

Modified: grass-addons/grass7/display/d.legend.vect/main.c
===================================================================
--- grass-addons/grass7/display/d.legend.vect/main.c	2016-08-05 12:40:26 UTC (rev 69090)
+++ grass-addons/grass7/display/d.legend.vect/main.c	2016-08-05 12:51:10 UTC (rev 69091)
@@ -13,6 +13,8 @@
  *****************************************************************************/
 
 #include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
 #include <grass/display.h>
 #include <grass/glocale.h>
 #include "local_proto.h"
@@ -25,7 +27,7 @@
     struct Option *opt_at, *opt_cols, *opt_font, *opt_fontsize,
             *opt_fontcolor, *opt_title, *opt_tit_font, *opt_tit_fontsize, *opt_sub_font,
             *opt_sub_fontsize, *opt_bcolor, *opt_bgcolor, *opt_symb_size,
-            *opt_bg_width;
+            *opt_bg_width, *opt_output, *opt_input;
     struct Flag *fl_bg;
 
     double LL, LT;
@@ -34,6 +36,9 @@
     int fontsize, fontcolor, tit_size, sub_size;
     char *font, *tit_font, *sub_font;
     int cols, symb_size, bg_width;
+    char *out_file;
+    FILE *source, *target;
+    char buf[512];
 
 
     /* Initialize the GIS calls */
@@ -160,7 +165,19 @@
     fl_bg->description = _("Display legend background");
     fl_bg->guisection = _("Background");
 
+    opt_output = G_define_standard_option(G_OPT_F_OUTPUT);
+    opt_output->label = _("Output csv file");
+    opt_output->description = _("Path to output file or '-' "
+                                "for standard output");
+    opt_output->required = NO;
+    opt_output->guisection = _("In/Out");
 
+    opt_input = G_define_standard_option(G_OPT_F_INPUT);
+    opt_input->label = _("Input legend file");
+    opt_input->description = _("Path to legend file ");
+    opt_input->required = NO;
+    opt_input->guisection = _("In/Out");
+
     /* Check command line */
     if (G_parser(argc, argv)) {
         exit(EXIT_FAILURE);
@@ -170,10 +187,6 @@
     D_setup_unity(0);
 
     /* parse and check options and flags */
-    file_name = getenv("GRASS_LEGEND_FILE");
-    if (!file_name)
-        G_fatal_error("No legend file defined.");
-
     if (opt_at->answer) {
         sscanf(opt_at->answers[0], "%lf", &LL);
         sscanf(opt_at->answers[1], "%lf", &LT);
@@ -231,6 +244,41 @@
 
     fontcolor = D_parse_color(opt_fontcolor->answer, FALSE); /*default color: black */
 
+    /* I/O */
+    if (opt_input->answer) {
+        file_name = opt_input->answer;
+        if (!file_name)
+            G_fatal_error(_("Unable to open input file <%s>"), file_name);
+    }
+    else {
+        file_name = getenv("GRASS_LEGEND_FILE");
+        if (!file_name)
+            G_fatal_error("No legend file defined.");
+    }
+
+
+    if (opt_output->answer) {
+        source = fopen(file_name, "r");
+        if (!source)
+            G_fatal_error(_("Unable to open input file <%s>"), file_name);
+
+        if (strcmp(opt_output->answer,"-") == 0)
+            while (fgets (buf, sizeof(buf), source) != NULL)
+                puts (buf);
+        else {
+            out_file = opt_output->answer;
+            target = fopen(out_file, "w");
+            if (!target) {
+                fclose(source);
+                G_fatal_error(_("Unable to create output file <%s>"), out_file);
+            }
+            while (fgets (buf, sizeof(buf), source) != NULL)
+                fputs (buf, target);
+            fclose(target);
+        }
+        fclose(source);
+    }
+
     /* Pre-calculate the layout */
     if (do_bg)
         draw(file_name, LL, LT, title, cols, bgcolor, bcolor, bg_width, 1, tit_font, tit_size, sub_font, sub_size, font, fontsize, fontcolor, symb_size);



More information about the grass-commit mailing list