[GRASS-SVN] r47975 - in sandbox/martinl: . v.topo.to.postgis

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Aug 30 06:47:28 EDT 2011


Author: martinl
Date: 2011-08-30 03:47:28 -0700 (Tue, 30 Aug 2011)
New Revision: 47975

Added:
   sandbox/martinl/v.topo.to.postgis/
   sandbox/martinl/v.topo.to.postgis/Makefile
   sandbox/martinl/v.topo.to.postgis/main.c
   sandbox/martinl/v.topo.to.postgis/proto.h
   sandbox/martinl/v.topo.to.postgis/topo.c
   sandbox/martinl/v.topo.to.postgis/v.topo.to.postgis.html
Log:
experimental v.topo.to.postgis (currently support only for areas)


Added: sandbox/martinl/v.topo.to.postgis/Makefile
===================================================================
--- sandbox/martinl/v.topo.to.postgis/Makefile	                        (rev 0)
+++ sandbox/martinl/v.topo.to.postgis/Makefile	2011-08-30 10:47:28 UTC (rev 47975)
@@ -0,0 +1,14 @@
+
+MODULE_TOPDIR = ../..
+
+PGM=v.topo.to.postgis
+
+LIBES = $(VECTORLIB) $(GISLIB) $(PQLIB) $(DBMILIB)
+DEPENDENCIES = $(VECTORDEP) $(GISDEP) $(DBMIDEP)
+EXTRA_INC = $(VECT_INC)
+EXTRA_CFLAGS = $(VECT_CFLAGS) $(PQINCPATH)
+EXTRA_LDFLAGS = $(PQLIBPATH)
+
+include $(MODULE_TOPDIR)/include/Make/Module.make
+
+default: cmd	


Property changes on: sandbox/martinl/v.topo.to.postgis/Makefile
___________________________________________________________________
Added: svn:mime-type
   + text/x-makefile
Added: svn:eol-style
   + native

Added: sandbox/martinl/v.topo.to.postgis/main.c
===================================================================
--- sandbox/martinl/v.topo.to.postgis/main.c	                        (rev 0)
+++ sandbox/martinl/v.topo.to.postgis/main.c	2011-08-30 10:47:28 UTC (rev 47975)
@@ -0,0 +1,85 @@
+
+/***************************************************************
+ *
+ * MODULE:       v.topo.to.postgis
+ * 
+ * AUTHOR(S):    Martin Landa <landa.martin gmail.com>
+ *               
+ * PURPOSE:      Converts GRASS topology and converts to PostGIS topology
+ *               
+ * COPYRIGHT:    (C) 2011 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 <string.h>
+#include <grass/gis.h>
+#include <grass/vector.h>
+#include <grass/glocale.h>
+
+#include <libpq-fe.h>
+
+#include "proto.h"
+
+int main(int argc, char *argv[])
+{
+    struct GModule *module;
+    struct {
+      struct Option *map, *db;
+    } opt;
+    
+    char *conninfo;
+    const char *name, *dbname;
+    struct Map_info Map;
+    
+    PGconn *conn;
+    
+    G_gisinit(argv[0]);
+
+    module = G_define_module();
+    module->overwrite = TRUE;
+    G_add_keyword(_("vector"));
+    G_add_keyword(_("topology"));
+    G_add_keyword(_("postgis"));
+    module->description = _("Converts GRASS topology to PostGIS.");
+
+    opt.map = G_define_standard_option(G_OPT_V_MAP);
+
+    opt.db = G_define_option();
+    opt.db->key = "dbname";
+    opt.db->description = _("Name of PostGIS database");
+    opt.db->required = YES;
+    opt.db->type = TYPE_STRING;
+    opt.db->key_desc = "name";
+    
+    if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+
+    name = opt.map->answer;
+    dbname = opt.db->answer;
+    
+    /* open vector on topo level */
+    Vect_set_open_level(2);
+    Vect_open_old(&Map, name, "");
+
+    /* connect database */
+    G_asprintf(&conninfo, "dbname=%s", dbname);
+    conn = PQconnectdb(conninfo);
+    if (PQstatus(conn) != CONNECTION_OK) {
+	PQfinish(conn);
+	G_fatal_error(_("Unable to open database <%s>"), dbname);
+    }
+    
+    /* convert GRASS topo to PostGIS */
+    Vect_topo_to_pg(&Map, conn,
+		    G_check_overwrite(argc, argv));
+    
+    /* close the connection to the database and cleanup */
+    PQfinish(conn);
+
+    exit(EXIT_SUCCESS);
+}


