[GRASS-SVN] r52580 - grass/trunk/vector/v.out.postgis

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Aug 7 07:56:54 PDT 2012


Author: martinl
Date: 2012-08-07 07:56:53 -0700 (Tue, 07 Aug 2012)
New Revision: 52580

Added:
   grass/trunk/vector/v.out.postgis/create.c
Removed:
   grass/trunk/vector/v.out.postgis/export.c
   grass/trunk/vector/v.out.postgis/export_topo.c
Modified:
   grass/trunk/vector/v.out.postgis/local_proto.h
   grass/trunk/vector/v.out.postgis/main.c
   grass/trunk/vector/v.out.postgis/options.c
Log:
v.out.postgis: simplify code, use Vect_copy_map_lines()


Added: grass/trunk/vector/v.out.postgis/create.c
===================================================================
--- grass/trunk/vector/v.out.postgis/create.c	                        (rev 0)
+++ grass/trunk/vector/v.out.postgis/create.c	2012-08-07 14:56:53 UTC (rev 52580)
@@ -0,0 +1,82 @@
+#include <unistd.h>
+
+#include <grass/gis.h>
+#include <grass/glocale.h>
+
+#include "local_proto.h"
+
+static void file_handler(void *);
+
+void create_table(struct Map_info *In, struct Map_info *Out)
+{
+    int type;
+    struct Format_info_pg *pg_info;
+
+    pg_info = &(Out->fInfo.pg);
+    if (pg_info->feature_type != SF_UNKNOWN)
+        return;
+    
+    /* create PostGIS table if doesn't exist */
+    type = Vect_read_next_line(In, NULL, NULL);
+    Vect_rewind(In);
+    
+    if (V2_open_new_pg(Out, type) < 0)
+        G_fatal_error(_("Unable to create PostGIS layer <%s>"),
+                      Vect_get_finfo_layer_name(Out));
+}
+
+char *create_pgfile(const char *dsn, const char *schema, int topo)
+{
+    char *filename, *conninfo;
+    FILE *fp;
+    
+    struct Key_Value *key_val;
+    
+    filename = NULL;
+    G_asprintf(&filename, "PG_%d", (int) getpid());
+    G_debug(1, "PG file: %s", filename);
+    
+    fp = G_fopen_new("", filename);
+    if (!fp)
+        G_fatal_error(_("Unable to create <%s> file"), filename);
+    setenv("GRASS_VECTOR_PGFILE", filename, TRUE);
+    G_add_error_handler(file_handler, filename);
+
+    key_val = G_create_key_value();
+    
+   /* be friendly, ignored 'PG:' prefix for GRASS-PostGIS data driver */
+    if (G_strncasecmp(dsn, "PG:", 3) == 0) {
+        int i, length;
+        
+        length = strlen(dsn);
+        conninfo = (char *) G_malloc(length - 3);
+        for (i = 3; i < length; i++)
+            conninfo[i-3] = dsn[i];
+        conninfo[length-3] = '\0';
+    }
+    else {
+        conninfo = G_store(dsn);
+    }
+    
+    G_set_key_value("conninfo", conninfo, key_val);
+    if (schema)
+        G_set_key_value("schema", schema, key_val);
+    if (topo)
+        G_set_key_value("topology", "on", key_val);
+
+    if (G_fwrite_key_value(fp, key_val) < 0)
+        G_fatal_error(_("Error writing <%s> file"), filename);
+
+    fclose(fp);
+
+    G_free(conninfo);
+    
+    return filename;
+}
+
+void file_handler(void *p) {
+    const char *filename = (const char *) p;
+    
+    G_remove("", filename);
+    unsetenv("GRASS_VECTOR_PGFILE");
+}


Property changes on: grass/trunk/vector/v.out.postgis/create.c
___________________________________________________________________
Added: svn:mime-type
   + text/x-csrc
Added: svn:eol-style
   + native

