[GRASS-SVN] r70619 - in grass/branches/releasebranch_7_2: lib/vector/Vlib vector/v.out.postgis

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Feb 18 07:19:10 PST 2017


Author: martinl
Date: 2017-02-18 07:19:09 -0800 (Sat, 18 Feb 2017)
New Revision: 70619

Modified:
   grass/branches/releasebranch_7_2/lib/vector/Vlib/copy.c
   grass/branches/releasebranch_7_2/lib/vector/Vlib/write_pg.c
   grass/branches/releasebranch_7_2/vector/v.out.postgis/main.c
Log:
v.out.postgis: fix type option (closes #2460)


Modified: grass/branches/releasebranch_7_2/lib/vector/Vlib/copy.c
===================================================================
--- grass/branches/releasebranch_7_2/lib/vector/Vlib/copy.c	2017-02-18 14:56:06 UTC (rev 70618)
+++ grass/branches/releasebranch_7_2/lib/vector/Vlib/copy.c	2017-02-18 15:19:09 UTC (rev 70619)
@@ -122,7 +122,10 @@
         /* copy features */
         ret += copy_lines_2(In, field, topo, Out);
 
-        if (topo == TOPO_NONE) {
+        if (topo == TOPO_NONE &&
+            /* check output feature type, centroids can be exported as
+             * points; boundaries as linestrings */
+            strcmp(Vect_get_finfo_geometry_type(Out), "polygon") == 0) {
             /* copy areas - external formats and simple features access only */
             ret += Vect__copy_areas(In, field, Out);
         }
@@ -210,6 +213,8 @@
     struct line_pnts *Points, *CPoints, *NPoints;
     struct line_cats *Cats, *CCats;
 
+    const char *ftype = NULL;
+    
     Points  = Vect_new_line_struct();
     CPoints = Vect_new_line_struct();
     NPoints = Vect_new_line_struct();
@@ -221,8 +226,6 @@
     ret = 0;
     nlines = Vect_get_num_lines(In);
     if (topo == TOPO_NONE) {
-        const char *ftype;
-
         ftype = Vect_get_finfo_geometry_type(Out);
         G_debug(2, "feature type: %s", ftype ? ftype : "?");
         if (!ftype)
@@ -249,14 +252,37 @@
         }
         if (type == 0)
             continue;       /* dead line */
-        
-        if (topo == TOPO_NONE && (type == GV_CENTROID || type == GV_BOUNDARY)) {
-            /* OGR/PostGIS layers (simple features): centroids are
-               stored in topo polygon defined by areas (topo required)
-            */
-            continue;
+        if (In->constraint.type_flag) {
+            /* skip feature by type */
+            if (!(type & In->constraint.type))
+                continue;
         }
 
+        if (topo == TOPO_NONE) {
+            /* OGR/PostGIS layers (simple features) */
+            int skip = FALSE;
+            
+            if (type == GV_BOUNDARY)
+                /* boundaries are written as linestrings when output
+                 * feature type is defined as 'linestring', otherwise
+                 * they are skipped */
+                if (ftype && strcmp(ftype, "linestring") != 0)
+                    skip = TRUE;
+
+            /* centroids are stored in topo polygon defined by areas
+               (topo required) */
+            if (type == GV_CENTROID) {
+                /* centroids are written as points when output feature
+                 * type is defined as 'point', otherwise they are
+                 * skipped */
+                if (ftype && strcmp(ftype, "point") != 0)
+                    skip = TRUE;
+            }
+
+            if (skip)
+                continue;
+        }
+
         /* don't skips boundaries if field != -1 */
         if (field != -1) {
             if (type & GV_BOUNDARY) {
@@ -500,11 +526,13 @@
             /* no centroid - check if area forms an isle */
 	    /* this check does not make sense because the area is also
 	     * not exported if it is part of an isle inside another
-	     * area: the isle gets exported as an inner ring */
+	     * area: the isle gets exported as an inner ring
             if (!is_isle(In, area))
                 G_warning(_("No centroid defined for area %d. "
                             "Area not exported."),
                           area);
+            */
+            G_debug(3, "Area %d: is_isle() -> %d", area, is_isle(In, area));
             continue;
         }
         

Modified: grass/branches/releasebranch_7_2/lib/vector/Vlib/write_pg.c
===================================================================
--- grass/branches/releasebranch_7_2/lib/vector/Vlib/write_pg.c	2017-02-18 14:56:06 UTC (rev 70618)
+++ grass/branches/releasebranch_7_2/lib/vector/Vlib/write_pg.c	2017-02-18 15:19:09 UTC (rev 70619)
@@ -1085,12 +1085,13 @@
     
     switch (type) {
     case GV_POINT:
+    case GV_CENTROID:
         pg_info->feature_type = SF_POINT;
         break;
     case GV_LINE:
+    case GV_BOUNDARY:
         pg_info->feature_type = SF_LINESTRING;
         break;
-    case GV_BOUNDARY:
     case GV_AREA:
         pg_info->feature_type = SF_POLYGON;
         break;
@@ -1259,14 +1260,20 @@
             return 0;
         }
     }
-    else if (type & GV_BOUNDARY || type & GV_CENTROID) {
-        if (sf_type != SF_POLYGON) {
-            G_warning(_("Boundary/centroid skipped (output feature type: %s)"),
+    else if (type & GV_CENTROID) {
+        if (sf_type != SF_POLYGON && sf_type != SF_POINT) {
+            G_warning(_("Centroid skipped (output feature type: %s)"),
                       Vect_get_finfo_geometry_type(Map));
-            G_warning(_("Feature is not a polygon. Skipping."));
             return 0;
         }
     }
+    else if (type & GV_BOUNDARY) {
+        if (sf_type != SF_POLYGON && sf_type != SF_LINESTRING) {
+            G_warning(_("Boundary skipped (output feature type: %s)"),
+                      Vect_get_finfo_geometry_type(Map));
+            return 0;
+        }
+    }
     else if (type & GV_FACE) {
         if (sf_type != SF_POLYGON25D) {
             G_warning(_("Face skipped (output feature type: %s)"),
@@ -1772,7 +1779,8 @@
     wkb_data = NULL;
     if (type & GV_POINTS) /* point or centroid */
         wkb_data = point_to_wkb(byte_order, points[0], with_z, &nbytes);
-    else if (type == GV_LINE)
+    else if (type == GV_LINE ||
+             (type == GV_BOUNDARY && pg_info->feature_type == SF_LINESTRING))
         wkb_data = linestring_to_wkb(byte_order, points[0], with_z, &nbytes);
     else if (type & (GV_BOUNDARY | GV_FACE | GV_AREA)) {
         if (!pg_info->toposchema_name || type == GV_AREA) {

Modified: grass/branches/releasebranch_7_2/vector/v.out.postgis/main.c
===================================================================
--- grass/branches/releasebranch_7_2/vector/v.out.postgis/main.c	2017-02-18 14:56:06 UTC (rev 70618)
+++ grass/branches/releasebranch_7_2/vector/v.out.postgis/main.c	2017-02-18 15:19:09 UTC (rev 70619)
@@ -142,11 +142,27 @@
         Vect_copy_map_dblinks(&In, &Out, TRUE);
 
     /* check output type */
-    if (otype > 0) { /* type is not 'auto' */
+    if (otype < 1 && Vect_level(&In) > 1) {
+        /* type 'auto' -> try to guess output feature type on level 2 */
+        if (Vect_get_num_areas(&In) > 0)
+            otype = GV_AREA;
+        else if (Vect_get_num_primitives(&In, GV_LINE) > 0)
+            otype = GV_LINE;
+        else if (Vect_get_num_primitives(&In, GV_POINT) > 0)
+            otype = GV_POINT;
+    }
+    if (otype > 0) {
+        if (otype & (GV_FACE | GV_KERNEL))
+            G_fatal_error(_("Feature type '%s' not supported"),
+                          params.type->answer);
+        
+        /* set up output feature type if possible */
         if (Vect_write_line(&Out, otype, NULL, NULL) < 0)
             G_fatal_error(_("Feature type %d is not supported"), otype);
+
+        Vect_set_constraint_type(&In, otype);
     }
-
+    
     /* copy vector features & create PostGIS table */
     if (Vect_copy_map_lines_field(&In, field, &Out) != 0)
         G_fatal_error(_("Copying features failed"));
@@ -209,9 +225,13 @@
     G_debug(1, "output_handler(): schema = %s; olayer = %s", pg_info->schema_name, pg_info->table_name);
     sprintf(stmt, "SELECT DropGeometryTable('%s', '%s')", pg_info->schema_name, pg_info->table_name);
     result = PQexec(pg_info->conn, stmt);
+    /*
+      be quiet - table may do not exists
+
     if (!result || PQresultStatus(result) != PGRES_TUPLES_OK) {
         G_warning(_("Unable to drop table <%s.%s>"), pg_info->schema_name, pg_info->table_name);
     }
+    */
     PQclear(result);
 
     if (pg_info->toposchema_name) {



More information about the grass-commit mailing list