1

I have a following problem: I have a layer with numerous polygons inside (Over 400). I need to generate grids inside every polygon. When using the Grid process over a layer, the generated grid covers the whole area of the layer. This is not what I want, as I need the grid in the polygon to be centered in this polygon. Now, although many processes do have an option to iterate same process through all features, the grid making process does not have it. So Im wondering if there is a workaround as I want to implement that gridding step into a more complex model for a survey design.

This is what I get when I run "grid" over a layer. You can see that the grids are off in the individual polygons.

This is what I get when I run "grid" over a layer. You can see that the grids are off in the individual polygons.

This is how I would want the grids to be created in each feature

This is how I would want the grids to be created in each feature

A manual workaround for now is to run "Collect Geometries" process, which creates a separate layer for each polygon of my layer which is several hundred layers with the same name. And then I run the grid process, manually changing the extent from layer to layer. You can imagine, this is a very dirty work, leaving a huge data mess. I was looking into geometry generator options but I´m struggling to find a proper guide/course/tutorial for expressions that would allow me use this option extensively, so im stuck. Used QGIS 3.18 and 3.28 to test.

Any ideas are welcome!

1 Answer 1

1

Create a model with an "Vector Features" input:

  • Set the vector features as grid extent in "Create grid".
  • When executing the model select "Iterate over this layer...." (red in screenshot). A grid will be created for each input feature/polygon.
  • Then merge all output grids (I havent below).

enter image description here

Or use python to do the same thing:

layer = QgsProject.instance().mapLayersByName("Polygon")[0] #Change Polygon to the name of your layer

horizontal_grid_spacing = 100
vertical_grid_spacing = 100

grids = [] #A list to hold each grid created for each polygon
for feature in layer.getFeatures(): #For each polygon
    box = feature.geometry().boundingBox() #Find its bounding box
    #Calculate the extent of it
    extent = f"{box.xMinimum()},{box.xMaximum()},{box.yMinimum()},{box.yMaximum()} [{layer.crs().authid()}]"
    #Create a grid
    grid = processing.run("native:creategrid", {'TYPE':2,
        'EXTENT':extent,'HSPACING':horizontal_grid_spacing,
        'VSPACING':vertical_grid_spacing,'HOVERLAY':0,'VOVERLAY':0,
        'CRS':QgsCoordinateReferenceSystem(layer.crs().authid()),'OUTPUT':'TEMPORARY_OUTPUT'})['OUTPUT']
    grids.append(grid) #Store it in the grids list

#Merge all grids
processing.runAndLoadResults("native:mergevectorlayers", 
    {'LAYERS':grids,'CRS':None,'OUTPUT':'TEMPORARY_OUTPUT'})
  

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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