<div>Hello Steve,</div><div><br></div><div>Thanks, I will check that solution as well. </div><div><br></div><div>I can add one more property of my use case: consider that not only the points are moving without predictable direction, but that part of the processing (the clustering) is about knowing if they can detect each other as if they had a radar or some other similar instrument. So using ellipses or rectangles would have a very big effect on my calculations and is unacceptable in my scenario.<br>
</div><div><br></div><div>Sébastien</div><div><br></div><div class="gmail_quote">On Wed, Dec 8, 2010 at 21:41, Steve <span dir="ltr"><<a href="mailto:steve@subwest.com">steve@subwest.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im">On 12/8/2010 1:35 AM, Sébastien Lorion wrote:<br>
> To give a specific number, assume there are about 1 *billion* points,<br>
> not a million. The total number of clusters should over around 10<br>
> millions with about 25% having a high concentration of points in them.<br>
> It is not a query that will be run repeatedly (once every half hour),<br>
> and the hardware can be scaled up without problem (up to a point of<br>
> course ..).<br>
><br>
> Sébastien<br>
<br>
</div>You can roll your own kmeans directly in the database (this is probably<br>
what you would have done with R), but with that many clusters, it might<br>
take many cycles to converge.<br>
<br>
Here is some pseudo-code that will do kmeans, but you would have to<br>
implement the function/query to return the closest cluster.<br>
<br>
<br>
> create table tempdata (<br>
> Â position_id integer,<br>
> Â pos geometry,<br>
> Â cluster int<br>
> );<br>
><br>
> create table centriods (<br>
> Â cluster_id int,<br>
> Â pos geometry,<br>
> Â x double precision,<br>
> Â y double precision<br>
> );<br>
><br>
> CREATE OR REPLACE FUNCTION kmeans()<br>
> Â RETURNS void AS<br>
> $BODY$<br>
> DECLARE<br>
> Â row_changed integer;<br>
> Â cycles integer;<br>
> BEGIN<br>
> Â delete from centriods;<br>
> Â //put in x random points...<br>
> Â for i in 1..x LOOP<br>
> Â insert into centriods values (i, POINT);<br>
><br>
> Â row_changed := 1;<br>
> Â cycles := 0;<br>
> Â while (row_changed > 0) LOOP<br>
> Â Â cycles := cycles + 1;<br>
> Â Â --Map ids to cluster<br>
> Â Â update tempdata set cluster = closestid(cost); Â --Need a function<br>
> to find closest point<br>
><br>
> Â Â --build new clusters<br>
> Â Â update centriods set pos = geomfromtext('POINT ('||avgy||' '||avgx')')<br>
> Â Â FROM (select avg(x) as avgx, avg(y) as avgy,cluster from tempdata<br>
> group by cluster) as foo<br>
> Â Â where id = foo.cluster and (x <> avgx and y <> avgy);<br>
><br>
> Â Â GET DIAGNOSTICS row_changed = ROW_COUNT;<br>
> Â Â RAISE NOTICE 'Changed %',row_changed;<br>
> Â END LOOP;<br>
> RAISE NOTICE 'Cycles %',cycles;<br>
> END; $BODY$<br>
> Â LANGUAGE 'plpgsql' VOLATILE<br>
> Â COST 100;<br>
<br>
But... if you are really handling that much data (like vehicle traffic),<br>
you probably don't want to use circles anyway. Â Every new posit (unless<br>
it is in the exact center) will cause the cluster to move & need to be<br>
reevaluated.<br>
<br>
Ideally, you might want to consider using ellipses. Not necessarily the<br>
postgis definition of an ellipse, but a polygon that can wrap close<br>
positions with buffered rounding. Â It helps if you have some direction<br>
with positions. Â Under this design, each ellipse should expand to<br>
natural boundaries. Â Any further positions, might expand the polygon, or<br>
just fall within. Â If too many fall within the same ellipse, you can<br>
re-evaluate and have smaller ellipse in the ellipse for heavier<br>
densities. Â But, since they aren't moving, we aren't re-clustering every<br>
half an hour.<br>
Just another idea to think about.<br>
<div><div class="h5"><br>
<br>
<br>
<br>
<br>
<br>
_______________________________________________<br>
postgis-users mailing list<br>
<a href="mailto:postgis-users@postgis.refractions.net">postgis-users@postgis.refractions.net</a><br>
<a href="http://postgis.refractions.net/mailman/listinfo/postgis-users" target="_blank">http://postgis.refractions.net/mailman/listinfo/postgis-users</a><br>
</div></div></blockquote></div><br>