Geographic Information Systems Asked by GSC_Mapper on January 14, 2021
Using QGIS Process Modeler, I need a way to create a grid that is centered on an input feature. My issue is that the [Create grid] algorithm, creates a grid whose upper left corner is aligned to the upper left corner of the extents defined during the grid creation process (as opposed to aligning the center of the grid to the center of the input feature’s extents like I want).
I am able to shift the grid using the [Translate] algorithm, but (in Process Modeler) I do not know how to easily calculate the XY shift between the two respective centroids and incorporate that shift back into an expression in Process Modeler.
Does anyone have any suggestions?
To anticipate/resume the most important step: use affine transformation to shift the grid. The shift distance in x and y coordinates corresponds to the difference between the centroids of the input polygon and the grid, shifting the grid centroid to the one of the input feature. In the model, you can use the variables available there that refer to the inputs/algorithm outputs to create the expression from below that defines the distance and direction the grid is shifted.
In detail:
In a first step, let's consider the simple workflow. In a second step (see below), we adapt this to create a model. Be careful what CRS the input layer has - if it is in a geographic CRS, creating a grid will use degrees as distance unit, what does not make sense. So use a porjected CRS.
Simple Workflow
Let's suppose you have a polygon layer (blue) with it's centroid (red point). Than you create a polygon rectangle grid (black lines) which has it's centroid at the white dot. So what you want is to shift the grid in a way that the white point overlaps the red one.
For shifting, you can use Menu Processing / Toolbox / Affine Transformation
where you can set the distance in x and y direction. In both cases, the distance is equal to the distance of the two centroids. Thus as input for the Translation (x-axis), select data driven override (ε symbol) and paste this expression by replacing 'polygon' with the name of your polygon layer (and accordingly for the y-axis, just replace x with y):
x ( centroid ( geometry ( get_feature_by_id ( 'polygon' , 1 ) ) ) ) -
x ( centroid ( collect ( $geometry ) ) )
The expression takes the x coordinate of the polygon's centroid and subtracts from it the x coordinate of the grid's centroid, getting thus the distance between both points.
The result looks like on the screenshot: a shifte grid (red lines) with it's centroid (white cross) exactely on the centroid of the polygon.
This solution supposes you have just one input feature (you don't state otherwise). In case of more than one feature, you should adapt the solution.
Create Model
Now we can adapt this as a model. See the following screenshot how the working model looks like. You have two inputs: select a polygon (layer), no 1, and set a grid distance: no. 2 (if you want different distances for x and y, you can easily adapt this).
From these two inputs, a grid is created, no. 3. This original grid will be one of two outputs for comparison: 3a. If you don't need it, delete it from the model.
We also create the centroid of the input polygon: no. 4. Now, we are ready for Affine transformation: no. 5. See on the left side the dialog window for this tool. As input layer, we select the output of create_grid. For x and y transformation, we adapt the expression to what works in the model. Set the button on the left to ε to use expressions and press the ε on the right to open the expression editor where we build the expression. Here, you see the context variables available at this part of the model and how to adress them.
We have among others the variable @centroid_polygon_OUTPUT_maxx
- because we are dealing with the centroid, thus a point, we could also use _minx
at the end as they have the same value. In the same manner, we select the variables for the grid and create the centroid of the grid using the average of min and max values for x and y coordinates each. Thus our expression looks like this (replace the x of _maxx and _minx by y in the y field) - don't forget to adapt the names to those used in the model (here: centroid_polygon
and Create_grid
):
@centroid_polygon_OUTPUT_maxx
-
((@Create_grid_OUTPUT_maxx + @Create_grid_OUTPUT_minx ) /2 )
We now just have to define a name for the output to get an actual output of the transformed grid. Save the model and run it. After selecting a polygon layer and setting a grid distance, you get the original and the transformed (shifted) grid as output.
Correct answer by babel on January 14, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP