Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
podcast
Filter by Categories
Galleries
Print Category 1
Print Category 2
Uncategorized

How to select by expression in QGIS

Selecting by expression in QGIS – What does it mean?

Selecting by expression in QGIS means using a specific set of rules or conditions to select a subset of features from a layer based on the attribute data or geometry of the features.

This process allows you to create targeted selections of features based on specific criteria, rather than selecting features manually or by drawing a bounding box around them.

An expression is a combination of operators, field values, and functions that are used to define the selection criteria. The expression is evaluated for each feature in the layer, and the features that meet the criteria specified in the expression are selected.

Click here for an in-depth introduction to QGIS

Operators, Field Values, and Functions

In QGIS, operators, field values, and functions are used to create expressions that can be used to select features based on specific criteria.

  1. Operators are symbols or keywords that are used to compare values or perform logical operations. Examples of operators include: “=”, “>”, “<“, “LIKE”, “INTERSECTS”, and “IN”.
  2. Field values are the data stored in the attributes of a feature. They can be numbers, strings, or dates, and are used to identify the feature and provide information about it. For example, a field named “name” might have a value of “John Smith” for a specific feature.
  3. Functions are predefined commands or procedures that perform specific operations. They can be used to modify field values, perform calculations, or manipulate geometries. For example, the “length” function can be used to calculate the length of a line feature, while the “GeometryFromWKT” function can be used to create a geometry from a Well-Known Text (WKT) string.

Here is a table that describes the difference between operators, field values, and functions in QGIS:

OperatorField valuesFunctions
Symbols or keywords used to compare values or perform logical operationsData stored in the attributes of a featurePre-defined commands or procedures that perform specific operations
Examples: “=”, “>”, “<“, “LIKE”, “INTERSECTS”, “IN”Examples: numbers, strings, datesExamples: “length”, “GeometryFromWKT”, “round”, “concatenate”
Used to compare the valuesUsed to identify the feature and provide information about itUsed to modify field values, perform calculations, or manipulate geometries

Click here for an in-depth discussion on the Structured Query Language ( SQL ) in GIS

It’s important to note that the actual operators, field values, and functions that are available to you will depend on the data and fields in your layer, and the specific selection criteria you need.

When creating an expression in QGIS, you can use operators to compare field values, use functions to manipulate those values, and combine them with logical operators (such as “AND”, “OR”, and “NOT”) to create complex expressions that can be used to select features based on specific criteria.

Using operators when selecting by expression in QGIS

This table shows some examples of operators that you can use in QGIS to select features based on attribute data. You can use these operators in combination with field values, constants, and functions to create expressions that match your specific selection criteria.

OperatorDescriptionExample
=Equal to“age” = 30
>Greater than“income” > 50000
<Less than“year_built” < 2000
>=Greater than or equal to“area” >= 1000
<=Less than or equal to“Temperature” <= 30
LIKEContains a specific substring“address” LIKE ‘%Street%’
INTERSECTSGeometry intersection“Geometry” INTERSECTS GeometryFromWKT(‘POINT(10 10)’)
INIf a value is found in a set“field_name” IN selected_attribute(“layer_name”)
ANDThe logical operator used to combine two or more conditions“income” > 50000 AND “gender” = ‘male’
ORThe logical operator used to combine two or more conditions, returns true if at least one condition is met“property_type” = ‘apartment’ OR “property_type” = ‘condo’
NOTThe logical operator used to negate a condition“zipcode” != ‘98101’

This table describes some common geometry operators and how they work in QGIS:

OperatorDescriptionExample
INTERSECTSReturns true if the features’ geometry intersects with the specified geometry.“geometry” INTERSECTS GeometryFromWKT(‘POLYGON((10 10, 20 15, 10 20, 10 10))’)
WITHINReturns true if the features’ geometry is completely contained within the specified geometry.“Geometry” WITHIN GeometryFromWKT(‘POLYGON((10 10, 20 15, 10 20, 10 10))’)
CONTAINSReturns true if the features’ geometry completely contains the specified geometry.“Geometry” CONTAINS GeometryFromWKT(‘POINT(10 10)’)
DISJOINTReturns true if the features’ geometry does not intersect with the specified geometry.“Geometry” DISJOINT GeometryFromWKT(‘POINT(10 10)’)
TOUCHESReturns true if the features’ geometry touches the specified geometry.“geometry” TOUCHES GeometryFromWKT(‘POLYGON((10 10, 20 15, 10 20, 10 10))’)
CROSSESReturns true if the features’ geometry crosses the specified geometry.“Geometry” CROSSES GeometryFromWKT(‘LINESTRING(10 10, 20 20)’)
OVERLAPSReturns true if the features’ geometry overlaps the specified geometry.“geometry” OVERLAPS GeometryFromWKT(‘POLYGON((10 10, 20 15, 10 20, 10 10))’)

