<div dir="ltr"><div dir="ltr">It looks like for polygon / box intersection area can be calculated in O(n), by just clamping the ordinates (<a href="https://github.com/postgis/postgis/commit/3233ccd19cdb6958b53164262cd812d3f6d70fb2">https://github.com/postgis/postgis/commit/3233ccd19cdb6958b53164262cd812d3f6d70fb2</a>). </div><div dir="ltr"><br><div>It also may be adapted for any convex polygons, with O(n*m) though. Convex check also seems to be O(n). Building dataset tends to be mostly convex (and mostly 4-corner), so this may be a specialized fast path.</div><div><br></div><div>Is there a chance we may want BOX() as new geometry primitive to save a is_convex / is_box check? :)</div><div><br></div><div>Any other thoughts?</div><div><br><div class="gmail_quote"><div dir="ltr">On Tue, Oct 23, 2018 at 8:04 PM Paul Ramsey <<a href="mailto:pramsey@cleverelephant.ca">pramsey@cleverelephant.ca</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="overflow-wrap: break-word;">The first GIS system I ever used had an “intersection area” function that was distinct from, and faster than, the full-on “intersection” function that output a new geometry. I wonder if there is some possibility there…<div><br></div><div>P<br><div><br><blockquote type="cite"><div>On Oct 23, 2018, at 6:01 PM, Darafei Komяpa Praliaskouski <<a href="mailto:me@komzpa.net" target="_blank">me@komzpa.net</a>> wrote:</div><br class="gmail-m_126253197510110631Apple-interchange-newline"><div><div dir="ltr">Hi,<div><br></div><div>I have two polygonal layers to intersect and need to measure area of intersections.</div><div><br></div><div>ST_Area(ST_Intersection(a.geom, b.geom)) works fine, but is rather slow. Typical polygon on first side has <10 edges, on second 200..1000 edges.</div><div><br></div><div>What are my options to make it faster? Any ideas, links, experiences to share are welcome.</div><div><br></div><div>For starters, this seems to work faster than vanilla ST_Intersection in my case:</div><div><pre style="font-family:"Courier New""><span style="color:rgb(0,0,128);font-weight:bold">create or replace function </span><span style="font-style:italic">clipped_st_intersection</span>(geom1 <span style="color:rgb(0,0,128);font-weight:bold">geometry</span>, geom2 <span style="color:rgb(0,0,128);font-weight:bold">geometry</span>)<br> <span style="color:rgb(0,0,128);font-weight:bold">returns geometry<br></span><span style="color:rgb(0,0,128);font-weight:bold">as </span><span style="color:rgb(0,128,0);font-weight:bold">$$</span><span style="color:rgb(0,0,128);font-weight:bold">select </span><span style="font-style:italic">ST_Intersection</span>(<br> <span style="color:rgb(0,0,128);font-weight:bold">case<br></span><span style="color:rgb(0,0,128);font-weight:bold"> when </span><span style="font-style:italic">ST_MemSize</span>(geom1) > <span style="color:rgb(0,0,255)">160 </span><span style="color:rgb(0,0,128);font-weight:bold">then </span><span style="font-style:italic">ST_ClipByBox2D</span>(geom1, geom2)<br> <span style="color:rgb(0,0,128);font-weight:bold">else </span>geom1 <span style="color:rgb(0,0,128);font-weight:bold">end</span>, <span style="color:rgb(0,0,128);font-weight:bold">case<br></span><span style="color:rgb(0,0,128);font-weight:bold"> when </span><span style="font-style:italic">ST_MemSize</span>(geom2) > <span style="color:rgb(0,0,255)">160 </span><span style="color:rgb(0,0,128);font-weight:bold">then </span><span style="font-style:italic">ST_ClipByBox2D</span>(geom2, geom1)<br> <span style="color:rgb(0,0,128);font-weight:bold">else </span>geom2 <span style="color:rgb(0,0,128);font-weight:bold">end</span>)<span style="color:rgb(0,128,0);font-weight:bold">$$<br></span><span style="color:rgb(0,0,128);font-weight:bold">language sql<br></span><span style="color:rgb(0,0,128);font-weight:bold">immutable<br></span><span style="color:rgb(0,0,128);font-weight:bold">strict<br></span><span style="color:rgb(0,0,128);font-weight:bold">parallel safe</span>;</pre></div></div>-- <br><div dir="ltr" class="gmail-m_126253197510110631gmail_signature"><div dir="ltr">Darafei Praliaskouski<br>Support me: <a href="http://patreon.com/komzpa" target="_blank">http://patreon.com/komzpa</a></div></div>
_______________________________________________<br>postgis-devel mailing list<br><a href="mailto:postgis-devel@lists.osgeo.org" target="_blank">postgis-devel@lists.osgeo.org</a><br><a href="https://lists.osgeo.org/mailman/listinfo/postgis-devel" target="_blank">https://lists.osgeo.org/mailman/listinfo/postgis-devel</a></div></blockquote></div><br></div></div>_______________________________________________<br>
postgis-devel mailing list<br>
<a href="mailto:postgis-devel@lists.osgeo.org" target="_blank">postgis-devel@lists.osgeo.org</a><br>
<a href="https://lists.osgeo.org/mailman/listinfo/postgis-devel" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/postgis-devel</a></blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><div>Darafei Praliaskouski</div><div>Support me: <a href="http://patreon.com/komzpa" target="_blank">http://patreon.com/komzpa</a></div></div></div></div></div></div></div>