Example output
Certainly! Here's a step-by-step guide on how to create a geometry expression to style polygons like Lego® bricks in QGIS:
Step 1: Prepare your data
Ensure you have a polygon layer in QGIS that you want to style as Lego® bricks. You can either create a new polygon layer or use an existing one.
Step 2: Open the Layer Styling panel
Select the polygon layer you want to style, right-click on it, and choose "Properties" from the context menu. In the Layer Properties window, go to the "Symbology" tab.
Step 3: Enable Geometry Generator
Under the "Symbol layer type" section, choose "Geometry generator" from the drop-down menu. This enables you to create a custom geometry expression to style the polygons.
Step 4: Create the Lego® brick geometry expression
In the Expression string builder, you will construct the geometry expression to generate Lego® brick patterns. The expression to use is as follows:
qgis code:
collect_geometries(
array_foreach(
generate_series(0, floor($area / 1000)),
make_polygon(
make_line(
make_point($x + 10 * @element, $y),
make_point($x + 10 * @element, $y + 5),
make_point($x + 10 * @element + 5, $y + 5),
make_point($x + 10 * @element + 5, $y)
)
)
)
)
This expression creates a set of rectangular polygons resembling Lego® bricks. Each brick is 10 units wide and 5 units high. Adjust these values if you want different-sized bricks.
Step 5: Apply the geometry expression
Copy the above expression and paste it into the Expression string builder. Make sure it replaces any existing content. Click "Apply" to see the result.
Step 6: Refine the styling
Once you've applied the geometry expression, you can refine the styling of your Lego® brick polygons. Adjust the fill color, stroke color, stroke width, and other properties in the Symbol layer type section to achieve the desired visual effect.
Step 7: Fine-tune the Lego® brick size and spacing
If you want to modify the size and spacing of the Lego® bricks, you can adjust the values in the geometry expression. The numbers "10" and "5" in the expression represent the brick's width and height, respectively. Increase or decrease these values to resize the bricks. Additionally, you can modify the spacing between bricks by adjusting the distances between the points in the make_line function.
Step 8: Save and enjoy your Lego® brick styling
Once you are satisfied with the Lego® brick styling, you can save your project and enjoy the visual appeal of your styled polygons. To save your project, go to "Project" > "Save" or use the shortcut Ctrl+S (or Command+S on Mac).
Congratulations! You have successfully created a geometry expression to style polygons as Lego® bricks in QGIS. Enjoy exploring the creative possibilities this technique offers for your GIS projects.