TransWikia.com

ST_SnapToGrid doesn't work properly, e.g. 41.94186153740355 -> 41.941861499999995

Geographic Information Systems Asked by kujaw on February 9, 2021

I’m using ST_SnapToGrid: ST_SnapToGrid(somePoint, 0.0000001) and I get strange result on many points, such as:

- POINT (21.619820510769063 41.94186153740355) -> POINT (21.6198205 41.941861499999995)  
- POINT (22.49594094391644 41.20357506925623) -> POINT (22.495940899999997 41.203575099999995)  

What am I doing wrong?

One Answer

Turns out that all of the geometries that couldn't be divided by ST_SnapToGrid had form of xx.xxxxxxx9999999x, where x is a digit. That means, that if I want its precision to be centimetric, it's safe to treat such geometries as a number and just round it mathematically, all of them will round to the higher number.
After such process, they matched with the geometries I should get after reducing precision. So, first I snapped the X and Y geometries separately, then rounded them and recreated geometry from concatenated string.

ST_GeomFromText(concat(
'POINT (', 
round(ST_X(ST_SnapToGrid(point, 0.0000001))::numeric, 7), 
' ', 
round(ST_Y(ST_SnapToGrid(point, 0.0000001))::numeric, 7),
')'
))

Answered by kujaw on February 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP