[GRASS-SVN] r35052 - in grass/branches/develbranch_6: scripts vector vector/v.to.3d

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Dec 26 11:48:31 EST 2008


Author: martinl
Date: 2008-12-26 11:48:31 -0500 (Fri, 26 Dec 2008)
New Revision: 35052

Added:
   grass/branches/develbranch_6/vector/v.to.3d/
   grass/branches/develbranch_6/vector/v.to.3d/Makefile
   grass/branches/develbranch_6/vector/v.to.3d/args.c
   grass/branches/develbranch_6/vector/v.to.3d/description.html
   grass/branches/develbranch_6/vector/v.to.3d/local_proto.h
   grass/branches/develbranch_6/vector/v.to.3d/main.c
   grass/branches/develbranch_6/vector/v.to.3d/trans2.c
   grass/branches/develbranch_6/vector/v.to.3d/trans3.c
Removed:
   grass/branches/develbranch_6/scripts/v.to.3d/
Modified:
   grass/branches/develbranch_6/scripts/Makefile
   grass/branches/develbranch_6/vector/Makefile
Log:
v.to.3d: bash script -> C module


Modified: grass/branches/develbranch_6/scripts/Makefile
===================================================================
--- grass/branches/develbranch_6/scripts/Makefile	2008-12-26 16:43:29 UTC (rev 35051)
+++ grass/branches/develbranch_6/scripts/Makefile	2008-12-26 16:48:31 UTC (rev 35052)
@@ -78,7 +78,6 @@
 	v.out.gpsbabel \
 	v.rast.stats \
 	v.report \
-	v.to.3d \
 	v.univar.sh \
 	v.what.vect
 

Modified: grass/branches/develbranch_6/vector/Makefile
===================================================================
--- grass/branches/develbranch_6/vector/Makefile	2008-12-26 16:43:29 UTC (rev 35051)
+++ grass/branches/develbranch_6/vector/Makefile	2008-12-26 16:48:31 UTC (rev 35052)
@@ -60,6 +60,7 @@
 	v.surf.idw \
 	v.surf.rst \
 	v.transform \
+	v.to.3d \
 	v.to.db \
 	v.to.points \
 	v.to.rast \


Property changes on: grass/branches/develbranch_6/vector/v.to.3d
___________________________________________________________________
Name: svn:ignore
   + *.tmp.html
*OBJ*


Added: grass/branches/develbranch_6/vector/v.to.3d/Makefile
===================================================================
--- grass/branches/develbranch_6/vector/v.to.3d/Makefile	                        (rev 0)
+++ grass/branches/develbranch_6/vector/v.to.3d/Makefile	2008-12-26 16:48:31 UTC (rev 35052)
@@ -0,0 +1,11 @@
+MODULE_TOPDIR = ../..
+
+PGM=v.to.3d
+
+LIBES = $(VECTLIB) $(DBMILIB) $(GISLIB)
+DEPENDENCIES = $(VECTDEP) $(DBMIDEP) $(GISDEP)
+EXTRA_CFLAGS = $(VECT_CFLAGS)
+
+include $(MODULE_TOPDIR)/include/Make/Module.make
+
+default: cmd	


Property changes on: grass/branches/develbranch_6/vector/v.to.3d/Makefile
___________________________________________________________________
Name: svn:mime-type
   + text/x-sh
Name: svn:keywords
   + Author Date Id
Name: svn:eol-style
   + native