Deleted: grass/trunk/vector/v.out.postgis/export.c
===================================================================
--- grass/trunk/vector/v.out.postgis/export.c	2012-08-07 14:33:38 UTC (rev 52579)
+++ grass/trunk/vector/v.out.postgis/export.c	2012-08-07 14:56:53 UTC (rev 52580)
@@ -1,105 +0,0 @@
-#include <grass/gis.h>
-#include <grass/glocale.h>
-
-#include "local_proto.h"
-
-int export_lines(struct Map_info *In, int field, struct Map_info *Out)
-{
-    int type, nlines;
-
-    struct line_pnts *Points;
-    struct line_cats *Cats;
-
-    Points  = Vect_new_line_struct();
-    Cats    = Vect_new_cats_struct();
-
-    nlines = 0;
-    while(TRUE) {
-        type = Vect_read_next_line(In, Points, Cats);
-        if (type == -2)
-            break; /* eof */
-        
-        G_debug(3, "Export line %d", ++nlines);
-
-        if (-1 == Vect_write_line(Out, type, Points, Cats))
-            G_fatal_error(_("Unable to export feature %d. Exiting."), nlines);
-        
-        G_progress(nlines, 1e3);
-    }
-    G_progress(1, 1);
-
-    if (nlines < 1)
-        G_warning(_("Nothing exported"));
-    
-    Vect_destroy_line_struct(Points);
-    Vect_destroy_cats_struct(Cats);
-    
-    return nlines;
-}
-
-int export_areas(struct Map_info *In, int field, struct Map_info *Out)
-{
-    int cat, i, isle;
-    int area, nareas, nisles, nisles_alloc;
-
-    struct line_pnts *Points, **IPoints;
-    struct line_cats *Cats;
-
-    Points = Vect_new_line_struct();
-    Cats   = Vect_new_cats_struct();
-    
-    IPoints = NULL;
-    nisles_alloc = 0;
-    
-    nareas = Vect_get_num_areas(In);
-    for(area = 1; area <= nareas; area++) {
-        Vect_reset_cats(Cats);
-        
-        G_percent(area, nareas, 3);
-        G_debug(3, "Export area %d", area);
-
-        /* get outer ring geometry */
-        Vect_get_area_points(In, area, Points);
-        
-        /* get category */
-        cat = Vect_get_area_cat(In, area, field);
-        if (cat < 0) {
-            G_warning(_("No centroid found for area %d. "
-                        "Area not exported."),
-                      area);
-            continue;
-        }
-        G_debug(3, " -> cat %d", cat);
-        Vect_cat_set(Cats, field, cat);
-
-        nisles = Vect_get_area_num_isles(In, area);
-        if (nisles > nisles_alloc) {
-            /* reallocate space for isles */
-            IPoints = (struct line_pnts **) G_realloc(IPoints,
-                                                      nisles *
-                                                      sizeof(struct line_pnts *));
-            for (i = nisles_alloc; i < nisles; i++)
-                IPoints[i] = Vect_new_line_struct();
-            nisles_alloc = nisles;
-        }
-        G_debug(3, " -> nisles=%d", nisles);
-        
-        /* get isles geometry */
-        for (i = 0; i < nisles; i++) {
-            isle = Vect_get_area_isle(In, area, i);
-            Vect_get_isle_points(In, isle, IPoints[i]);
-        }
-
-        V2_write_area_pg(Out, Points, Cats,
-                         (const struct line_pnts **) IPoints, nisles);
-
-        /* TODO */
-    }
-    
-    Vect_destroy_line_struct(Points);
-    Vect_destroy_cats_struct(Cats);
-    for (i = 0; i < nisles_alloc; i++)
-        Vect_destroy_line_struct(IPoints[i]);
-    
-    return nareas;
-}

