[GRASS-SVN] r58349 - in sandbox/martinl: . v.merge

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Dec 1 21:30:28 PST 2013


Author: martinl
Date: 2013-12-01 21:30:28 -0800 (Sun, 01 Dec 2013)
New Revision: 58349

Added:
   sandbox/martinl/v.merge/
   sandbox/martinl/v.merge/Makefile
   sandbox/martinl/v.merge/local_proto.h
   sandbox/martinl/v.merge/main.c
   sandbox/martinl/v.merge/merge.c
   sandbox/martinl/v.merge/v.merge.html
Log:
v.merge (sandbox) - experiments

Added: sandbox/martinl/v.merge/Makefile
===================================================================
--- sandbox/martinl/v.merge/Makefile	                        (rev 0)
+++ sandbox/martinl/v.merge/Makefile	2013-12-02 05:30:28 UTC (rev 58349)
@@ -0,0 +1,12 @@
+MODULE_TOPDIR = ../..
+
+PGM = v.merge
+
+LIBES = $(VECTORLIB) $(GISLIB)
+DEPENDENCIES = $(VECTORDEP) $(GISDEP)
+EXTRA_INC = $(VECT_INC)
+EXTRA_CFLAGS = $(VECT_CFLAGS)
+
+include $(MODULE_TOPDIR)/include/Make/Module.make
+
+default: cmd 

Added: sandbox/martinl/v.merge/local_proto.h
===================================================================
--- sandbox/martinl/v.merge/local_proto.h	                        (rev 0)
+++ sandbox/martinl/v.merge/local_proto.h	2013-12-02 05:30:28 UTC (rev 58349)
@@ -0,0 +1,2 @@
+int merge_map_lines(struct Map_info *, double,
+                    struct Map_info *);

Added: sandbox/martinl/v.merge/main.c
===================================================================
--- sandbox/martinl/v.merge/main.c	                        (rev 0)
+++ sandbox/martinl/v.merge/main.c	2013-12-02 05:30:28 UTC (rev 58349)
@@ -0,0 +1,93 @@
+/****************************************************************
+ *
+ * MODULE:     v.merge
+ *
+ * PURPOSE:    Merge two vector maps
+ *
+ * AUTHOR(S):  Martin Landa <landa.martin gmail.com>
+ *
+ * COPYRIGHT:  (C) 2013 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.
+ * 
+ * TODO:       add 'layer' options
+ *             handle feature types (currently assuming boundaries or lines)
+ ****************************************************************/
+
+#include <stdlib.h>
+
+#include <grass/gis.h>
+#include <grass/vector.h>
+#include <grass/glocale.h>
+
+#include "local_proto.h"
+
+int main(int argc, char *argv[])
+{
+    struct GModule *module;
+    struct {
+        struct Option *refer_map, *patch_map, *thresh, *output_map;
+    } opt;
+
+    struct Map_info Refer, Patch, Out;
+    
+    double thresh;
+    
+    G_gisinit(argv[0]);
+
+    module = G_define_module();
+    G_add_keyword(_("vector"));
+    G_add_keyword(_("geometry"));
+    G_add_keyword(_("editing"));
+    module->description = _("Merges two vector maps.");
+
+    opt.refer_map = G_define_standard_option(G_OPT_V_MAP);
+    opt.refer_map->key = "reference_map";
+    opt.refer_map->label = _("Name of reference vector map to be edited");
+
+    opt.patch_map = G_define_standard_option(G_OPT_V_MAP);
+    opt.patch_map->key = "patch_map";
+    opt.patch_map->label = _("Name of vector map to be patched");
+
+    opt.thresh = G_define_option();
+    opt.thresh->key = "thresh";
+    opt.thresh->type = TYPE_DOUBLE;
+    opt.thresh->required = YES;
+    opt.thresh->description = _("Threshold in map units");
+
+    opt.output_map = G_define_standard_option(G_OPT_V_OUTPUT);
+    
+    if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+
+    thresh = atof(opt.thresh->answer);
+    
+    Vect_check_input_output_name(opt.refer_map->answer, opt.output_map->answer,
+				 G_FATAL_EXIT);
+    Vect_check_input_output_name(opt.patch_map->answer, opt.output_map->answer,
+				 G_FATAL_EXIT);
+
+    /* open input maps */
+    Vect_open_old(&Refer, opt.refer_map->answer, "");
+    Vect_open_old(&Patch, opt.patch_map->answer, "");
+    /* open output map */
+    Vect_open_new(&Out,   opt.output_map->answer, Vect_is_3d(&Refer));
+
+    /* copy features from reference map to output map */
+    Vect_copy_map_lines(&Refer, &Out);
+    Vect_close(&Refer);
+    
+    Vect_build(&Out);
+    
+    /* read and patch features from patch map */
+    merge_map_lines(&Patch, thresh, &Out);
+
+    Vect_close(&Patch);
+
+    Vect_build(&Out);
+    Vect_close(&Out);
+
+    exit(EXIT_SUCCESS);
+}