Added: grass/branches/develbranch_6/vector/v.to.3d/args.c
===================================================================
--- grass/branches/develbranch_6/vector/v.to.3d/args.c	                        (rev 0)
+++ grass/branches/develbranch_6/vector/v.to.3d/args.c	2008-12-26 16:48:31 UTC (rev 35052)
@@ -0,0 +1,44 @@
+#include <grass/gis.h>
+#include <grass/glocale.h>
+
+#include "local_proto.h"
+
+void parse_args(struct opts *opt)
+{
+    opt->reverse = G_define_flag();
+    opt->reverse->key = 'r';
+    opt->reverse->description =
+	_("Reverse transformation; 3D vector features to 2D");
+
+    opt->table = G_define_flag();
+    opt->table->key = 't';
+    opt->table->description = _("Do not copy table");
+
+    opt->input = G_define_standard_option(G_OPT_V_INPUT);
+
+    opt->output = G_define_standard_option(G_OPT_V_OUTPUT);
+
+    opt->type = G_define_standard_option(G_OPT_V_TYPE);
+    opt->type->options = "point,line,boundary,centroid";
+    opt->type->answer = "point,line,boundary,centroid";
+
+    opt->height = G_define_option();
+    opt->height->key = "height";
+    opt->height->type = TYPE_DOUBLE;
+    opt->height->required = NO;
+    opt->height->multiple = NO;
+    opt->height->description = _("Fixed height for 3D vector features");
+    opt->height->guisection = _("Height");
+
+    opt->field = G_define_standard_option(G_OPT_V_FIELD);
+    opt->field->guisection = _("Height");
+
+    opt->column = G_define_standard_option(G_OPT_COLUMN);
+    opt->column->label = _("Name of attribute column used for height");
+    opt->column->description =
+	_("Can be used for reverse transformation, to store height of points");
+
+    opt->column->guisection = _("Height");
+
+    return;
+}


Property changes on: grass/branches/develbranch_6/vector/v.to.3d/args.c
___________________________________________________________________
Name: svn:mime-type
   + text/x-csrc
Name: svn:keywords
   + Author Date Id
Name: svn:eol-style
   + native

Added: grass/branches/develbranch_6/vector/v.to.3d/description.html
===================================================================
--- grass/branches/develbranch_6/vector/v.to.3d/description.html	                        (rev 0)
+++ grass/branches/develbranch_6/vector/v.to.3d/description.html	2008-12-26 16:48:31 UTC (rev 35052)
@@ -0,0 +1,50 @@
+<h2>DESCRIPTION</h2>
+
+The <em>v.to.3d</em> module is used to transform 2D vector features
+to 3D. Height (z-coordinate) of 3D vector features can be specified
+by <b>height</b> parameter as fixed value or by <b>column</b>
+parameter.
+
+<p>
+Flag <b>-r</b> enables to perform reverse transformation, i.e.,
+transform 3D vector to 2D by omitting z-coordinate.
+
+<h2> NOTES</h2>
+
+When transforming 2D vector features to 3D based on attribute, all
+NULL values are silently converted to height 0.0.
+
+<h2>EXAMPLES</h2>
+
+<h3>Transform 2D vector features to 3D</h3>
+
+<div class="code"><pre>
+# convert z-values from string to double
+v.db.addcol map=geodetic_pts columns="Z_VALUE_D double precision"
+v.db.update map=geodetic_pts column=Z_VALUE_D qcolumn=Z_VALUE
+v.db.select map=geodetic_pts columns=cat,Z_VALUE,Z_VALUE_
+
+# convert 2D vector point map to 3D based on attribute
+v.to.3d input=geodetic_pts out=geodetic_pts_3d column=Z_VALUE_D
+</pre></div>
+
+<h3>Transform 3D vector features to 2D</h3>
+
+<div class="code"><pre>
+v.to.3d -rt input=elev_lid792_bepts output=elev_lid_2d
+</pre></div>
+
+<h2>SEE ALSO</h2>
+
+<em>
+<a href="v.transform.html">v.transform</a>,
+<a href="v.extrude.html">v.extrude</a>,
+<a href="v.drape.html">v.drape</a>
+</em>
+
+<h2>AUTHORS</h2>
+
+Martin Landa, CTU in Prague, Czech Republic
+
+<p>
+<i>Last changed: $Date$</i>


Property changes on: grass/branches/develbranch_6/vector/v.to.3d/description.html
___________________________________________________________________
Name: svn:mime-type
   + text/html
Name: svn:keywords
   + Author Date Id
Name: svn:eol-style
   + native

