Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
podcast
Filter by Categories
ArcGIS Pro
GDAL
GeoJson
Map
Python
QGIS
Uncategorized

Filtering in QGIS: A Comprehensive Guide

Filtering Spatial Data in QGIS

Spatial data analysis often involves filtering and refining datasets to focus on specific features or criteria. QGIS, a popular open-source Geographic Information System (GIS), provides various tools and techniques to perform filtering on both vector and raster datasets.

This blog post, inspired by a recent conversation with a GIS enthusiast, aims to guide you through the process of filtering data in QGIS, including attribute-based filtering, spatial filtering, query-based filtering, and even filtering using Python scripting with PyQGIS. We will also discuss best practices for working with large datasets in QGIS to improve performance and efficiency.

Filtering in QGIS refers to the process of selecting a subset of features or records from a dataset based on specific attribute values, spatial criteria, or expressions. Filtering is useful when you want to focus on a particular group of features or records in your dataset, making it easier to analyze, visualize, or manipulate the data.

Want to stay ahead of the geospatial curve? Listen to our podcast!

In QGIS, there are multiple ways to filter data:

Attribute-based filtering

This involves filtering features based on values in their attribute fields, such as selecting all the features with a population greater than a specific value. You can use expressions with SQL-like syntax to create complex attribute filters.

Attribute-based filtering in QGIS allows you to select a subset of features based on their attribute values. To perform attribute-based filtering, you can use the following steps:

  1. Load your dataset in QGIS: Open QGIS and load the vector layer (shapefile or another supported format) you want to filter.
  2. Open the Layer’s Attribute Table: Right-click on the layer in the “Layers” panel and select “Open Attribute Table” to view the layer’s attributes.
  3. Apply a filter expression: In the attribute table window, click on the “Select by expression” button. This button is typically an icon that looks like a pair of curly brackets {} or a function symbol f(x).
  4. Write a filter expression: In the “Select by Expression” window, you can write an expression to filter features based on their attributes. The expression uses SQL-like syntax. For example, to filter features with a population greater than 5000, you could write: “population” > 5000.You can also use the available functions, operators, and fields listed in the window to build your expression. To test your expression, click the “Test” button. This will show you the number of features that match the expression.
  5. Select the features: Click the “Select” button to apply the filter and select the features that match your expression. The selected features will be highlighted in the attribute table and the map canvas.
  6. Create a new layer with filtered features (optional): If you want to create a new layer containing only the filtered features, right-click the original layer in the “Layers” panel, and select “Export” > “Save Selected Features As.” Choose the format you’d like to save the new layer as, specify a file name and location, and click “OK.”

By following these steps, you can perform attribute-based filtering in QGIS and focus on a subset of features that meet your criteria.

Spatial filtering

Spatial filtering in QGIS allows you to select features based on their spatial relationship with other features or geometries. There are multiple ways to perform spatial filtering, but one common method is using the “Select by Location” tool. Here’s a step-by-step guide on how to do spatial filtering in QGIS using the “Select by Location” tool:

  1. Load your datasets in QGIS: Open QGIS and load the vector layers (shapefiles or other supported formats) you want to work with. For spatial filtering, you’ll typically have at least two layers: a target layer (the layer with the features you want to select) and a reference layer (the layer with the features or geometries you want to use as a spatial filter).
  2. Open the “Select by Location” tool: Go to “Vector” > “Research Tools” > “Select by Location.” This will open the “Select by Location” window.
  3. Set the parameters for the “Select by Location” tool: In the “Select by Location” window, you’ll see several parameters that you need to configure:
    • “Select features from”: Choose the target layer containing the features you want to select.
    • “By comparing to the features from”: Choose the reference layer that you want to use as a spatial filter.
    • “Where the features”: Select the spatial relationship you want to use for the filtering. Examples include “intersect,” “are within,” “touch,” or “are disjoint.” You can choose one or multiple relationships by holding down the Ctrl key (or Cmd key on Mac) while selecting.
    • “Modify current selection by”: Choose how you want to modify the current selection (if any). Options include “creating new selection,” “adding to current selection,” “removing from current selection,” or “selecting from the current selection.”
    • (Optional) “Precision”: Set the precision value for the spatial relationship calculation.
  4. Run the “Select by Location” tool: Click the “Run” button to execute the spatial filtering based on your chosen parameters. The features in the target layer that meet the spatial relationship criteria with the reference layer will be selected.
  5. Create a new layer with filtered features (optional): If you want to create a new layer containing only the selected features, right-click the target layer in the “Layers” panel and select “Export” > “Save Selected Features As.” Choose the format you’d like to save the new layer as, specify a file name and location, and click “OK.”

