QGIS Geometry – Using QGIS Expressions to Create Geometries: Difference Between Geometry Generator and Geometry by Expression

expressiongeometry-generatorqgis

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 or Geometry by expression:

Geometry generator: Open Layer Styling panel / add additional symbol layer (green +) / Symbol layer type: Geometry generator / set appropriate Geometry type (point/line/polygon) / add your expression

Geometry by expression: Menu Processing / Toolbox / Geometry by expression / choose input layer / set appropriate Output geometry type/ add your expression

Overview Differences: Table Geometry generator vs. Geometry by expression - highlighted in green which option is better fit for the corresponding task: enter image description here


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:

  1. Open Layer Styling panel and add an additional symbol layer by hitting the green plus symbol: +.
  2. Than set the Symbol layer type to Geometry generator.
  3. Select a 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 select Polygon / MultiPolygon, irrespective if you buffer a point, line or polygon layer. If you create a centroid, however, always set Geometry type to Point / MultiPoint.
  4. Add the expression. You might use the epsilon (ε) button to open the powerful Expression string builder: see details about it in the documentation

Screenshot: how to use 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 expression buffer ($geometry, 100). Change the shape of the black line or the buffer-distance from 100 to any other value to see how the orange buffer changes: enter image description here

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:

  1. Go to Menu Processing / Toolbox / Geometry by expression
  2. Choose the input layer: the layer the expression is apllied to
  3. As Output geometry type, set the kind of geoemtry you want to create (see above for details)
  4. In the field Geometry expression, introduce the expression you want to use for generating geometries. You can use Expression 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 Geometry by expression, creating the centroid of the line layer. It will be generated as a new point layer: enter image description here


Reference:

Related Question