Added: grass/branches/develbranch_6/vector/v.to.3d/local_proto.h
===================================================================
--- grass/branches/develbranch_6/vector/v.to.3d/local_proto.h	                        (rev 0)
+++ grass/branches/develbranch_6/vector/v.to.3d/local_proto.h	2008-12-26 16:48:31 UTC (rev 35052)
@@ -0,0 +1,22 @@
+#include <grass/gis.h>
+#include <grass/Vect.h>
+
+struct opts {
+  struct Flag *reverse, *table;
+  
+  struct Option *input, *output;
+  struct Option *type;
+  struct Option *height;
+  struct Option *field, *column;
+};
+
+/* args.c */
+void parse_args(struct opts *);
+
+/* trans2.c */
+int trans2d(struct Map_info *, struct Map_info *, int,
+	    double, int, const char *);
+
+/* trans3.c */
+int trans3d(struct Map_info *, struct Map_info *, int,
+	    int, const char *);


Property changes on: grass/branches/develbranch_6/vector/v.to.3d/local_proto.h
___________________________________________________________________
Name: svn:mime-type
   + text/x-chdr
Name: svn:keywords
   + Author Date Id
Name: svn:eol-style
   + native

Added: grass/branches/develbranch_6/vector/v.to.3d/main.c
===================================================================
--- grass/branches/develbranch_6/vector/v.to.3d/main.c	                        (rev 0)
+++ grass/branches/develbranch_6/vector/v.to.3d/main.c	2008-12-26 16:48:31 UTC (rev 35052)
@@ -0,0 +1,136 @@
+
+/****************************************************************************
+ *
+ * MODULE:       v.to.3d
+ *
+ * AUTHOR(S):    Martin Landa <landa.martin gmail.com>
+ *               
+ * PURPOSE:      Performs transformation of 2D vector features to 3D
+ *
+ * COPYRIGHT:    (C) 2008 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 <stdlib.h>
+#include <grass/gis.h>
+#include <grass/Vect.h>
+#include <grass/glocale.h>
+
+#include "local_proto.h"
+
+int main(int argc, char **argv)
+{
+    struct GModule *module;
+    struct opts opt;
+    struct Map_info In, Out;
+    BOUND_BOX box;
+    int field, type;
+
+    G_gisinit(argv[0]);
+
+    module = G_define_module();
+    module->keywords = _("vector, transformation, 3D");
+    module->description =
+	_("Performs transformation of 2D vector features to 3D.");
+
+    parse_args(&opt);
+
+    if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+
+    field = atoi(opt.field->answer);
+    type = Vect_option_to_types(opt.type);
+
+    if (!opt.reverse->answer) {
+	if ((!opt.height->answer && !opt.column->answer) ||
+	    (opt.height->answer && opt.column->answer)) {
+	    G_fatal_error(_("Either '%s' or '%s' parameter have to be used"),
+			  opt.height->key, opt.column->key);
+	}
+    }
+    else {
+	if (opt.height->answer) {
+	    G_warning(_("Parameters '%s' ignored"), opt.height->key);
+	}
+    }
+
+    if (opt.reverse->answer && opt.table->answer) {
+	G_fatal_error(_("Attribute table required"));
+    }
+
+    Vect_check_input_output_name(opt.input->answer, opt.output->answer,
+				 GV_FATAL_EXIT);
+
+    /* open input vector, topology not needed */
+    Vect_set_open_level(1);
+    if (Vect_open_old(&In, opt.input->answer, "") < 1)
+	G_fatal_error(_("Unable to open vector map <%s>"), opt.input->answer);
+
+    if (opt.reverse->answer && !Vect_is_3d(&In)) {
+	Vect_close(&In);
+	G_fatal_error(_("Vector map <%s> is 2D"), opt.input->answer);
+    }
+
+    if (!opt.reverse->answer && Vect_is_3d(&In)) {
+	Vect_close(&In);
+	G_fatal_error(_("Vector map <%s> is 3D"), opt.input->answer);
+    }
+
+    /* create output vector */
+    Vect_set_open_level(2);
+    if (Vect_open_new(&Out, opt.output->answer,
+		      opt.reverse->answer ? WITHOUT_Z : WITH_Z) == -1)
+	G_fatal_error(_("Unable to create vector map <%s>"),
+		      opt.output->answer);
+
+    /* copy history & header */
+    Vect_hist_copy(&In, &Out);
+    Vect_hist_command(&Out);
+    Vect_copy_head_data(&In, &Out);
+
+    if (opt.reverse->answer && !opt.table->answer) {
+	G_message(_("Copying attributes..."));
+	if (Vect_copy_tables(&In, &Out, 0) == -1) {
+	    G_warning(_("Unable to copy attributes"));
+	}
+    }
+
+    G_message(_("Transforming features..."));
+    if (opt.reverse->answer) {
+	/* 3d -> 2d */
+	trans3d(&In, &Out, type, field, opt.column->answer);
+    }
+    else {
+	/* 2d -> 3d */
+	double height = 0.;
+
+	if (opt.height->answer) {
+	    height = atof(opt.height->answer);
+	}
+	trans2d(&In, &Out, type, height, field, opt.column->answer);
+    }
+
+    if (!opt.reverse->answer && !opt.table->answer) {
+	G_message(_("Copying attributes..."));
+	if (Vect_copy_tables(&In, &Out, 0) == -1) {
+	    G_warning(_("Unable to copy attributes"));
+	}
+    }
+
+    Vect_close(&In);
+    Vect_build(&Out);
+
+    if (!opt.reverse->answer) {
+	Vect_get_map_box(&Out, &box);
+	G_message(_("Vertical extent of vector map <%s>: B: %f T: %f"),
+		  opt.output->answer, box.B, box.T);
+    }
+
+    Vect_close(&Out);
+
+    exit(EXIT_SUCCESS);
+}