Added: sandbox/martinl/v.merge/merge.c
===================================================================
--- sandbox/martinl/v.merge/merge.c	                        (rev 0)
+++ sandbox/martinl/v.merge/merge.c	2013-12-02 05:30:28 UTC (rev 58349)
@@ -0,0 +1,100 @@
+#include <grass/vector.h>
+
+int merge_map_lines(struct Map_info *Patch, double thresh,
+                    struct Map_info *Out)
+{
+    int i, j, iline, type, with_z, nearest_line, last_line;
+    int iseg, pseg, nseg; /* current/previous/next segment */
+    int last_modified, last_type;
+    double dist;
+    
+    struct line_pnts *Points, *Points_ref;
+    struct line_cats *Cats_ref;
+    
+    Points = Vect_new_line_struct();
+    Points_ref = Vect_new_line_struct();
+    Cats_ref   = Vect_new_cats_struct();
+    
+    with_z = Vect_is_3d(Out) && Vect_is_3d(Patch);
+    
+    /* read features from patch map */
+    Vect_rewind(Patch);
+    iline = 1;
+    while(TRUE) {
+        last_line = -1;
+        last_modified = FALSE;
+        type = Vect_read_next_line(Patch, Points, NULL);
+        if (type == -2)
+            break;
+
+        /* read line verteces */
+        for (i = 0; i < Points->n_points; i++) {
+            /* find nearest features from reference map */
+            nearest_line = Vect_find_line(Out, Points->x[i], Points->y[i], Points->z[i],
+                                          GV_LINES, thresh, with_z, -1);
+            G_debug(0, "line: %d, vertex: %d -> nearest: %d", iline, i, nearest_line);
+
+            if (nearest_line > 0) {
+                if (nearest_line != last_line) {
+                    if (last_modified) {
+                        Vect_rewrite_line(Out, last_line, last_type, Points_ref, Cats_ref);
+                        last_modified = FALSE;
+                    }
+                        
+                    last_type = Vect_read_line(Out, Points_ref, Cats_ref, nearest_line);
+                    last_line = nearest_line;
+                    
+                }
+                Vect_line_distance(Points_ref, Points->x[i], Points->y[i], Points->z[i],
+                                   with_z, NULL, NULL, NULL, &dist, NULL, NULL);
+                G_debug(0, "\t-> dist: %f", dist);
+            }
+            else {
+                if (last_line != -1) {
+                    /* modify cached line, insert new point */
+                    iseg = Vect_line_distance(Points_ref, Points->x[i], Points->y[i], Points->z[i],
+                                       with_z, NULL, NULL, NULL, &dist, NULL, NULL);
+                    G_debug(0, "\t-> dist: %f, iseg: %d", dist, iseg);
+                    pseg = nseg = 0;
+                    if (i > 0) {
+                        /* find previous vertex */
+                        pseg = Vect_line_distance(Points_ref, Points->x[i-1], Points->y[i-1],
+                                                  Points->z[i-1],
+                                                  with_z, NULL, NULL, NULL, &dist, NULL, NULL);
+                        G_debug(0, "\t(previous) -> dist: %f, pseg: %d", dist, pseg);
+                    }
+                    
+                    if (i < Points->n_points-1) {
+                      /* find last vertex */
+                      nseg = Vect_line_distance(Points_ref, Points->x[i+1], Points->y[i+1],
+                                                Points->z[i+1],
+                                                with_z, NULL, NULL, NULL, &dist, NULL, NULL);
+                        G_debug(0, "\t(next) -> dist: %f, nseg: %d", dist, nseg);
+                    }
+
+                    for (j = 1; j < nseg-pseg; j++) {
+                        G_debug(0, "%d", pseg);
+                        Vect_line_delete_point(Points_ref, pseg+1);
+                    }
+
+                    Vect_line_insert_point(Points_ref, pseg > 0 ? pseg+1 : iseg,
+                                           Points->x[i], Points->y[i], Points->z[i]);
+
+                    last_modified = TRUE;
+                }
+            }
+        }
+
+        if (last_modified) {
+            Vect_rewrite_line(Out, last_line, last_type, Points_ref, Cats_ref);
+            last_modified = FALSE;
+        }        
+        iline++;
+    }
+
+    Vect_destroy_line_struct(Points);
+    Vect_destroy_line_struct(Points_ref);
+    Vect_destroy_cats_struct(Cats_ref);
+
+    return 0;
+}

Added: sandbox/martinl/v.merge/v.merge.html
===================================================================
--- sandbox/martinl/v.merge/v.merge.html	                        (rev 0)
+++ sandbox/martinl/v.merge/v.merge.html	2013-12-02 05:30:28 UTC (rev 58349)
@@ -0,0 +1 @@
+TODO



More information about the grass-commit mailing list