[GIS] Split polygons into equal squares using custom QGIS tool

pythonqgisqgis-3qgis-processing

I am trying to split polygons into equally sized pieces using a grid. The polygons are all image segments derived from 2.4 meter satellite imagery. So each polygon is a grouping of 2.4m x 2.4m squares. I want to produce a QGIS processing tool to simplify my current workflow of individually running the "Create Grid" and "Split with Lines" QGIS processing tools. The workflow should also edit the selected polygon in the existing layer, not create a new layer.

Example input polygon (with the polygon to be split selected):enter image description here

Expected output from processing tool:
enter image description here

from qgis.core import *
from qgis.gui import *
import qgis.utils
import processing
import re
from PyQt5.QtCore import *

#the spacing of the cut grid
cell_size = 2.4

#get current layer, selection, CRS from QGIS map canvas
layer = qgis.utils.iface.activeLayer()
selected = layer.selectedFeatures()
crs = qgis.utils.iface.activeLayer().crs().authid()

for lay in selected:
    #get the ID of the feature we've selected in QGIS map canvas
    id = lay.id
    print("working on: " + str(id))

    #get the extent of the selected feature
    x_max, y_max, x_min, y_min = re.split(":|,",  lay.geometry().boundingBox().toString().replace(" ", ""))
    extent = x_min + "," + x_max + "," + y_min + "," + y_max
    print(extent)

    #create our grid in memory
    grid_parameters = {"TYPE":  1,
                                    "EXTENT": extent,
                                    "HSPACING": cell_size,
                                    "VSPACING":cell_size,
                                    "HOVERLAY": 0,
                                    "VOVERLAY": 0,
                                    "CRS": crs,
                                    "OUTPUT": "memory_grid"}

    grid = processing.run("qgis:creategrid",  grid_parameters)

    #split the selected feature, store the results in memory
    split_parameters = {"INPUT": layer.getFeature(id),
                                    "LINES": grid["OUTPUT"],
                                    "OUTPUT": "memory_split"}

    split = processing.run("qgis:splitwithlines", split_parameters)

    #delete the original feature,  add the split results to the original layer

with edit(layer):
    layer.deleteFeature(id)
    layer.addFeatures(split["OUTPUT"])

When I run this code I get an error on line 39 (split parameters dictionary) that reads: "TypeError: QgsVectorLayer.getFeature: argument 1 has unexpected type 'builtin_function_or_method"

I have tried several different approaches, including directly feeding my selection, the entire layer, layer.getFeatures(), etc. but nothing has been an acceptable input for the Split with Lines tool.

Best Answer

If you don't need a script, "Create Grid" and "Intersection" tool are enough to solve your problem.

"Create Grid" tool: For CRS, select your layer CRS. (In this case, CRS must be projected, not geographic)

enter image description here

Result of "Create Grid":

enter image description here

"Intersection" tool:

enter image description here

Final Result:

enter image description here

Output Attribute Table: ('Segment_x' comes from polygons and expresses that which segment/polygon the grid is in)

enter image description here

If pixel size is 2.4, result will be as you need.

Related Question