8

The question

Using QGIS expression, it can occur that you want to identify the geometry-type of a layer (point, line or polygon? Multiple or single part?). This can be the case to write an expression that works for different kind of geometry types, using an if-clause - pseudocode: if point, do A, if line, do B, if polygon, do C.

See here for an example (go to the expression at B. Get list of all the angles of a line or polygon).

What I tried

I looked if there is a variable of the kind @layer_type, but there is not. There are variables for @layer_name or @layer_crs, but not for the geometry type of the layer (see screenshot).

As in the example above, a workaround is to find out if a geometry is a polygon or not to use if(num_rings($geometry) is null, 1, 0): returns 0 for polygons, 1 for lines and points.

enter image description here

Babel
  • 71,072
  • 14
  • 78
  • 208

4 Answers4

12

For features use this expression:

string_to_array(geom_to_wkt($geometry),' ')[0]

And then you can get one of WKT geometry type: 'Point', 'LineString', 'Polygon', 'MultiPoint', 'MultiLineString', 'MultiPolygon', 'GeometryCollection' etc.

Taras
  • 32,823
  • 4
  • 66
  • 137
6

Another solution for features using a custom function via the Function Editor

function_editor

from qgis.utils import iface
from qgis.core import *
from qgis.gui import *

@qgsfunction(args='auto', group='Custom') def geomfunction(feature, parent): layer = iface.activeLayer() layer_wkb = layer.wkbType() layer_wkb_type = QgsWkbTypes.displayString(layer_wkb) return layer_wkb_type

Call the above function geomfunction() and get an output

result

Taras
  • 32,823
  • 4
  • 66
  • 137
6

For a layer this is probably the most suitable expression:

layer_property(@layer,'geometry_type')

Check QGIS Documentation for more details.

Please keep in mind, that this expression will return strictly the geometry type of a layer, e.g. it is 'Polygon' even though this layer contain 'MultiPolygon' features in it.

example

Result of using the layer_property() result_1

And result of using the string_to_array(geom_to_wkt($geometry),' ')[0] for features of the same layer result_2

Taras
  • 32,823
  • 4
  • 66
  • 137
  • won't that just be geometry or collection for a mixed geometry layer? – Ian Turton May 25 '21 at 09:58
  • Nice question, I think I need to aggregate my answers now – Taras May 25 '21 at 10:50
  • Three solutions, but just one option to accept - so it's a bit arbitrary which one to select. However, that does not matter, as all are by the same user - thanks, @Taras. – Babel May 25 '21 at 11:21
  • You are welcome, but be careful the one you choose! layer_property will give you simply a Polygon even though you have a MultiPolygon inside of it. Here some additional investigations are needed. – Taras May 25 '21 at 11:24
4

Starting with QGIS version 3.24, forthcoming these days, a new expression has been introduced that does exactly this. It works like geometry_type($geometry), returning the geometry type.

The new geometry_type function returns the high-level type of a geometry (i.e. ‘Point’, ‘Line’ or ‘Polygon’).

From the Changelog for QGIS 3.24.

Taras
  • 32,823
  • 4
  • 66
  • 137
Babel
  • 71,072
  • 14
  • 78
  • 208