<html><head><style type="text/css"><!-- DIV {margin:0px;} --></style></head><body><div style="font-family:arial, helvetica, sans-serif;font-size:10pt"><div style="font-family: arial,helvetica,sans-serif; font-size: 10pt;">Hmm, i just tried to recreate the error message i got and using this sql `select * from intersection_all( (select collect(the_geom) from polygons) );`&nbsp; It returns 3 blank records...&nbsp; That means that the function is working correctly to an extent, and i just need to have it actually return information...<br><br>It appears the arrays don't have any information in them when i go to return the information...<br><br>Argh... I wish i had some SQL fu ...<br><br>Alright after some toying around i was able to make it return some rows...&nbsp; Below is the new function.&nbsp; Now it only returns the 3 input rows (following the function).&nbsp; I reworked the function to return the rows as it creates them which leads me to believe my SQL is broken in
 assembling the storage arrays num_intersections &amp; geometries;&nbsp; From what i can tell it doesn't store anything to the arrays.&nbsp; Anyone have any ideas?<br><br>CREATE OR REPLACE FUNCTION intersection_all(in_polys geometry, OUT id int, OUT intersection_count int, OUT the_geom geometry)<br>&nbsp; RETURNS SETOF record AS<br>$BODY$<br>DECLARE<br>&nbsp;&nbsp;&nbsp; total_count int := 0;<br>&nbsp;&nbsp;&nbsp; current_level_count int := 0;<br>&nbsp;&nbsp;&nbsp; first_id_of_previous_level int := 0;<br>&nbsp;&nbsp;&nbsp; last_id_of_previous_level int:= 0;<br>&nbsp;&nbsp;&nbsp; num_intersections int ARRAY[0];<br>&nbsp;&nbsp;&nbsp; geometries geometry ARRAY[0];<br>BEGIN<br>&nbsp;&nbsp;&nbsp; -- handle level one by adding all input polygons<br>&nbsp;&nbsp;&nbsp; FOR i IN 1..NumGeometries(in_polys) LOOP<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; the_geom := GeometryN(in_polys,i);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; intersection_count := 1;<br>&nbsp;&nbsp;&nbsp;
 &nbsp;&nbsp;&nbsp; geometries := array_append(geometries, the_geom);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; num_intersections := array_append(num_intersections, intersection_count);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; last_id_of_previous_level := last_id_of_previous_level+1;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; total_count := total_count +1;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; id := total_count;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; RAISE NOTICE 'Inserting polygon number: % at level: %',total_count,1;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; RETURN NEXT;<br>&nbsp;&nbsp;&nbsp; END LOOP;<br>&nbsp;<br>&nbsp;&nbsp;&nbsp; -- handle all higher levels<br>&nbsp;&nbsp;&nbsp; FOR level IN 2..100 LOOP<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; RAISE NOTICE 'level: %',level;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; FOR i IN first_id_of_previous_level..last_id_of_previous_level LOOP<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; RAISE NOTICE 'i:
 %',i;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; FOR j IN (i+1) .. last_id_of_previous_level LOOP<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; RAISE NOTICE 'j: %&nbsp;&nbsp; last_id_of_previous_level: %',j,last_id_of_previous_level;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; IF Intersects(geometries[i],geometries[j]) THEN<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; the_geom := Intersection(geometries[i],geometries[j]);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; intersection_count := level;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; current_level_count := current_level_count+1;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; geometries := array_append(geometries,
 temp_geom);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; num_intersections := array_append(num_intersections, level);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; total_count := total_count +1;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; id := total_count;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; RAISE NOTICE 'Inserting polygon number %',total_count;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; RETURN NEXT;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; END IF;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; END LOOP;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; END LOOP;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; IF current_level_count &gt; 0 THEN<br>&nbsp;&nbsp;&nbsp;
 &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; first_id_of_previous_level := last_id_of_previous_level+1;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; last_id_of_previous_level := first_id_of_previous_level+current_level_count;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; current_level_count := 0;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ELSE <br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; EXIT;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; END IF;<br>&nbsp;&nbsp;&nbsp; END LOOP;<br>&nbsp;<br>&nbsp;&nbsp;&nbsp; RETURN;<br>END;<br>$BODY$<br>&nbsp; LANGUAGE 'plpgsql' IMMUTABLE;<br><br>Input Polygons (srid 4326):<br>POLYGON((-0.998092458778228 1.17422642209203,-1.14314395797783 0.490412211579614,-0.821958495464423 -0.383350390741811,0.255566927161207 -0.138144284952004,0.33845349813241 0.766700781483622,-0.35917514087521 1.26402020731084,-0.35917514087521 1.26402020731084,-0.587113211046016 1.26056660018704,-0.587113211046016
 1.26056660018704,-0.998092458778228 1.17422642209203))<br>POLYGON((0.290102998399208 1.20876249333003,-0.00690721424760012 0.849587352454824,-0.172680356190005 0.407525640608412,0.386803997865611 5.89805981832114e-17,1.15695838647303 0.34881431950381,1.34690677828204 1.02572131576863,0.946288351921227 1.33999956403444,0.528401889941415 1.42633974212944,0.324639069637209 1.34690677828204,0.290102998399208 1.20876249333003))<br>POLYGON((-0.34536071238001 -0.72871110312182,-0.483504997332014 -0.179587570437605,-0.203762820304206 0.0794329638474023,0.321185462513409 0.252113320037407,0.877216209445225 0.0863401780950025,1.04989656563523 -0.35572153375141,0.745979138740821 -0.967009994664027,-0.120876249333003 -0.877216209445225,-0.34536071238001 -0.72871110312182))<br><br><br><div style="font-family: times new roman,new york,times,serif; font-size: 12pt;">----- Original Message ----<br>From: Mark Cave-Ayland &lt;mark.cave-ayland@ilande.co.uk&gt;<br>To: PostGIS Users Discussion
 &lt;postgis-users@postgis.refractions.net&gt;<br>Sent: Tuesday, October 10, 2006 6:01:18 AM<br>Subject: Re: [postgis-users] Chained Intersections?<br><br><div>On Mon, 2006-10-09 at 22:23 -0700, Curtis W. Ruck wrote:<br><br>&gt; I wrote a pl/pgsql function that i thought would do this, far below<br>&gt; labeled Intersection_All.&nbsp;&nbsp;The problem with this is that it needs to<br>&gt; be called from a FROM clause because it returns a SETOF records, but<br>&gt; within a FROM clause i can't do an aggregate method collect(the_geom).<br>&gt; (I also tried a subquery in the FROM clause).<br>&gt; <br>&gt; Does anyone have any suggestions on how to warp Postgres/PostGIS into<br>&gt; doing this?<br>&gt; <br>&gt; Curtis W. Ruck<br>&gt; &lt;unnamed large entity&gt;<br><br><br>Hi Curtis,<br><br>Can you provide examples of the queries you have tried (along with any<br>error messages that you get). Subqueries and aggregates in a FROM clause<br>work fine, for
 example:<br><br>&nbsp;&nbsp;&nbsp;&nbsp;select * from (select count(*) FROM pg_class) AS foo;<br><br><br>Kind regards,<br><br>Mark.<br><br><br>_______________________________________________<br>postgis-users mailing list<br>postgis-users@postgis.refractions.net<br><a target="_blank" href="http://postgis.refractions.net/mailman/listinfo/postgis-users">http://postgis.refractions.net/mailman/listinfo/postgis-users</a><br></div></div><br></div></div></body></html>