geos icon indicating copy to clipboard operation
geos copied to clipboard

Bug with oriented_envelope of MultiLineString

Open Nicolas-Weaver-Optimiz opened this issue 5 months ago • 6 comments

Hello,

I wrote an issue for the python library shapely (https://github.com/shapely/shapely/issues/2315). I was told to foward it the the geos library as it contains the source algorithms of shapely.

My problem is has follow: the oriented envelope of my MultiLineString does not return the true value of the oriented envelope as we can see on the following image. The python code is available is in the open shapely issue.

Image

Nicolas-Weaver-Optimiz avatar Jul 22 '25 09:07 Nicolas-Weaver-Optimiz

Local reproduction

./bin/geosop \
-a "MULTILINESTRING((1418991903.483397 4185938508.444688, 1418995834.961524 4185937283.90729),(1418998483.493762 4185936458.970139, 1419000750.412936 4185935752.893123))" \
minAreaRectangle
Image

pramsey avatar Jul 22 '25 22:07 pramsey

Neither of these cases (removing the fraction, removing the most significant portion) shows the problem, it's a precision issue...

./bin/geosop \
-a "MULTILINESTRING((1418991903 4185938508, 1418995834 4185937283),(1418998483 4185936458, 1419000750 4185935752))" \
minAreaRectangle


./bin/geosop \
-a "MULTILINESTRING((8991903.483397 5938508.444688, 8995834.961524 5937283.90729),(8998483.493762 5936458.970139, 9000750.412936 5935752.893123))" \
minAreaRectangle

pramsey avatar Jul 22 '25 22:07 pramsey

~~Well I can reproduce with a pretty simple case:~~

from shapely import Polygon
simple = Polygon([(30.0, 10.0), (100.0, 50.0), (90.0, 60.0), (20.0, 20.0), (30.0, 10.0)])
print(simple.minimum_rotated_rectangle)
>>> <POLYGON ((20 20, 93.231 61.846, 100 50, 26.769 8.154, 20 20))>
Image

~~In this case I don't think the precision is the issue ?~~


Edit: this message is completely wrong

cometurrata avatar Sep 29 '25 13:09 cometurrata

I'm feeling stupid here... that blue polygon looks like the rotated envelope of the pink parallelogram to me?

pramsey avatar Sep 29 '25 15:09 pramsey

Oh damn ... My message is completely wrong , sorry for this

cometurrata avatar Sep 29 '25 16:09 cometurrata

Agreed, this is because width of the oriented envelope ( 0.00749, or about 1.0e-3 ) is 12 orders of magnitude smaller than the magnitude of the input data ( about 1.0e9 ). This doesn't leave enough "headroom" to provide a robust calculation.

The oriented envelope was computed by translating the input to remove some of the precision:

MULTILINESTRING ((8991903.483397 5938508.444688, 8995834.961524 5937283.90729), (8998483.493762 5936458.970139, 9000750.412936 5935752.893123))

which produces:

POLYGON ((8991903.483397 5938508.444688, 9000750.41020276 5935752.886973559, 9000750.410012472 5935752.886362627, 8991903.480434904 5938508.437805017, 8991903.483397 5938508.444688))
Image

This result can be translated back to the original coordinate system. This will require some custom code to determine the offset for translation.

dr-jts avatar Oct 10 '25 17:10 dr-jts