How to prevent shapely.wkt.loads from changing precision of coordinates in Polygon

41 views Asked by At

I am having an issue using shapely.wkt.loads to load geometry, validate it, and check if the loaded wkt string is the same as the one I will get by exporting after load. I am doing this to fix issues like having multiple parentheses at the end that are valid by shapely.wkt but would cause issues in my app. However, when I export the same Polygon, sometimes the coordinate changes from e.g. 8.531671 to 8.531670999999999 which leads to showing more differences than there really are.

value = 'POLYGON ((8.531671 51.1963499, 8.532257 51.1963549, 8.5322543 51.1969172, 8.5316535 51.1969163, 8.531671 51.1963499))'
geometry = wkt.loads(value)
print(f'Is valid: {geometry.is_valid}')
print(f'Is equal: {geometry.wkt == value}')
print(f'Exported Polygon: {geometry.wkt}')
Is valid: True
Is equal: False
Exported Polygon: POLYGON ((8.531670999999999 51.1963499, 8.532257 51.1963549, 8.5322543 51.1969172, 8.531653499999999 51.1969163, 8.531670999999999 51.1963499))

Do you know if there is any way to prevent this behavior? It is quite important for me as I don't want to change polygons provided by customers even though this change would have no effect in the end and it is also useful to use it to fix other issues.

Thank you!

0

There are 0 answers