<div dir="ltr">Excellent. <div><br></div><div>It seems there might be some bugs lurking in GEOSClipByRect. If you come across any failure cases or things that don't work as expected, please file an issue in GEOS or PostGIS for them. It would be nice to get rectangle clipping working error-free and as fast as possible.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Nov 27, 2024 at 9:32 AM arno <<a href="mailto:arno@renevier.net">arno@renevier.net</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">Thanks a lot. I was not aware of ST_ClipByBox2D, but it seems to be what I need.<br>
<br>
On Tue, Nov 26, 2024 at 07:56:26AM -0800, Martin Davis wrote:<br>
> One reason RectangleIntersection is not enabled in the Intersection code<br>
> path is that it has different semantics than the OverlayNG algorithm. This<br>
> might be undesirable or confusing in some use cases.<br>
> <br>
> It is exposed by GEOSClipByRect. And this is used by PostGIS (at least in<br>
> some circumstances). So have you tried using ST_ClipByBox2D?<br>
> <br>
> On Tue, Nov 26, 2024 at 1:02 AM arno <<a href="mailto:arno@renevier.net" target="_blank">arno@renevier.net</a>> wrote:<br>
> <br>
> > Hi,<br>
> ><br>
> > Long story short:<br>
> > Enabling USE_RECTANGLE_INTERSECTION flag speeds up postgis ST_Intersection<br>
> > by 60x for my use case<br>
> ><br>
> > Long story long:<br>
> > I recently wrote a strava like heatmap. It requires computing the<br>
> > intersections of my gpx tracks with tile layers. The PostGIS query used is:<br>
> ><br>
> > ```<br>
> > const query = `SELECT ST_ASGeoJson(ST_Intersection(geom,<br>
> > ST_MakeEnvelope($1, $2, $3, $4, 4326))) AS geom from ${this.#tableName}`<br>
> > ```<br>
> ><br>
> > It is the performance bottleneck on my application. I have about 2000 gpx<br>
> > tracks. On a large tile, where 1300 geometries intersect, the query takes<br>
> > about 2500ms to run. I tried various query optimizations without sucess.<br>
> ><br>
> > But if I rebuild libgeos with USE_RECTANGLE_INTERSECTION flag set, the<br>
> > query succeeds in about 40ms. (ie: 60x speedup!) (<br>
> > <a href="https://github.com/libgeos/geos/blob/main/src/geom/Geometry.cpp#L76" rel="noreferrer" target="_blank">https://github.com/libgeos/geos/blob/main/src/geom/Geometry.cpp#L76</a>)<br>
> ><br>
> > Is there a way this optimization could be enabled in libgeos?<br>
> ><br>
> > I understand there are differences between the current intersection<br>
> > operation and the rectangle intersection optimization:<br>
> ><br>
> > + If the geometries don't intersect, the result is an empty geometry of<br>
> > type geometryA. With RectangleIntersection, it is an empty<br>
> > GEOMETRYCOLLECTION<br>
> > ```<br>
> > % geosop -a 'LINESTRING(10 10, 20 20)' -b 'POLYGON((110 10, 120 10, 120<br>
> > 20, 110 20, 110 10))' clipRect<br>
> > GEOMETRYCOLLECTION EMPTY<br>
> > % geosop -a 'LINESTRING(10 10, 20 20)' -b 'POLYGON((110 10, 120 10, 120<br>
> > 20, 110 20, 110 10))' intersection<br>
> > LINESTRING EMPTY<br>
> > ```<br>
> ><br>
> > But hopefully, it shouldn't be too hard to add a special mode to<br>
> > RectangleIntersection to return the expected geometry.<br>
> ><br>
> > + The resulting geometries gets sometimes rearranged differently. See for<br>
> > example unit test 4 of capi::GEOSIntersection. 2 linestrings are expected.<br>
> > They would not appear with rectangle intersection. By the way, I am pretty<br>
> > novice with geometry computation, and I don't really understand why the<br>
> > linestrings are expected in that case.<br>
> ><br>
> > Another example: where a linestring self-intersects, and the result will<br>
> > be split at the intersection point. For some reason, it does not seem to<br>
> > happen when the linestring is fully within the rectangle boundary (I'm also<br>
> > confused why that is the case).<br>
> ><br>
> > ```<br>
> > % geosop -a 'LINESTRING(13 12, 50 50, 50 15, 10 15)' -b 'POLYGON((10 10,<br>
> > 20 10, 20 20, 10 20, 10 10))' intersection<br>
> > MULTILINESTRING ((13 12, 15.921052631578947 15), (15.921052631578947 15,<br>
> > 20 19.18918918918919), (20 15, 15.921052631578947 15), (15.921052631578947<br>
> > 15, 10 15))<br>
> > % geosop -a 'LINESTRING(13 12, 50 50, 50 15, 10 15)' -b 'POLYGON((10 10,<br>
> > 20 10, 20 20, 10 20, 10 10))' clipRect<br>
> > MULTILINESTRING ((13 12, 20 19.189189189189186), (20 15, 10 15))<br>
> > ```<br>
> ><br>
> > Are there other differences between the current intersection operation,<br>
> > and the rectangle intersection operation? Or any other issue or reasons why<br>
> > that optimization is not enabled?<br>
> ><br>
> > Thank you<br>
> ><br>
> > Arno<br>
> ><br>
> > PS: I attach the patch I used to enable Rectangle Intersection<br>
> ><br>
</blockquote></div>