[postgis-users] Different results of ST_SnapToGrid variants

Marcin Mionskowski mionskowskimarcin at gmail.com
Fri Jul 22 04:04:16 PDT 2016


Dear Users!
I would like to ask you for help with ST_SnapToGrid...

The background:

ST_SnapToGrid (http://postgis.net/docs/ST_SnapToGrid.html) has 4 variants, of which I'm interested in first 3.
Results of variant II and III are the same (those functions are using variant I function, with grid origin set to (0,0)):
II.  SELECT ST_AsText(ST_SnapToGrid(ST_GeomFromText('POINT(100000.005 100000.006)'),0.01,0.01))
III. SELECT ST_AsText(ST_SnapToGrid(ST_GeomFromText('POINT(100000.005 100000.006)'),0.01))
Result:
> "POINT(100000 100000.01)"
.005 is rounded to .00 (!) and .006 to .01

I get different (correct) result with variant I, when I set custom grid origin:
SELECT ST_AsText(ST_SnapToGrid(ST_GeomFromText('POINT(100000.005 100000.006)'),100000,100000,0.01,0.01))
> "POINT(100000.01 100000.01)"

but when I change coordinates:
SELECT ST_AsText(ST_SnapToGrid(ST_GeomFromText('POINT(141944.005 150100.006)'),100000,100000,0.01,0.01))
I get wrong results again:
> "POINT(141944 150100.01)"

I've done some generate_series tests - it seams, that the problem is related to:
- "distance" between coordinate and grid origin, with respect to grid size
and/or
- "conversion" between numeric types: decimal (point and grid definition in my examples) and double precision (coordinates in geometry type).

My question:
What are the real reasons for above-mentioned problems?
Can I somehow bypass them to properly (.005->.01) and efficiently (approx 20 mln polygons) round coordinates?

Regards,
Marcin


More information about the postgis-users mailing list