MySQL and more

Friday, July 31, 2009

MySQL Midpoint Stored Function

MySQL GIS functions don't seem to include a Midpoint function; it's trivial to write one, but I thought posting this might save someone a few minutes. Enjoy!

DROP FUNCTION IF EXISTS midpoint;
DELIMITER //
CREATE FUNCTION midpoint(line GEOMETRY)
RETURNS POINT DETERMINISTIC
BEGIN

IF NumPoints(line) != 2 THEN
RETURN NULL;
END IF;

RETURN GeomFromText(CONCAT('POINT(',(X(StartPoint(line)) +X(EndPoint(line))) / 2,' ',(Y(StartPoint(line))+Y(EndPoint(line))) / 2,')'));
END //
DELIMITER ;

0 comments:

Followers