QGIS has a powerful expression machine that can be used (between other things) to create new geometries, based on existing layers. Basically, there are two options: Geometry generator and Geometry by expression. What is the difference between these two options?
Remark: this question is a case of
you’d like to document [a question] in public so others (including
yourself) can find it later
As I often answer questions that involve creating geometries using expressions, I always have to repeat some basics. I think it's better to have the relevant information in a separate post that can be referred to keep answers shorter. Feel free to add further answers with additional information.
Best Answer
Quick overview how to create new geometries using QGIS expressions with either
Geometry generator
orGeometry by expression
:Geometry generator: Open Layer Styling panel / add additional symbol layer (green
+
) /Symbol layer type
:Geometry generator
/ set appropriateGeometry type
(point/line/polygon) / add your expressionGeometry by expression:
Menu Processing / Toolbox / Geometry by expression
/ choose input layer / set appropriateOutput geometry type
/ add your expressionOverview Differences: Table![enter image description here](https://i.stack.imgur.com/2YsSa.png)
Geometry generator
vs.Geometry by expression
- highlighted in green which option is better fit for the corresponding task:In more details: You can use the same expressions in both ways, depending on what you use it for.
Geometry generator: This is useful for creating dynamic geometries on a layer for visualization purposes only. Disadvantage: They are symbologies, thus you can't use it for further geoprocessing.
Advantage: However, they are great because you can see in realtime how the geometries change when you change the expression. You can try this with a simple example: use
buffer ($geometry, 1)
and add zeros to the number 1 to see the buffer successively increase with each digit you add. Like this, you can control the appearance of the geometry you create in a very intuitive way and learn how different settings affect the geometry.How to do it:
+
.Symbol layer type
toGeometry generator
.Geometry type
: it is the kind of geoemtry you want to create, not the one of the geometry the expression is based on. A simple example: if you want to create a buffer, always selectPolygon / MultiPolygon
, irrespective if you buffer a point, line or polygon layer. If you create a centroid, however, always setGeometry type
toPoint / MultiPoint
.ε
) button to open the powerfulExpression string builder
: see details about it in the documentationScreenshot: how to use![enter image description here](https://i.stack.imgur.com/f4xfd.png)
Geometry generator
. Starting point is a line-layer (black line). Add a symbol layer in the layer styling panel and on this symbol layer create a buffer (transparent orange), here with the expressionbuffer ($geometry, 100)
. Change the shape of the black line or the buffer-distance from100
to any other value to see how the orange buffer changes:Geometry by expression: This is useful if you need the created geometries for further processing. It will create a new layer containing the generated geometries and will thus be permanent (if you save the layer). Once executed, you can't however adapt the settings any more (as in the more flexible case of geometry generator, where you can always change settings, however the geometries can not be used for further processing).
How to do it:
Menu Processing / Toolbox / Geometry by expression
Output geometry type
, set the kind of geoemtry you want to create (see above for details)Geometry expression
, introduce the expression you want to use for generating geometries. You can useExpression string builder
(ε
): see details above.Be aware that some functions available in geometry generator are not properly supported when using
Geometry by expression
, e.g. the variable refering to the current layer@layer
or aggregate functions.Screenshot: using![enter image description here](https://i.stack.imgur.com/woNyo.png)
Geometry by expression
, creating the centroid of the line layer. It will be generated as a new point layer:Reference:
Anita Graser: Quick guide to geometry generator symbol layers
Klas Karlsson: QGIS User 0010 - Geometry Generator [video tutorial]
Hamish Campbell: FOSS4G / QGIS Geometry Generators [basic and advanced examples]
QGIS 3.16 Training Manual: Follow Along: Geometry generator symbology
QGIS 3.16 documentation: Geometry by expression
GIS Stackexchange: Questions tagged
Geometry generator