Property changes on: sandbox/martinl/v.topo.to.postgis/main.c
___________________________________________________________________
Added: svn:mime-type
   + text/x-csrc
Added: svn:eol-style
   + native

Added: sandbox/martinl/v.topo.to.postgis/proto.h
===================================================================
--- sandbox/martinl/v.topo.to.postgis/proto.h	                        (rev 0)
+++ sandbox/martinl/v.topo.to.postgis/proto.h	2011-08-30 10:47:28 UTC (rev 47975)
@@ -0,0 +1 @@
+int Vect_topo_to_pg(struct Map_info *Map, PGconn *conn, int);


Property changes on: sandbox/martinl/v.topo.to.postgis/proto.h
___________________________________________________________________
Added: svn:mime-type
   + text/x-chdr
Added: svn:eol-style
   + native

Added: sandbox/martinl/v.topo.to.postgis/topo.c
===================================================================
--- sandbox/martinl/v.topo.to.postgis/topo.c	                        (rev 0)
+++ sandbox/martinl/v.topo.to.postgis/topo.c	2011-08-30 10:47:28 UTC (rev 47975)
@@ -0,0 +1,156 @@
+#include <grass/vector.h>
+#include <grass/dbmi.h>
+#include <grass/glocale.h>
+
+#include <libpq-fe.h>
+
+#define TOPO_SCHEMA "topology"
+
+static void execute(const dbString *, PGconn *, PGresult **, const char *,
+		    ExecStatusType);
+static void add_adge_face(struct Map_info *, int, const struct line_pnts *,
+			  const char *, dbString *);
+
+int Vect_topo_to_pg(struct Map_info *Map, PGconn *conn, int overwrite)
+{
+    PGresult   *res;
+
+    int node, line, nnodes, nlines, ltype;
+    double x, y, z;
+    char *sql;
+    const char *name;
+    dbString stmt;
+    
+    struct ilist *list;
+    struct line_pnts *Points;
+
+    db_init_string(&stmt);
+    
+    res = NULL;
+    name = Vect_get_name(Map);
+    list = Vect_new_list();
+    Points = Vect_new_line_struct();
+    
+    /* set search path */
+    db_set_string(&stmt, "SET search_path TO ");
+    db_append_string(&stmt, TOPO_SCHEMA);
+    db_append_string(&stmt, ", public");
+    execute(&stmt, conn, &res, "SELECT", PGRES_COMMAND_OK);
+    PQclear(res);
+
+    /* check if topo already exists */
+    db_set_string(&stmt, "SELECT * FROM topology WHERE name = '");
+    db_append_string(&stmt, name);
+    db_append_string(&stmt, "'");
+    execute(&stmt, conn, &res, "SELECT", PGRES_TUPLES_OK);
+    if (res && PQntuples(res) > 0) {
+	if (!overwrite)
+	    G_fatal_error(_("Topology for vector map <%s> already exists"),
+			  Vect_get_full_name(Map));
+	G_warning(_("Topology for vector map <%s> already exists "
+		    "and will be overwritten"), Vect_get_full_name(Map));
+	
+	/* drop topology */
+	db_set_string(&stmt, "SELECT DropTopology ('");
+	db_append_string(&stmt, name);
+	db_append_string(&stmt, "')");
+	execute(&stmt, conn, &res, "SELECT", PGRES_TUPLES_OK);
+   }
+    PQclear(res);
+
+    /* Start a transaction block */
+    res = PQexec(conn, "BEGIN");
+    PQclear(res);
+
+    /* create topology tables */
+    db_set_string(&stmt, "SELECT CreateTopology('");
+    db_append_string(&stmt, Vect_get_name(Map));
+    db_append_string(&stmt, "')");
+    execute(&stmt, conn, &res, "CreateTopology", PGRES_TUPLES_OK);
+    
+    /* add nodes */
+    nnodes = Vect_get_num_nodes(Map);
+    for (node = 1; node <= nnodes; node++) {
+	Vect_get_node_coor(Map, node, &x, &y, &z);
+	G_debug(0, "node %d: x = %f y = %f z = %f", node, x, y, z);
+	G_asprintf(&sql, "SELECT topology.ST_AddIsoNode('%s', 0, "
+		  "'POINT(%f %f)')", name, x, y);
+	db_set_string(&stmt, sql);
+	execute(&stmt, conn, &res, "SELECT", PGRES_TUPLES_OK);
+    }
+    
+    /* add edges, faces and isolated nodes */
+    nlines = Vect_get_num_lines(Map);
+    for (line = 1; line <= nlines; line++) {
+	ltype = Vect_read_next_line(Map, Points, NULL);
+	if (ltype == -1) 
+	    G_fatal_error(_("Unable to read line %d"), line);
+	if (ltype == -2)
+	    break; /* should not happen */
+	G_debug(0, "line %d: type = %d npoints = %d", line, ltype,
+		Points->n_points);
+	
+	switch(ltype) {
+	case GV_POINT:
+	    /* TODO: isolated node */
+	    break;
+	case GV_LINE:
+	    /* TODO: isolated edge */
+	    break;
+	case GV_CENTROID:
+	    /* TODO: face ? */
+	    break;
+	case GV_BOUNDARY:
+	    /* -> edge */
+	    add_adge_face(Map, line, Points, name, &stmt);
+	    execute(&stmt, conn, &res, "SELECT", PGRES_TUPLES_OK);
+	default:
+	    G_warning(_("Unsupported type %d"), ltype);
+	    break;
+	}
+    }
+
+    /* end the transaction */
+    res = PQexec(conn, "END");
+    PQclear(res);
+
+    Vect_destroy_list(list);
+    Vect_destroy_line_struct(Points);
+    
+    return 0;
+}
+
+void execute(const dbString *stmt, PGconn *conn, PGresult **res,
+	     const char *cmd, ExecStatusType status)
+{
+    G_debug(0, "SQL: %s", db_get_string(stmt));
+	    
+    *res = PQexec(conn, db_get_string(stmt));
+    if (PQresultStatus(*res) != status) {
+	PQclear(*res);
+	G_fatal_error(_("%s command failed: %s"), cmd,
+		      PQerrorMessage(conn));
+	PQfinish(conn);	
+    }
+}
+
+void add_adge_face(struct Map_info *Map,
+		   int line, const struct line_pnts *Points,
+		   const char *name, dbString *stmt)
+{
+    int i, n1, n2;
+    char *sql;
+    
+    Vect_get_line_nodes(Map, line, &n1, &n2);
+
+    G_asprintf(&sql, "SELECT ST_AddEdgeModFace('%s', %d, %d, 'LINESTRING(",
+	       name, n1, n2);
+    db_set_string(stmt, sql);
+    for (i = 0; i < Points->n_points; i++) {
+	if (i > 0)
+	    db_append_string(stmt, ", ");
+	G_asprintf(&sql, "%f %f", Points->x[i], Points->y[i]);
+	db_append_string(stmt, sql);
+    }
+    db_append_string(stmt, ")')");
+}


