3

I've been trying for a while now to create a grid inside a square buffer, so that it can be divided into a pre-defined number of sub-units. Creating a grid that covers the whole area is not ideal because the grid's squares will not be properly aligned with the square buffers and I need that in order to be able to count how many grid's squares are occupied by houses inside the buffer. I have 150 buffers in a single layer, and i'd like to have the grids only inside them.

I am still quite new to GIS.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Ginevra
  • 39
  • 2

2 Answers2

1

Using QGIS expressions, you can subdive any square, indifferenz of size and orientation (angle) into a freely definable number of smaller squares: 2 x 2, 3 x 3, 4 x 4 etc. Output are lines; if you want to split the initial polygon in sub-polygons, use Menu Processing > Toolbox > Split with lines with the lines created by the expression from below.

Use the following expression with Geometry generator or Geoemtry by expression (see here for details) and change the value in line 4 (here: 5): the square will be split in n * n little squares, based on this number (if you insert 3: 3 x 3 = 9; for 5 -> 5 x 5 = 25 etc.).

Number set to 5: Square is split in 5*5=25 smaller squares: enter image description here

collect_geometries (
    with_variable(
        'no',
        5, 
        with_variable (
            'len',
            length (
                make_line (
                    point_n($geometry, 1),
                    point_n($geometry, 2)
                )
            )
        ,
        array_foreach (
            generate_series (1, @no-1),
            collect_geometries(
                make_line (
                    line_interpolate_point( 
                        make_line (
                            point_n($geometry, 1),
                            point_n($geometry, 2)
                        ),
                        @len/@no*@element
                    ),
                    line_interpolate_point( 
                        make_line (
                            point_n($geometry, 4),
                            point_n($geometry, 3)
                        ),
                        @len/@no*@element
                    )
                ),
                make_line (
                    line_interpolate_point( 
                        make_line (
                            point_n($geometry, 2),
                            point_n($geometry, 3)
                        ),
                        @len/@no*@element
                    ),
                    line_interpolate_point( 
                        make_line (
                            point_n($geometry, 1),
                            point_n($geometry, 4)
                        ),
                        @len/@no*@element
                    )
                )
            )
        )
    )
)

)

Babel
  • 71,072
  • 14
  • 78
  • 208
0

See this answer to Drawing graticule with GDAL/OGR?, which basically uses the built-in Create grid plugin:

Vector -> Research Tools -> Create grid

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
RafDouglas C. Tommasi
  • 6,479
  • 1
  • 15
  • 41
  • 1
    This does only create a horizontal/vertical grid in the extent of the layer, not subdividing squares of any rotation/size – Babel Aug 03 '22 at 20:36