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,