Advertisement
Guest User

CreateBuffer

a guest
Jul 12th, 2013
793
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 1.40 KB | None | 0 0
  1. DROP FUNCTION IF EXISTS createBuffer;
  2. CREATE FUNCTION createBuffer(ponto POINT, distancia INT) RETURNS POLYGON
  3.  
  4.     BEGIN
  5.     /**
  6.      *
  7.      * @description : Function to create a circle around a given point and a radius in meters
  8.      * @author : Fernando Norte (fnorte at gmail dot com)
  9.      *
  10.      * @params : ponto POINT (geometry), distancia INT (in meters)
  11.      * @return : Polygon (geometry)
  12.      *
  13.      **/
  14.         SET @angle = 0;
  15.         SET @buffer = ''; # point collection string
  16.         SET @firstPoint = ''; # to close to polygon
  17.         SET @xP = X(ponto);  # Lat of given point
  18.         SET @yP = Y(ponto); # Long of given point
  19.         SET @radius = (distancia / 111200); # Conversion from meters to decimal degres (number to confirm)
  20.  
  21.         REPEAT
  22.             # Algorithm to put the points around the center
  23.             SET @Nx = @xP + @radius * COS(@angle);  
  24.             SET @Ny = @yP + @radius * SIN(@angle);
  25.  
  26.             # Concatenate the border point X,Y to the buffer string
  27.             SET @buffer = CONCAT(@buffer, @Nx, ' ', @Ny, ', ');
  28.  
  29.             IF @angle = 0 THEN
  30.                 # reserve the first point collected to close the polygon
  31.                 SET @firstPoint = CONCAT(@Nx, ' ', @Ny);
  32.             END IF;
  33.             SET @angle = @angle + (2*PI()/20); # increment angle to put 20 points around the center
  34.         UNTIL @angle >= (2*PI()) END REPEAT;
  35.  
  36.         # create WKT for the Polygon
  37.         SET @buffer = CONCAT('POLYGON((', @buffer, @firstPoint, '))');
  38.  
  39.         RETURN GeomFromText(@buffer);
  40.  
  41.     END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement