]> granicus.if.org Git - postgis/commitdiff
Fix GCC warnings
authorSandro Santilli <strk@kbt.io>
Thu, 12 Oct 2017 21:21:42 +0000 (21:21 +0000)
committerSandro Santilli <strk@kbt.io>
Thu, 12 Oct 2017 21:21:42 +0000 (21:21 +0000)
Obey const pointer in autofix logic.
Move ring closing logic to ptarray_to_GEOSCoordSeq.
Fix memory access.

Patch by Darafei Praliaskouski <me@komzpa.net>

Fixes #3900

git-svn-id: http://svn.osgeo.org/postgis/trunk@15976 b70326c6-7e19-0410-871a-916f4a2858ee

liblwgeom/lwgeom_geos.c
liblwgeom/ptarray.c

index 1d3ccf707324accf7202d430430a1a0fd900a0ac..e61c28fe990d5f02ee155b18b4591dadd260dbc7 100644 (file)
@@ -19,6 +19,7 @@
  **********************************************************************
  *
  * Copyright 2011-2014 Sandro Santilli <strk@kbt.io>
+ * Copyright 2017 Darafei Praliaskouski <me@komzpa.net>
  *
  **********************************************************************/
 
@@ -202,21 +203,18 @@ GEOS2LWGEOM(const GEOSGeometry *geom, char want3d)
        default:
                lwerror("GEOS2LWGEOM: unknown geometry type: %d", type);
                return NULL;
-
        }
-
 }
 
 
-
-GEOSCoordSeq ptarray_to_GEOSCoordSeq(const POINTARRAY *);
-
+GEOSCoordSeq ptarray_to_GEOSCoordSeq(const POINTARRAY *, int fix_ring);
 
 GEOSCoordSeq
-ptarray_to_GEOSCoordSeq(const POINTARRAY *pa)
+ptarray_to_GEOSCoordSeq(const POINTARRAY *pa, int fix_ring)
 {
        uint32_t dims = 2;
        uint32_t i;
+       int append_points = 0;
        const POINT3DZ *p3d;
        const POINT2D *p2d;
        GEOSCoordSeq sq;
@@ -224,7 +222,27 @@ ptarray_to_GEOSCoordSeq(const POINTARRAY *pa)
        if ( FLAGS_GET_Z(pa->flags) )
                dims = 3;
 
-       if ( ! (sq = GEOSCoordSeq_create(pa->npoints, dims)) )
+       if ( fix_ring )
+       {
+               if (pa->npoints < 1)
+               {
+                       lwerror("ptarray_to_GEOSCoordSeq called with fix_ring and 0 vertices in ring, cannot fix");
+                       return NULL;
+               }
+               else
+               {
+                       if ( pa->npoints < 4 )
+                       {
+                               append_points = 4 - pa->npoints;
+                       }
+                       if ( ! ptarray_is_closed_2d(pa) && append_points == 0 )
+                       {
+                               append_points = 1;
+                       }
+               }
+       }
+
+       if ( ! (sq = GEOSCoordSeq_create(pa->npoints + append_points, dims)) )
        {
                lwerror("Error creating GEOS Coordinate Sequence");
                return NULL;
@@ -248,43 +266,43 @@ ptarray_to_GEOSCoordSeq(const POINTARRAY *pa)
                GEOSCoordSeq_setY(sq, i, p2d->y);
 
                if ( dims == 3 )
+               {
                        GEOSCoordSeq_setZ(sq, i, p3d->z);
+               }
        }
-       return sq;
-}
 