By following these steps, you can perform spatial filtering in QGIS using the “Select by Location” tool. Other spatial filtering methods, such as using “Select by Polygon,” “Select by Freehand,” or “Select by Radius” tools, can also be applied depending on your specific requirements.

Query-based filtering

Query-based filtering in QGIS typically involves filtering features using SQL queries when working with databases that support SQL queries, such as PostGIS or SQLite. In this example, I’ll show you how to perform query-based filtering using a PostGIS database in QGIS:

  1. Connect to your PostGIS database: To connect to a PostGIS database, go to the “Browser” panel in QGIS, right-click “PostGIS,” and choose “New Connection.” Enter your connection details, such as the name, host, port, database, user, and password, then click “OK.”
  2. Load your dataset from the PostGIS database: Once connected, expand the PostGIS connection in the “Browser” panel, and locate the desired layer. Drag and drop the layer into the “Layers” panel to load it into the map canvas.
  3. Open the DB Manager: Go to “Database” > “DB Manager” in the main menu to open the DB Manager. In the DB Manager, connect to the PostGIS database by expanding “PostGIS” in the left panel and selecting your connection.
  4. Write and execute your SQL query: Click on the “SQL Window” button in the toolbar (or press F2) to open the SQL query window. Write your SQL query to filter features based on attributes or spatial relationships. For example:
SELECT * FROM your_table WHERE population > 5000;
  1. Replace “your_table” with the actual table name from your PostGIS database. To execute the query, click the “Execute” button or press F5. The query result will be displayed in the lower part of the window.
  2. Load the filtered dataset as a new layer: To load the query result as a new layer in QGIS, check the box “Load as a new layer,” and provide a name for the new layer in the “Layer name (prefix)” field, and click the “Load” button. The new layer, containing the filtered features, will be added to the “Layers” panel and displayed in the map canvas.

By following these steps, you can perform query-based filtering in QGIS when working with SQL-supporting databases like PostGIS. The process would be similar for other database types, like SQLite or Spatialite, with minor differences depending on the database system’s SQL syntax and capabilities.

Filtering in Layer Properties

This method allows you to filter features directly at the layer level, which means only the filtered features will be loaded in the map canvas, making the layer lighter and improving performance. To do this, right-click the layer in the “Layers” panel, go to “Properties” > “Source” tab, and enter an expression in the “Query Builder” under the “Provider Feature Filter” section.

Filtering using a layer’s subset string in QGIS allows you to define a filter expression that’s applied directly at the layer level. This means that only the features matching the filter criteria will be loaded in the map canvas, making the layer lighter and improving performance. Here’s how to set a subset string for filtering in the Layer properties:

  1. Load your dataset in QGIS: Open QGIS and load the vector layer (shapefile or another supported format) you want to filter.
  2. Access the Layer properties: Right-click the layer in the “Layers” panel and select “Properties” to open the Layer properties window.
  3. Open the Query Builder: In the Layer properties window, go to the “Source” tab, and find the “Provider Feature Filter” section. Click the “Query Builder” button (an icon that looks like a pair of curly brackets {}). This will open the Query Builder window.
  4. Write a filter expression: In the Query Builder window, you can write an expression to filter features based on their attributes. The expression uses SQL-like syntax. For example, to filter features with a population greater than 5000, you could write: “population” > 5000.You can also use the available operators and fields listed in the window to build your expression. To test your expression, click the “Test” button. This will show you the number of features that match the expression.
  5. Apply the filter expression: Click the “OK” button to apply the filter expression. The Query Builder window will close, and you’ll see your expression in the “Provider Feature Filter” field of the Layer properties window. Click “OK” again to close the Layer properties window and apply the filter to the layer.
  6. View the filtered layer: In the map canvas, you will now see only the features that match your filter criteria. The layer’s attribute table will also display only the filtered features.

