[GRASS-SVN] r38381 - in grass-addons/vector: . v.in.gama
svn_grass at osgeo.org
svn_grass at osgeo.org
Mon Jul 13 05:50:49 EDT 2009
Author: martinl
Date: 2009-07-13 05:50:47 -0400 (Mon, 13 Jul 2009)
New Revision: 38381
Added:
grass-addons/vector/v.in.gama/
grass-addons/vector/v.in.gama/Makefile
grass-addons/vector/v.in.gama/ToDo
grass-addons/vector/v.in.gama/description.html
grass-addons/vector/v.in.gama/gama-local-adjustment.dtd
grass-addons/vector/v.in.gama/global.h
grass-addons/vector/v.in.gama/head.c
grass-addons/vector/v.in.gama/main.c
grass-addons/vector/v.in.gama/parse.c
grass-addons/vector/v.in.gama/table.c
grass-addons/vector/v.in.gama/tables.xml
grass-addons/vector/v.in.gama/tree.c
grass-addons/vector/v.in.gama/vect.c
Log:
v.in.gama (2006) moved to GRASS Add-ons
Property changes on: grass-addons/vector/v.in.gama
___________________________________________________________________
Added: svn:ignore
+ *.tmp.html
OBJ.*
Added: grass-addons/vector/v.in.gama/Makefile
===================================================================
--- grass-addons/vector/v.in.gama/Makefile (rev 0)
+++ grass-addons/vector/v.in.gama/Makefile 2009-07-13 09:50:47 UTC (rev 38381)
@@ -0,0 +1,18 @@
+# MODULE_TOPDIR = ../..
+MODULE_TOPDIR = /home/landa/src/grass6_devel
+
+PGM = v.in.gama
+
+LIBES = $(VECTLIB) $(GISLIB) `xml2-config --libs`
+DEPENDENCIES= $(VECTDEP) $(GISDEP)
+EXTRA_INC = $(VECT_INC)
+EXTRA_CFLAGS = $(VECT_CFLAGS) `xml2-config --cflags`
+
+include $(MODULE_TOPDIR)/include/Make/Module.make
+
+default: cmd xml
+
+xml:
+ if [ ! -d $(GISBASE)/etc ]; then $(MKDIR) $(GISBASE)/etc; fi
+ if [ ! -d $(GISBASE)/etc/v.in.gama ]; then $(MKDIR) $(GISBASE)/etc/v.in.gama; fi
+ $(INSTALL) -m 644 tables.xml $(GISBASE)/etc/v.in.gama/
Property changes on: grass-addons/vector/v.in.gama/Makefile
___________________________________________________________________
Added: svn:mime-type
+ text/x-makefile
Added: svn:eol-style
+ native
Added: grass-addons/vector/v.in.gama/ToDo
===================================================================
--- grass-addons/vector/v.in.gama/ToDo (rev 0)
+++ grass-addons/vector/v.in.gama/ToDo 2009-07-13 09:50:47 UTC (rev 38381)
@@ -0,0 +1,8 @@
+ToDo
+----------------------------------
+* SQL X C types ?
+* use DTD
+* G_debug ()
+***
+* patch d.what.vect
+* patch v.info
\ No newline at end of file
Added: grass-addons/vector/v.in.gama/description.html
===================================================================
--- grass-addons/vector/v.in.gama/description.html (rev 0)
+++ grass-addons/vector/v.in.gama/description.html 2009-07-13 09:50:47 UTC (rev 38381)
@@ -0,0 +1,61 @@
+<h2>DESCRIPTION</h2>
+
+<em>v.in.gama</em> creates a GRASS vector map layer based on GNU GaMa
+XML output file. This file (which contains adjustment results of
+geodetic network) can be created by <em>gama-local</em> tool.
+
+<h2>EXAMPLE</h2>
+
+First, we adjust geodetic network by <em>gama-local</em> tool. The
+adjustment results are saved also in XML data format.
+
+<div class="code"><pre>
+gama-local --xmlout network.xml network.gkf
+</pre></div>
+
+Afterwards we create a map layer <i>network</i> by the
+module <em>v.in.gama</em>:
+
+<div class="code"><pre>
+v.in.gama input=network.xml output=network
+</pre></div>
+
+This map layer contains two different layers. Layer 1 is connected
+with attribute table 'network_point', layer 2 with 'network_obs'. In the first
+layer are stored points of geodetic network. Layer 2 contains
+observation data (in geometry way lines and points).
+
+<h2>NOTES</h2>
+
+<b>GNU Gama</b> is a project dedicated to adjustment of geodetic
+networks. GNU Gama is released under GNU General Public Licence. For
+parsing XML documents GNU Gama calls <em>expat</em> parser written by James
+Clark.
+
+<p>
+GNU Gama currently contains two main development branches. Stable
+branch of the project is limited to network adjustment in a local
+coordinate system (program <em>gama-local</em>). In the unstable development
+branch the adjustment model is based on geocentric coordinate system
+(program <em>gama-g3</em>).
+
+<p>
+The development version of the program <em>gama-local</em> enables
+to save adjustment results besides of the standard text format also in
+newly designed XML output data format. This feature is fundamental for
+GRASS module <em>v.in.gama</em>. Currently it is possible to
+use <em>v.in.gama</em> only in connection with
+<em>gama-local</em> from unstable development branch.
+
+<h2>REFERENCES</h2>
+
+<ul>
+ <li><a href="http://www.gnu.org/software/gama/">GNU Gama</a> adjustment of geodetic networks</li>
+</ul>
+
+<h2>AUTHOR</h2>
+
+Martin Landa, CTU in Prague, Czech Republic
+
+<p>
+<i>Last changed: $Date$</i>
Property changes on: grass-addons/vector/v.in.gama/description.html
___________________________________________________________________
Added: svn:mime-type
+ text/html
Added: svn:keywords
+ Author Date Id
Added: svn:eol-style
+ native
Added: grass-addons/vector/v.in.gama/gama-local-adjustment.dtd
===================================================================
--- grass-addons/vector/v.in.gama/gama-local-adjustment.dtd (rev 0)
+++ grass-addons/vector/v.in.gama/gama-local-adjustment.dtd 2009-07-13 09:50:47 UTC (rev 38381)
@@ -0,0 +1,217 @@
+<!--
+ gama-local-adjustment.dtd version 0.4a 2006-02-19
+ =========================
+
+ http://www.gnu.org/software/gama/gama-local-adjustment.dtd
+-->
+
+<!ELEMENT gama-local-adjustment (
+ description,
+ network-general-parameters,
+ network-processing-summary,
+ coordinates,
+ observations ) >
+
+<!ATTLIST gama-local-adjustment version NMTOKEN #REQUIRED >
+
+<!ELEMENT description ( #PCDATA ) >
+
+<!ELEMENT network-general-parameters EMPTY >
+
+<!ATTLIST network-general-parameters
+ axes-xy (ne | sw | es | wn |
+ en | nw | se | ws ) "ne"
+ angles (right-handed | left-handed) "right-handed"
+ epoch NMTOKEN "0.0" >
+
+<!ELEMENT network-processing-summary (
+ coordinates-summary,
+ observations-summary,
+ project-equations,
+ standard-deviation ) >
+
+<!ELEMENT coordinates-summary (
+ coordinates-summary-adjusted,
+ coordinates-summary-constrained,
+ coordinates-summary-fixed ) >
+
+<!ELEMENT observations-summary (
+ distances,
+ directions,
+ angles,
+ xyz-coords,
+ h-diffs,
+ z-angles,
+ s-dists,
+ vectors ) >
+
+<!ELEMENT project-equations (
+ equations,
+ unknowns,
+ degrees-of-freedom,
+ defect,
+ sum-of-squares,
+ ( connected-network | disconnected-network ) ) >
+
+<!ELEMENT standard-deviation (
+ apriori,
+ aposteriori,
+ used,
+ probability,
+ ratio,
+ lower,
+ upper,
+ ( failed | passed ),
+ confidence-scale ) >
+
+<!ELEMENT coordinates (
+ fixed,
+ approximate,
+ adjusted,
+ orientation-shifts,
+ cov-mat ) >
+
+<!ELEMENT orientation-shifts ( orientation* ) >
+
+<!ELEMENT cov-mat ( dim, band, flt+ ) >
+
+<!ELEMENT observations (
+ distance |
+ direction |
+ angle |
+ height-diff |
+ slope-distance |
+ zenith-angle |
+ dx |
+ dy |
+ dz |
+ coordinate-x |
+ coordinate-y |
+ coordinate-z )* >
+
+<!ELEMENT adjusted ( point* ) >
+<!ELEMENT constrained ( point* ) >
+<!ELEMENT fixed ( point* ) >
+
+<!ELEMENT connected-network EMPTY >
+<!ELEMENT disconnected-network EMPTY >
+
+<!ELEMENT failed EMPTY >
+<!ELEMENT passed EMPTY >
+
+<!ELEMENT coordinates-summary-adjusted ( count-xyz, count-xy, count-z ) >
+<!ELEMENT coordinates-summary-constrained ( count-xyz, count-xy, count-z ) >
+<!ELEMENT coordinates-summary-fixed ( count-xyz, count-xy, count-z ) >
+
+<!ELEMENT count-xyz ( #PCDATA ) >
+<!ELEMENT count-xy ( #PCDATA ) >
+<!ELEMENT count-z ( #PCDATA ) >
+
+<!ELEMENT point ( id, ((X | x), (Y | y))?, (Z | z)? ) >
+
+<!ELEMENT orientation ( id, approx, adj ) >
+
+<!ENTITY % observation-elements
+ "(obs, adj, stdev, f, (std-residual, (err-obs, err-adj)? )?)" >
+
+<!ELEMENT distance ( from, to, %observation-elements; ) >
+<!ELEMENT direction ( from, to, %observation-elements; ) >
+<!ELEMENT angle ( from, left, right, %observation-elements; ) >
+<!ELEMENT slope-distance ( from, to, %observation-elements; ) >
+<!ELEMENT zenith-angle ( from, to, %observation-elements; ) >
+<!ELEMENT dx ( from, to, %observation-elements; ) >
+<!ELEMENT dy ( from, to, %observation-elements; ) >
+<!ELEMENT dz ( from, to, %observation-elements; ) >
+<!ELEMENT height-diff ( from, to, %observation-elements; ) >
+<!ELEMENT coordinate-x ( id, %observation-elements; ) >
+<!ELEMENT coordinate-y ( id, %observation-elements; ) >
+<!ELEMENT coordinate-z ( id, %observation-elements; ) >
+
+<!ELEMENT id ( #PCDATA ) >
+<!ELEMENT from ( #PCDATA ) >
+<!ELEMENT left ( #PCDATA ) >
+<!ELEMENT right ( #PCDATA ) >
+
+<!ELEMENT X ( #PCDATA ) >
+<!ELEMENT x ( #PCDATA ) >
+<!ELEMENT Y ( #PCDATA ) >
+<!ELEMENT y ( #PCDATA ) >
+<!ELEMENT Z ( #PCDATA ) >
+<!ELEMENT z ( #PCDATA ) >
+
+<!ELEMENT adj ( #PCDATA ) >
+
+<!ELEMENT angles ( #PCDATA ) >
+
+<!ELEMENT aposteriori ( #PCDATA ) >
+
+<!ELEMENT approx ( #PCDATA ) >
+
+<!ELEMENT approximate ( point+ ) >
+
+<!ELEMENT apriori ( #PCDATA ) >
+
+<!ELEMENT axes-xy ( #PCDATA ) >
+
+<!ELEMENT band ( #PCDATA ) >
+
+<!ELEMENT confidence-scale ( #PCDATA ) >
+
+<!ELEMENT defect ( #PCDATA ) >
+
+<!ELEMENT degrees-of-freedom ( #PCDATA ) >
+
+<!ELEMENT dim ( #PCDATA ) >
+
+<!ELEMENT directions ( #PCDATA ) >
+
+<!ELEMENT distances ( #PCDATA ) >
+
+<!ELEMENT epoch ( #PCDATA ) >
+
+<!ELEMENT equations ( #PCDATA ) >
+
+<!ELEMENT err-adj ( #PCDATA ) >
+
+<!ELEMENT err-obs ( #PCDATA ) >
+
+<!ELEMENT f ( #PCDATA ) >
+
+<!ELEMENT flt ( #PCDATA ) >
+
+<!ELEMENT h-diffs ( #PCDATA ) >
+
+<!ELEMENT lower ( #PCDATA ) >
+
+<!ELEMENT obs ( #PCDATA ) >
+
+<!ELEMENT probability ( #PCDATA ) >
+
+<!ELEMENT ratio ( #PCDATA ) >
+
+<!ELEMENT s-dists ( #PCDATA ) >
+
+<!ELEMENT std-residual ( #PCDATA ) >
+
+<!ELEMENT stdev ( #PCDATA ) >
+
+<!ELEMENT sum-of-squares ( #PCDATA ) >
+
+<!ELEMENT to ( #PCDATA ) >
+
+<!ELEMENT unknowns ( #PCDATA ) >
+
+<!ELEMENT upper ( #PCDATA ) >
+
+<!ELEMENT used ( #PCDATA ) >
+
+<!ELEMENT vectors ( #PCDATA ) >
+
+<!ELEMENT xy ( #PCDATA ) >
+
+<!ELEMENT xyz ( #PCDATA ) >
+
+<!ELEMENT xyz-coords ( #PCDATA ) >
+
+<!ELEMENT z-angles ( #PCDATA ) >
+
Added: grass-addons/vector/v.in.gama/global.h
===================================================================
--- grass-addons/vector/v.in.gama/global.h (rev 0)
+++ grass-addons/vector/v.in.gama/global.h 2009-07-13 09:50:47 UTC (rev 38381)
@@ -0,0 +1,103 @@
+#ifndef MAIN
+# define EXT extern
+#else
+# define EXT
+#endif
+
+#ifndef GLOBAL___H
+#define GLOBAL___H
+
+#include <grass/gis.h>
+#include <grass/Vect.h>
+#include <grass/dbmi.h>
+#include <grass/glocale.h>
+
+#include <libxml/parser.h>
+#include <libxml/tree.h>
+
+#define FIX 0
+#define ADJ 1
+#define APP 2
+
+#define FIELD_POINT 1
+#define FIELD_OBS 2
+
+#define DIM 3 /* coordinates */
+
+#define ID_LENGTH 128 /* varchar (ID_LENGTH) */
+
+#define OPTIONS struct _options_
+EXT OPTIONS
+{
+ char* input;
+ char* output;
+ int dotable;
+} options;
+
+struct coor
+{
+ double val [3]; /* x_fix, x_adj, x_app */
+ short is_null [3];
+ short is_constrained;
+};
+
+typedef struct _point_
+{
+ int cat; /* -1 -> not writen to the map due missing x|y */
+ dbString id;
+ struct coor xyz [3];
+} Point;
+
+struct list_points
+{
+ Point* point;
+ struct list_points* next;
+};
+
+struct list_nodes
+{
+ xmlNode* node;
+ struct list_nodes* next;
+};
+
+/* head.c */
+int write_head (struct Map_info*, xmlDoc*);
+int find_substring (dbString*, const char*, dbString*);
+
+/* parser.c */
+int parse_command_line(int, char**);
+
+/* vect.c */
+int create_map (struct Map_info*, xmlDoc*, int);
+int create_points (struct Map_info*, xmlDoc*, int, struct list_points**);
+void point_init (Point*);
+void point_set (Point*, xmlNode*, int, xmlNode*, xmlNode*, xmlNode*);
+void write_points (struct Map_info*, xmlNode*, struct list_points*, int);
+void list_points_free (struct list_points**);
+int list_points_push_back (struct list_points**, Point*);
+Point* find_point (struct list_points*, dbString* id);
+void point_update (Point*, int, xmlNode*, xmlNode*, xmlNode*);
+int point_get_xyz (Point*, char*, double*);
+int point_exists (struct list_points*, dbString*);
+int create_obs (struct Map_info*, xmlDoc*, int, struct list_points*);
+int write_line (struct Map_info*, int, int, int, int);
+
+/* table.c */
+int create_tables (struct Map_info*, xmlDoc*);
+void insert_point (struct field_info*, dbDriver*, dbTable*, Point*, int);
+void insert_obs (struct field_info*, dbDriver*, dbTable*, xmlNode*, int);
+
+/* tree.c */
+int find_node (xmlNode*, const char*, xmlNode**);
+int find_nodes (xmlNode*, const char*, struct list_nodes**);
+void find__nodes (xmlNode*, xmlNode*, const char*, struct list_nodes**, int*, int*);
+void list_init (void**);
+void list_nodes_free (struct list_nodes**);
+void list_nodes_push_back (struct list_nodes** lnodes, xmlNode* node);
+int is_3d_map (xmlDoc*);
+int node_get_value_int (xmlNode*, int*);
+int node_get_value_double (xmlNode*, double*);
+int node_get_value_string (xmlNode*, char*, dbString*);
+int axes_xy_str2type (char *);
+
+#endif
Property changes on: grass-addons/vector/v.in.gama/global.h
___________________________________________________________________
Added: svn:mime-type
+ text/x-chdr
Added: svn:eol-style
+ native
Added: grass-addons/vector/v.in.gama/head.c
===================================================================
--- grass-addons/vector/v.in.gama/head.c (rev 0)
+++ grass-addons/vector/v.in.gama/head.c 2009-07-13 09:50:47 UTC (rev 38381)
@@ -0,0 +1,122 @@
+#include "global.h"
+
+int write_head(struct Map_info *Map, xmlDoc * doc)
+{
+ xmlNode *root;
+ struct list_nodes *desc;
+ dbString val, value;
+
+ int scale;
+
+ root = xmlDocGetRootElement(doc);
+ list_init((void **)&desc);
+ db_init_string(&val);
+ db_init_string(&value);
+
+ if (!root) {
+ G_fatal_error(_("Unable to get the root element"));
+ }
+ else {
+ find_nodes(root, "description", &desc);
+ if (desc) {
+ if (node_get_value_string(desc->node, NULL, &val)) {
+ if (find_substring(&val, "scale", &value)) {
+ scale = atoi(db_get_string(&value));
+ if (scale > 0) {
+ Vect_set_scale(Map, scale);
+ }
+ }
+ else {
+ Vect_set_scale(Map, 1000); /* default scale 1000 */
+ }
+
+ if (find_substring(&val, "date", &value)) {
+ Vect_set_map_date(Map, db_get_string(&value));
+ }
+ else {
+ Vect_set_map_date(Map, G_date()); /* default map date = date */
+ }
+
+ if (find_substring(&val, "name", &value)) {
+ Vect_set_map_name(Map, db_get_string(&value));
+ }
+ else {
+ Vect_set_map_name(Map, "GNU GaMa network");
+ }
+
+ if (find_substring(&val, "organization", &value)) {
+ Vect_set_organization(Map, db_get_string(&value));
+ }
+ else {
+ char *organization;
+
+ if ((organization =
+ (char *)G_getenv("GRASS_ORGANIZATION"))) {
+ Vect_set_organization(Map, organization);
+ }
+ else {
+ Vect_set_organization(Map, "GRASS Development Team");
+ }
+ }
+
+ if (find_substring(&val, "person", &value)) {
+ Vect_set_person(Map, db_get_string(&value));
+ }
+ else {
+ Vect_set_person(Map, G_whoami());
+ }
+
+ if (find_substring(&val, "other info", &value)) {
+ const short int odim = 65;
+ char other_info[odim];
+
+ G_warning(_("Other info is too long, "
+ "only first %d chars will be stored"), odim);
+ strncpy(other_info, db_get_string(&value), odim);
+ Vect_set_comment(Map, other_info);
+ }
+ else {
+ Vect_set_comment(Map, "created by v.in.gama");
+ }
+
+ }
+ Vect_set_date(Map, G_date());
+ }
+ }
+
+ list_nodes_free(&desc);
+
+ return 0;
+}
+
+int find_substring(dbString * str, const char *pattern, dbString * substring)
+{
+ char *c, *s, z[2];
+ dbString buf;
+
+ db_init_string(&buf);
+
+ c = strstr(db_get_string(str), pattern);
+
+ if (c) {
+ s = c + strlen(pattern);
+ if (*(s++) == ':') {
+ while (*s != '\n' && *s != '\0') {
+ z[0] = *(s++);
+ z[1] = '\0';
+ db_append_string(&buf, z);
+ }
+ db_copy_string(substring, &buf);
+ return 1;
+ }
+ /* maybe a bit confusing
+ else
+ {
+ G_warning (_("Cannot recognize substring '%s'."),
+ db_get_string (str));
+ }
+ */
+ }
+
+ return 0;
+}
Property changes on: grass-addons/vector/v.in.gama/head.c
___________________________________________________________________
Added: svn:mime-type
+ text/x-csrc
Added: svn:eol-style
+ native
Added: grass-addons/vector/v.in.gama/main.c
===================================================================
--- grass-addons/vector/v.in.gama/main.c (rev 0)
+++ grass-addons/vector/v.in.gama/main.c 2009-07-13 09:50:47 UTC (rev 38381)
@@ -0,0 +1,143 @@
+
+/************************************************************************/
+/* */
+/* v.in.gama - convert GNU GaMa XML output file to */
+/* a GRASS vector map layer */
+/* */
+/* Martin Landa, 5/2006 */
+/* */
+/* This file is part of GRASS GIS. It is free software. You can */
+/* redistribute it and/or modify it under the terms of */
+/* the GNU General Public License as published by the Free Software */
+/* Foundation; either version 2 of the License, or (at your option) */
+/* any later version. */
+/* */
+
+/************************************************************************/
+
+#define MAIN
+#include <unistd.h>
+#include "global.h"
+
+int main(int argc, char *argv[])
+{
+ FILE *input_xml_file, *tables_xml_file;
+
+ struct Map_info Map;
+ struct GModule *module;
+
+ xmlDoc *doc, *tables_doc;
+ dbString path_tables_xml;
+
+ G_gisinit(argv[0]);
+
+ module = G_define_module();
+
+ module->description = _("Converts GNU GaMa XML output file "
+ "to a GRASS vector map layer.");
+
+ /* parse command line */
+ parse_command_line(argc, argv);
+
+ /* open temporary file - needed? */
+ char *tmp;
+ FILE *tmpfile;
+
+ tmp = G_tempfile();
+ tmpfile = fopen(tmp, "w+");
+
+ if (tmpfile == NULL) {
+ G_fatal_error(_("Unable to open temp file '%s'"), tmp);
+ }
+
+ unlink(tmp);
+ fclose(tmpfile);
+
+ /*
+ this initialize the library and check potential ABI mismatches
+ between the version it was compiled for and the actual shared
+ library used.
+ */
+ LIBXML_TEST_VERSION;
+
+ /* read input file
+ open the XML file and get the DOM */
+ if (options.input) {
+ input_xml_file = fopen(options.input, "r");
+ if (!input_xml_file) {
+ G_fatal_error(_("Unable to open input file '%s'"),
+ options.input);
+ }
+ doc = xmlReadFile(options.input, NULL, 0);
+ }
+ else {
+ /* doc = xmlReadFile ("-", NULL, 0); */
+ doc = xmlReadFd(0, NULL, NULL, 0);
+ }
+
+ if (doc == NULL) {
+ G_fatal_error("Parsing file '%s' failed", options.input);
+ }
+
+ G_debug(1, "input XML file opened");
+
+ /* open the output vector map */
+ Vect_open_new(&Map, options.output, is_3d_map(doc));
+
+ /* metadata */
+ write_head(&Map, doc);
+
+ Vect_hist_command(&Map);
+
+ /* create empty attribute table */
+ if (options.dotable) {
+ db_init_string(&path_tables_xml);
+
+ db_set_string(&path_tables_xml, G_gisbase());
+ db_append_string(&path_tables_xml, "/etc/");
+ db_append_string(&path_tables_xml, G_program_name());
+ db_append_string(&path_tables_xml, "/tables.xml");
+
+ tables_xml_file = fopen(db_get_string(&path_tables_xml), "r");
+
+ if (!tables_xml_file) {
+ G_fatal_error(_("Unable to open file '%s'"),
+ db_get_string(&path_tables_xml));
+ }
+
+ tables_doc = xmlReadFile(db_get_string(&path_tables_xml), NULL, 0);
+
+ if (!tables_doc) {
+ G_fatal_error(_("Unable to parse parse file '%s'"),
+ db_get_string(&path_tables_xml));
+ }
+
+ create_tables(&Map, tables_doc);
+
+ xmlFreeDoc(tables_doc);
+
+ if (tables_xml_file) {
+ fclose(tables_xml_file);
+ }
+ }
+
+ /* point data
+ -> insert attribute data (layer 1) and create vector points
+ observation data
+ -> insert attribute data (layer 2) and create vector lines */
+ create_map(&Map, doc, options.dotable);
+
+ xmlFreeDoc(doc);
+ xmlCleanupParser();
+
+ if (options.input) {
+ fclose(input_xml_file);
+ }
+
+ Vect_build_partial(&Map, GV_BUILD_NONE);
+ Vect_build(&Map);
+
+ Vect_close(&Map);
+
+ exit(EXIT_SUCCESS);
+}
Property changes on: grass-addons/vector/v.in.gama/main.c
___________________________________________________________________
Added: svn:mime-type
+ text/x-csrc
Added: svn:eol-style
+ native
Added: grass-addons/vector/v.in.gama/parse.c
===================================================================
--- grass-addons/vector/v.in.gama/parse.c (rev 0)
+++ grass-addons/vector/v.in.gama/parse.c 2009-07-13 09:50:47 UTC (rev 38381)
@@ -0,0 +1,36 @@
+#include "global.h"
+
+int parse_command_line(int argc, char *argv[])
+{
+ struct
+ {
+ struct Option *input;
+ struct Option *output;
+ } parms;
+
+ struct
+ {
+ struct Flag *t;
+ } flags;
+
+ parms.input = G_define_standard_option(G_OPT_F_INPUT);
+ parms.input->description =
+ _("GNU GaMa XML output file to be converted to "
+ "binary vector file, if not given reads from standard input");
+
+ parms.output = G_define_standard_option(G_OPT_V_OUTPUT);
+
+ flags.t = G_define_flag();
+ flags.t->key = 't';
+ flags.t->description = _("Do not create attribute table");
+
+ if (G_parser(argc, argv)) {
+ exit(EXIT_FAILURE);
+ }
+
+ options.input = parms.input->answer;
+ options.output = parms.output->answer;
+ options.dotable = flags.t->answer ? 0 : 1;
+
+ return 0;
+}
Property changes on: grass-addons/vector/v.in.gama/parse.c
___________________________________________________________________
Added: svn:mime-type
+ text/x-csrc
Added: svn:eol-style
+ native
Added: grass-addons/vector/v.in.gama/table.c
===================================================================
--- grass-addons/vector/v.in.gama/table.c (rev 0)
+++ grass-addons/vector/v.in.gama/table.c 2009-07-13 09:50:47 UTC (rev 38381)
@@ -0,0 +1,315 @@
+#include "global.h"
+
+int create_tables(struct Map_info *Map, xmlDoc * doc)
+{
+ int i, num_of_cols, col_type, num_of_tables;
+
+ struct field_info *fi;
+ int field;
+
+ dbDriver *driver;
+ dbTable *dbtable;
+ dbColumn *dbcolumn;
+ dbString table_name, col_name, suffix;
+
+ xmlNode *root, *table, *column, *name, *datatype;
+ struct list_nodes *tables, *columns, *tmp_col, *tmp_tab;
+
+ num_of_tables = 0;
+ field = 1;
+ dbtable = NULL;
+ db_init_string(&table_name);
+ db_init_string(&col_name);
+ db_init_string(&suffix);
+
+ list_init((void **)&tables);
+ list_init((void **)&columns);
+
+ root = xmlDocGetRootElement(doc);
+
+ if (!root) {
+ G_fatal_error(_("Unable to get the root element"));
+ }
+
+ if (!find_nodes(root, "table", &tables)) {
+ G_fatal_error(_("Invalid table structure"));
+ }
+ else {
+ for (tmp_tab = tables; tmp_tab; tmp_tab = tmp_tab->next) {
+ table = tmp_tab->node;
+ fi = Vect_default_field_info(Map, field, NULL, GV_1TABLE);
+
+ if (!fi) {
+ G_fatal_error(_("Unable to get default vector layer"));
+ }
+
+ 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);
+ }
+
+ db_begin_transaction(driver);
+
+ num_of_cols = find_nodes(table, "column", &columns);
+ if (num_of_cols < 1) {
+ G_fatal_error(_("Invalid table structure"));
+ }
+ else {
+ dbtable = db_alloc_table(num_of_cols);
+ for (tmp_col = columns, i = 0; tmp_col;
+ i++, tmp_col = tmp_col->next) {
+ column = tmp_col->node;
+ dbcolumn = db_get_table_column(dbtable, i);
+ if (!find_node(column, "name", &name) ||
+ !find_node(column, "datatype", &datatype) ||
+ !node_get_value_string(name, NULL, &col_name) ||
+ !node_get_value_int(datatype, &col_type)) {
+ G_fatal_error(_("Invalid table structure"));
+ }
+
+ db_set_column_name(dbcolumn, db_get_string(&col_name));
+ db_set_column_sqltype(dbcolumn, col_type);
+ if (db_get_column_sqltype(dbcolumn) ==
+ DB_SQL_TYPE_CHARACTER) {
+ db_set_column_length(dbcolumn, ID_LENGTH);
+ }
+ }
+
+ db_set_string(&table_name, fi->table);
+ if (!node_get_value_string(table, "type", &suffix)) {
+ G_fatal_error(_("Invalid table structure"));
+ }
+ else {
+ db_append_string(&table_name, "_");
+ db_append_string(&table_name, db_get_string(&suffix));
+ }
+
+ fi->table = G_store(db_get_string(&table_name)); /* nasty */
+ db_set_table_name(dbtable, fi->table);
+
+ if (db_create_table(driver, dbtable) != DB_OK) {
+ G_fatal_error(_("Unable to create table"));
+ }
+
+ if (driver) {
+ db_commit_transaction(driver);
+ db_close_database_shutdown_driver(driver);
+ }
+
+ Vect_map_add_dblink(Map, field++, NULL, fi->table,
+ fi->key, fi->database, fi->driver);
+ num_of_tables++;
+ }
+ }
+ }
+
+ list_nodes_free(&tables);
+ list_nodes_free(&columns);
+
+ return num_of_tables;
+}
+
+void insert_point(struct field_info *fi, dbDriver * driver, dbTable * table,
+ Point * point, int cat)
+{
+
+ int i, ret;
+ double coor_val;
+
+ char *name;
+ char buff[1024];
+
+ dbColumn *column;
+ dbString sql;
+
+ db_init_string(&sql);
+
+ sprintf(buff, "INSERT INTO %s (", fi->table);
+ db_set_string(&sql, buff);
+
+ for (i = 0; i < db_get_table_number_of_columns(table); i++) {
+ column = db_get_table_column(table, i);
+ if (i > 0) {
+ db_append_string(&sql, ",");
+ }
+ db_append_string(&sql, db_get_column_name(column));
+ }
+
+ db_append_string(&sql, ") VALUES (");
+
+ for (i = 0; i < db_get_table_number_of_columns(table); i++) {
+ column = db_get_table_column(table, i);
+ name = (char *)db_get_column_name(column);
+
+ if (i > 0) {
+ db_append_string(&sql, ",");
+ }
+ /* category number */
+ if (!G_strcasecmp(name, "cat")) {
+ sprintf(buff, "%d", cat);
+ db_append_string(&sql, buff);
+ continue;
+ }
+ /* id */
+ if (!G_strcasecmp(name, "id")) {
+ if (db_sizeof_string(&(point->id)) > 1) {
+ if (db_sizeof_string(&(point->id)) > ID_LENGTH) {
+ G_warning
+ ("Only first %d characters of point id [%s] will be stored.",
+ ID_LENGTH, db_get_string(&(point->id)));
+ }
+ db_append_string(&sql, "'");
+ db_append_string(&sql, db_get_string(&(point->id)));
+ db_append_string(&sql, "'");
+ }
+ else {
+ db_append_string(&sql, "NULL");
+ }
+ continue;
+ }
+ /* coordinates */
+ ret = point_get_xyz(point, name, &coor_val);
+ if (ret) {
+ if (ret == 1)
+ sprintf(buff, "%f", coor_val);
+ else /* constrained */
+ sprintf(buff, "%d", (int)coor_val);
+
+ db_append_string(&sql, buff);
+ }
+ else {
+ db_append_string(&sql, "NULL");
+ }
+ }
+
+ db_append_string(&sql, ")");
+
+ G_debug(3, "SQL: %s", db_get_string(&sql));
+
+ if (db_execute_immediate(driver, &sql) != DB_OK) {
+ G_fatal_error(_("Unable to insert new row: '%s'"), db_get_string(&sql));
+ }
+
+ return;
+}
+
+void insert_obs(struct field_info *fi, dbDriver * driver, dbTable * table,
+ xmlNode * node, int cat)
+{
+ /*
+ column name | xml
+ -----------------
+ from_id | from
+ to_id | to
+ left_id | left
+ right_id | right
+ _ | -
+ */
+
+ int i;
+ double val;
+ char *c, *cnxml, *cnxml_tmp;
+ char buf[512];
+
+ dbColumn *column;
+ dbString sql, sval, col_name_xml;
+ xmlNode *obs_val;
+
+ db_init_string(&sql);
+ db_init_string(&sval);
+ db_init_string(&col_name_xml);
+
+ db_set_string(&sql, "INSERT INTO ");
+ db_append_string(&sql, fi->table);
+ db_append_string(&sql, " (");
+
+ for (i = 0; i < db_get_table_number_of_columns(table); i++) {
+ column = db_get_table_column(table, i);
+ if (i > 0) {
+ db_append_string(&sql, ",");
+ }
+ db_append_string(&sql, db_get_column_name(column));
+ }
+
+ db_append_string(&sql, ") VALUES (");
+ sprintf(buf, "%d", cat);
+ db_append_string(&sql, buf);
+
+ for (i = 1; i < db_get_table_number_of_columns(table); i++) {
+ column = db_get_table_column(table, i);
+ db_set_string(&col_name_xml, db_get_column_name(column));
+ cnxml = db_get_string(&col_name_xml);
+
+ if (!G_strcasecmp(cnxml, "obs_type")) {
+ db_append_string(&sql, ",'");
+ db_append_string(&sql, (char *)node->name);
+ db_append_string(&sql, "'");
+ continue;
+ }
+
+ /* remove "_id" from original (xml) column name */
+ if (!G_strcasecmp(cnxml, "from_id") ||
+ !G_strcasecmp(cnxml, "to_id") ||
+ !G_strcasecmp(cnxml, "left_id") ||
+ !G_strcasecmp(cnxml, "right_id")) {
+ cnxml_tmp = G_str_replace(cnxml, "_id", "");
+ db_set_string(&col_name_xml, cnxml_tmp);
+ }
+
+ /* replace '_' -> '-' */
+ c = strstr(cnxml, "_");
+ if (c) {
+ cnxml_tmp = G_str_replace(cnxml, "_", "-");
+ db_set_string(&col_name_xml, cnxml_tmp);
+ }
+
+ if (find_node(node, cnxml, &obs_val)) {
+ switch (db_get_column_sqltype(column)) {
+ case DB_SQL_TYPE_CHARACTER:
+ {
+ if (node_get_value_string(obs_val, NULL, &sval)) {
+ db_append_string(&sql, ",");
+ sprintf(buf, "'%s'", db_get_string(&sval));
+ db_append_string(&sql, buf);
+ }
+ else {
+ db_append_string(&sql, ",NULL");
+ }
+ break;
+ }
+ case DB_SQL_TYPE_DOUBLE_PRECISION:
+ {
+ if (node_get_value_double(obs_val, &val)) {
+ db_append_string(&sql, ",");
+ sprintf(buf, "%f", val);
+ db_append_string(&sql, buf);
+ }
+ else {
+ db_append_string(&sql, ",NULL");
+ }
+ break;
+ }
+ default:
+ {
+ G_fatal_error(_("Unable to determine data type of the column <%s>"),
+ db_get_column_name(column));
+ break;
+ }
+ }
+ }
+ else {
+ db_append_string(&sql, ",NULL");
+ }
+ }
+
+ db_append_string(&sql, ")");
+
+ G_debug(3, "SQL: %s", db_get_string(&sql));
+
+ if (db_execute_immediate(driver, &sql) != DB_OK) {
+ G_fatal_error(_("Unable to insert new row: '%s'"), db_get_string(&sql));
+ }
+
+ return;
+}
Property changes on: grass-addons/vector/v.in.gama/table.c
___________________________________________________________________
Added: svn:mime-type
+ text/x-csrc
Added: svn:eol-style
+ native
Added: grass-addons/vector/v.in.gama/tables.xml
===================================================================
--- grass-addons/vector/v.in.gama/tables.xml (rev 0)
+++ grass-addons/vector/v.in.gama/tables.xml 2009-07-13 09:50:47 UTC (rev 38381)
@@ -0,0 +1,128 @@
+<?xml version="1.0" ?>
+
+<!--
+DB_SQL_TYPE_CHARACTER 1
+DB_SQL_TYPE_SMALLINT 2
+DB_SQL_TYPE_INTEGER 3
+DB_SQL_TYPE_DOUBLE_PRECISION 6
+-->
+
+<tables>
+ <table type="point">
+ <column>
+ <name>cat</name>
+ <datatype>3</datatype>
+ <required/>
+ </column>
+ <column>
+ <name>id</name>
+ <datatype>1</datatype>
+ </column>
+ <column>
+ <name>x_fix</name>
+ <datatype>6</datatype>
+ </column>
+ <column>
+ <name>y_fix</name>
+ <datatype>6</datatype>
+ </column>
+ <column>
+ <name>z_fix</name>
+ <datatype>6</datatype>
+ </column>
+ <column>
+ <name>x_adj</name>
+ <datatype>6</datatype>
+ </column>
+ <column>
+ <name>y_adj</name>
+ <datatype>6</datatype>
+ </column>
+ <column>
+ <name>z_adj</name>
+ <datatype>6</datatype>
+ </column>
+ <column>
+ <name>x_app</name>
+ <datatype>6</datatype>
+ </column>
+ <column>
+ <name>y_app</name>
+ <datatype>6</datatype>
+ </column>
+ <column>
+ <name>z_app</name>
+ <datatype>6</datatype>
+ </column>
+ <column>
+ <name>x_con</name>
+ <datatype>3</datatype>
+ </column>
+ <column>
+ <name>y_con</name>
+ <datatype>3</datatype>
+ </column>
+ <column>
+ <name>z_con</name>
+ <datatype>3</datatype>
+ </column>
+ </table>
+ <table type="obs">
+ <column>
+ <name>cat</name>
+ <datatype>3</datatype>
+ </column>
+ <column>
+ <name>obs_type</name>
+ <datatype>1</datatype>
+ </column>
+ <column>
+ <name>from_id</name>
+ <datatype>1</datatype>
+ </column>
+ <column>
+ <name>to_id</name>
+ <datatype>1</datatype>
+ </column>
+ <column>
+ <name>left_id</name>
+ <datatype>1</datatype>
+ </column>
+ <column>
+ <name>right_id</name>
+ <datatype>1</datatype>
+ </column>
+ <column>
+ <name>id</name>
+ <datatype>1</datatype>
+ </column>
+ <column>
+ <name>obs</name>
+ <datatype>6</datatype>
+ </column>
+ <column>
+ <name>adj</name>
+ <datatype>6</datatype>
+ </column>
+ <column>
+ <name>stdev</name>
+ <datatype>6</datatype>
+ </column>
+ <column>
+ <name>f</name>
+ <datatype>6</datatype>
+ </column>
+ <column>
+ <name>std_residual</name>
+ <datatype>6</datatype>
+ </column>
+ <column>
+ <name>err_obs</name>
+ <datatype>6</datatype>
+ </column>
+ <column>
+ <name>err_adj</name>
+ <datatype>6</datatype>
+ </column>
+ </table>
+</tables>
Added: grass-addons/vector/v.in.gama/tree.c
===================================================================
--- grass-addons/vector/v.in.gama/tree.c (rev 0)
+++ grass-addons/vector/v.in.gama/tree.c 2009-07-13 09:50:47 UTC (rev 38381)
@@ -0,0 +1,259 @@
+#include "global.h"
+
+int find_node(xmlNode * root, const char *name, xmlNode ** fnode)
+{
+ int found = 0;
+ struct list_nodes *fnodes;
+
+ list_init((void **)&fnodes);
+
+ found = find_nodes(root, name, &fnodes);
+
+ if (fnodes) {
+ *fnode = fnodes->node;
+ }
+ else {
+ *fnode = NULL;
+ }
+
+ list_nodes_free(&fnodes);
+
+ return found;
+}
+
+int find_nodes(xmlNode * root, const char *name, struct list_nodes **fnodes)
+{
+ int fcount, stop;
+ xmlNode *last;
+
+ fcount = stop = 0;
+
+ if (root->next) {
+ last = root->next;
+ }
+ else { /* root element */
+
+ last = root->last;
+ }
+
+ list_nodes_free(fnodes);
+
+ find__nodes(root, last, name, fnodes, &fcount, &stop);
+
+ return fcount;
+}
+
+void find__nodes(xmlNode * root, xmlNode * last, const char *name,
+ struct list_nodes **fnodes, int *fcount, int *stop)
+{
+
+ xmlNode *node = NULL;
+
+ for (node = root; node && !(*stop); node = node->next) {
+ if (node == last) {
+ *stop = 1;
+ break;
+ }
+
+ if (node->type == XML_ELEMENT_NODE &&
+ !G_strcasecmp((char *)node->name, name)) {
+ list_nodes_push_back(fnodes, node);
+ (*fcount)++;
+ }
+
+ find__nodes(node->children, last, name, fnodes, fcount, stop);
+ }
+
+ return;
+}
+
+void list_init(void **lnodes)
+{
+ *lnodes = NULL;
+
+ return;
+}
+
+void list_nodes_free(struct list_nodes **lnodes)
+{
+ struct list_nodes *tmp, *next;
+
+ tmp = *lnodes;
+
+ while (tmp) {
+ next = tmp->next;
+ G_free((struct list_nodes *)tmp);
+ tmp = next;
+ }
+
+ if (*lnodes)
+ *lnodes = NULL;
+
+ return;
+}
+
+void list_nodes_push_back(struct list_nodes **lnodes, xmlNode * node)
+{
+
+ struct list_nodes *ln, *tmp;
+
+ tmp = *lnodes;
+ ln = (struct list_nodes *)G_malloc((unsigned int)
+ sizeof(struct list_nodes));
+
+ ln->node = node;
+ ln->next = NULL;
+
+ if (tmp) {
+ while (tmp->next)
+ tmp = tmp->next;
+
+ tmp->next = ln;
+ }
+ else {
+ *lnodes = ln;
+ }
+
+ return;
+}
+
+int is_3d_map(xmlDoc * doc)
+{
+
+ int count;
+
+ xmlNode *root;
+ struct list_nodes *summary, *count_xyz, *count_z;
+
+ list_init((void **)&summary);
+ list_init((void **)&count_xyz);
+ list_init((void **)&count_z);
+
+ root = xmlDocGetRootElement(doc);
+ if (!root) {
+ G_fatal_error(_("Unable to get the root element"));
+ }
+
+ find_nodes(root, "coordinates-summary", &summary);
+
+ if (summary) {
+ struct list_nodes *tmp;
+
+ find_nodes(summary->node, "count-xyz", &count_xyz);
+ if (count_xyz) {
+ for (tmp = count_xyz; tmp; tmp = tmp->next) {
+ if (node_get_value_int(tmp->node, &count))
+ if (count > 0)
+ return WITH_Z;
+ }
+ }
+ find_nodes(summary->node, "count-z", &count_z);
+ if (count_z) {
+ for (tmp = count_z; tmp; tmp = tmp->next) {
+ if (node_get_value_int(tmp->node, &count))
+ if (count > 0)
+ return WITH_Z;
+ }
+ }
+ }
+
+ list_nodes_free(&summary);
+ list_nodes_free(&count_xyz);
+ list_nodes_free(&count_z);
+
+ return WITHOUT_Z;
+}
+
+int node_get_value_int(xmlNode * node, int *value)
+{
+
+ xmlNode *node_value;
+
+ if (node) {
+ node_value = node->children;
+ if (node_value && node_value->type == XML_TEXT_NODE) {
+ *value = (long)atoi((char *)node_value->content);
+ return 1;
+ }
+ }
+
+ return 0;
+}
+
+int node_get_value_double(xmlNode * node, double *value)
+{
+
+ xmlNode *node_value;
+
+ if (node) {
+ node_value = node->children;
+ if (node_value && node_value->type == XML_TEXT_NODE) {
+ *value = (double)atof((char *)node_value->content);
+ return 1;
+ }
+ }
+
+ return 0;
+}
+
+int node_get_value_string(xmlNode * node, char *attr, dbString * value)
+{
+ if (node) {
+ if (!attr) {
+ xmlNode *node_value;
+
+ node_value = node->children;
+ if (node_value && node_value->type == XML_TEXT_NODE) {
+ db_set_string(value, (char *)node_value->content);
+ return 1;
+ }
+ }
+ else { /* attribute */
+
+ xmlAttr *attr_value;
+
+ attr_value = node->properties;
+ while (attr_value) {
+ if (attr_value->type == XML_ATTRIBUTE_NODE &&
+ !G_strcasecmp((char *)attr_value->name, attr)) {
+ if (attr_value->children) {
+ db_set_string(value,
+ (char *)attr_value->children->content);
+ if (db_sizeof_string(value) > 0) {
+ return 1;
+ }
+ }
+ }
+ attr_value = attr_value->next;
+ }
+ }
+ }
+
+ return 0;
+}
+
+int axes_xy_str2type(char *attr)
+{
+ if (attr) {
+ /* right-handed */
+ if (!G_strcasecmp(attr, "ne"))
+ return 0;
+ if (!G_strcasecmp(attr, "sw"))
+ return 1;
+ if (!G_strcasecmp(attr, "es"))
+ return 2;
+ if (!G_strcasecmp(attr, "wn"))
+ return 3;
+ /* left-handed */
+ if (!G_strcasecmp(attr, "en"))
+ return 4;
+ if (!G_strcasecmp(attr, "ws"))
+ return 5;
+ if (!G_strcasecmp(attr, "se"))
+ return 6;
+ if (!G_strcasecmp(attr, "nw"))
+ return 7;
+ }
+
+ return -1;
+}
Property changes on: grass-addons/vector/v.in.gama/tree.c
___________________________________________________________________
Added: svn:mime-type
+ text/x-csrc
Added: svn:eol-style
+ native
Added: grass-addons/vector/v.in.gama/vect.c
===================================================================
--- grass-addons/vector/v.in.gama/vect.c (rev 0)
+++ grass-addons/vector/v.in.gama/vect.c 2009-07-13 09:50:47 UTC (rev 38381)
@@ -0,0 +1,798 @@
+#include "global.h"
+
+int create_map(struct Map_info *Map, xmlDoc * doc, int dotable)
+{
+ struct list_points *vpoints;
+
+ list_init((void **)&vpoints);
+
+ create_points(Map, doc, dotable, &vpoints);
+
+ create_obs(Map, doc, options.dotable, vpoints);
+
+ list_points_free(&vpoints);
+
+ return 0;
+}
+
+int create_points(struct Map_info *Map, xmlDoc * doc, int dotable,
+ struct list_points **vpoints)
+{
+
+ unsigned int i;
+
+ xmlNode *root_element = NULL;
+
+ xmlNode *points;
+ struct list_nodes *point;
+ xmlNode *id, *coor_x, *coor_y, *coor_z;
+
+ Point *vpoint;
+
+ const char *coor_type[] = { "fixed", "adjusted", "approximate" };
+
+ dbString sid;
+
+ db_init_string(&sid);
+
+ list_init((void **)&point);
+
+ root_element = xmlDocGetRootElement(doc);
+
+ if (root_element == NULL) {
+ G_fatal_error(_("Unable to get the root element"));
+ }
+
+ for (i = 0; i < sizeof(coor_type) / sizeof(char *); i++) {
+ find_node(root_element, coor_type[i], &points);
+ if (points) {
+ find_nodes(points, "point", &point);
+
+ if (point) {
+ struct list_nodes *tp = point;
+
+ while (tp) {
+ find_node(tp->node, "id", &id);
+ find_node(tp->node, "x", &coor_x);
+ find_node(tp->node, "y", &coor_y);
+ find_node(tp->node, "z", &coor_z);
+
+ if ((node_get_value_string(id, NULL, &sid)) &&
+ (vpoint = find_point(*vpoints, &sid))) {
+ point_update(vpoint, i, coor_x, coor_y, coor_z);
+ }
+ else {
+ /* G_free () in function list_points_free () */
+ vpoint = (Point *)
+ G_malloc((unsigned int)sizeof(Point));
+
+ point_init(vpoint);
+
+ point_set(vpoint, id, i, coor_x, coor_y, coor_z);
+
+ list_points_push_back(vpoints, vpoint);
+ }
+ tp = tp->next;
+ }
+ }
+ }
+ }
+
+ /* write new vector element && insert records into table */
+ write_points(Map, root_element, *vpoints, dotable);
+
+ list_nodes_free(&point);
+
+ return 0;
+}
+
+void point_init(Point * point)
+{
+ int i, j;
+
+ point->cat = -1;
+
+ db_init_string(&(point->id));
+
+ for (i = 0; i < DIM; i++) { /* x | y | z */
+ for (j = 0; j < DIM; j++) { /* fix | adj | app */
+ point->xyz[i].is_null[j] = 1;
+ }
+ point->xyz[i].is_constrained = 0;
+ }
+
+ return;
+}
+void point_set(Point * point, xmlNode * id, int type,
+ xmlNode * x, xmlNode * y, xmlNode * z)
+{
+
+ unsigned int i;
+ dbString idvalue;
+ double dvalue;
+ const char *XYZ[] = { "X", "Y", "Z" };
+
+ xmlNode *nodes[] = { x, y, z };
+
+ db_init_string(&idvalue);
+
+ if (id && node_get_value_string(id, NULL, &idvalue)) {
+ db_copy_string(&(point->id), &idvalue);
+ }
+ else {
+ G_warning("Missing point id");
+ }
+
+ for (i = 0; i < sizeof(nodes) / sizeof(struct list_nodes *); i++) {
+ if (nodes[i]) {
+ if (node_get_value_double(nodes[i], &dvalue)) {
+
+ point->xyz[i].val[type] = dvalue;
+ point->xyz[i].is_null[type] = 0;
+
+ if (!strcmp((char *)nodes[i]->name, XYZ[i])) { /* cannot be G_strcasecmp */
+ point->xyz[i].is_constrained = 1;
+ }
+ }
+ }
+ }
+
+ return;
+}
+
+void write_points(struct Map_info *Map, xmlNode * root,
+ struct list_points *points, int dotable)
+{
+ int i, j, cat;
+ double xyz[DIM];
+ short xyz_null[DIM];
+
+ struct list_points *lp;
+ Point *p;
+
+ struct line_pnts *ps;
+ struct line_cats *cs;
+
+ struct field_info *fi;
+
+ dbDriver *driver;
+ dbTable *table;
+ dbString axes_xy, table_name;
+
+ struct list_nodes *network_param;
+
+ enum axes_xy_type
+ {
+ NE, SW, ES, WN, /* right-handed */
+ EN, WS, SE, NW /* left-handed */
+ };
+
+ int xy_type;
+
+ cat = 1;
+ lp = points;
+ list_init((void **)&network_param);
+ db_init_string(&axes_xy);
+
+ ps = Vect_new_line_struct();
+ cs = Vect_new_cats_struct();
+
+ if (dotable) {
+ fi = Vect_get_dblink(Map, FIELD_POINT - 1);
+
+ if (!fi) {
+ G_fatal_error(_("Database connection not defined for layer %d"), FIELD_POINT - 1);
+ }
+
+ 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);
+ }
+
+ db_init_string(&table_name);
+ db_set_string(&table_name, fi->table);
+
+ if (db_describe_table(driver, &table_name, &table) != DB_OK) {
+ G_fatal_error(_("Unable to describe table <%s>"), fi->table);
+ }
+
+ db_begin_transaction(driver);
+ }
+
+ xy_type = -1;
+ find_nodes(root, "network-general-parameters", &network_param);
+ if (network_param) {
+ if (node_get_value_string(network_param->node, "axes-xy", &axes_xy)) {
+ if (db_sizeof_string(&axes_xy) > 0) {
+ xy_type = axes_xy_str2type(db_get_string(&axes_xy));
+ }
+ }
+ }
+
+ if (xy_type == -1) {
+ G_warning("Unable to recognize 'axes-xy' attribute, "
+ "switching to the default value 'en'");
+ xy_type = NE;
+ }
+
+ while (lp) {
+ p = lp->point; /* should not be NULL */
+ if (p) {
+ for (i = 0; i < DIM; i++) {
+ xyz_null[i] = 1;
+ }
+
+ for (i = 0; i < DIM; i++) { /* x | y | z */
+ for (j = 0; j < DIM; j++) { /* fix | adj | app */
+ if (xyz_null[i] && !(p->xyz[i].is_null[j])) {
+ xyz[i] = p->xyz[i].val[j];
+ xyz_null[i] = 0;
+ }
+ }
+ }
+
+ if (xyz_null[2] && Vect_is_3d(Map)) {
+ G_warning(_("Point id '%s': cannot set coordinate z"),
+ db_get_string(&(p->id)));
+ xyz[2] = 0.0;
+ }
+
+ if (xyz_null[0] || xyz_null[1]) {
+ G_warning(_("Point id '%s': missing coordinate x|y!"),
+ db_get_string(&(p->id)));
+ }
+ else {
+ /* acceptable values
+ right-handed:
+ * ne
+ * sw
+ * es
+ * wn
+ left-handed:
+ * en
+ * ws
+ * se
+ * nw
+ ---
+ */
+
+ switch (xy_type) {
+ case NE:
+ {
+ Vect_append_point(ps, xyz[1], xyz[0], xyz[2]);
+ break;
+ }
+ case SW:
+ {
+ Vect_append_point(ps, -1.0 * xyz[1], -1.0 * xyz[0],
+ xyz[2]);
+ break;
+ }
+ case ES:
+ {
+ Vect_append_point(ps, xyz[0], -1.0 * xyz[1], xyz[2]);
+ break;
+ }
+ case WN:
+ {
+ Vect_append_point(ps, -1.0 * xyz[0], xyz[1], xyz[2]);
+ break;
+ }
+ case EN:
+ {
+ Vect_append_point(ps, xyz[0], xyz[1], xyz[2]);
+ break;
+ }
+ case WS:
+ {
+ Vect_append_point(ps, -1.0 * xyz[0], -1.0 * xyz[1],
+ xyz[2]);
+ break;
+ }
+ case SE:
+ {
+ Vect_append_point(ps, xyz[1], -1.0 * xyz[0], xyz[2]);
+ break;
+ }
+ case NW:
+ {
+ Vect_append_point(ps, -1.0 * xyz[1], xyz[0], xyz[2]);
+ break;
+ }
+ default:
+ break;
+ }
+
+ Vect_cat_set(cs, FIELD_POINT, cat);
+
+ Vect_write_line(Map, GV_POINT, ps, cs);
+
+ p->cat = cat;
+ }
+
+ if (dotable) {
+ /* insert record into table */
+ insert_point(fi, driver, table, p, cat);
+ }
+ cat++;
+ }
+ lp = lp->next;
+ Vect_reset_line(ps);
+ Vect_reset_cats(cs);
+ }
+
+ if (dotable && driver) {
+ db_commit_transaction(driver);
+ db_close_database_shutdown_driver(driver);
+ }
+
+
+ Vect_destroy_line_struct(ps);
+ Vect_destroy_cats_struct(cs);
+
+ list_nodes_free(&network_param);
+
+ return;
+}
+
+void list_points_free(struct list_points **lpoints)
+{
+ struct list_points *tmp, *next;
+
+ tmp = *lpoints;
+
+ while (tmp) {
+ next = tmp->next;
+ G_free((Point *) tmp->point);
+ G_free((struct list_point *)tmp);
+ tmp = next;
+ }
+
+ if (*lpoints)
+ *lpoints = NULL;
+
+ return;
+}
+
+int list_points_push_back(struct list_points **lpoints, Point * np)
+{
+
+ struct list_points *lp, *tmp;
+
+ tmp = *lpoints;
+ lp = (struct list_points *)G_malloc((unsigned int)
+ sizeof(struct list_points));
+
+ lp->point = np;
+ lp->next = NULL;
+
+ if (tmp) {
+ while (tmp->next)
+ tmp = tmp->next;
+
+ tmp->next = lp;
+ }
+ else {
+ *lpoints = lp;
+ }
+
+ return 1;
+}
+
+Point *find_point(struct list_points * lp, dbString * id)
+{
+ struct list_points *tmp;
+
+ for (tmp = lp; tmp; tmp = tmp->next) {
+ if (!G_strcasecmp
+ (db_get_string(&(tmp->point->id)), db_get_string(id))) {
+ return tmp->point;
+ }
+ }
+
+ return NULL;
+}
+
+void point_update(Point * point, int type,
+ xmlNode * x, xmlNode * y, xmlNode * z)
+{
+ int i;
+ double val;
+
+ xmlNode *xyz[] = { x, y, z };
+
+ for (i = 0; i < DIM; i++) { /* x | y | z */
+ if (xyz[i]) {
+ if (node_get_value_double(xyz[i], &val)) {
+ point->xyz[i].val[type] = val;
+ point->xyz[i].is_null[type] = 0;
+ }
+ }
+ }
+
+ return;
+}
+
+/*
+ 0 ... error
+ 1 ... coordinate
+ 2 ... is constrained ?
+ */
+int point_get_xyz(Point * point, char *name, double *val)
+{
+
+ int index_xyz, index_type;
+ char *typesubstr;
+
+ index_xyz = index_type = -1;
+
+ /* name -> x_fix */
+ typesubstr = &name[2];
+
+ switch (name[0]) {
+ case 'x':
+ {
+ index_xyz = 0;
+ break;
+ }
+ case 'y':
+ {
+ index_xyz = 1;
+ break;
+ }
+ case 'z':
+ {
+ index_xyz = 2;
+ break;
+ }
+ default:
+ {
+ G_fatal_error(_("Unable to determine coordinate x|y|z!"));
+ break;
+ }
+ }
+
+ if (!G_strcasecmp(typesubstr, "fix")) {
+ index_type = FIX;
+ }
+ else {
+ if (!G_strcasecmp(typesubstr, "adj")) {
+ index_type = ADJ;
+ }
+ else {
+ if (!G_strcasecmp(typesubstr, "app")) {
+ index_type = APP;
+ }
+ else {
+ if (!G_strcasecmp(typesubstr, "con")) {
+ *val = point->xyz[index_xyz].is_constrained;
+ return 2;
+ }
+ else {
+ G_fatal_error(_("Unable to determine coordinate x|y|z!"));
+ }
+ }
+ }
+ }
+
+ if (index_type == -1 || index_xyz == -1 ||
+ point->xyz[index_xyz].is_null[index_type]) {
+ return 0;
+ }
+
+ *val = point->xyz[index_xyz].val[index_type];
+
+ return 1;
+}
+
+int create_obs(struct Map_info *Map, xmlDoc * doc, int dotable,
+ struct list_points *vpoints)
+{
+ unsigned int i;
+ int write;
+ int cat, cat1, cat2, cat3;
+ struct field_info *fi;
+ const int field = 1; /* layer 2 */
+
+ dbDriver *driver;
+ dbTable *table;
+ dbString table_name, id1, id2, id3;
+
+ xmlNode *root, *obss, *obs_id;
+ struct list_nodes *obs, *tmp;
+
+ struct line_pnts *ps;
+ struct line_cats *cs;
+
+ /* should be determined from DTD ... */
+ const char *obs_tag[] = {
+ "distance", "direction", "angle",
+ "height-diff", "slope-distance", "zenith-angle",
+ "dx", "dy", "dz",
+ "coordinate-x", "coordinate-y", "coordinate-z"
+ };
+
+ cat = 1;
+ db_init_string(&table_name);
+ db_init_string(&id1);
+ db_init_string(&id2);
+ db_init_string(&id3);
+
+ ps = Vect_new_line_struct();
+ cs = Vect_new_cats_struct();
+
+ list_init((void **)&obs);
+
+ if (dotable) {
+ fi = Vect_get_dblink(Map, 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);
+ }
+
+ db_init_string(&table_name);
+ db_set_string(&table_name, fi->table);
+
+ if (db_describe_table(driver, &table_name, &table) != DB_OK) {
+ G_fatal_error(_("Unable to describe table <%s>"), fi->table);
+ }
+
+ db_begin_transaction(driver);
+ }
+
+ root = xmlDocGetRootElement(doc);
+
+ if (!root) {
+ G_fatal_error(_("Unable to get the root element"));
+ }
+
+ find_node(root, "observations", &obss);
+
+ if (!obss) {
+ G_warning(_("No observations found"));
+ }
+ else {
+ for (i = 0; i < sizeof(obs_tag) / sizeof(char *); i++) {
+ if (find_nodes(obss, obs_tag[i], &obs)) {
+ tmp = obs;
+ while (tmp) {
+ write = 0;
+
+ if (dotable) {
+ insert_obs(fi, driver, table, tmp->node, cat);
+ }
+
+ if (!G_strcasecmp(obs_tag[i], "coordinate-x") ||
+ !G_strcasecmp(obs_tag[i], "coordinate-y") ||
+ !G_strcasecmp(obs_tag[i], "coordinate-z")) {
+ if (find_node(tmp->node, "id", &obs_id)) {
+ node_get_value_string(obs_id, NULL, &id1);
+ }
+ cat1 = point_exists(vpoints, &id1);
+ cat2 = cat3 = -1;
+ if (cat1 != -1) {
+ write = 1;
+ }
+ }
+ else {
+ if (!G_strcasecmp(obs_tag[i], "angle")) {
+ if (find_node(tmp->node, "from", &obs_id)) {
+ node_get_value_string(obs_id, NULL, &id1);
+ }
+ if (find_node(tmp->node, "left", &obs_id)) {
+ node_get_value_string(obs_id, NULL, &id2);
+ }
+ if (find_node(tmp->node, "right", &obs_id)) {
+ node_get_value_string(obs_id, NULL, &id3);
+ }
+
+ cat1 = point_exists(vpoints, &id1);
+ cat2 = point_exists(vpoints, &id2);
+ cat3 = point_exists(vpoints, &id3);
+ if (cat1 != -1 && cat2 != -1 && cat3 != -1) {
+ write = 1;
+ }
+ }
+ else {
+ if (find_node(tmp->node, "from", &obs_id)) {
+ node_get_value_string(obs_id, NULL, &id1);
+ }
+ if (find_node(tmp->node, "to", &obs_id)) {
+ node_get_value_string(obs_id, NULL, &id2);
+ }
+
+ cat1 = point_exists(vpoints, &id1);
+ cat2 = point_exists(vpoints, &id2);
+ cat3 = -1;
+ if (cat1 != -1 && cat2 != -1) {
+ write = 1;
+ }
+ }
+ }
+ if (write) {
+ write_line(Map, cat1, cat2, cat3, cat++);
+ }
+ else {
+ char buf[1024];
+ dbString err;
+
+ db_init_string(&err);
+
+ sprintf(buf,
+ "Line number %d: unable write vector line, "
+ " missing point id ", tmp->node->line);
+ db_set_string(&err, buf);
+
+ if (db_sizeof_string(&id1) > 0) {
+ sprintf(buf, "[%s]", db_get_string(&id1));
+ db_append_string(&err, buf);
+ }
+ if (db_sizeof_string(&id2) > 0) {
+ sprintf(buf, " or [%s]", db_get_string(&id2));
+ db_append_string(&err, buf);
+ }
+
+ if (db_sizeof_string(&id3) > 0) {
+ sprintf(buf, " or [%s]", db_get_string(&id3));
+ db_append_string(&err, buf);
+ }
+
+ db_append_string(&err, ".");
+ G_warning(_(db_get_string(&err)));
+
+ }
+ tmp = tmp->next;
+ }
+ }
+ }
+ }
+
+ if (dotable && driver) {
+ db_commit_transaction(driver);
+ db_close_database_shutdown_driver(driver);
+ }
+
+ list_nodes_free(&obs);
+
+ Vect_destroy_line_struct(ps);
+ Vect_destroy_cats_struct(cs);
+
+ return 0;
+}
+
+int point_exists(struct list_points *vpoints, dbString * from_id)
+{
+
+ Point *fp;
+
+ fp = find_point(vpoints, from_id);
+ if (fp) {
+ return fp->cat;
+ }
+
+ return -1;
+}
+
+int write_line(struct Map_info *Map, int cat_id1, int cat_id2, int cat_id3,
+ int cat)
+{
+
+ int line, nlines, i, j, k;
+ int pcat, cat_line, fcat;
+ int points[3];
+
+ struct line_pnts *ps_line, *ps[3];
+ struct line_cats *cs_line, *cs[3];
+
+ int cats[] = { cat_id1, cat_id2, cat_id3 };
+ const short dim = sizeof(cats) / sizeof(int);
+
+ for (i = 0; i < dim; i++) {
+ points[i] = 0;
+ ps[i] = Vect_new_line_struct();
+ cs[i] = Vect_new_cats_struct();
+ }
+
+ ps_line = Vect_new_line_struct();
+ cs_line = Vect_new_cats_struct();
+
+ Vect_build_partial(Map, GV_BUILD_BASE);
+
+ nlines = Vect_get_num_lines(Map);
+
+ for (line = 1; line <= nlines; line++) {
+ if (!Vect_line_alive(Map, line) ||
+ Vect_read_line(Map, ps_line, cs_line, line) != GV_POINT)
+ continue;
+
+ pcat = Vect_get_line_cat(Map, line, FIELD_POINT);
+
+ for (i = 0; i < dim; i++) {
+ if (cats[i] != -1 && pcat == cats[i]) {
+ points[i] = line;
+ }
+ }
+ }
+
+ for (i = 0; i < dim; i++) {
+ if (points[i] > 0) {
+ /* add category number to the point */
+ Vect_read_line(Map, ps[i], cs[i], points[i]);
+ Vect_cat_set(cs[i], FIELD_OBS, cat);
+ Vect_rewrite_line(Map, points[i], GV_POINT, ps[i], cs[i]);
+ }
+ }
+
+ if (cats[0] != -1) {
+ for (i = 1; cats[i] != -1 && i < dim; i++) {
+ cat_line = -1;
+ Vect_reset_line(ps_line);
+ Vect_reset_cats(cs_line);
+ for (j = 0; cat_line == -1 && j < cs[0]->n_cats; j++) {
+ if (cs[0]->field[j] != FIELD_OBS)
+ continue;
+
+ for (k = 0; cat_line == -1 && k < cs[i]->n_cats; k++) {
+ if (cs[i]->field[k] == FIELD_OBS &&
+ cs[i]->cat[k] != cat &&
+ cs[0]->cat[j] == cs[i]->cat[k]) {
+ cat_line = cs[i]->cat[k];
+ }
+ }
+ }
+
+ if (cat_line == -1) {
+ /* write new line with category number 'cat' */
+ Vect_cat_set(cs_line, FIELD_OBS, cat);
+ Vect_append_points(ps_line, ps[0], GV_FORWARD);
+ Vect_append_points(ps_line, ps[i], GV_FORWARD);
+ Vect_write_line(Map, GV_LINE, ps_line, cs_line);
+ }
+ else {
+ Vect_build_partial(Map, GV_BUILD_BASE);
+ nlines = Vect_get_num_lines(Map);
+
+ /* add category number to the existing vector line */
+ for (line = 1; line <= nlines; line++) {
+ if (!Vect_line_alive(Map, line) ||
+ Vect_read_line(Map, ps_line, cs_line,
+ line) != GV_LINE)
+ continue;
+
+ Vect_cat_get(cs_line, FIELD_OBS, &fcat);
+ if (fcat == cat_line) {
+ /* works only if fcat is the first category number in given field
+ -> it should be (?)
+ */
+
+ Vect_cat_set(cs_line, FIELD_OBS, cat);
+ Vect_rewrite_line(Map, line, GV_LINE, ps_line,
+ cs_line);
+ break;
+ }
+ }
+ }
+ }
+ }
+
+ Vect_destroy_line_struct(ps_line);
+ Vect_destroy_cats_struct(cs_line);
+
+ for (i = 0; i < dim; i++) {
+ Vect_destroy_line_struct(ps[i]);
+ Vect_destroy_cats_struct(cs[i]);
+ }
+
+
+
+ return 0;
+}
Property changes on: grass-addons/vector/v.in.gama/vect.c
___________________________________________________________________
Added: svn:mime-type
+ text/x-csrc
Added: svn:eol-style
+ native
More information about the grass-commit
mailing list