2

In QGIS I have the data for all the continents except Antarctica. Now I want to create a grid of size 1km * 1km for each country however the problem I am running into is that if I take two neighbouring countries the grids don't align properly as shown in the image below. Both grids and polygons follow the same CRS i.e. EPSG 4326 - WGS 84

enter image description here

I want to generate the grids in a way that it aligns with the other grids. I am complete beginner when it comes to GIS. I will get rid of the overlapping rectangles later, but I want these lines to align so that when shown on a map, it must look like one grid and not a collection of grids. Any guidance or tips?

Edit 1: I want to generate the whole data for world in the form of grids but I am unable to do so in one go as it always crashes after 2 - 3 days of continuous processing with only 6 -8 % progress. Any suggestions that can help me accomplish that will also be very helpful if this country by country route is too complicated.

3
  • 1
    Before anything else, do some math and ask yourself if you really want to generate +100 000 000 Polygons - and if so, how to work with them after. The ad-hoc gridding of coordinates is extremely cheap in comparison.
    – geozelot
    Commented Oct 17, 2023 at 8:33
  • @geozelot Client wants to create plots over the whole world so this is the solution I could come up with my limited knowledge as we have done the same thing for north America only. however if you can just elaborate a little on your solution or point me towards a article on it I would be really grateful. Commented Oct 17, 2023 at 8:58
  • E.g. for an exemplary input Point coordinate pair P[X: -12550035.7, Y: 3798621.8] (in EPSG:3857, which you should not use) and a grid cell edge length CL=1000 [m], the Point P_C[X: Floor(P.X / CL) * CL, Y: Floor(P.Y / CL) * CL] would be the bottom left corner of the grid cell point P falls into. No need to check 100M Polygons for that...
    – geozelot
    Commented Oct 17, 2023 at 10:06

2 Answers 2

3

Simply use PostGIS - run

CREATE TABLE global_grid AS (
  SELECT
    ROW_NUMBER() OVER() AS id,
    grid.i AS cell_x,
    grid.j AS cell_y,
    grid.geom
  FROM
    ST_SquareGrid(
      <CELL_SIZE>,
      ST_Transform(
        'SRID=4326;POLYGON((-180 -90, -180 90, 180 90, 180 -90, -180 -90))',
        <METRIC_UNIT_SRID>
      )
    ) AS grid
);

for a global grid, or

CREATE TABLE country_grid AS (
  SELECT
    cty.id,
    grid.i AS cell_x,
    grid.j AS cell_y,
    grid.geom
  FROM
    <countries_table> AS cty,
    LATERAL ST_SquareGrid(
      <CELL_SIZE>,
      ST_Transform(
        cty.geom,
        <METRIC_UNIT_SRID>
      )
    ) AS grid
);

to create aligning grids over <countries_table>.geom.


Notes:

  • Choosing a <METRIC_UNIT_SRID> is trickier than you may think, as there cannot exists an (equi)rectangular equal area grid covering a spheroidal body precisely - you may want to resolve using a Cylindrical Equal Area projection:

    • EPSG:54034, ESRI's default Spheroidal CEA projection
    • Behrends projection via proj4 string:
      '+proj=cea +lat_ts=30 +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs'
    • Gall-Peters projection via proj4 string:
      '+proj=cea +lat_ts=44.138 +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs'
  • Do not create a grid! Especially when working with Point geometries that have to be referenced to the grid, getting the cell properties from rounding coordinates on-the-fly and lazily creating/storing cell geometries if needed is the much much better approach - especially especially if you need the grid reference mainly for metric aggregation and sporadic mapping. But also very generally!

1
  • Thank you for your suggestion I will try this out and update my question accordingly. Commented Oct 17, 2023 at 12:26
2

Create one single grid worldwide (be aware: using EPSG:4326, you can't create grids in km, only in degrees. Reproject instead). In a second step, for each country (as you want a separate grid for each country, right?) make a selection based on location: select only grid cells overlapping with the current country.

To do so, you can use Menu Processing > Toolbox > Geometry by expression with this expression:

collect_geometries (overlay_intersects ('grid_layer', $geometry))

It creates one multipart geometry for each country, with overlapping cells where two countries border each other.

Grid (=cells in red) created for France, Spain and Portugal, selected (yellow) cells for France. Darker cells belong to two countries: enter image description here


Edit

Based on your additional information, be aware of this: As mentioned by @geozelot, creating 1km * 1km grid cells worldwide will simply create too large data to be handled. Either reduce the no. of cells (larger grid cells) or use a more performant computer system.

A workaround could be to first create a worldwide grid with much larger resolution (like 1000 km * 1000 km) and then subdivide it (100 km * 100 km etc.) for certain regions only until you reach the desired grid size. Based on the initial worldwide grid, cells will allign if you create the smaller grids based on the extent of one of these initial cells.

You will need to do this repeatedly for several regions of the earth (countries) separately. You could maybe create a model for ceratin steps to reduce reproducing repeating steps manually.

6
  • creating one single grid is what I want but unfortunately with the computing power at my disposal it just isn't possible tried it three times each time qgis crashed after 2 - 3 days of processing with just 6 - 10% of progress max. This is why I thought if I could break it into countries it might help but then I am unable to solve this alignment issue. Anyway thank you for your help. Commented Oct 17, 2023 at 9:02
  • It was my error for not mentioning this information in the question so I have added a new edit. Commented Oct 17, 2023 at 9:10
  • See my edit in my answer above
    – Babel
    Commented Oct 17, 2023 at 9:15
  • Thank you this is the direction I needed. For now I am marking your suggestion as the solution. On a side note Client wanted 100m * 100m grid I had to negotiate it to 1 km by 1 km. Thanks again for this prompt response :) Commented Oct 17, 2023 at 9:19
  • is it possible to hook qgis up with a powerful ec2 instance so I could get my processing done? Commented Oct 17, 2023 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.