<br><br><div class="gmail_quote">On Thu, Jan 26, 2012 at 9:33 PM, Nicolas Ribot <span dir="ltr"><<a href="mailto:nicolas.ribot@gmail.com">nicolas.ribot@gmail.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="HOEnZb"><div class="h5">>><br>
>> From: Nicolas Ribot <<a href="mailto:nicolas.ribot@gmail.com">nicolas.ribot@gmail.com</a>><br>
>><br>
>> Hi<br>
>><br>
>> WITH RECURSIVE needs an UNION ALL to link the "non recursive" term<br>
>> with the "recursive" one.<br>
>><br>
><br>
> Wow, that at least runs... though I think the recursion might be infinite.<br>
> Thanks for the tip Nicolas.<br>
><br>
<br>
</div></div>Hi,<br>
<br>
Concerning the infinite iteration, one trick could be to use a boolean<br>
value to test if some condition is reached (for instance, no more<br>
polygon is found). Using an array to accumulate some values and test<br>
the current value against the array may be very efficient to control<br>
the iteration. Here is an extract of code that uses array to store<br>
already treated values and compare this list with the current id: (the<br>
purpose of this query was to find buildings by proximity search from a<br>
given building)<br>
<br>
with recursive mon_select as (<br>
       select -1 as id, ref, array[-1] as ids, 1 as depth, geometry from<br>
table_ori where ref = 1<br>
<br>
       UNION ALL<br>
<br>
       select distinct on(s.gid) s.gid as sel_gid, m.ref, m.ids ||<br>
s.gid, m.depth+1, s.geometry<br>
       from table_selection s, mon_select m<br>
       where st_touches(m.geometry, s.geometry)<br>
       and not (s.gid = any(ids))<br>
) select distinct on (id) id, ref, ids, depth, geometry from mon_select;<br>
<br>
The key parts are:<br>
    • The array[-1] in the non-recursive term, to initiate the array<br>
    • the "m.ids || s.gid" array concatenation in the select, to fill<br>
up the array of ids<br>
    • the "not (s.gid = any(ids))" in the where clause, returning true<br>
if any value in the arrays (ids) meets the "s.gid = ..." condition.<br>
<br></blockquote><div><br></div><div>Thank you very much for your help with this, I'll give it a try.</div><div><br></div><div>Leslie</div><div><br></div></div>