By using the layer’s subset string to filter features in QGIS, you can improve performance and focus on a specific subset of your data. Note that this filtering method is applied at the layer level, meaning that any analysis or export performed on the layer will only include the filtered features. To clear the filter, go back to the Layer properties > Source tab > Provider Feature Filter and clear the expression, then click “OK.”

Filtering in QGIS helps you narrow down your dataset to the relevant features, simplifying your analysis, and allowing you to focus on specific aspects of the data.

Can I filter features using Python scripting in QGIS with PyQGIS?

Yes, you can filter features using Python scripting in QGIS with PyQGIS. PyQGIS is the Python library for QGIS that allows you to automate and extend the functionality of QGIS through custom scripts and plugins. To filter features using PyQGIS, you can follow these steps:

  1. Open the Python Console in QGIS: Go to “Plugins” > “Python Console” or press Ctrl+Alt+P to open the Python Console.
  2. Load the layer and get the layer’s reference: If you have already loaded a layer in QGIS, you can get its reference using the layer name or ID. Alternatively, you can load a layer directly from your script. Here’s an example of loading a layer using its path:
layer_path = 'path/to/your/shapefile.shp'
layer = QgsVectorLayer(layer_path, 'layer_name', 'ogr')
QgsProject.instance().addMapLayer(layer)

Define a filter expression: Create a filter expression using the QgsExpression class. This expression will be used to filter features based on attribute values. For example, to filter features with a population greater than 5000, you can write:

expression_str = '"population" > 5000'
expression = QgsExpression(expression_str)

Apply the filter expression to select features: To apply the filter expression and select the features that match the criteria, you can use the QgsFeatureRequest class and iterate over the matching features. Here’s an example:

request = QgsFeatureRequest(expression)
selected_features = [feature for feature in layer.getFeatures(request)]

# Select features in the layer
layer.selectByIds([f.id() for f in selected_features])

Create a new layer with filtered features (optional): If you want to create a new layer containing only the filtered features, you can use the following code snippet:

# Create a new memory layer with the same CRS, fields, and geometry type as the original layer
filtered_layer = QgsVectorLayer(f"{layer.geometryType()}?crs={layer.crs().authid()}&index=yes", 'filtered_layer', 'memory')
filtered_layer_data = filtered_layer.dataProvider()

# Add the same fields as the original layer
filtered_layer_data.addAttributes(layer.fields())
filtered_layer.updateFields()

# Add filtered features to the new layer
filtered_layer_data.addFeatures(selected_features)
filtered_layer.updateExtents()

# Add the new filtered layer to the project
QgsProject.instance().addMapLayer(filtered_layer)

You can filter features using Python scripting with PyQGIS in QGIS. You can modify the filter expression or combine multiple expressions as needed for your specific use case. This approach allows you to automate and customize the filtering process, making it more efficient for advanced GIS workflows.

How do I combine attribute-based and spatial filtering in QGIS?

