Browse Source

v.out.postgis: avoid segfault when no features exported
print warning about number of skipped features


git-svn-id: https://svn.osgeo.org/grass/grass/trunk@56374 15284696-431f-4ddb-bdfa-cd5b030d7da7

Martin Landa 12 years ago
parent
commit
f04cf337d9
3 changed files with 17 additions and 4 deletions
  1. 3 0
      lib/vector/Vlib/build_pg.c
  2. 8 2
      lib/vector/Vlib/copy.c
  3. 6 2
      vector/v.out.postgis/main.c

+ 3 - 0
lib/vector/Vlib/build_pg.c

@@ -63,6 +63,9 @@ int Vect_build_pg(struct Map_info *Map, int build)
         return 0;
     pg_info->inTransaction = FALSE;
     
+    if (pg_info->feature_type == SF_UNKNOWN)
+        return 1;
+    
     if (build == plus->built)
         return 1;            /* do nothing */
 

+ 8 - 2
lib/vector/Vlib/copy.c

@@ -199,7 +199,7 @@ int copy_lines_1(struct Map_info *In, int field, struct Map_info *Out)
 */
 int copy_lines_2(struct Map_info *In, int field, int topo, struct Map_info *Out)
 {
-    int i, type, nlines;
+  int i, type, nlines, nskipped;
     int ret, left, rite, centroid;
 
     struct line_pnts *Points, *CPoints;
@@ -225,6 +225,7 @@ int copy_lines_2(struct Map_info *In, int field, int topo, struct Map_info *Out)
     else
         G_message(_("Copying features..."));    
     
+    nskipped = 0;
     for (i = 1; i <= nlines; i++) {
         if (!Vect_line_alive(In, i))
             continue;
@@ -281,8 +282,10 @@ int copy_lines_2(struct Map_info *In, int field, int topo, struct Map_info *Out)
                         continue;
                 }
             }
-            else if (Vect_cat_get(Cats, field, NULL) == 0)
+            else if (Vect_cat_get(Cats, field, NULL) == 0) {
+                nskipped++;
                 continue;   /* different layer */
+            }
         }
         
         if (-1 == Vect_write_line(Out, type, Points, Cats)) {
@@ -291,6 +294,9 @@ int copy_lines_2(struct Map_info *In, int field, int topo, struct Map_info *Out)
         }
     }
 
+    if (nskipped > 0)
+        G_warning(_("%d features without category or from different layer skipped"), nskipped);
+    
     Vect_destroy_line_struct(Points);
     Vect_destroy_line_struct(CPoints);
     Vect_destroy_cats_struct(Cats);

+ 6 - 2
vector/v.out.postgis/main.c

@@ -141,12 +141,16 @@ int main(int argc, char *argv[])
 
     /* close input map */
     Vect_close(&In);
-    
+
     /* build topology for output map */
     if (Vect_build(&Out) != 1)
         G_fatal_error(_("Building %s topology failed"),
                       flags.topo->answer ? "PostGIS" : "pseudo");
 
+    if (Vect_get_num_lines(&Out) < 1)
+        G_fatal_error(_("No features exported. PostGIS layer <%s> not created."),
+                      Vect_get_name(&Out));
+
     G_done_msg(_("%d features (%s) written to <%s>."),
                Vect_get_num_lines(&Out), Vect_get_finfo_geometry_type(&Out),
                Vect_get_name(&Out));
@@ -194,6 +198,6 @@ void output_handler(void *p)
         if (!result || PQresultStatus(result) != PGRES_TUPLES_OK) {
             G_warning(_("Unable to drop topology schema <%s>"), pg_info->toposchema_name);
         }
+        PQclear(result);
     }
-    PQclear(result);
 }