[GRASS-SVN] r56563 - in grass-addons/grass7/vector: . v.net.turntable

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Jun 2 10:14:10 PDT 2013


Author: turek
Date: 2013-06-02 10:14:10 -0700 (Sun, 02 Jun 2013)
New Revision: 56563

Added:
   grass-addons/grass7/vector/v.net.turntable/
   grass-addons/grass7/vector/v.net.turntable/Makefile
   grass-addons/grass7/vector/v.net.turntable/main.c
   grass-addons/grass7/vector/v.net.turntable/ttb.c
   grass-addons/grass7/vector/v.net.turntable/v.net.turntable.html
Modified:
   grass-addons/grass7/vector/Makefile
Log:
v.net.turntable module added

Modified: grass-addons/grass7/vector/Makefile
===================================================================
--- grass-addons/grass7/vector/Makefile	2013-06-02 17:02:37 UTC (rev 56562)
+++ grass-addons/grass7/vector/Makefile	2013-06-02 17:14:10 UTC (rev 56563)
@@ -6,6 +6,7 @@
 	v.in.redwg \
 	v.in.wfs2 \
 	v.median \
+	v.net.turntable \
 	v.out.ply \
 	v.surf.icw
 

Added: grass-addons/grass7/vector/v.net.turntable/Makefile
===================================================================
--- grass-addons/grass7/vector/v.net.turntable/Makefile	                        (rev 0)
+++ grass-addons/grass7/vector/v.net.turntable/Makefile	2013-06-02 17:14:10 UTC (rev 56563)
@@ -0,0 +1,12 @@
+MODULE_TOPDIR = ../..
+
+PGM=v.net.turntable
+
+LIBES = $(VECTORLIB) $(GISLIB) $(DBMILIB)
+DEPENDENCIES = $(VECTORDEP) $(GISDEP) $(DBMIDEP)
+EXTRA_INC = $(VECT_INC)
+EXTRA_CFLAGS = $(VECT_CFLAGS)
+
+include $(MODULE_TOPDIR)/include/Make/Module.make
+
+default: cmd	


Property changes on: grass-addons/grass7/vector/v.net.turntable/Makefile
___________________________________________________________________
Added: svn:executable
   + *