To combine attribute-based and spatial filtering in QGIS, you can follow these steps:

  1. Load your datasets in QGIS: Open QGIS and load the vector layers (shapefiles or other supported formats) you want to work with. You’ll typically have at least two layers: a target layer (the layer with the features you want to filter) and a reference layer (the layer with the features or geometries you want to use as a spatial filter).
  2. Perform attribute-based filtering: Follow the instructions for attribute-based filtering mentioned earlier in this conversation. Use the “Select by expression” button in the attribute table to filter features based on their attributes.
  3. Perform spatial filtering on the selected features: After filtering the features based on their attributes, you can use the “Select by Location” tool to perform spatial filtering on the already selected features.
    • Go to “Vector” > “Research Tools” > “Select by Location.”
    • In the “Select by Location” window, set the parameters:
      • “Select features from”: Choose the target layer containing the features you previously filtered by attributes.
      • “By comparing to the features from”: Choose the reference layer to use as a spatial filter.
      • “Where the features”: Select the spatial relationship for the filtering.
      • “Modify current selection by”: Choose “selecting from the current selection” to apply the spatial filter only on the features that are already selected.
    • Click the “Run” button to perform spatial filtering on the attribute-filtered features.
  4. Save filtered features as a new layer (optional): If you want to create a new layer containing only the features that meet both attribute-based and spatial filtering criteria, right-click the target layer in the “Layers” panel, select “Export” > “Save Selected Features As,” choose the format, specify a file name and location, and click “OK.”

You can combine attribute-based and spatial filtering in QGIS. First, filter the features based on their attributes, and then perform spatial filtering on the resulting subset. This allows you to refine your selection based on multiple criteria and focus on a specific set of features that meet both attribute and spatial requirements.

How can I filter features based on their relationship with multiple other layers or geometries?

To filter features based on their relationship with multiple other layers or geometries in QGIS, you can use the “Select by Location” tool iteratively or combine multiple spatial queries using the “Geometry by expression” tool. Here’s how to do this:

Option 1: Iterative “Select by Location”

  1. Load your datasets in QGIS: Open QGIS and load the vector layers (shapefiles or other supported formats) you want to work with. You’ll have one target layer and multiple reference layers.
  2. Perform attribute-based filtering (optional): If you need to apply attribute-based filtering on the target layer, follow the attribute-based filtering steps mentioned earlier in this conversation.
  3. Perform spatial filtering iteratively: Run the “Select by Location” tool iteratively for each reference layer.
    • Go to “Vector” > “Research Tools” > “Select by Location.”
    • For the first reference layer, choose “creating new selection” in the “Modify current selection by” parameter.
    • For subsequent reference layers, choose “adding to the current selection,” “removing from the current selection,” or “selecting from the current selection,” depending on your requirements.
    • Repeat this process for each reference layer, modifying the current selection accordingly.
  4. Save the filtered features as a new layer (optional): To create a new layer containing only the filtered features, right-click the target layer in the “Layers” panel, select “Export” > “Save Selected Features As,” choose the format, specify a file name and location, and click “OK.”

Option 2: Geometry by expression

  1. Create a merged geometry from multiple reference layers:
    • Go to “Processing” > “Toolbox” and search for “Geometry by expression.”
    • Select your reference layer in the “Input layer” parameter.
    • In the “Output geometry type” parameter, choose the appropriate geometry type based on your reference layer (e.g., “Polygon”).
    • Write an expression using the combine or collect function to merge geometries from multiple layers. For example:
combine(collect(geometry(get_feature('reference_layer1', 'attribute', 'value'))), collect(geometry(get_feature('reference_layer2', 'attribute', 'value'))))
  • Replace ‘reference_layer1’ and ‘reference_layer2’ with the actual layer names, and ‘attribute’ and ‘value’ with appropriate attribute names and values to identify the desired features from each layer.
  • Run the tool and add the resulting layer to your project.

You can filter features based on their relationship with multiple other layers or geometries in QGIS. This allows you to perform more complex spatial queries, combining multiple spatial relationships, and focusing on a specific set of features that meet your criteria.

Can I filter raster data in QGIS? If so, how?

Yes, you can filter raster data in QGIS. There are different types of filters you can perform on raster data, including filtering based on pixel values or applying spatial filters. Here’s an overview of these methods:

1. Filtering based on pixel values:

You can filter a raster dataset based on its pixel values using the Raster Calculator or by reclassifying the raster. Here’s how to do this with the Raster Calculator:

  1. Load your raster dataset in QGIS.
  2. Go to “Raster” > “Raster Calculator” in the main menu to open the Raster Calculator.
  3. In the Raster Calculator, you can write an expression to filter the raster based on pixel values. For example, if you want to keep only the pixels with values greater than 100, you can write: "your_raster@1" > 100 Replace “your_raster” with the actual raster name from the “Raster bands” list.
  4. Choose an output file format and specify an output file name and location.
  5. Click “OK” to run the Raster Calculator. The output raster will be added to the map canvas, with pixels meeting the criteria set to 1 and other pixels set to 0 (no data).