Property changes on: sandbox/martinl/v.topo.to.postgis/topo.c
___________________________________________________________________
Added: svn:mime-type
   + text/x-csrc
Added: svn:eol-style
   + native

Added: sandbox/martinl/v.topo.to.postgis/v.topo.to.postgis.html
===================================================================
--- sandbox/martinl/v.topo.to.postgis/v.topo.to.postgis.html	                        (rev 0)
+++ sandbox/martinl/v.topo.to.postgis/v.topo.to.postgis.html	2011-08-30 10:47:28 UTC (rev 47975)
@@ -0,0 +1,13 @@
+<h2>DESCRIPTION</h2>
+
+<em>v.topo.to.postgis</em> converts GRASS topology to the structure
+compatible
+with <a href="http://trac.osgeo.org/postgis/wiki/UsersWikiPostgisTopology">PostGIS
+Topology</a>.
+
+<h2>AUTHORS</h2>
+
+Martin Landa
+
+<p>
+<i>Last changed: $Date$</i>


Property changes on: sandbox/martinl/v.topo.to.postgis/v.topo.to.postgis.html
___________________________________________________________________
Added: svn:mime-type
   + text/html
Added: svn:keywords
   + Author Date Id
Added: svn:eol-style
   + native



More information about the grass-commit mailing list