[postgis-tickets] r15976 - Fix GCC warnings

Sandro Santilli strk at kbt.io
Thu Oct 12 14:21:43 PDT 2017


Author: strk
Date: 2017-10-12 14:21:42 -0700 (Thu, 12 Oct 2017)
New Revision: 15976

Modified:
   trunk/liblwgeom/lwgeom_geos.c
   trunk/liblwgeom/ptarray.c
Log:
Fix GCC warnings

Obey const pointer in autofix logic.
Move ring closing logic to ptarray_to_GEOSCoordSeq.
Fix memory access.

Patch by Darafei Praliaskouski <me at komzpa.net>

Fixes #3900

Modified: trunk/liblwgeom/lwgeom_geos.c
===================================================================
--- trunk/liblwgeom/lwgeom_geos.c	2017-10-12 16:30:17 UTC (rev 15975)
+++ trunk/liblwgeom/lwgeom_geos.c	2017-10-12 21:21:42 UTC (rev 15976)
@@ -19,6 +19,7 @@
  **********************************************************************
  *
  * Copyright 2011-2014 Sandro Santilli <strk at kbt.io>
+ * Copyright 2017 Darafei Praliaskouski <me at komzpa.net>
  *
  **********************************************************************/
 
@@ -202,21 +203,18 @@
 	default:
 		lwerror("GEOS2LWGEOM: unknown geometry type: %d", type);
 		return NULL;
-
 	}
-
 }
 
 
+GEOSCoordSeq ptarray_to_GEOSCoordSeq(const POINTARRAY *, int fix_ring);
 
-GEOSCoordSeq ptarray_to_GEOSCoordSeq(const POINTARRAY *);
-
-
 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,8 +222,28 @@
 	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 @@
 		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 @@
 		}
 		else
 		{
-			sq = ptarray_to_GEOSCoordSeq(lwp->point);
+			sq = ptarray_to_GEOSCoordSeq(lwp->point, 0);
 			g = GEOSGeom_createPoint(sq);
 		}
 		if ( ! g )
@@ -392,7 +410,7 @@
 		                           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 @@
 	return lwgeom_result;
 #endif /* POSTGIS_GEOS_VERSION < 35 */
 }
-

Modified: trunk/liblwgeom/ptarray.c
===================================================================
--- trunk/liblwgeom/ptarray.c	2017-10-12 16:30:17 UTC (rev 15975)
+++ trunk/liblwgeom/ptarray.c	2017-10-12 21:21:42 UTC (rev 15976)
@@ -1454,7 +1454,7 @@
 	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 @@
 		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)
 				{



More information about the postgis-tickets mailing list