1

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?

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 ;
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.