Browse Source

v.distance: Linear matrix becomes default, even with -pa, new -s flag for square matrix (#601) [API change]

mlennert 4 years ago
parent
commit
2c614dd322
2 changed files with 32 additions and 20 deletions
  1. 15 11
      vector/v.distance/main.c
  2. 17 9
      vector/v.distance/v.distance.html

+ 15 - 11
vector/v.distance/main.c

@@ -73,7 +73,7 @@ int main(int argc, char *argv[])
 	struct Option *sep;
     } opt;
     struct {
-	struct Flag *print, *all;
+	struct Flag *print, *all, *square;
     } flag;
 
     int print;			/* -p: print to stdout */
@@ -184,7 +184,7 @@ int main(int argc, char *argv[])
     opt.upload = G_define_option();
     opt.upload->key = "upload";
     opt.upload->type = TYPE_STRING;
-    opt.upload->required = NO;
+    opt.upload->required = YES;
     opt.upload->multiple = YES;
     opt.upload->guisection = _("From");
     opt.upload->options = "cat,dist,to_x,to_y,to_along,to_angle,to_attr";
@@ -254,19 +254,23 @@ int main(int argc, char *argv[])
 	  "or uploaded to a new table created by the 'table' option; "
 	  "multiple 'upload' options may be used.");
 
+    flag.square = G_define_flag();
+    flag.square->key = 's';
+    flag.square->label =
+	_("Print output as square matrix (only possible for one single upload variable)");
+
     /* GUI dependency */
     opt.from->guidependency = G_store(opt.from_field->key);
     sprintf(buf1, "%s,%s", opt.to_field->key, opt.to_column->key);
     opt.to->guidependency = G_store(buf1);
     opt.to_field->guidependency = G_store(opt.to_column->key);
 
-    G_option_required(opt.upload, opt.out, NULL);
-    G_option_exclusive(opt.column, flag.print, NULL);
-    G_option_exclusive(opt.table, flag.print, NULL);
-    G_option_requires(opt.upload, flag.print, opt.column, NULL);
+    G_option_required(opt.out, opt.table, flag.print, NULL);
+    G_option_exclusive(opt.out, opt.table, flag.print, NULL);
+    G_option_requires(opt.upload, flag.print, opt.table, opt.column, NULL);
     G_option_requires(opt.column, opt.upload, NULL);
     G_option_requires(flag.print, opt.upload, NULL);
-    G_option_requires_all(opt.table, opt.out, opt.upload, NULL);
+    G_option_requires_all(opt.table, opt.upload, NULL);
 
     if (G_parser(argc, argv))
 	exit(EXIT_FAILURE);
@@ -286,8 +290,9 @@ int main(int argc, char *argv[])
     print = flag.print->answer;
     create_map = opt.out->answer != NULL;
     create_table = opt.table->answer != NULL;
-    update_table = !create_table && opt.column->answer;
+    update_table = !print && !create_table && opt.column->answer;
     do_all = flag.all->answer;
+    print_as_matrix = flag.square->answer;
 
     if (do_all && update_table)
 	G_fatal_error(_("Updating the from= table is not supported with -a"));
@@ -298,9 +303,8 @@ int main(int argc, char *argv[])
     while (opt.upload->answer && opt.upload->answers[i])
 	i++;
 
-    /* -a !table= upload=one_value from=map to=map: print as matrix */
-    print_as_matrix = do_all && !create_table && i == 1 &&
-		      strcmp(opt.from->answer, opt.to->answer) == 0;
+    if (i > 1 && print_as_matrix)
+	G_fatal_error(_("Square matrix output only possible with one single upload variable"));
 
     /* TODO: Known issue. Segmentation fault on print_as_matrix with dmin= or
      * dmax= because count may not be nfrom^2. Needs to populate Near[] fully

+ 17 - 9
vector/v.distance/v.distance.html

@@ -64,6 +64,10 @@ degrees but in meters calculated as geodesic distances on a sphere.
 If one or both of the input vector maps are 3D, the user is notified
 accordingly.
 
+<p>The <em>-p</em> flag prints the results to standard output. By default the
+output is in form of a linear matrix. If only only variable is upploaded and 
+a square matrix is desired, the user can set the <em>-s</em> flag.
+
 <h2>EXAMPLES</h2>
 
 <h3>Find nearest lines</h3>
@@ -192,19 +196,22 @@ from_cat|distance
 
 <h3>Print distance matrix</h3>
 
-Note: Matrix-style output is enabled only for flag <em>-a</em> and one
-given upload option.
-<p>
-Spearfish sample data location:
+North Carolina sample data location
+
+<p>As linear matrix:
 <div class="code"><pre>
-v.distance -pa from=archsites to=archsites upload=dist
+v.distance -pa from=hospitals to=hospitals upload=dist,to_attr to_column=NAME separator=tab
+from_cat    to_cat  dist    to_attr
+1   1   0   Cherry Hospital
+1   2   7489.1043632983983  Wayne Memorial Hospital
+1   3   339112.17046729225  Watauga Medical Center
+1   4   70900.392145909267  Central Prison Hospital
+1   5   70406.227393921712  Dorothea Dix Hospital
 </pre></div>
 
-<p>
-North Carolina sample data location:
-
+<p>As square matrix (only possible with single upload option):
 <div class="code"><pre>
-v.distance -pa from=hospitals to=hospitals upload=dist separator=tab
+v.distance -pas from=hospitals to=hospitals upload=dist separator=tab
 from_cat to_cat       dist
               1          2          3          4          5 ...
 1             0    7489.10  339112.17   70900.39   70406.23 ...
@@ -234,6 +241,7 @@ Matrix-like output by Martin Landa, FBK-irst, Trento, Italy<br>
 Improved processing speed: Markus Metz<br>
 Distance from any feature to any feature: Markus Metz<br>
 New table without the -p flag: Huidae Cho
+Make linear matrix the default for all outputs: Moritz Lennert
 
 <!--
 <p>