From: Nathan Wagner Date: Mon, 23 Feb 2015 14:03:32 +0000 (+0000) Subject: work X-Git-Url: https://pd.if.org/git/?p=hexagon;a=commitdiff_plain;h=e18c84c99d50c450bcbe8ad8f66d4e57eadf0911 work --- diff --git a/pgsql/hexfuncs.sql b/pgsql/hexfuncs.sql index 8826e17..b080539 100644 --- a/pgsql/hexfuncs.sql +++ b/pgsql/hexfuncs.sql @@ -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,