2

Given that MySQL 5.7 has spatial data types Is there an equivalent to PostGIS's ST_SnapToGrid in MySQL?

Looking at the reference:

http://dev.mysql.com/doc/refman/5.7/en/spatial-function-reference.html

I can't find one. Is there something else I could use that would do the same thing?

zod
  • 555
  • 1
  • 5
  • 16

1 Answers1

3

I wrote this function which does the same thing in MySQL.

DROP FUNCTION IF EXISTS ST_SnapToGrid;
DELIMITER $$
CREATE FUNCTION ST_SnapToGrid(geom POINT, sizeX FLOAT, sizeY FLOAT)
RETURNS POINT
BEGIN

    DECLARE x, y FLOAT;

    SET x = FLOOR(ST_X(geom) / sizeX) * sizeX;
    SET y = FLOOR(ST_Y(geom) / sizeY) * sizeY;

    SET geom = POINT(x, y);

    RETURN geom;

END$$
DELIMITER ;
lschmid
  • 66
  • 4
  • 1
    Please use Equivalent of ROUND rather than floor. If you have a change in a calculation that goes from 15.00000001 to 14.9999999999 the point changes grid location. 14.5000000001 to 14.499999999999 is a more reasonable place to put this change. – MichaelStoner Apr 13 '16 at 17:38