]> granicus.if.org Git - postgis/commitdiff
Be a little more careful w/ input sets full of nulls and empties
authorPaul Ramsey <pramsey@cleverelephant.ca>
Thu, 3 Mar 2016 21:16:36 +0000 (21:16 +0000)
committerPaul Ramsey <pramsey@cleverelephant.ca>
Thu, 3 Mar 2016 21:16:36 +0000 (21:16 +0000)
git-svn-id: http://svn.osgeo.org/postgis/trunk@14742 b70326c6-7e19-0410-871a-916f4a2858ee

liblwgeom/lwkmeans.c

index 70b7005bbe80967f57cc99a0a4f7c000e01d03b4..83b39ab11fd64c898768c9c9d8642c3628213fe3 100644 (file)
@@ -19,16 +19,18 @@ static double lwkmeans_pt_distance(const Pointer a, const Pointer b)
 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 = 1; i < num_objs; i++)
+       for (i = 0; i < num_objs; i++)
        {
+               /* Skip nulls/empties */
+               if (!objs[i])
+                       continue;
+
                d = lwkmeans_pt_distance(objs[i], a);
                if (d < d_closest)
                {
@@ -189,6 +191,10 @@ lwgeom_cluster_2d_kmeans(const LWGEOM **geoms, int ngeoms, int k)
 
                /* 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;