Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DROP FUNCTION IF EXISTS createBuffer;
- CREATE FUNCTION createBuffer(ponto POINT, distancia INT) RETURNS POLYGON
- BEGIN
- /**
- *
- * @description : Function to create a circle around a given point and a radius in meters
- * @author : Fernando Norte (fnorte at gmail dot com)
- *
- * @params : ponto POINT (geometry), distancia INT (in meters)
- * @return : Polygon (geometry)
- *
- **/
- SET @angle = 0;
- SET @buffer = ''; # point collection string
- SET @firstPoint = ''; # to close to polygon
- SET @xP = X(ponto); # Lat of given point
- SET @yP = Y(ponto); # Long of given point
- SET @radius = (distancia / 111200); # Conversion from meters to decimal degres (number to confirm)
- REPEAT
- # Algorithm to put the points around the center
- SET @Nx = @xP + @radius * COS(@angle);
- SET @Ny = @yP + @radius * SIN(@angle);
- # Concatenate the border point X,Y to the buffer string
- SET @buffer = CONCAT(@buffer, @Nx, ' ', @Ny, ', ');
- IF @angle = 0 THEN
- # reserve the first point collected to close the polygon
- SET @firstPoint = CONCAT(@Nx, ' ', @Ny);
- END IF;
- SET @angle = @angle + (2*PI()/20); # increment angle to put 20 points around the center
- UNTIL @angle >= (2*PI()) END REPEAT;
- # create WKT for the Polygon
- SET @buffer = CONCAT('POLYGON((', @buffer, @firstPoint, '))');
- RETURN GeomFromText(@buffer);
- END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement