2

I need to create a grid, based on parameters.

  1. Dimensions of the grid (10 km x 10 km)
  2. Upper-left coordinate, which defines the start of the grid (upper-left corner)

I was reviewing the following approach, but the metrics are not clear. How can I make the dimension in km format? How can I start with the upper-left side with coordinates?

CREATE OR REPLACE FUNCTION ST_CreateFishnet(
        nrow integer, ncol integer,
        xsize float8, ysize float8,
        x0 float8 DEFAULT 0, y0 float8 DEFAULT 0,
        OUT "row" integer, OUT col integer,
        OUT geom geometry)
    RETURNS SETOF record AS
$$
SELECT i + 1 AS row, j + 1 AS col, ST_Translate(cell, j * $3 + $5, i * $4 + $6) AS geom
FROM generate_series(0, $1 - 1) AS i,
     generate_series(0, $2 - 1) AS j,
(
SELECT ('POLYGON((0 0, 0 '||$4||', '||$3||' '||$4||', '||$3||' 0,0 0))')::geometry AS cell
) AS foo;
$$ LANGUAGE sql IMMUTABLE STRICT;
Kadir Şahbaz
  • 76,800
  • 56
  • 247
  • 389
  • 1
    where are you stuck exactly? what is not working for you - please provide example inputs and outputs – Ian Turton May 22 '22 at 16:18
  • @IanTurton i am not sure what metrics have been used here. I need 10 km x 10 km grid, but not sure from where to start – Gulaiym Makhambetova May 22 '22 at 16:23
  • Maybe an explanation in the answer will clarify a little bit about the order of the grid where I was building a degree grid, although it was a long time ago for me, ....https://gis.stackexchange.com/a/284894/120129 – Cyril Mikhalchenko May 22 '22 at 17:42
  • @CyrilMikhalchenko thanks for the response. SELECT * FROM st_createfishnet(43,69,1.0,1.0,111.0,-51.0); you say that the first two parameters are number of rows and columns. and the next two are the size of the grid cell and the last is the origin. for example, I need a grid with a size of 10 km x 10 km and with resolution of 1000x1000 pixels. can you please direct me to create such a grid? I assume these 1.0,1.0, parameters should be converted to the km and 43,69 to pixels right? – Gulaiym Makhambetova May 23 '22 at 03:39
  • @CyrilMikhalchenko can you have a look at this question pls? https://gis.stackexchange.com/q/435587/183537 – Manap Shymyr Jul 12 '22 at 05:14

1 Answers1

1

So, if I understand the question correctly, there are many ways to solve your question, and this is one of them. Proceed as follows:

  1. Replace the values of the northwest corner coordinate 100,100 with required values;
  2. Replace 3857 with your SRID value;
  3. Run the SQL script with your parameter values, for my example:
SELECT ST_SetSrid(ST_Collect(geom), 3857) geom FROM ST_CreateFishnet(10,10,1000,-1000,100,100);
  1. Don't forget to thank the author and developer of the function ST_CreateFishnet() - Mike Taves (https://gis.stackexchange.com/users/1872/mike-t).

Good luck in learning,

and promise me that you will learn how to create polygonal square grids from any values of the corners of the outer edge of the grid, i.e. southwest, northwest, northeast and southeast... :-)...

Translated with www.DeepL.com/Translator (free version)

Cyril Mikhalchenko
  • 4,397
  • 7
  • 14
  • 45