<div dir="ltr">One reason RectangleIntersection is not enabled in the Intersection code path is that it has different semantics than the OverlayNG algorithm.  This might be undesirable or confusing in some use cases.<div><br></div><div>It is exposed by GEOSClipByRect.  And this is used by PostGIS (at least in some circumstances).  So have you tried using ST_ClipByBox2D?</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Nov 26, 2024 at 1:02 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">Hi,<br>
<br>
Long story short:<br>
Enabling USE_RECTANGLE_INTERSECTION flag speeds up postgis ST_Intersection by 60x for my use case<br>
<br>
Long story long:<br>
I recently wrote a strava like heatmap. It requires computing the intersections of my gpx tracks with tile layers. The PostGIS query used is:<br>
<br>
```<br>
const query = `SELECT ST_ASGeoJson(ST_Intersection(geom, 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 tracks. On a large tile, where 1300 geometries intersect, the query takes 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 query succeeds in about 40ms. (ie: 60x speedup!) (<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 operation and the rectangle intersection optimization:<br>
<br>
+ If the geometries don't intersect, the result is an empty geometry of type geometryA. With RectangleIntersection, it is an empty GEOMETRYCOLLECTION<br>
```<br>
% geosop -a 'LINESTRING(10 10, 20 20)' -b 'POLYGON((110 10, 120 10, 120 20, 110 20, 110 10))' clipRect<br>
GEOMETRYCOLLECTION EMPTY<br>
% geosop -a 'LINESTRING(10 10, 20 20)' -b 'POLYGON((110 10, 120 10, 120 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 RectangleIntersection to return the expected geometry.<br>
<br>
+ The resulting geometries gets sometimes rearranged differently. See for example unit test 4 of capi::GEOSIntersection. 2 linestrings are expected. They would not appear with rectangle intersection. By the way, I am pretty novice with geometry computation, and I don't really understand why the linestrings are expected in that case.<br>
<br>
Another example: where a linestring self-intersects, and the result will be split at the intersection point. For some reason, it does not seem to happen when the linestring is fully within the rectangle boundary (I'm also confused why that is the case).<br>
<br>
```<br>
% geosop -a 'LINESTRING(13 12, 50 50, 50 15, 10 15)' -b 'POLYGON((10 10, 20 10, 20 20, 10 20, 10 10))' intersection<br>
MULTILINESTRING ((13 12, 15.921052631578947 15), (15.921052631578947 15, 20 19.18918918918919), (20 15, 15.921052631578947 15), (15.921052631578947 15, 10 15))<br>
% geosop -a 'LINESTRING(13 12, 50 50, 50 15, 10 15)' -b 'POLYGON((10 10, 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, and the rectangle intersection operation? Or any other issue or reasons why 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>
</blockquote></div>