瀏覽代碼

v.net: new flag to snap points to network

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@53665 15284696-431f-4ddb-bdfa-cd5b030d7da7
Markus Metz 12 年之前
父節點
當前提交
6d44572bf1
共有 4 個文件被更改,包括 43 次插入14 次删除
  1. 7 0
      vector/v.net/args.c
  2. 32 11
      vector/v.net/connect.c
  3. 2 1
      vector/v.net/main.c
  4. 2 2
      vector/v.net/proto.h

+ 7 - 0
vector/v.net/args.c

@@ -83,6 +83,13 @@ void define_options(struct opt *opt)
     opt->cats_flag->label = _("Assign unique categories to new points");
     opt->cats_flag->description = _("For operation 'nodes'");
     opt->cats_flag->guisection = _("Nodes");
+
+    opt->snap_flag = G_define_flag();
+    opt->snap_flag->key = 's';
+    opt->snap_flag->label = _("Snap points to network");
+    opt->snap_flag->description =
+	_("For operation 'connect'. By default, a new line from the point to the network is created.");
+    opt->snap_flag->guisection = _("Nodes");
 }
 
 void parse_arguments(const struct opt *opt,

+ 32 - 11
vector/v.net/connect.c

@@ -18,10 +18,10 @@
  * \return number of new arcs
  */
 int connect_arcs(struct Map_info *In, struct Map_info *Pnts,
-		 struct Map_info *Out, int nfield, double thresh)
+		 struct Map_info *Out, int nfield, double thresh, int snap)
 {
     int narcs;
-    int type, line, seg, i, ltype;
+    int type, line, seg, i, ltype, broken;
     double px, py, pz, spdist, dist;
 
     struct line_pnts *Points, *Pline, *Pout;
@@ -63,13 +63,17 @@ int connect_arcs(struct Map_info *In, struct Map_info *Pnts,
 	if (seg == 0)
 	    G_fatal_error(_("Failed to find intersection segment"));
 	/* break the line */
+	broken = 0;
 	Vect_reset_line(Pout);
 	for (i = 0; i < seg; i++) {
 	    Vect_append_point(Pout, Pline->x[i], Pline->y[i], Pline->z[i]);
 	}
 	Vect_append_point(Pout, px, py, pz);
 	Vect_line_prune(Pout);
-	Vect_rewrite_line(Out, line, ltype, Pout, Cline);
+	if (Pout->n_points > 1) {
+	    Vect_rewrite_line(Out, line, ltype, Pout, Cline);
+	    broken++;
+	}
 
 	Vect_reset_line(Pout);
 	Vect_append_point(Pout, px, py, pz);
@@ -77,23 +81,40 @@ int connect_arcs(struct Map_info *In, struct Map_info *Pnts,
 	    Vect_append_point(Pout, Pline->x[i], Pline->y[i], Pline->z[i]);
 	}
 	Vect_line_prune(Pout);
-	Vect_write_line(Out, ltype, Pout, Cline);
+	if (Pout->n_points > 1) {
+	    if (broken)
+		Vect_write_line(Out, ltype, Pout, Cline);
+	    else
+		Vect_rewrite_line(Out, line, ltype, Pout, Cline);
+	    broken++;
+	}
+	if (broken == 2)
+	    narcs++;
 
 	if (dist > 0.0) {
-	    /* write new arc */
-	    Vect_reset_line(Pout);
-	    Vect_append_point(Pout, px, py, pz);
-	    Vect_append_point(Pout, Points->x[0], Points->y[0], Points->z[0]);
-	    Vect_write_line(Out, ltype, Pout, Cline);
+	    if (snap) {
+		/* snap point */
+		Points->x[0] = px;
+		Points->y[0] = py;
+		Points->z[0] = pz;
+	    }
+	    else {
+		/* write new arc */
+		Vect_reset_line(Pout);
+		Vect_append_point(Pout, px, py, pz);
+		Vect_append_point(Pout, Points->x[0], Points->y[0], Points->z[0]);
+		Vect_write_line(Out, ltype, Pout, Cline);
+
+		narcs++;
+	    }
 	}
 
 	/* add points to 'nfield' layer */
 	for (i = 0; i < Cats->n_cats; i++) {
 	    Cats->field[i] = nfield;	/* all points to 'nfield' layer */
 	}
-	Vect_write_line(Out, type, Points, Cats);
 
-	narcs++;
+	Vect_write_line(Out, type, Points, Cats);
     }
 
     Vect_destroy_line_struct(Points);

+ 2 - 1
vector/v.net/main.c

@@ -138,7 +138,8 @@ int main(int argc, char **argv)
 	    int narcs;
 
 	    if (act == TOOL_CONNECT)
-		narcs = connect_arcs(In, Points, Out, nfield, thresh);
+		narcs = connect_arcs(In, Points, Out, nfield, thresh,
+		                     opt.snap_flag->answer);
 	    else
 		narcs = create_arcs(file_arcs, Points, Out, afield, nfield);
 

+ 2 - 2
vector/v.net/proto.h

@@ -10,7 +10,7 @@ struct opt {
     struct Option *action;
     struct Option *afield_opt, *nfield_opt, *thresh_opt;
     struct Option *file;
-    struct Flag *cats_flag;
+    struct Flag *cats_flag, *snap_flag;
 };
 
 /* arcs.c */
@@ -24,7 +24,7 @@ void parse_arguments(const struct opt *,
 
 /* connect.c */
 int connect_arcs(struct Map_info *, struct Map_info *,
-		 struct Map_info *, int, double);
+		 struct Map_info *, int, double, int);
 
 /* nodes.c */
 int nodes(struct Map_info *, struct Map_info *, int,