<div dir="ltr"><div>Hi Paul,</div><div>apologies for the confusion, I just can't type the right word sometimes. Only ST_Intersects is being used.</div><div>Here are again the numbers with the actual query (reminder, it's being run against the original table, hence the different table name compared to</div><div>the test I shared):</div><div><br></div><div>Baseline (intersection), provides the wrong answer (two polygons):</div><div><br></div><div>SELECT ogc_fid FROM kiinteisto_alue WHERE geom && ST_GeomFromText('POINT (25492818 6677399.98)', 3879) and ST_Intersects(geom, ST_GeomFromText('POINT (25492818 6677399.98)', 3879));</div><div>tps = 10227.810087 ((without initial connection time)<br></div><div><br></div><div>Distance, provides the right answer:</div><div><br></div><div>SELECT ogc_fid FROM kiinteisto_alue WHERE geom && ST_GeomFromText('POINT (25492818 6677399.98)', 3879) and ST_Distance(geom, ST_GeomFromText('POINT (25492818 6677399.98)', 3879)) = 0;</div><div>tps = 19989.627257 (without initial connection time)</div><div><br></div><div>Manual linearization with higher accuracy (provides the right answer):</div><div><br>SELECT ogc_fid FROM kiinteisto_alue WHERE geom && ST_GeomFromText('POINT (25492818 6677399.98)', 3879) and ST_Intersects(ST_CurveToLine(geom, 0.01, 1, 1), ST_GeomFromText('POINT (25492818 6677399.98)', 3879))<br></div><div>tps = 8984.594159 (without initial connection time)<br></div><div><br></div><div>In the full dataset, there are around 10k records having actual curved edges, out of 36k.</div><div>I was guessing the ideal would be to check if curves are there, and change on the fly the test.</div><div>Something like this:</div><div><br></div><div>SELECT ogc_fid FROM kiinteisto_alue WHERE geom && ST_GeomFromText('POINT (25492818 6677399.98)', 3879) and <br>case <br> when ST_HasArc(geom) then ST_Distance(geom, ST_GeomFromText('POINT (25492818 6677399.98)', 3879)) = 0<br> else ST_Intersects(geom, ST_GeomFromText('POINT (25492818 6677399.98)', 3879))<br>end;</div><div><br></div><div>And yet, a pgbench of the above only returns 14500 TPS, whilst the simple distance test for all,</div><div>gets up to around 20000.... looks like a ST_Distance test is faster even for geometries with straight segments.... isn't this odd?</div><div>In my mind, checking if two geometries interfere (in any way) should be less expensive than calculating their</div><div>exact distance.</div><div><br></div><div>By the way, thanks for the details, I love to hear about how things are implemented!</div><div><br></div><div><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><span><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap">Regards,</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap">Andrea Aime</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap"><br></span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap">==</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap"><br></span><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap">GeoServer Professional Services from the experts!</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap">Visit </span><a href="http://bit.ly/gs-services-us" target="_blank"><span style="font-family:Arial;color:rgb(17,85,204);vertical-align:baseline;white-space:pre-wrap">http://bit.ly/gs-services-us</span></a><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap"> for more information.</span><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap"><br></span><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap"><br></span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap">==</span><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap"><br></span><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap"><br></span><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap">Ing. Andrea Aime </span><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap"><br></span><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap">@geowolf</span><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap"><br></span><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap">Technical Lead</span><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap"><br><br></span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap">GeoSolutions Group</span><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap"><br></span><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap">phone: +39 0584 962313</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap">fax: +39 0584 1660272</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap">mob: +39 339 8844549</span></p><br><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><a href="https://www.geosolutionsgroup.com/" target="_blank"><span style="font-family:Arial;color:rgb(17,85,204);vertical-align:baseline;white-space:pre-wrap">https://www.geosolutionsgroup.com/</span></a></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><a href="http://twitter.com/geosolutions_it" target="_blank"><span style="font-family:Arial;color:rgb(17,85,204);vertical-align:baseline;white-space:pre-wrap">http://twitter.com/geosolutions_it</span></a></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap">-------------------------------------------------------</span></p><br><span style="font-family:Arial;color:rgb(0,0,0);background-color:transparent;vertical-align:baseline;white-space:pre-wrap">Con riferimento alla normativa sul trattamento dei dati personali (Reg. UE 2016/679 - Regolamento generale sulla protezione dei dati “GDPR”), si precisa che ogni circostanza inerente alla presente email (il suo contenuto, gli eventuali allegati, etc.) è un dato la cui conoscenza è riservata al/i solo/i destinatario/i indicati dallo scrivente. Se il messaggio Le è giunto per errore, è tenuta/o a cancellarlo, ogni altra operazione è illecita. Le sarei comunque grato se potesse darmene notizia.</span><span style="font-family:Arial;color:rgb(0,0,0);background-color:transparent;vertical-align:baseline;white-space:pre-wrap"><br></span><span style="font-family:Arial;color:rgb(0,0,0);background-color:transparent;vertical-align:baseline;white-space:pre-wrap"><br></span><span style="font-family:Arial;color:rgb(0,0,0);background-color:transparent;vertical-align:baseline;white-space:pre-wrap">This email is intended only for the person or entity to which it is addressed and may contain information that is privileged, confidential or otherwise protected from disclosure. We remind that - as provided by European Regulation 2016/679 “GDPR” - copying, dissemination or use of this e-mail or the information herein by anyone other than the intended recipient is prohibited. If you have received this email by mistake, please notify us immediately by telephone or e-mail</span></span><br></div></div></div><br></div><br><div class="gmail_quote gmail_quote_container"><div dir="ltr" class="gmail_attr">On Tue, Jan 7, 2025 at 7:36 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><div>I’m not 100% sure this is apples and apples?</div><div><div><br><blockquote type="cite"><div>On Jan 7, 2025, at 2:55 AM, Andrea Aime <<a href="mailto:andrea.aime@geosolutionsgroup.com" target="_blank">andrea.aime@geosolutionsgroup.com</a>> wrote:</div><div><div dir="ltr"><div> testing 3 scenarios:</div><div><ul><li>The current query using ST_Intersection as is (returns 2 polygons, wrong result but I'm treating it as the baseline)</li></ul></div></div></div></blockquote><div>ST_Intersection() is an expensive operation, constructing new geometry. ST_Intersects() is a cheaper operation, returning true/false.</div><blockquote type="cite"><div><div dir="ltr"><div><ul><li>Using && + ST_Distance</li></ul></div></div></div></blockquote><div>ST_Distance() is cheaper than ST_Intersection(), and similar to ST_Intersects() in how it calculates an answer.</div><blockquote type="cite"><div><div dir="ltr"><div><ul><li>Using && + linearized intersection, with a precision good enough to get the correct result (0.01 meters)</li></ul></div></div></div></blockquote><div>Still using Intersection() so on an expensive path.</div><div><br></div><div>Some things to note in general:</div><div><br></div><div>- ST_Intersection() will always delegate to GEOS and if handed curves will linearize them first. Delegating to GEOS does have some fixed overheads, a full copy of the geometry is made, and GEOS set up.</div><div>- ST_Intersects() will sometimes delegate to GEOS, one of those cases being handed a curve polygon. Which will again coast you linearization and a full copy.</div><div>- Finer linearization will naturally result in more segments and more processing cost.</div><div>- ST_Distance() with native curve support trades the expense of some extra trig doing edge/arc calculations for avoiding the trip to GEOS and processing a much smaller number of edges than the linearized path.</div><div><br></div><blockquote type="cite"><div><div dir="ltr"><div><div>I find the results surprsing:</div></div><div><br></div><div>> pgbench -U cite helsinki -f baseline.sql -c 32 -t 2000<br>tps = 10227.810087 ((without initial connection time)<br><br>> pgbench -U cite helsinki -f distance.sql -c 32 -t 2000<br>tps = 19989.627257 (without initial connection time)<br><br>> pgbench -U cite helsinki -f linearized.sql -c 32 -t 2000<br>tps = 8984.594159 (without initial connection time)<br><br>So... testing with the distance is twice as fast as the other options? Wow, have we been doing intersection tests wrong all this time? ROFL</div><div>Other possible ideas:</div><div><ul><li>There is something specific to having curves in the mix, and having to pay the cost of linearization makes distance competitive?</li><li>The specific dataset is playing an important role in the result</li></ul></div></div></div></blockquote><div>I am not, in general, surprised at your results, knowing the different code paths your tests could be running through.</div><div><br></div><div>P.</div><div><br></div><br><blockquote type="cite"><div><div dir="ltr"><div>Ah, since there seems to be a real issue, I've also opened a ticket in trac: <a href="https://trac.osgeo.org/postgis/ticket/5832#ticket" target="_blank">https://trac.osgeo.org/postgis/ticket/5832#ticket</a></div><div><br></div><div><div dir="ltr" class="gmail_signature"><div dir="ltr"><span><div style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap">Regards,</span></div><div style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap">Andrea Aime</span></div><div style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap"><br></span></div><div style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap">==</span></div><div style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap"><br></span><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap">GeoServer Professional Services from the experts!</span></div><div style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap">Visit </span><a href="http://bit.ly/gs-services-us" target="_blank"><span style="font-family:Arial;color:rgb(17,85,204);vertical-align:baseline;white-space:pre-wrap">http://bit.ly/gs-services-us</span></a><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap"> for more information.</span><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap"><br></span><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap"><br></span></div><div style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap">==</span><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap"><br></span><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap"><br></span><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap">Ing. Andrea Aime </span><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap"><br></span><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap">@geowolf</span><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap"><br></span><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap">Technical Lead</span><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap"><br><br></span></div><div style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap">GeoSolutions Group</span><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap"><br></span><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap">phone: +39 0584 962313</span></div><div style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap">fax: +39 0584 1660272</span></div><div style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap">mob: +39 339 8844549</span></div><br><div style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><a href="https://www.geosolutionsgroup.com/" target="_blank"><span style="font-family:Arial;color:rgb(17,85,204);vertical-align:baseline;white-space:pre-wrap">https://www.geosolutionsgroup.com/</span></a></div><div style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><a href="http://twitter.com/geosolutions_it" target="_blank"><span style="font-family:Arial;color:rgb(17,85,204);vertical-align:baseline;white-space:pre-wrap">http://twitter.com/geosolutions_it</span></a></div><div style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-family:Arial;color:rgb(34,34,34);vertical-align:baseline;white-space:pre-wrap">-------------------------------------------------------</span></div><br><span style="font-family:Arial;background-color:transparent;vertical-align:baseline;white-space:pre-wrap">Con riferimento alla normativa sul trattamento dei dati personali (Reg. UE 2016/679 - Regolamento generale sulla protezione dei dati “GDPR”), si precisa che ogni circostanza inerente alla presente email (il suo contenuto, gli eventuali allegati, etc.) è un dato la cui conoscenza è riservata al/i solo/i destinatario/i indicati dallo scrivente. Se il messaggio Le è giunto per errore, è tenuta/o a cancellarlo, ogni altra operazione è illecita. Le sarei comunque grato se potesse darmene notizia.</span><span style="font-family:Arial;background-color:transparent;vertical-align:baseline;white-space:pre-wrap"><br></span><span style="font-family:Arial;background-color:transparent;vertical-align:baseline;white-space:pre-wrap"><br></span><span style="font-family:Arial;background-color:transparent;vertical-align:baseline;white-space:pre-wrap">This email is intended only for the person or entity to which it is addressed and may contain information that is privileged, confidential or otherwise protected from disclosure. We remind that - as provided by European Regulation 2016/679 “GDPR” - copying, dissemination or use of this e-mail or the information herein by anyone other than the intended recipient is prohibited. If you have received this email by mistake, please notify us immediately by telephone or e-mail</span></span><br></div></div></div><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Dec 22, 2024 at 4:44 PM Andrea Aime <<a href="mailto:andrea.aime@geosolutionsgroup.com" target="_blank">andrea.aime@geosolutionsgroup.com</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 dir="ltr"><div dir="ltr"><div>Hi Paul,</div><div>thanks a lot for following up. Comments inline below.</div><div><br></div></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div>These are literally CurvePolygon type? </div></blockquote><div><br></div><div>The column type is just "geometry(Geometry,3879)", while ST_GeometryType returns "multisurface" for both.</div><div>When doing a ST_AsText instead, you'll get something like:</div><div><br></div><div>MULTISURFACE(CURVEPOLYGON(COMPOUNDCURVE((...</div><div><br></div><div>for both.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div>It’s probably getting caught in our lack of full curve support.<div>I would be interested in the ST_Distance between the point and those two CurvePolygons. (Because, for distance, we have a postgis-native implementation that supports curves). </div></div></blockquote><div><br></div><div>=# SELECT ogc_fid, ST_Distance(ST_GeomFromText('POINT (25492818 6677399.98)', 3879), geom) FROM testdata;<br></div><div><br></div><div> ogc_fid | st_distance <br>---------+---------------------<br> 1258 | 0.01234572446598792<br> 12875 | 0<br>(2 rows)<br></div><div><br></div><div>Indeed, the correct answer, 12875 contains the point, while the other polygon is close to it.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div>Whereas for intersection, the calculation is delegated to GEOS *after linearizing the inputs*. In that linearization, could sit the logically problem you’re seeing.</div></div></blockquote><div><br></div><div>Let's check with different tolerances... yes, changing the tolerance changes the result:</div><div><br></div><div>=# SELECT ogc_fid FROM testdata WHERE ST_Intersects(ST_CurveToLine(geom, 0.01, 1, 1), ST_GeomFromText('POINT (25492818 6677399.98)', 3879));<br> ogc_fid <br>---------<br> 12875<br>(1 row)<br><br># SELECT ogc_fid FROM testdata WHERE ST_Intersects(ST_CurveToLine(geom, 0.02, 1, 1), ST_GeomFromText('POINT (25492818 6677399.98)', 3879));<br> ogc_fid <br>---------<br> 1258<br>(1 row)</div><div><br></div><div>In the immediate future, I guess I could have the GeoTools PostGIS store use either approach, when knowing curves are involved... </div><div>First using && to perform a first rough filter, and then either use either</div><div>* ST_Distance equals to 0</div><div>* An explicit linearization with a target tolerance (this is an urban application, so I'm guessing they will need centimeter, if not millimeter, precision)</div><div>.</div><div>Is there a clear winner here in terms of performance, or performance of distance vs linearized intersection is more contextual to the geometries involved?</div><div><br></div><div>Cheers</div><div>Andrea</div></div></div>
</blockquote></div>
</div></blockquote></div><br></div></div></blockquote></div>