-static GEOSGeometry *
-ptarray_to_GEOSLinearRing(const POINTARRAY *pa, int autofix)
-{
-       GEOSCoordSeq sq;
-       GEOSGeom g;
-       POINTARRAY *npa = 0;
-
-       if ( autofix )
+       if ( append_points )
        {
-               if (pa->npoints < 1)
+               if ( dims == 3 )
                {
-                       lwerror("ptarray_to_GEOSLinearRing called with autofix and 0 vertices in ring, cannot fix");
+                       p3d = getPoint3dz_cp(pa, 0);
+                       p2d = (const POINT2D *)p3d;
                }
-
-               /*
-               * Check ring for being closed and fix if not.
-               * Also create a copy of geometry to operate on.
-               */
-               if ( ! ptarray_is_closed_2d(pa) || pa->npoints < 4 )
+               else
                {
-                       pa = ptarray_addPoint(pa, getPoint_internal(pa, 0), FLAGS_NDIMS(pa->flags), pa->npoints);
-                       npa = pa;
+                       p2d = getPoint2d_cp(pa, 0);
                }
-               /* Check ring for having at least 4 vertices */
-               while ( pa->npoints < 4 )
+               for ( i = pa->npoints; i < pa->npoints + append_points; i++ )
                {
-                       ptarray_append_point(pa, getPoint_internal(pa, 0), LW_TRUE);
+                       GEOSCoordSeq_setX(sq, i, p2d->x);
+                       GEOSCoordSeq_setY(sq, i, p2d->y);
+
+                       if ( dims == 3 )
+                       {
+                               GEOSCoordSeq_setZ(sq, i, p3d->z);
+                       }
                }
        }
 
-       sq = ptarray_to_GEOSCoordSeq(pa);
-       if ( npa ) ptarray_free(npa);
+       return sq;
+}
+
+static inline GEOSGeometry *
+ptarray_to_GEOSLinearRing(const POINTARRAY *pa, int autofix)
+{
+       GEOSCoordSeq sq;
+       GEOSGeom g;
+       sq = ptarray_to_GEOSCoordSeq(pa, autofix);
        g = GEOSGeom_createLinearRing(sq);
        return g;
 }
@@ -373,7 +391,7 @@ LWGEOM2GEOS(const LWGEOM *lwgeom, int autofix)
                }
                else
                {
-                       sq = ptarray_to_GEOSCoordSeq(lwp->point);
+                       sq = ptarray_to_GEOSCoordSeq(lwp->point, 0);
                        g = GEOSGeom_createPoint(sq);
                }
                if ( ! g )
@@ -392,7 +410,7 @@ LWGEOM2GEOS(const LWGEOM *lwgeom, int autofix)
                                           FLAGS_NDIMS(lwl->points->flags),
                                           lwl->points->npoints);
                }
-               sq = ptarray_to_GEOSCoordSeq(lwl->points);
+               sq = ptarray_to_GEOSCoordSeq(lwl->points, 0);
                g = GEOSGeom_createLineString(sq);
                if ( ! g )
                {
@@ -2140,4 +2158,3 @@ LWGEOM* lwgeom_voronoi_diagram(const LWGEOM* g, const GBOX* env, double toleranc
        return lwgeom_result;
 #endif /* POSTGIS_GEOS_VERSION < 35 */
 }
-
index c7ffa8ae9069ed4da6da7f6583f5d8e1eed4fab1..639e7b5a3627bf5d51eec5d2e5453e0b4e720662 100644 (file)
@@ -1454,7 +1454,7 @@ ptarray_remove_repeated_points_in_place(POINTARRAY *pa, double tolerance, int mi
        int n_points = pa->npoints;
        int n_points_out = 0;
        int pt_size = ptarray_point_size(pa);
-       double dsq;
+       double dsq = FLT_MAX;
 
        /* No-op on short inputs */
        if ( n_points <= 2 ) return;
@@ -1470,7 +1470,7 @@ ptarray_remove_repeated_points_in_place(POINTARRAY *pa, double tolerance, int mi
                if (last)
                {
                        /* Don't drop points if we are running short of points */
-               if (n_points - i > min_points - n_points_out)
+                       if (n_points - i > min_points - n_points_out)
                        {
                                if (tolerance > 0.0)
                                {