Property changes on: grass/branches/develbranch_6/vector/v.to.3d/main.c
___________________________________________________________________
Name: svn:mime-type
   + text/x-csrc
Name: svn:keywords
   + Author Date Id
Name: svn:eol-style
   + native

Added: grass/branches/develbranch_6/vector/v.to.3d/trans2.c
===================================================================
--- grass/branches/develbranch_6/vector/v.to.3d/trans2.c	                        (rev 0)
+++ grass/branches/develbranch_6/vector/v.to.3d/trans2.c	2008-12-26 16:48:31 UTC (rev 35052)
@@ -0,0 +1,126 @@
+#include <grass/gis.h>
+#include <grass/Vect.h>
+#include <grass/dbmi.h>
+#include <grass/glocale.h>
+
+#include "local_proto.h"
+
+/*!
+   \brief transform 2d vector features to 3d
+
+   \param In input vector
+   \param Out output vector
+   \param type feature type to be transformed
+   \param height fixed height (used only if column is NULL)
+   \param field layer number
+   \param column attribute column used for height
+
+   \return number of writen features
+ */
+int trans2d(struct Map_info *In, struct Map_info *Out, int type,
+	    double height, int field, const char *column)
+{
+    int i, ltype, line;
+    int cat;
+    int ret, ctype;
+
+    struct line_pnts *Points;
+    struct line_cats *Cats;
+
+    dbCatValArray cvarr;
+
+    Points = Vect_new_line_struct();
+    Cats = Vect_new_cats_struct();
+
+    db_CatValArray_init(&cvarr);
+
+    if (column) {
+	struct field_info *Fi;
+
+	dbDriver *driver;
+
+	Fi = Vect_get_field(In, field);
+	if (!Fi)
+	    G_fatal_error(_("Database connection not defined for layer %d"),
+			  field);
+
+	driver = db_start_driver_open_database(Fi->driver, Fi->database);
+	if (!driver) {
+	    G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
+			  Fi->database, Fi->driver);
+	}
+
+	/* column type must numeric */
+	ctype = db_column_Ctype(driver, Fi->table, column);
+	if (ctype == -1)
+	    G_fatal_error(_("Column <%s> not found in table <%s>"),
+			  column, Fi->table);
+	if (ctype != DB_C_TYPE_INT && ctype != DB_C_TYPE_DOUBLE) {
+	    G_fatal_error(_("Column must be numeric"));
+	}
+
+	db_select_CatValArray(driver, Fi->table, Fi->key,
+			      column, NULL, &cvarr);
+
+	G_debug(3, "%d records selected", cvarr.n_values);
+
+	db_close_database_shutdown_driver(driver);
+    }
+
+    line = 1;
+    while (1) {
+	ltype = Vect_read_next_line(In, Points, Cats);
+	if (ltype == -1) {
+	    G_fatal_error(_("Unable to read vector map"));
+	}
+	if (ltype == -2) {	/* EOF */
+	    break;
+	}
+
+	if (G_verbose() > G_verbose_min() && (line - 1) % 1000 == 0) {
+	    fprintf(stderr, "%7d\b\b\b\b\b\b\b", (line - 1));
+	}
+
+	if (!(ltype & type))
+	    continue;
+
+	if (column) {
+	    Vect_cat_get(Cats, field, &cat);
+	    if (cat < 0) {
+		G_warning(_("Skipping feature without category"));
+		continue;
+	    }
+
+	    if (ctype == DB_C_TYPE_DOUBLE)
+		ret = db_CatValArray_get_value_double(&cvarr, cat, &height);
+	    else {		/* integer */
+
+		int height_i;
+
+		ret = db_CatValArray_get_value_int(&cvarr, cat, &height_i);
+		height = (double)height_i;
+	    }
+
+	    if (ret != DB_OK)
+		G_warning(_("Unable to get height for feature category %d"),
+			  cat);
+	}
+
+	for (i = 0; i < Points->n_points; i++) {
+	    Points->z[i] = height;
+	}
+
+	Vect_write_line(Out, ltype, Points, Cats);
+
+	line++;
+    }
+
+    if (G_verbose() > G_verbose_min())
+	fprintf(stderr, "\r");
+
+
+    Vect_destroy_line_struct(Points);
+    Vect_destroy_cats_struct(Cats);
+
+    return line - 1;
+}


