[postgis-tickets] r14742 - Be a little more careful w/ input sets full of nulls and empties

Paul Ramsey pramsey at cleverelephant.ca
Thu Mar 3 13:16:36 PST 2016


Author: pramsey
Date: 2016-03-03 13:16:36 -0800 (Thu, 03 Mar 2016)
New Revision: 14742

Modified:
   trunk/liblwgeom/lwkmeans.c
Log:
Be a little more careful w/ input sets full of nulls and empties


Modified: trunk/liblwgeom/lwkmeans.c
===================================================================
--- trunk/liblwgeom/lwkmeans.c	2016-03-03 11:34:40 UTC (rev 14741)
+++ trunk/liblwgeom/lwkmeans.c	2016-03-03 21:16:36 UTC (rev 14742)
@@ -19,16 +19,18 @@
 static int lwkmeans_pt_closest(const Pointer * objs, size_t num_objs, const Pointer a)
 {
 	int i;
-	double d, d_closest;
-	double closest;
+	double d;
+	double d_closest = FLT_MAX;
+	int closest = -1;
 
-	assert(num_objs>0);
+	assert(num_objs > 0);
 
-	d_closest = lwkmeans_pt_distance(objs[0], a);
-	closest = 0;
+	for (i = 0; i < num_objs; i++)
+	{
+		/* Skip nulls/empties */
+		if (!objs[i])
+			continue;
 
-	for (i = 1; i < num_objs; i++)
-	{
 		d = lwkmeans_pt_distance(objs[i], a);
 		if (d < d_closest)
 		{
@@ -189,6 +191,10 @@
 
 		/* Find the data point closest to the calculated point */
 		closest = lwkmeans_pt_closest(config.objs, config.num_objs, &p);
+		
+		/* If something is terrible wrong w/ data, cannot find a closest */
+		if (closest < 0)
+			lwerror("unable to calculate cluster seed points, too many NULLs or empties?");
 
 		/* Ensure we aren't already using that point as a seed */
 		j = 0;



More information about the postgis-tickets mailing list