]> pd.if.org Git - hexagon/commitdiff
work
authorNathan Wagner <nw@hydaspes.if.org>
Mon, 23 Feb 2015 14:03:32 +0000 (14:03 +0000)
committerNathan Wagner <nw@hydaspes.if.org>
Mon, 23 Feb 2015 14:03:32 +0000 (14:03 +0000)
pgsql/hexfuncs.sql

index 8826e17090b7b69e5a5d64a5967306502d2d0046..b080539cac71a03a54f1bece2ed4c417b971bf89 100644 (file)
@@ -5,6 +5,41 @@ create schema hex;
 
 set search_path to hex,public,gis;
 
+-- return a postgis text polygon
+-- the 0,0 hex center is at the origin
+-- we rotate, then translate
+create or replace function hexgeom(
+       xc integer, yc integer, scale float = 1.0, angle float = 0.0, xorigin float = 0.0, yorigin float = 0.0
+) returns text language 'plpgsql' as $$
+declare
+       x float[];
+       y float[];
+       stride float;
+       xoff float;
+       yoff float;
+begin
+       stride := scale * 2.0 / sqrt(3.0);
+       xoff := stride;
+       yoff = scale / 2.0;
+       x[1] := (2 + 3 * xc ) * stride;
+       x[4] := (-2 + 3 * xc) * stride;
+       x[2] := (1 + 3 * xc ) * stride;
+       x[6] := (1 + 3 * xc ) * stride;
+       x[3] := (-1 + 3 * xc ) * stride;
+       x[5] := (-1 + 3 * xc ) * stride;
+
+       y[1] := (0 + 2 * yc + xc % 2) * yoff;
+       y[4] := (0 + 2 * yc + xc % 2) * yoff;
+       y[2] := (1 + 2 * yc + xc % 2) * yoff;
+       y[3] := (1 + 2 * yc + xc % 2) * yoff;
+       y[5] := (-1 + 2 * yc + xc % 2) * yoff;
+       y[6] := (-1 + 2 * yc + xc % 2) * yoff;
+
+       return format('POLYGON((%s %s, %s %s, %s %s, %s %s, %s %s, %s %s))',
+               x[1], y[1], x[2], y[2], x[3], y[3], x[4], y[4], x[5], y[5], x[6], y[6]);
+end;
+$$ immutable strict;
+
 create or replace function hexgrid(
        xdim integer,
        ydim integer,