[postgis-tickets] r14719 - #3479, error out if kmeans has k > ngeoms

Paul Ramsey pramsey at cleverelephant.ca
Sun Feb 28 15:46:41 PST 2016


Author: pramsey
Date: 2016-02-28 15:46:41 -0800 (Sun, 28 Feb 2016)
New Revision: 14719

Modified:
   trunk/liblwgeom/kmeans.c
   trunk/liblwgeom/lwkmeans.c
   trunk/postgis/lwgeom_window.c
Log:
#3479, error out if kmeans has k > ngeoms


Modified: trunk/liblwgeom/kmeans.c
===================================================================
--- trunk/liblwgeom/kmeans.c	2016-02-28 06:39:01 UTC (rev 14718)
+++ trunk/liblwgeom/kmeans.c	2016-02-28 23:46:41 UTC (rev 14719)
@@ -260,6 +260,7 @@
 	assert(config->centers);
 	assert(config->k);
 	assert(config->clusters);
+	assert(config->k < config->num_objs);
 
 	/* Zero out cluster numbers, just in case user forgets */
 	memset(config->clusters, 0, clusters_sz);

Modified: trunk/liblwgeom/lwkmeans.c
===================================================================
--- trunk/liblwgeom/lwkmeans.c	2016-02-28 06:39:01 UTC (rev 14718)
+++ trunk/liblwgeom/lwkmeans.c	2016-02-28 23:46:41 UTC (rev 14719)
@@ -90,6 +90,11 @@
 	assert(k>0);
 	assert(ngeoms>0);
 	assert(geoms);
+	
+	if (ngeoms<k)
+	{
+		lwerror("%s: number of geometries is less than the number of clusters requested", __func__);
+	}
 
 	/* We'll hold the temporary centroid objects here */
 	centroids = lwalloc(sizeof(LWGEOM*) * ngeoms);

Modified: trunk/postgis/lwgeom_window.c
===================================================================
--- trunk/postgis/lwgeom_window.c	2016-02-28 06:39:01 UTC (rev 14718)
+++ trunk/postgis/lwgeom_window.c	2016-02-28 23:46:41 UTC (rev 14719)
@@ -196,6 +196,12 @@
 			PG_RETURN_NULL();
 		}
 
+		/* Error out if N < K */
+		if (N<k)
+		{
+			lwpgerror("K (%d) must be smaller than the number of rows in the group (%d)", k, N);
+		}
+
 		/* Read all the geometries from the partition window into a list */
 		geoms = palloc(sizeof(LWGEOM*) * N);
 		for (i = 0; i < N; i++)



More information about the postgis-tickets mailing list