Added: grass-addons/grass7/vector/v.net.turntable/main.c
===================================================================
--- grass-addons/grass7/vector/v.net.turntable/main.c	                        (rev 0)
+++ grass-addons/grass7/vector/v.net.turntable/main.c	2013-06-02 17:14:10 UTC (rev 56563)
@@ -0,0 +1,228 @@
+
+/****************************************************************
+ *
+ * MODULE:     v.net.turntable
+ *
+ * AUTHOR(S):  GRASS Development Team
+ *             Štěpán Turek <stepan.turek seznam.cz>
+ *             
+ * PURPOSE:    Create sql table representing line graph,
+ *             which describes possible turns on network.
+ *
+ * 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.
+ *
+ ****************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <grass/gis.h>
+#include <grass/vector.h>
+#include <grass/dbmi.h>
+#include <grass/glocale.h>
+
+/*
+IDEA: This module could be included into v.net module 
+     (both modules do service for vector network analyses)???
+*/
+
+
+void close_db(void *p)
+{
+    dbDriver *driver = (dbDriver *) p;
+
+    db_close_database_shutdown_driver(driver);
+}
+
+int main(int argc, char *argv[])
+{
+    struct GModule *module;
+
+    struct Map_info InMap, OutMap;
+    struct field_info *fi;
+
+    struct Option *opt_in_map, *opt_out_map;
+    struct Option *opt_ttb_name, *opt_afield, *opt_tfield, *opt_tucfield, *opt_type;
+    char *database_name, *driver_name;
+
+    int i_field_num, field_num, i_field, type;
+
+    char *ttb_name;
+    char *key_col;
+    int tfield, tucfield, afield;
+
+    char buf[DB_SQL_MAX];
+    dbDriver *driver;
+
+    dbString db_buf;
+
+    /* initialize module */
+    module = G_define_module();
+    G_add_keyword(_("vector"));
+    G_add_keyword(_("network"));
+    G_add_keyword(_("turntable"));
+    G_add_keyword(_("turns"));
+    module->description = _("Create table which represents turns.");
+
+    opt_in_map = G_define_standard_option(G_OPT_V_INPUT);
+    opt_out_map = G_define_standard_option(G_OPT_V_OUTPUT);
+
+    opt_type = G_define_standard_option(G_OPT_V_TYPE);
+    opt_type->options = "line,boundary";
+    opt_type->answer = "line,boundary";
+    opt_type->label = _("Arc type");
+
+    opt_afield = G_define_standard_option(G_OPT_V_FIELD);
+    opt_afield->description =
+	_
+	("Arcs layer which will be expanded by turntable. Format: layer number[/layer name]");
+    opt_afield->answer = "1";
+    opt_afield->key = "alayer";
+    opt_afield->required = YES;
+
+    opt_tfield = G_define_standard_option(G_OPT_V_FIELD);
+    opt_tfield->description =
+	_
+	("Layer where turntable will be attached. Format: layer number[/layer name]");
+    opt_tfield->answer = "3";
+    opt_tfield->key = "tlayer";
+    opt_tfield->required = YES;
+
+    opt_tucfield = G_define_standard_option(G_OPT_V_FIELD);
+    opt_tucfield->description =
+	_
+	("Node layer with unique categories for every line in alayer and point. The points are placed on every node. Format: layer number[/layer name]");
+    opt_tucfield->answer = "4";
+    opt_tucfield->key = "tuclayer";
+    opt_tucfield->required = YES;
+
+    opt_ttb_name = G_define_option();
+    opt_ttb_name->key = "tname";
+    opt_ttb_name->label = _("Turntable name");
+    opt_ttb_name->description =
+	_
+	("If not given, name consists of input vector map name + \"_\" + tlayer + \"_turntable_\" + alayer e. g. roads_3_turntable_1");
+    opt_ttb_name->type = TYPE_STRING;
+
+    G_gisinit(argv[0]);
+
+    if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+
+    if (Vect_open_old(&InMap, opt_in_map->answer, "") < 2) {
+	G_fatal_error(_("Unable to open vector map <%s>."),
+		      opt_in_map->answer);
+    }
+    Vect_set_error_handler_io(&InMap, &OutMap);
+
+    type = Vect_option_to_types(opt_type);
+
+    afield   = Vect_get_field_number(&InMap, opt_afield->answer);
+    tfield   = Vect_get_field_number(&InMap, opt_tfield->answer);
+    tucfield = Vect_get_field_number(&InMap, opt_tucfield->answer);
+
+    if(!Vect_get_field (&InMap, afield))
+        G_fatal_error(_("Arc layer <%s> does not exists in map <%s>."),
+                         opt_afield->answer, opt_out_map->answer);        
+
+    if(Vect_get_field (&InMap, tfield))
+        G_warning(_("Layer <%s> already exists in map <%s>.\nIt will be overwritten by tlayer data."),
+                     opt_tfield->answer, opt_out_map->answer);  
+
+    if(Vect_get_field (&InMap, tucfield))
+        G_warning(_("Layer <%s> already exists in map <%s>.\nIt will be overwritten by tuclayer data."),
+                    opt_tucfield->answer, opt_out_map->answer); 
+
+    ttb_name = NULL;
+    if (!opt_ttb_name->answer) {
+    G_asprintf(&ttb_name, "%s_%s_turntable_%s", 
+               opt_out_map->answer, opt_tfield->answer, opt_afield->answer);
+    }
+    else {
+    ttb_name = G_store(opt_out_map->answer);
+    }
+
+    if (Vect_open_new(&OutMap, opt_out_map->answer, WITHOUT_Z) < 1) {
+	G_fatal_error(_("Unable to create vector map <%s>."),
+		      opt_out_map->answer);
+    }
+
+    /*Use database and driver as layer with lowest number, 
+       if the layer is not present use def settings. */
+    field_num = -1;
+    for (i_field = 0; i_field < Vect_cidx_get_num_fields(&InMap); i_field++) {
+	i_field_num = Vect_cidx_get_field_number(&InMap, i_field);
+	if (Vect_map_check_dblink(&InMap, i_field_num, NULL) == 0)
+	    continue;
+
+    if(field_num == -1)
+	   field_num = i_field_num;
+
+    if(field_num != tfield && field_num != tucfield)
+        Vect_copy_tables(&InMap, &OutMap, field_num);
+    }
+
+    if (field_num < 0) {
+	driver_name = (char *)db_get_default_driver_name();
+	database_name = (char *)db_get_default_database_name();
+    }
+    else {
+	fi = Vect_get_field(&InMap, field_num);
+	driver_name = fi->driver;
+	database_name = fi->database;
+    }
+
+    driver = db_start_driver_open_database(driver_name, database_name);
+    if (driver == NULL)
+	G_fatal_error(_("Unable to open database <%s> using driver <%s>"),
+		      database_name, driver_name);
+    G_add_error_handler(close_db, driver);
+
+    key_col = "cat";
+    sprintf(buf,
+	    "CREATE TABLE %s (%s INTEGER, ln_from INTEGER, ln_to INTEGER, "
+	    "cost DOUBLE PRECISION, isec INTEGER, angle DOUBLE PRECISION)",
+	    ttb_name, key_col);
+
+    db_init_string(&db_buf);
+    db_set_string(&db_buf, buf);
+
+    if (db_execute_immediate(driver, &db_buf) != DB_OK) {
+	db_free_string(&db_buf);
+	G_fatal_error(_("Unable to create turntable <%s>."), ttb_name);
+    }
+    db_free_string(&db_buf);
+
+    if (Vect_map_add_dblink(&OutMap, tfield,
+			    NULL, ttb_name, key_col,
+			    database_name, driver_name) == -1) {
+	G_fatal_error(_("Unable to connect table <%s> to vector map <%s>."),
+		      ttb_name, opt_in_map->answer);
+    }
+
+    if (db_create_index2(driver, ttb_name, key_col) != DB_OK)
+	G_warning(_("Unable to create index for column <%s> in table <%s>."),
+		  key_col, ttb_name);
+
+    Vect_build_partial(&OutMap, GV_BUILD_BASE);	/* switch to topological level */
+
+    populate_turntable(driver, &InMap, &OutMap, ttb_name, tfield,
+		       tucfield, afield, type);
+    Vect_close(&InMap);
+
+    close_db(driver);
+
+    Vect_close(&OutMap);
+
+    /*TODO why must be closed and opened again?*/
+    Vect_open_old(&OutMap, opt_out_map->answer, G_mapset());
+    Vect_build(&OutMap);
+
+    Vect_close(&OutMap);
+
+    exit(EXIT_SUCCESS);
+}


