Guest User

Untitled

a guest
Apr 25th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 1.08 KB | None | 0 0
  1. DROP FUNCTION IF EXISTS MidPoint;
  2. DELIMITER //
  3.  
  4. CREATE FUNCTION MidPoint(ls LINESTRING)
  5. RETURNS POINT
  6. DETERMINISTIC
  7. BEGIN
  8.     DECLARE len double;
  9.     DECLARE workLength double DEFAULT 0;
  10.     DECLARE workPoint int DEFAULT 1;
  11.     DECLARE point0 POINT;
  12.     DECLARE point1 POINT;
  13.     DECLARE distanceOver double;
  14.     DECLARE segmentLength double;
  15.  
  16.     IF (NumPoints(ls) = 1) THEN return PointN(ls, 1); END IF;
  17.  
  18.     -- find half the length of the linestring
  19.     SET len := GLength(ls) / 2;
  20.  
  21.     -- walk the linestring until we exceed the distance
  22.     WHILE (workLength < len) DO
  23.         SET point0 = PointN(ls, workPoint);
  24.         SET workPoint := workPoint + 1;
  25.         SET point1 = PointN(ls, workPoint);
  26.         SET segmentLength = GLength(LineString(point0, point1));
  27.         SET workLength := workLength + segmentLength;
  28.     END WHILE;
  29.  
  30.     -- distance to backup
  31.     SET distanceOver = workLength - len;
  32.  
  33.     -- midpoint is distanceOver back down the last segement
  34.     RETURN POINT(
  35.             X(point1) - distanceOver / segmentLength * (X(point1) - X(point0)),
  36.             Y(point1) - distanceOver / segmentLength * (Y(point1) - Y(point0))
  37.         );
  38.  
  39. END//
  40. DELIMITER ;
Add Comment
Please, Sign In to add comment