2. Spatial filtering:

You can apply spatial filtering to raster data by cropping or masking the raster based on a vector layer or specific geometry.

  1. Load your raster dataset and a vector layer in QGIS.
  2. Go to “Raster” > “Extraction” > “Clip raster by mask layer” to open the Clip Raster by Mask Layer tool.
  3. In the tool window, choose the input raster layer and the mask layer (the vector layer that you want to use for spatial filtering).
  4. Specify an output file format, file name, and location.
  5. Click “Run” to perform spatial filtering. The output raster will be added to the map canvas, cropped, or masked based on the vector layer’s geometries.

Alternatively, you can use the “Clip raster by extent” tool to filter the raster based on a specific bounding box.

These are just a few examples of how you can filter raster data in QGIS. There are various other tools and methods available for different types of filtering, such as applying convolution filters for edge detection, smoothing, or sharpening, depending on your specific requirements and goals.

What are some best practices for filtering large datasets in QGIS to improve performance?

Working with large datasets in QGIS can be challenging due to performance and memory limitations. However, you can follow some best practices to improve performance and efficiency while filtering large datasets in QGIS:

  1. Use indexed formats and databases: When working with large datasets, using indexed file formats like GeoPackage or spatial databases like PostGIS can significantly improve performance. They enable faster access and querying of spatial data compared to non-indexed formats like shapefiles.
  2. Create spatial indexes: If your dataset doesn’t have spatial indexing, create one. Spatial indexes can significantly speed up spatial queries and operations. You can create a spatial index for a layer by right-clicking the layer in the Layers panel, going to “Properties” > “Source,” and clicking the “Create Spatial Index” button.
  3. Filter at the provider level: When possible, use the layer’s subset string to filter data at the provider level. This approach ensures that only the features meeting the filter criteria are loaded, reducing memory usage and improving performance. To apply a subset string, right-click the layer, go to “Properties” > “Source,” and enter an SQL-like expression in the “Provider Feature Filter” field.
  4. Use attribute table filters: When working with large datasets, avoid selecting and exporting features to a new layer unless necessary. Instead, use the attribute table filters to create a temporary filtered view, which is faster and more memory-efficient.
  5. Use QGIS expressions efficiently: When writing expressions for filtering, use the available functions and operators efficiently. For example, use the IN operator to filter based on multiple attribute values, which is more efficient than using multiple OR conditions.
  6. Limit rendering options: Limit the use of complex symbology, labeling, or styles when working with large datasets. Turning off unnecessary rendering options or using simplified styles can improve the performance of QGIS.
  7. Split the dataset: If the dataset is too large to work with efficiently, consider splitting it into smaller parts based on attributes, spatial extent, or a grid. Work with smaller subsets of the data and merge the results when needed.
  8. Use multi-threaded rendering: Ensure that multi-threaded rendering is enabled in QGIS to speed up rendering. Go to “Settings” > “Options” > “Rendering” and check the “Render layers in parallel using many CPU cores” option.
  9. Use the 64-bit version of QGIS: If you’re working with large datasets, it’s recommended to use the 64-bit version of QGIS, which can access more memory than the 32-bit version.
  10. Adjust memory usage settings: If you experience performance issues while working with large datasets, consider adjusting QGIS memory usage settings. Go to “Settings” > “Options” > “System” and tweak settings like “Cache size” and “Max rendered map tiles.”

You can improve the performance of QGIS when filtering and working with large datasets. This can help you work more efficiently and manage your system’s resources effectively.

About the Author
I'm Daniel O'Donohue, the voice and creator behind The MapScaping Podcast ( A podcast for the geospatial community ). With a professional background as a geospatial specialist, I've spent years harnessing the power of spatial to unravel the complexities of our world, one layer at a time.

Leave a Reply