These operators can be used in combination with logical operators and field values to create more complex expressions for selecting features based on their spatial characteristics. This table should be helpful for you to understand how these geometry operators work in QGIS, and how you can use them for your selection.

Building complex expressions in QIGS

Logical operators such as “AND”, “OR” and “NOT” can be used in combination with geometry operators to create more complex expressions for selecting features in QGIS.

For example, you can select all features that have a specific attribute value and also intersect with a specific geometry: “field_name” = ‘value’ AND “geometry” INTERSECTS GeometryFromWKT(‘POLYGON((10 10, 20 15, 10 20, 10 10))’)

You can also use the “OR” operator to select features that meet one of the multiple conditions. For example, you can select all features that have a specific attribute value or fall within a specific geometry: “field_name” = ‘value’ OR “geometry” WITHIN GeometryFromWKT(‘POLYGON((10 10, 20 15, 10 20, 10 10))’)

The “NOT” operator negates the conditions. For example, you can select all features that do not have a specific attribute value and are not within a specific geometry: “field_name” != ‘value’ AND “geometry” NOT WITHIN GeometryFromWKT(‘POLYGON((10 10, 20 15, 10 20, 10 10))’)

Here is a table that describes some common geometry functions and examples of how they might be used in a complex expression in QGIS:

FunctionDescriptionExample
areaCalculates the area of a geometry in the units of the spatial reference system.area(“geometry”) > 10
lengthCalculates the length of a geometry in the units of the spatial reference system.length(“geometry”) < 100
centroidReturns the centroid of a geometry.“geometry” INTERSECTS centroid(GeometryFromWKT(‘POLYGON((10 10, 20 15, 10 20, 10 10))’))
xReturn the x-coordinate of a pointx( “geometry”) < 20
yReturn the y-coordinate of a pointy(“geometry”) > 50
bufferReturns a geometry that represents all points whose distance from this geometry is less than or equal to the distance.“geometry” INTERSECTS buffer(GeometryFromWKT(‘POINT(10 10)’), 20)

These are examples of how you can use built-in geometry functions in QGIS to create complex expressions that select features based on specific spatial characteristics, such as area, length, position, and distance.

You can use logical operators and field values together with these functions to create a more complex expression, for example, you can select all the polygons that have an area greater than 10 square units, and their centroid is within a specific geometry:

Copy codearea("geometry") > 10 AND "geometry" INTERSECTS centroid(GeometryFromWKT('POLYGON((10 10, 20 15, 10 20, 10 10))'))

Where can you use expressions in QGIS?

In QGIS, expressions can be used in several places

  1. Select by expression tool: You can use the “Select by expression” option in the layer properties or the “Select” menu in the attribute table to apply an expression to a layer. You can use this option to select features based on attribute data or spatial characteristics.
  2. Field calculator: You can use the field calculator to create new fields or update existing fields with values calculated from an expression. You can use this option to update the values of specific fields based on the values of other fields or based on a specific calculation.
  3. Vector layer properties: you can use the expression in the Layer properties, for example in the “Label with” option you can use an expression to label features based on the values of specific fields or based on a specific calculation.
  4. Virtual fields: you can use the virtual fields option in the attribute table to create new fields that are calculated using an expression. The values of these fields are calculated on the fly and are not stored in the attribute table.
  5. Processing tool: some of the Processing tools in QGIS accept expressions as input, for example, the “extract by expression” allows you to extract features from a layer based on a specific expression.
  6. Layer Properties: Under the “General” tab, you will find the “Filter” option. This allows you to apply a filter expression to the current layer.
  7. Layer Styling: You can use the filter function to filter features when styling a vector layer

In Conclusion

Expressions can be used to select features based on attribute data, spatial characteristics, or a combination of both. Attribute operators can be used to compare field values, logical operators can be used to combine multiple conditions, and functions can be used to perform specific operations on field values or manipulate geometries.

Expressions are a powerful feature in QGIS that allows you to quickly and easily select features based on specific criteria, making it easier to analyze, visualize, and edit your data. They are particularly useful when working with large datasets and when you need to create targeted selections of features based on specific attribute data or spatial characteristics.