How to do it...

We can consider geometry and a float value as the inputs, and we want the function to return geometry:

CREATE OR REPLACE FUNCTION chp04.create_grid (geometry, float) RETURNS geometry AS $$ 

In order to construct the subplots, we will require three lines running parallel to the X axis:

WITH middleline AS ( 
  SELECT ST_MakeLine(ST_Translate($1, -10, 0),     ST_Translate($1, 40.0, 0)) AS the_geom 
), 
topline AS ( 
  SELECT ST_MakeLine(ST_Translate($1, -10, 10.0),    ST_Translate($1, 40.0, 10)) AS the_geom 
), 
bottomline AS ( 
  SELECT ST_MakeLine(ST_Translate($1, -10, -10.0),     ST_Translate($1, 40.0, -10)) AS the_geom 
),

And we will require six lines running parallel to the Y axis:

oneline AS ( SELECT ST_MakeLine(ST_Translate($1, -10, 10.0), ...

Get PostGIS Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.