Property changes on: grass/branches/develbranch_6/vector/v.to.3d/trans2.c
___________________________________________________________________
Name: svn:mime-type
   + text/x-csrc
Name: svn:keywords
   + Author Date Id
Name: svn:eol-style
   + native

Added: grass/branches/develbranch_6/vector/v.to.3d/trans3.c
===================================================================
--- grass/branches/develbranch_6/vector/v.to.3d/trans3.c	                        (rev 0)
+++ grass/branches/develbranch_6/vector/v.to.3d/trans3.c	2008-12-26 16:48:31 UTC (rev 35052)
@@ -0,0 +1,158 @@
+#include <grass/gis.h>
+#include <grass/Vect.h>
+#include <grass/dbmi.h>
+#include <grass/glocale.h>
+
+#include "local_proto.h"
+
+static int srch(const void *, const void *);
+
+/*!
+   \brief transform 3d vector features to 2d (z-coordinate is omitted)
+
+   \param In input vector
+   \param Out output vector
+   \param type feature type to be transformed
+   \param field layer number
+   \param zcolumn attribute column where to store height
+
+   \return number of writen features
+ */
+int trans3d(struct Map_info *In, struct Map_info *Out, int type,
+	    int field, const char *zcolumn)
+{
+    int ltype;
+    int line;
+    int ctype;
+
+    struct line_pnts *Points;
+    struct line_cats *Cats;
+
+    struct field_info *Fi;
+    dbDriver *driver;
+    dbString stmt;
+    char buf[2000];
+    int ncats, *cats, cat, *cex;
+
+    Points = Vect_new_line_struct();
+    Cats = Vect_new_cats_struct();
+
+    db_init_string(&stmt);
+
+    if (zcolumn) {
+	Fi = Vect_get_field(Out, field);
+	if (!Fi)
+	    G_fatal_error(_("Database connection not defined for layer %d"),
+			  field);
+
+	driver = db_start_driver_open_database(Fi->driver, Fi->database);
+	if (!driver) {
+	    G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
+			  Fi->database, Fi->driver);
+	}
+
+	/* column type must numeric */
+	ctype = db_column_Ctype(driver, Fi->table, zcolumn);
+	if (ctype == -1)
+	    G_fatal_error(_("Column <%s> not found in table <%s>"),
+			  zcolumn, Fi->table);
+	if (ctype != DB_C_TYPE_INT && ctype != DB_C_TYPE_DOUBLE) {
+	    G_fatal_error(_("Column must be numeric"));
+	}
+
+	db_begin_transaction(driver);
+
+	/* select existing categories (layer) to array (array is sorted) */
+	ncats = db_select_int(driver, Fi->table, Fi->key, NULL, &cats);
+	G_debug(3, "Existing categories: %d", ncats);
+    }
+
+    line = 1;
+    while (1) {
+	ltype = Vect_read_next_line(In, Points, Cats);
+	if (ltype == -1) {
+	    G_fatal_error(_("Unable to read vector map"));
+	}
+	if (ltype == -2) {	/* EOF */
+	    break;
+	}
+
+	if (G_verbose() > G_verbose_min() && (line - 1) % 1000 == 0) {
+	    fprintf(stderr, "%7d\b\b\b\b\b\b\b", (line - 1));
+	}
+
+	if (!(ltype & type))
+	    continue;
+
+	Vect_cat_get(Cats, field, &cat);
+
+	/* get first cat */
+	if (cat == -1) {
+	    G_warning(_("Feature id %d has no category - skipping"), line);
+	}
+	else if (Cats->n_cats > 1) {
+	    G_warning(_("Feature id %d has more categories. "
+			"Using category %d."), line, field, cat);
+	}
+
+	if (zcolumn && ltype == GV_POINT && cat > -1) {
+	    /* category exist in table ? */
+	    cex = (int *)bsearch((void *)&cat, cats, ncats, sizeof(int),
+				 srch);
+
+	    /* store height to the attribute table */
+	    if (ctype == DB_C_TYPE_INT)
+		sprintf(buf, "update %s set %s = %d where cat = %d",
+			Fi->table, zcolumn, (int)Points->z[0], cat);
+	    else		/* double */
+		sprintf(buf, "update %s set %s = %.8f where cat = %d",
+			Fi->table, zcolumn, Points->z[0], cat);
+
+	    G_debug(3, "SQL: %s", buf);
+	    db_set_string(&stmt, buf);
+
+	    if (cex) {
+		if (db_execute_immediate(driver, &stmt) == DB_OK) {
+		    /* TODO */
+		}
+	    }
+	    else {		/* cat does not exist in table */
+		G_warning(_("Record (cat %d) does not exist (not updated)"),
+			  cat);
+	    }
+	}
+
+	Vect_write_line(Out, ltype, Points, Cats);
+	line++;
+    }
+
+    if (G_verbose() > G_verbose_min())
+	fprintf(stderr, "\r");
+
+    if (zcolumn) {
+	db_commit_transaction(driver);
+
+	G_free(cats);
+
+	db_close_database_shutdown_driver(driver);
+	db_free_string(&stmt);
+    }
+
+    Vect_destroy_line_struct(Points);
+    Vect_destroy_cats_struct(Cats);
+
+    return line - 1;
+}
+
+int srch(const void *pa, const void *pb)
+{
+    int *p1 = (int *)pa;
+
+    int *p2 = (int *)pb;
+
+    if (*p1 < *p2)
+	return -1;
+    if (*p1 > *p2)
+	return 1;
+    return 0;
+}


Property changes on: grass/branches/develbranch_6/vector/v.to.3d/trans3.c
___________________________________________________________________
Name: svn:mime-type
   + text/x-csrc
Name: svn:keywords
   + Author Date Id
Name: svn:eol-style
   + native



More information about the grass-commit mailing list