<div dir="ltr"><div><div><div><div>so this kinda works, but:<br></div>* it now returns NULL as a geometry where it should return no record at all<br></div>* it is about a factor 20 (!) slower than st_intersection<br><br></div>
Besides that it can only be used for multipolygons.<br></div><div>And i would need a better name for it :)<br></div>Here's the code, i would really appreciate any comments that could help improve it:<br><br>create or replace function intersection_x (ageom geometry, bgeom geometry) returns geometry as $$<br>
declare<br>t_result geometry; --the result of st_intersection<br>t_out geometry; --the output<br>t_type text; --the geometrytype of the result<br>t_i integer; --a counter<br>begin<br>select st_intersection(ageom, bgeom) into t_result;<br>
select st_geometrytype(t_result) into t_type;<br>if t_type = 'ST_Polygon' then<br>    --cast to multi<br>    select st_multi(t_result) into t_out;<br>elsif t_type = 'ST_GeometryCollection' then<br>    --extract polys<br>
    t_out:=st_geomfromtext('GEOMETRYCOLLECTION EMPTY'); --so that st_merge will work<br>    for t_i in 1..st_numgeometries(t_result) loop<br>        select st_geometrytype(st_geometryn(t_result, t_i)) into t_type;<br>
        if t_type = 'ST_Polygon' then<br>            select st_union(st_multi(st_geometryn(t_result, t_i)), t_out) into t_out;<br>        elsif t_type = 'ST_MultiPolygon' then<br>            select st_union(t_result,t_out) into t_out;<br>
        else<br>            continue; --next geometry of the collection<br>        end if;<br>    end loop;<br>    if t_out=st_geomfromtext('GEOMETRYCOLLECTION EMPTY') then<br>        return null; --would like to return no record<br>
    end if;<br>elsif t_type = 'ST_MultiPolygon' then<br>    t_out:=t_result;<br>else<br>    return null;<br>    --would like to return no record at all.<br>end if;<br><br>return t_out;<br>end<br>$$ language plpgsql stable strict;<br>
</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Feb 27, 2014 at 5:01 PM, Willy-Bas Loos <span dir="ltr"><<a href="mailto:willybas@gmail.com" target="_blank">willybas@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 dir="ltr"><div><div><div><div><div><div><div>Hi,<br><br></div>When overlaying 2 tables that have polygons with st_intersection, i sometimes get linestrings or points back, or a geometrycollection with a combination of types.<br>

</div>What i actually want, is that i only get the overlapping parts (that is, overlapping interiors in DE9IM speak) in the geometrytype of the combined geometries.<br></div>Mostly multipolygon will do fine, so that would make that function a lot simpler.<br>

<br></div>Now i could make a function that does that, filtering results by their geometrytype.<br></div>I just wanted to check if something similar already exists?<br><br></div>Cheers,<br><br></div>WBL<span class="HOEnZb"><font color="#888888"><br clear="all">
<div>
<div><div><div><div><div><div><div><div><div><br>-- <br>"Quality comes from focus and clarity of purpose" -- Mark Shuttleworth<br>
</div></div></div></div></div></div></div></div></div></div></font></span></div>
</blockquote></div><br><br clear="all"><br>-- <br>"Quality comes from focus and clarity of purpose" -- Mark Shuttleworth<br>
</div>