daily pastebin goal
28%
SHARE
TWEET

M Jansen

a guest Dec 22nd, 2008 313 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Creates a star with given parameters (an optimized version exists).
  3.  *
  4.  * Needs both PostgreSQL and Postgis.
  5.  *
  6.  * Please contact jansen (at) terrestris (dot) de when you use this function.
  7.  *
  8.  * @language PL/PGSQL
  9.  * @see selectoid.wordpress.com
  10.  * @license MIT, http://www.opensource.org/licenses/mit-license.php
  11.  * @copyright 2008, M. Jansen jansen (at) terrestris (dot) de
  12.  */
  13. CREATE OR REPLACE FUNCTION ST_StarAtPoint(
  14.     IN point geometry,
  15.     IN inner_radius numeric,
  16.     IN outer_radius numeric,
  17.     IN number_of_spikes integer,
  18.     IN additional_rotation_degrees numeric
  19. ) RETURNS GEOMETRY AS
  20. $body$
  21.     DECLARE
  22.         star_geometry_wkt text := '';
  23.         i integer := 0;
  24.         angle numeric;
  25.         additional_rotation_angle numeric;
  26.         baseline_inner geometry;
  27.         baseline_outer geometry;
  28.         rotation_point geometry := 'POINT(0 0)'::geometry;
  29.     BEGIN
  30.         IF (inner_radius > outer_radius) THEN
  31.             RAISE EXCEPTION 'Inner radius must not be greater than outer radius.';
  32.         END IF;
  33.         IF (number_of_spikes < 3) THEN
  34.             RAISE EXCEPTION 'A star must have at least three corners.';
  35.         END IF;
  36.        
  37.         angle = radians(360/number_of_spikes);
  38.         additional_rotation_angle = radians(additional_rotation_degrees);
  39.        
  40.         baseline_outer =   ST_RotateZ(ST_MakeLine(rotation_point, ST_MakePoint(ST_X(rotation_point), ST_Y(rotation_point) + outer_radius)), additional_rotation_angle);
  41.         baseline_inner =   ST_RotateZ(ST_MakeLine(rotation_point, ST_MakePoint(ST_X(rotation_point), ST_Y(rotation_point) + inner_radius)), additional_rotation_angle + (angle/2));
  42.  
  43.         WHILE (i < number_of_spikes) LOOP
  44.             star_geometry_wkt = star_geometry_wkt || (ST_X(ST_EndPoint(ST_RotateZ(baseline_outer, angle * i))) + ST_X(point)) || ' ' || ST_Y(ST_EndPoint(ST_RotateZ(baseline_outer, angle * i))) + ST_Y(point)  || ',';
  45.             star_geometry_wkt = star_geometry_wkt || (ST_X(ST_EndPoint(ST_RotateZ(baseline_inner, angle * i))) + ST_X(point)) || ' ' || ST_Y(ST_EndPoint(ST_RotateZ(baseline_inner, angle * i))) + ST_Y(point) || ',';
  46.             i = i + 1;
  47.         END LOOP;
  48.         star_geometry_wkt = star_geometry_wkt ||  (ST_X(ST_EndPoint(baseline_outer)) + ST_X(point))  || ' ' ||  (ST_Y(ST_EndPoint(baseline_outer)) + ST_Y(point));
  49.         star_geometry_wkt = 'POLYGON((' || star_geometry_wkt || '))';
  50.         RETURN star_geometry_wkt::geometry;
  51.     END;
  52. $body$
  53. LANGUAGE plpgsql;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top