Deleted: grass/trunk/vector/v.out.postgis/export_topo.c
===================================================================
--- grass/trunk/vector/v.out.postgis/export_topo.c	2012-08-07 14:33:38 UTC (rev 52579)
+++ grass/trunk/vector/v.out.postgis/export_topo.c	2012-08-07 14:56:53 UTC (rev 52580)
@@ -1,73 +0,0 @@
-#include <grass/gis.h>
-#include <grass/glocale.h>
-
-#include "local_proto.h"
-
-static int export_nodes(struct Map_info *, struct Map_info *);
-
-int export_topo(struct Map_info *In, int field, struct Map_info *Out)
-{
-    int nfeat;
-
-    nfeat = 0;
-
-    /* export GRASS nodes -> PostGIS nodes */
-    nfeat += export_nodes(In, Out);
-    
-    /*
-      export GRASS points -> PostGIS nodes
-      export GRASS lines/boundaries -> PostGIS edges
-     
-      skip centroids
-    */
-    G_message(_("Exporting points/lines/boundaries..."));
-    nfeat += export_lines(In, field, Out);
-    
-    /* export GRASS areas as PostGIS faces */
-
-    /* export GRASS isles as PostGIS faces */
-
-    /* export GRASS centroids as nodes (in faces) */
-    
-    return nfeat;
-}
-
-int export_nodes(struct Map_info *In, struct Map_info *Out)
-{
-    int node, nnodes, with_z;
-    double x, y, z;
-    
-    struct line_pnts *Points;
-    struct line_cats *Cats;
-    
-    nnodes = Vect_get_num_nodes(In);
-    if (nnodes < 1)
-        return 0;
-    
-    with_z = Vect_is_3d(In);
-
-    Points = Vect_new_line_struct();
-    Cats    = Vect_new_cats_struct();
-
-    G_message(_("Exporting nodes..."));
-    Vect_append_point(Points, 0., 0., 0.);
-    for (node = 1; node <= nnodes; node++) {
-        G_debug(3, "Exporting GRASS node %d", node);
-        
-        G_percent(node, nnodes, 5);
-        Vect_get_node_coor(In, node, &x, &y, &z);
-        Points->x[0] = x;
-        Points->y[0] = y;
-        if (with_z)
-            Points->z[0] = z;
-
-        if (-1 == Vect_write_line(Out, GV_POINT, Points, Cats))
-            G_fatal_error(_("Unable to export node %d. Exiting."), node);
-    }
-        
-    Vect_destroy_line_struct(Points);
-    Vect_destroy_cats_struct(Cats);
-
-    return nnodes;
-}
-

Modified: grass/trunk/vector/v.out.postgis/local_proto.h
===================================================================
--- grass/trunk/vector/v.out.postgis/local_proto.h	2012-08-07 14:33:38 UTC (rev 52579)
+++ grass/trunk/vector/v.out.postgis/local_proto.h	2012-08-07 14:56:53 UTC (rev 52580)
@@ -11,15 +11,11 @@
     struct Flag *table, *topo;
 };
 
-/* export.c */
-int export_lines(struct Map_info *, int, struct Map_info *);
-int export_areas(struct Map_info *, int, struct Map_info *);
+/* create.c */
+void create_table(struct Map_info *, struct Map_info *);
+char *create_pgfile(const char *, const char *, int);
 
-/* export_topo.c */
-int export_topo(struct Map_info *, int, struct Map_info *);
-
 /* options.c */
 void define_options(struct params *, struct flags *);
-char *create_pgfile(const char *, const char *, int);
 
 #endif /* __LOCAL_PROTO_V_OUT_POSTGIS__ */

Modified: grass/trunk/vector/v.out.postgis/main.c
===================================================================
--- grass/trunk/vector/v.out.postgis/main.c	2012-08-07 14:33:38 UTC (rev 52579)
+++ grass/trunk/vector/v.out.postgis/main.c	2012-08-07 14:56:53 UTC (rev 52580)
@@ -9,7 +9,7 @@
  * COPYRIGHT:    (C) 2012 by Martin Landa, and the GRASS Development Team
  *
  *               This program is free software under the GNU General
- *               Public License (>=v2).  Read the file COPYING that
+ *               Public License (>=v2). Read the file COPYING that
  *               comes with GRASS for details.
  *
  **************************************************************/
@@ -65,45 +65,19 @@
     if (-1 == Vect_open_new(&Out, params.olayer->answer, Vect_is_3d(&In)))
         G_fatal_error(_("Unable to create PostGIS layer <%s>"),
                       params.olayer->answer);
