7

In PostGIS 3.1 the function ST_SquareGrid(size, bounds) generates a grid. The documentation for the functionality is available in the docs. Understanding how this is done is not clear from the documentation.

I want to create a grid which overlaps with a polygon, in my case a country. The grid size should be in 1:n size of decimal grids. I plan on building grids according to a system called quarter degree grid cells.

enter image description here

The reason for this is that I am currently trying to shift most of the processing of this process to PostGIS rather than having to do the processing on FME. By pushing the processing to PostGIS I get less information back and forth between the database and FME.

How can I utilise this PostGIS functionality towards creating a relevant grid?

Kadir Şahbaz
  • 76,800
  • 56
  • 247
  • 389
ragnvald
  • 3,191
  • 1
  • 31
  • 56
  • 1
    this could potentially point you in the right direction https://gis.stackexchange.com/questions/16374/creating-regular-polygon-grid-in-postgis - there are a number of responses in this thread, I have been using that of Alexander Palamarchuk which works quite well. – Timothy Dalton Jan 11 '21 at 08:21

2 Answers2

8

If you want a 1 degree grid, make your reference system by geographic (eg 4326) and then make a 1 unit grid, with your shape providing the bounds.

with grid as (
  select (st_squaregrid(1, st_transform(geom,4326))).* 
  from admin0 where name = 'Canada'
) 
select st_astext(geom), i, j from grid;
Paul Ramsey
  • 19,865
  • 1
  • 47
  • 57
4

I believe the example here should help you.
Basically if you want to create a grid based on the bounds of a country just use the country's polygon, transformed into the CRS you want the grid to be in, and use the size you want the cells to be in.

SELECT (ST_SquareGrid(400000, ST_Transform(a.geom, 3857))).* 
FROM admin a  
WHERE name = 'Brazil';

If you want the grid cells to go a size smaller, you could select one cell from the large grid and call ST_SquareGrid on it.

Dror Bogin
  • 3,475
  • 10
  • 24