Property changes on: grass-addons/grass7/vector/v.net.turntable/main.c
___________________________________________________________________
Added: svn:executable
   + *

Added: grass-addons/grass7/vector/v.net.turntable/ttb.c
===================================================================
--- grass-addons/grass7/vector/v.net.turntable/ttb.c	                        (rev 0)
+++ grass-addons/grass7/vector/v.net.turntable/ttb.c	2013-06-02 17:14:10 UTC (rev 56563)
@@ -0,0 +1,583 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <grass/gis.h>
+#include <grass/vector.h>
+#include <grass/dbmi.h>
+#include <grass/glocale.h>
+
+
+double compute_line_nodes_angle(struct line_pnts *points)
+{
+    double x_start, y_start, z;
+    double x_end, y_end;
+    double x, y;
+    int n_points = Vect_get_num_line_points(points);
+
+    if (n_points < 2)
+        return (-9.0);
+
+    Vect_line_get_point(points, 0, &x_start, &y_start, &z);
+    Vect_line_get_point(points, n_points - 1, &x_end, &y_end, &z);
+
+    x = x_end - x_start;
+    y = y_end - y_start;
+
+    if (y == 0.0 && x == 0.0)
+        return (0.0);
+    else
+        return (atan2(y, x));
+}
+
+/*\brief Compute angle of two lines, which is defined by start and end point of the lines.
+
+   Parameters from_dir, to_dir defines defines line direction to node for which is angle defined 
+   (negative - line goes from node / positive line goes into node).
+
+   Angle is zero when lines are straight. If line_pnts_j to is on the left from line_pnts_i 
+   the angle is negative.
+
+   \return lines angle
+   \return -9.0 if line is defined by one point or has same start and end point
+ */
+
+double compute_lines_angle(struct line_pnts *line_pnts_from, int from_dir, 
+                            struct line_pnts *line_pnts_to, int to_dir)
+{
+    double angle_from, angle_to;
+    double angle;
+
+    double x1, x2, y1, y2, z;
+
+    int n_points_from = Vect_get_num_line_points(line_pnts_from);
+    int n_points_to = Vect_get_num_line_points(line_pnts_to);
+
+    Vect_line_get_point(line_pnts_from, 0, &x1, &y1, &z);
+    Vect_line_get_point(line_pnts_from, n_points_from - 1, &x2, &y2, &z);
+
+    if (x1 == x2 && y1 == y2)
+         return -9.0;
+
+    Vect_line_get_point(line_pnts_to, 0, &x1, &y1, &z);
+    Vect_line_get_point(line_pnts_to, n_points_to - 1, &x2, &y2, &z);
+    if (x1== x2 && y1 == y2)
+         return -9.0;
+
+    if (from_dir > 0)
+        Vect_line_reverse(line_pnts_from);
+
+    if (to_dir < 0)
+        Vect_line_reverse(line_pnts_to);
+
+    angle_from = compute_line_nodes_angle(line_pnts_from);
+    angle_to = compute_line_nodes_angle(line_pnts_to);
+
+    if (angle_from == -9.0)
+        angle = angle_from;
+    else if (angle_to == -9.0)
+        angle = angle_to;
+    else {
+        angle = angle_from - angle_to;
+
+        if (angle > M_PI)
+            angle = -2 * M_PI + angle;
+
+        if (angle < -M_PI)
+            angle = 2 * M_PI + angle;
+    }
+
+    /* reverse it back to original order */
+    if (from_dir > 0)
+        Vect_line_reverse(line_pnts_from);
+
+    if (to_dir < 0)
+        Vect_line_reverse(line_pnts_to);
+
+    return angle;
+}
+
+/*\brief Add line uturns into turntable.
+        
+    Add two records into turntable because every line has two possible U-turns.
+*/
+int add_uturn(dbDriver * driver, char *ttb_name, int * next_ttb_cat, int ln_cat, int isec_start_cat, int isec_end_cat){
+    int i, isec;
+    dbString db_buf;
+    char buf[DB_SQL_MAX];
+    db_init_string(&db_buf);
+
+    ln_cat = abs(ln_cat);
+
+    isec = isec_end_cat;
+    for(i = 0; i < 2; ++i) {
+        if(i == 1) {
+            ln_cat = -1 * ln_cat;
+            isec = isec_start_cat;
+        }
+        /* cat, ln_from, ln_to, cost, isec, angle */
+        sprintf(buf,
+                "INSERT INTO %s values ( %d, %d, %d, %f, %d, %f);",
+                ttb_name, (*next_ttb_cat), ln_cat, ln_cat * -1, 0.0,
+                isec, M_PI);
+        db_set_string(&db_buf, buf);
+
+        G_debug(3, "Adding u-turn into turntable:\n%s", db_get_string(&db_buf));
+
+        if (db_execute_immediate(driver, &db_buf) != DB_OK){
+            db_free_string(&db_buf);
+            return -1;
+        }
+        ++(*next_ttb_cat);
+    }
+
+    db_free_string(&db_buf);
+    return 1;
+}
+
+
+/*\brief Add turns for two lines into turntable.
+        
+    Add two records into turntable because we can take the turn from two opposite directions.
+*/
+int add_turns(dbDriver * driver, char *ttb_name, int * next_ttb_cat, 
+             int ln_i_cat, struct line_pnts *line_pnts_i, int ln_j_cat, struct line_pnts *line_pnts_j, int isec_cat){
+    int i, combinations;
+    int ln_f, ln_t;
+    dbString db_buf;
+    char buf[DB_SQL_MAX];
+    double angle;
+
+    db_init_string(&db_buf);
+
+    int ln_j_dir, ln_i_dir;
+    int ln_to_cat, ln_from_cat;
+
+    ln_i_dir = ln_i_cat;
+    ln_j_dir = ln_j_cat;
+
+    /* compute angle if the lines angle is computed from ln_from_cat to ln_to_cat */
+    if (ln_to_cat == ln_i_cat)
+        angle =
+        compute_lines_angle(line_pnts_j, ln_j_dir, 
+                            line_pnts_i,  ln_i_dir);
+    else
+        angle =
+            compute_lines_angle(line_pnts_i, ln_i_dir,
+                                line_pnts_j, ln_j_dir);
+
+    ln_i_cat = abs(ln_i_cat);
+    ln_j_cat = abs(ln_j_cat);
+
+    ln_from_cat = ln_i_cat;
+    ln_to_cat = ln_j_cat;
+
+    /*Find right lines nodes (positive or negative), will be connected by the turn.*/
+    if (ln_j_dir < 0 && ln_i_dir < 0)
+        ln_to_cat *= -1;
+
+    else if (ln_j_dir > 0 && ln_i_dir > 0)
+        ln_from_cat *= -1;
+
+    else if (ln_j_dir < 0) {
+        ln_to_cat = ln_i_cat;
+        ln_from_cat = ln_j_cat;
+    }
+
+    ln_f = ln_from_cat;
+    ln_t = ln_to_cat;
+
+    /*Create first turn from i to j, then create turn in the opposite direction.*/
+    for(i = 0; i < 2; ++i) {
+
+        /* connect right nodes for opposite turn */
+        if(i == 1) {
+            ln_f = ln_to_cat * -1;
+            ln_t = ln_from_cat * -1;
+        }
+
+        /* cat, ln_from, ln_to, cost, isec, angle */
+        sprintf(buf,
+                "INSERT INTO %s values ( %d, %d, %d, %f, %d,",
+                ttb_name, (*next_ttb_cat), ln_f, ln_t, 0.0,
+                isec_cat);
+        db_set_string(&db_buf, buf);
+
+        if (angle == -9.0)
+            db_append_string(&db_buf, "NULL)");
+        else {
+            /* the angle is on the other side in opposite turn (e. g. left -> right)*/
+            if(i == 1)
+                angle *= -1;
+
+            sprintf(buf, "%f)", angle);
+            db_append_string(&db_buf, buf);
+        }
+
+        G_debug(3, "Adding turn into turntable:\n%s", db_get_string(&db_buf));
+
+        if (db_execute_immediate(driver, &db_buf) != DB_OK){
+            db_free_string(&db_buf);
+            return -1;
+        }
+        ++(*next_ttb_cat);
+    }
+
+    db_free_string(&db_buf);
+    return 1;
+
+}
+
+
+void populate_turntable(dbDriver * driver, struct Map_info *InMap,
+                        struct Map_info *OutMap, char *ttb_name,
+                        int tfield, int tucfield, int a_field, int arc_type)
+{
+    char buf[DB_SQL_MAX];
+    struct ilist list;
+
+    int *features_id;
+
+    int n_node_lines, n_features, i_line, j_line, next_ttb_cat, i_ucat,
+        i_rew_lines, n_lines;
+    int n_nodes, pivot_node, outside_node, isec_start_ucat, isec_end_ucat, node1, node2;
+    int ln_i_id, ln_j_id, ln_i_ucat, ln_j_ucat;
+
+    int ltype_i, ltype_j;
+
+    struct line_pnts *line_pnts_i, *line_pnts_j;
+    struct line_cats *cats_i, *cats_j;
+
+    double x, y, z;
+
+    line_pnts_i = Vect_new_line_struct();
+    line_pnts_j = Vect_new_line_struct();
+    cats_i = Vect_new_cats_struct();
+    cats_j = Vect_new_cats_struct();
+
+    n_lines = 0;
+    if(arc_type & GV_LINE)
+        n_lines += Vect_get_num_primitives(InMap, GV_LINE);
+    if(arc_type & GV_BOUNDARY)
+        n_lines += Vect_get_num_primitives(InMap, GV_BOUNDARY);
+
+    /*Converts feature input map id into current id in output map. 
+       When the feature is rewritten, it's original state still exists 
+       just marked as dead. The new feature is written to first possible 
+       position and that is the id (all dead + all alive features + 1).
+
+       If feature id is 0 the feature was not already written into
+       output map. */
+
+    n_features = Vect_get_num_lines(InMap);
+
+    G_debug(3, "Found %d line features in <%s> vector map", n_features,
+            InMap->name);
+
+    features_id = G_malloc(sizeof(int) * n_features);
+    G_zero(features_id, sizeof(int) * n_features);
+
+    n_nodes = Vect_get_num_nodes(InMap);
+    G_debug(3, "Found %d nodes in <%s> vector map", n_nodes, InMap->name);
+
+    db_begin_transaction(driver);
+
+    /* Stores category for a next record/turn in turntable. */
+    next_ttb_cat = 1;
+
+    /* Stores number of category which will be assigned to a next feature added into tucfield. */
+    i_ucat = 1;
+
+    /* Stores number of rewritten features. If feature is rewritten, it can be sought by sum of
+       i_ucat + i_rew_lines variables values (id), when was rewritten. 
+       The value is saved into features_id array. */
+    i_rew_lines = 0;
+
+    G_init_ilist(&list);
+
+    /* Every node represents one intersection. */
+    for (pivot_node = 1; pivot_node <= n_nodes; pivot_node++) {
+        n_node_lines = Vect_get_node_n_lines(InMap, pivot_node);
+
+        G_debug(3, "Found %d lines connected to node with id %d",
+                n_node_lines, pivot_node);
+
+        /*Creates record in turntable for every possible turn 
+           in intersection defined by node and lines which meets on the node.
+
+           It also takes in account  U-turns. */
+
+        for (i_line = 0; i_line < n_node_lines; i_line++) {
+
+            ln_i_id = Vect_get_node_line(InMap, pivot_node, i_line);
+
+            /*Line was not written into output map. */
+            if (features_id[abs(ln_i_id) - 1] < 1) {
+                ltype_i = Vect_read_line(InMap, line_pnts_i, cats_i, abs(ln_i_id));
+                
+                /* If line does not belong into arc layer, skip it. */
+                if (Vect_field_cat_get(cats_i, a_field, &list) < 0 || !(ltype_i & arc_type))
+                    continue;
+
+                /* Delete categories in tfield and tucfield if they are in input map defined. */
+                Vect_field_cat_del(cats_i, tfield, -1);
+                Vect_field_cat_del(cats_i, tucfield, -1);
+            }
+
+            /*If i line was already written into output map, 
+               we need to take it's categories from the output map with categories 
+               for tlayer and tuclayer. */
+            else {
+                ln_i_id = Vect_get_node_line(InMap, pivot_node, i_line);
+
+                ltype_i = Vect_read_line(OutMap, line_pnts_i, cats_i,
+                                         features_id[abs(ln_i_id) - 1]);
+                Vect_cat_get(cats_i, tucfield, &ln_i_ucat);
+                
+                /* add line direction information to ucat */
+                if(ln_i_id < 0)
+                    ln_i_ucat *= -1;
+
+            }
+
+            for (j_line = i_line; j_line < n_node_lines; j_line++) {
+
+                ln_j_id = Vect_get_node_line(InMap, pivot_node, j_line);
+
+                /* write line, which have not been written into new map yet. */
+                if (features_id[abs(ln_j_id) - 1] < 1) {
+                    /* Get line from input map. */
+                    ltype_j = Vect_read_line(InMap, line_pnts_j, cats_j, abs(ln_j_id));
+
+                    /* If line does not belong into arc layer, skip it. */
+                    if (Vect_field_cat_get(cats_j, a_field, &list) < 0 || !(ltype_i & arc_type))
+                        continue;
+
+                    /* Delete categories in tfield and tucfield if they are in input map defined. */
+                    Vect_field_cat_del(cats_j, tfield, -1);
+                    Vect_field_cat_del(cats_j, tucfield, -1);
+
+                    /* Write U-turn for every not yet written line. */
+
+                    /* Assign unique category (assigned only when feature is written). */
+                    Vect_cat_set(cats_j, tucfield, i_ucat);
+
+                    /* Assign turn category in turntable for the U-turn. */
+                    Vect_cat_set(cats_j, tfield, next_ttb_cat);
+                    Vect_cat_set(cats_j, tfield, next_ttb_cat + 1);
+
+                    /* If i line and j line are different, we also need to insert a turn which is defined
+                       by these two edges, therefore we need to add new tfield category to j line, which is corresponding
+                       to the turn. */
+                    if (ln_j_id != ln_i_id) {
+                        Vect_cat_set(cats_j, tfield, next_ttb_cat);
+                        Vect_cat_set(cats_j, tfield, next_ttb_cat + 1);
+                    }
+
+                    ln_j_ucat = i_ucat;
+                    
+                    /* add line direction information to ucat */
+                    if(ln_j_id < 0)
+                        ln_j_ucat *= -1;
+
+                    /* We create two nodes in turntable for every line. These nodes have 
+                       positive and negative values, with their absolute values identical.
+
+                       Every node corresponds to opposite line direction. The positive node 
+                       matches the direction of line. The negative node matches the opposite direction.
+
+                       Imagine that you are standing on some road/line before a intersection wanting to cross it.
+                       If you are going to cross intersection, which is in line direction,
+                       you are standing on the POSITIVE NODE. If you would cross the intersection from any other line 
+                       to the line, you would come into the NEGATIVE NODE. 
+
+                       These two nodes are connected with U-turns, which are two for both direction. 
+                       Every U-turn direction belongs to the another intersection. U-turn from the POSITIVE NODE 
+                       to the NEGATIVE one belongs to the intersection we are going to cross. The other U-turn belongs 
+                       to a intersection in opposite end of the line.
+
+                       Turntable columns:
+
+                       cat - category in tfield (layer with turntable), which are hold by both ln_from and ln_to lines 
+                       ln_from - unique category in tucfield assigned to the line 
+                       ln_to - unique category in tucfield assigned to the line 
+                       cost - cost for turn from ln_from to ln_to
+                       isec - point category in tucfield, which represents the intersection, where the turn belongs
+                       angle - in radians, see comments in compute_lines_angle function, it is PI for U-turns
+                     */
+
+                    /* Find second node (outside_node) of the line. */
+                    Vect_get_line_nodes(InMap, abs(ln_j_id), &node1, &node2);
+
+                    if (node1 == pivot_node)
+                        outside_node = node2;
+                    else
+                        outside_node = node1;
+
+                    /* Decide intersection where U-turns belongs. */
+                    if (ln_j_id < 0) {
+                        isec_start_ucat = outside_node + n_lines;
+                        isec_end_ucat = pivot_node + n_lines;
+                    }
+                    else {
+                        isec_start_ucat = pivot_node + n_lines;
+                        isec_end_ucat = outside_node + n_lines;
+                    }
+
+                   
+                    /* If i and j lines are identical, write these categories also into i line,
+                       otherwise they would be forgotten during rewriting of i line. */
+                    if (ln_j_id == ln_i_id) {
+                        Vect_cat_set(cats_i, tfield, next_ttb_cat);
+                        Vect_cat_set(cats_i, tfield, next_ttb_cat + 1);
+                        Vect_cat_set(cats_i, tucfield, i_ucat);
+                    }
+
+                    if (add_uturn(driver, ttb_name, &next_ttb_cat, abs(ln_j_ucat), isec_start_ucat, isec_end_ucat) < 0) {
+                        G_free(features_id);
+
+                        Vect_destroy_line_struct(line_pnts_i);
+                        Vect_destroy_line_struct(line_pnts_j);
+                        Vect_destroy_cats_struct(cats_i);
+                        Vect_destroy_cats_struct(cats_j);
+
+                        G_fatal_error(_("Unable to insert data into turntable."));
+                    }
+
+                    /* increment unique category number for next line, which will be written */
+                    ++i_ucat;
+
+                    /* If i line and j line are different, we also need to insert a turn which is defined
+                       by these two edges, therefore we need to add new category to j line, which is corresponding
+                       to the turn.  
+                       */
+                    if (abs(ln_j_id) != abs(ln_i_id))
+                    {
+                        Vect_cat_set(cats_j, tfield, next_ttb_cat);
+                        Vect_cat_set(cats_j, tfield, next_ttb_cat + 1);
+                    }
+                    
+                    /* Write new line into output map and save it's id to be possible to find it and edit it later 
+                       (when we get to intersection, which is in other end of the line.) */
+                    features_id[abs(ln_j_id) - 1] =
+                        Vect_write_line(OutMap, ltype_j, line_pnts_j, cats_j);
+
+                    /* i, j lines  are equal, it consists only U-turn
+                       Absolute values are there because in case of the lines which have same start and end point, we do not want 
+                       to create redundant lines between these points. This combination has been already done by uturn method.
+                     */
+                    if (abs(ln_j_id) == abs(ln_i_id)) {
+                        /* remember unique category also for i line */
+                        ln_i_ucat = ln_j_ucat;
+                        continue;
+                    }
+                }
+                /* skip if i, j lines are same (U-turn was already written) */
+                else if (abs(ln_j_id) == abs(ln_i_id))
+                    continue;
+                /* write new turn combination for already written i, j lines into output map */
+                else {
+                    /* Get modified cats from out map also for j line, which was already written and 
+                       cats differ from the former cats in the line in input map. */
+                    ltype_j = Vect_read_line(OutMap, line_pnts_j, cats_j,
+                                             features_id[abs(ln_j_id) - 1]);
+
+                    /* set category in turntable for new turn, which will be written */
+                    Vect_cat_set(cats_j, tfield, next_ttb_cat);
+                    Vect_cat_set(cats_j, tfield, next_ttb_cat + 1);
+
+                    /* get already assigned unique category of the j line 
+                       (used for ln_from_cat or ln_to_cat in turntable) */
+                    Vect_cat_get(cats_j, tucfield, &ln_j_ucat);
+
+                    /* add line direction information to ucat */
+                    if(ln_j_id < 0)
+                        ln_j_ucat *= -1;
+
+                    /* rewrite j line with added the new category for the turn */
+                    Vect_rewrite_line(OutMap, features_id[abs(ln_j_id) - 1],
+                                      ltype_j, line_pnts_j, cats_j);
+
+                    /* Because of rewriting, the id of j line was changed, 
+                       therefore we have to update it in features_id. */
+                    features_id[abs(ln_j_id) - 1] = i_ucat + i_rew_lines;
+
+                    /* increment number of rewritten elements,  for more see initialization of the variable */
+                    ++i_rew_lines;
+                }
+
+                /* We have to decide which nodes will be connected, which depends on lines directions.
+                   Line direction information is stored in ln_i_id/ln_j_id variables. It the variable is 
+                   negative, the line goes into the intersection. If it is positive the line goes from the 
+                   intersection. */
+
+
+                /* The turn belongs to same intersection regardless the direction. Only exception are the U-turns. */
+                isec_start_ucat = isec_end_ucat = pivot_node + n_lines;
+
+                Vect_cat_set(cats_i, tfield, next_ttb_cat);
+                Vect_cat_set(cats_i, tfield, next_ttb_cat + 1);
+
+                if (add_turns(driver, ttb_name, &next_ttb_cat, 
+                             ln_i_ucat,line_pnts_i, ln_j_ucat, line_pnts_j, isec_start_ucat) < 0) {
+                    G_free(features_id);
+
+                    Vect_destroy_line_struct(line_pnts_i);
+                    Vect_destroy_line_struct(line_pnts_j);
+                    Vect_destroy_cats_struct(cats_i);
+                    Vect_destroy_cats_struct(cats_j);
+
+                    G_fatal_error(_("Unable to insert data into turntable."));
+                }
+
+            }
+
+            /* rewrite i line */
+            Vect_rewrite_line(OutMap, features_id[abs(ln_i_id) - 1],
+                              ltype_i, line_pnts_i, cats_i);
+
+            features_id[abs(ln_i_id) - 1] = i_ucat + i_rew_lines;
+            i_rew_lines += 1;
+        }
+    }
+
+    /* Create point on every node. */
+    /* TODO what if nodes are not used in alayer? 
+     */
+    for (pivot_node = 1; pivot_node <= n_nodes; pivot_node++) {
+
+        Vect_get_node_coor(InMap, pivot_node, &x, &y, &z);
+
+        Vect_reset_line(line_pnts_i);
+        Vect_append_point(line_pnts_i, x, y, z);
+
+        Vect_reset_cats(cats_i);
+        Vect_cat_set(cats_i, tucfield, i_ucat);
+
+        Vect_write_line(OutMap, GV_POINT, line_pnts_i, cats_i);
+        i_ucat++;
+    }
+
+    /* copy rest of features, to output map */
+    while ((ltype_i = Vect_read_next_line(InMap, line_pnts_i, cats_i)) > 0) {
+
+        /* line features in alayer are already in output map*/
+        if (ltype_i == arc_type && Vect_field_cat_get(cats_i, a_field, &list) != -1)
+            continue;
+
+        /* Delete categories in tfield and tucfield if they are in input map defined. */
+        Vect_field_cat_del(cats_i, tucfield, -1);   
+        Vect_field_cat_del(cats_i, tfield, -1);   
+
+        Vect_write_line(OutMap, GV_POINT, line_pnts_i, cats_i);
+    }
+
+
+    G_free(features_id);
+
+    Vect_destroy_line_struct(line_pnts_i);
+    Vect_destroy_line_struct(line_pnts_j);
+    Vect_destroy_cats_struct(cats_i);
+    Vect_destroy_cats_struct(cats_j);
+
+    db_commit_transaction(driver);
+    return;
+}
\ No newline at end of file