-    Vect_set_error_handler_io(NULL, &Out); /* define error handler */
-
+    
     /* define attributes */
     field = Vect_get_field_number(&In, params.layer->answer);
     if (!flags.table->answer)
         Vect_copy_map_dblinks(&In, &Out, TRUE);
 
-    ret = 0;
-    if (flags.topo->answer) {
-        /* PostGIS topology export */
-        G_message(_("Exporting features (topology)..."));
-        ret = export_topo(&In, field, &Out);
-    }
-    else {
-        /* simple features export */
-        int do_areas;
+    /* create PostGIS layer */
+    create_table(&In, &Out);
 
-        do_areas = FALSE;
-        if (Vect_level(&In) >= 2) {
-            if (Vect_get_num_areas(&In) > 0)
-                do_areas = TRUE;
-        }
-        else {
-            G_warning(_("Unable to process areas"));
-        }
-        
-        if (!do_areas) {
-            G_message(_("Exporting features..."));
-            ret = export_lines(&In, field, &Out);
-        }
-        else {
-            G_message(_("Exporting areas..."));
-            ret = export_areas(&In, field, &Out);
-        }
-    }
+    /* copy vector features */
+    if (Vect_copy_map_lines_field(&In, field, &Out) != 0)
+        G_fatal_error(_("Copying features failed"));
     
-    if (ret > 0)
-        G_message(_("%d features exported"), ret);
-
     Vect_build(&Out);
     
     Vect_close(&In);

Modified: grass/trunk/vector/v.out.postgis/options.c
===================================================================
--- grass/trunk/vector/v.out.postgis/options.c	2012-08-07 14:33:38 UTC (rev 52579)
+++ grass/trunk/vector/v.out.postgis/options.c	2012-08-07 14:56:53 UTC (rev 52580)
@@ -1,14 +1,8 @@
-#include <unistd.h>
-#include <string.h>
-#include <stdlib.h>
-
 #include <grass/gis.h>
 #include <grass/glocale.h>
 
 #include "local_proto.h"
 
-static void file_handler(void *);
-
 void define_options(struct params *params, struct flags *flags)
 {
     params->input = G_define_standard_option(G_OPT_V_INPUT);
@@ -52,58 +46,3 @@
         _("Export PostGIS topology instead of simple features");
 }
 
-char *create_pgfile(const char *dsn, const char *schema, int topo)
-{
-    char *filename, *conninfo;
-    FILE *fp;
-    
-    struct Key_Value *key_val;
-    
-    filename = NULL;
-    G_asprintf(&filename, "PG_%d", (int) getpid());
-    G_debug(1, "PG file: %s", filename);
-    
-    fp = G_fopen_new("", filename);
-    if (!fp)
-        G_fatal_error(_("Unable to create <%s> file"), filename);
-    setenv("GRASS_VECTOR_PGFILE", filename, TRUE);
-    G_add_error_handler(file_handler, filename);
-
-    key_val = G_create_key_value();
-    
-   /* be friendly, ignored 'PG:' prefix for GRASS-PostGIS data driver */
-    if (G_strncasecmp(dsn, "PG:", 3) == 0) {
-        int i, length;
-        
-        length = strlen(dsn);
-        conninfo = (char *) G_malloc(length - 3);
-        for (i = 3; i < length; i++)
-            conninfo[i-3] = dsn[i];
-        conninfo[length-3] = '\0';
-    }
-    else {
-        conninfo = G_store(dsn);
-    }
-    
-    G_set_key_value("conninfo", conninfo, key_val);
-    if (schema)
-        G_set_key_value("schema", schema, key_val);
-    if (topo)
-        G_set_key_value("topology", "on", key_val);
-
-    if (G_fwrite_key_value(fp, key_val) < 0)
-        G_fatal_error(_("Error writing <%s> file"), filename);
-
-    fclose(fp);
-
-    G_free(conninfo);
-    
-    return filename;
-}
-
-void file_handler(void *p) {
-    const char *filename = (const char *) p;
-    
-    G_remove("", filename);
-    unsetenv("GRASS_VECTOR_PGFILE");
-}



More information about the grass-commit mailing list