Added: grass-addons/grass7/vector/v.net.turntable/v.net.turntable.html
===================================================================
--- grass-addons/grass7/vector/v.net.turntable/v.net.turntable.html	                        (rev 0)
+++ grass-addons/grass7/vector/v.net.turntable/v.net.turntable.html	2013-06-02 17:14:10 UTC (rev 56563)
@@ -0,0 +1,25 @@
+<h2>DESCRIPTION</h2>
+
+<em>v.net.turntable</em> creates a turntable with the costs for every possible turn on every possible node (intersection, crossroad) in given layer (alayer). U-turns are taken in account too.
+<p>These modules support the turntable extension: <a href="v.net.alloc.html">v.net.alloc</a>, <a href="v.net.iso.html">v.net.iso</a>, <a href="v.net.path.html">v.net.path</a>, <a href="v.net.salesman.html">v.net.salesman</a>, <a href="v.net.steiner.html">v.net.steiner</a>. 
+
+<p>
+
+For more information about turns in the vector network analysis see 
+<a href="http://grasswiki.osgeo.org/wiki/Turns_in_the_vector_network_analysis">wiki page</a>.
+
+<h2>EXAMPLE</h2>
+
+<div class="code"><pre>
+ v.net.turntable input=roadsmajor output=roadsmajor_ttb
+</pre></div>
+
+<h2>AUTHORS</h2>
+
+Stepan Turek
+Documentation: Lukas Bocan
+
+The module was created as part of turns costs project at Czech Technical University in Prague, Czech Republic where participated also Eliska Kyzlikova and Viera Bejdova.
+mentor: Martin Landa
+    
+<p><i>Last changed: $Date: 2013-05-19 18:56:09 +0200 (Thu, 19 May 2013) $</i>


Property changes on: grass-addons/grass7/vector/v.net.turntable/v.net.turntable.html
___________________________________________________________________
Added: svn:executable
   + *



More information about the grass-commit mailing list