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

Splitting a shapefile based on multiple attributes in QGIS

Step-by-Step Guide to Split a Shapefile by Multiple Attributes in QGIS

Splitting shapefiles based on attribute values. While this might sound straightforward with one attribute, what happens when you need to split by two, three, or even more attributes simultaneously? Enter the realm of multi-attribute splits in QGIS. This blog post delves deep into the nuances of this process, offering both manual and automated solutions to streamline your GIS workflows. Whether you’re a seasoned GIS analyst or a beginner just getting your feet wet, this guide promises insights that will elevate your data processing game.

Keep up to date with all things geospatial, Listen to our podcast!

Step-by-Step Guide to Split a Shapefile by Multiple Attributes in QGIS (Without Python):

1. Open QGIS:

Launch the QGIS application on your computer.

2. Load Your Shapefile:

  • Go to Layer > Add Layer > Add Vector Layer.
  • Browse to the location of your shapefile and select it.
  • Click Open to add it to the QGIS canvas.

3. Create a New Combined Attribute:

Before splitting the shapefile, you’ll need to create a new attribute that combines the values of the attributes you want to split by.

  • Open the attribute table of your shapefile by right-clicking on the layer in the Layers panel and selecting Open Attribute Table.
  • Click on the Open Field Calculator button (looks like an abacus).
  • In the Field Calculator:
  • Check Create a new field.
  • Name the new field (e.g., “Combined”).
  • Set the output field type to Text.
  • In the Expression box, combine your attributes using the || operator. For example, if you’re combining attributes named “tube”, “strand”, and “port”, your expression might look like: "tube" || '_' || "strand" || '_' || "port".
  • Click OK to create the new combined attribute.

4. Split the Shapefile Using the New Attribute:

  • Go to Processing > Toolbox.
  • In the search bar of the Toolbox, type “Split vector layer” and select the tool.
  • For Input layer, choose your shapefile.
  • For Unique ID field, select the new combined attribute you created (e.g., “Combined”).
  • Choose a directory for Output directory for split layers.
  • Click Run.

5. Check the Output:

Navigate to the output directory you specified. You should see separate shapefiles for each unique combination of the attributes you specified.

6. Load the Split Shapefiles (Optional):

If you want to view the split shapefiles in QGIS:

  • Go to Layer > Add Layer > Add Vector Layer.
  • Browse to the location of the split shapefiles and select the ones you want to view.
  • Click Open to add them to the QGIS canvas.

7. Save Your Project (Optional):

To save your QGIS project with all the layers:

  • Go to File > Save.
  • Choose a location and name for your project file.
  • Click Save.

That’s it! You’ve successfully split a shapefile by multiple attributes in QGIS without using a Python script.

Step-by-Step Guide to Split a Shapefile by Multiple Attributes in QGIS (with Python)

1. Open QGIS:

Launch the QGIS application on your computer.

2. Load Your Shapefile:

  • Go to Layer > Add Layer > Add Vector Layer.
  • Browse to the location of your shapefile and select it.
  • Click Open to add it to the QGIS canvas.

3. Open the Python Console:

  • Navigate to Plugins in the top menu.
  • Select Python Console from the dropdown. This will open a console at the bottom of the QGIS window.

4. Prepare the Script:

Copy the following script:

from qgis.core import QgsVectorLayer, QgsVectorFileWriter

# Load the shapefile
layer = QgsVectorLayer('YOUR_SHAPEFILE_PATH', 'layer_name', 'ogr')

# Define the attributes you want to split by
attributes = ['ATTRIBUTE1', 'ATTRIBUTE2', 'ATTRIBUTE3']

unique_combinations = set()
for feature in layer.getFeatures():
    values = tuple(feature[attr] for attr in attributes)
    unique_combinations.add(values)

for combination in unique_combinations:
    expression = ' AND '.join(f'"{attr}" = \'{val}\'' for attr, val in zip(attributes, combination))
    layer.selectByExpression(expression)
    output_path = f'YOUR_OUTPUT_DIRECTORY/{combination}.shp'
    QgsVectorFileWriter.writeAsVectorFormat(layer, output_path, "utf-8", layer.crs(), "ESRI Shapefile", onlySelected=True)

print("Splitting completed!")

5. Modify the Script:

  • Replace YOUR_SHAPEFILE_PATH with the path to your shapefile.
  • Replace ATTRIBUTE1, ATTRIBUTE2, and ATTRIBUTE3 with the names of the attributes you want to split by.
  • Replace YOUR_OUTPUT_DIRECTORY with the directory where you want to save the split shapefiles.

6. Run the Script:

  • Paste the modified script into the Python Console in QGIS.
  • Press Enter to execute the script.

7. Check the Output:

Navigate to the output directory you specified in the script. You should see separate shapefiles for each unique combination of the attributes you specified.

8. Load the Split Shapefiles (Optional):

If you want to view the split shapefiles in QGIS:

  • Go to Layer > Add Layer > Add Vector Layer.
  • Browse to the location of the split shapefiles and select the ones you want to view.
  • Click Open to add them to the QGIS canvas.

9. Save Your Project (Optional):

To save your QGIS project with all the layers:

  • Go to File > Save.
  • Choose a location and name for your project file.
  • Click Save.

That’s it! You’ve successfully split a shapefile by multiple attributes in QGIS using PyQGIS.

Frequently asked questions:

Why would I need to split a shapefile by multiple attributes?

Answer: Splitting a shapefile by multiple attributes allows users to create separate datasets based on unique combinations of attribute values. This can be useful in various scenarios, such as:

  • Analyzing or visualizing data for specific attribute combinations separately.
  • Sharing or distributing data subsets to different teams or stakeholders.
  • Preparing data for specific tasks where only certain attribute combinations are relevant.

Is there a limit to the number of attributes I can use to split a shapefile in QGIS?

Answer: Technically, there’s no strict limit on the number of attributes you can use to split a shapefile in QGIS. However, the number of resulting layers can grow exponentially with each added attribute, which might lead to performance issues or become unwieldy for manual management.

Will splitting the shapefile by multiple attributes affect the original data?

Answer: No, splitting a shapefile by multiple attributes in QGIS creates new separate files based on the specified attributes. The original shapefile remains unchanged and intact.

How can I automate the process of splitting by multiple attributes if I have to do it frequently?

Answer: For frequent or repetitive tasks, you can use PyQGIS scripts to automate the process. By writing a custom script, you can specify the attributes and conditions for splitting and run the script whenever needed. Additionally, QGIS allows for batch processing, which can be set up to run specific tasks on multiple files.

What file format will the split layers be saved in? Can I choose a different format?

Answer: By default, the split layers are saved as ESRI Shapefiles (.shp). However, when using tools in QGIS, you often have the option to choose from various output formats, such as GeoJSON, KML, GPKG, and more, depending on the specific tool and your requirements.

Is there a way to merge the split layers back into a single shapefile if needed?

Answer: Yes, you can merge the split layers back into a single shapefile using the Merge Vector Layers tool in QGIS. This tool allows you to combine multiple vector layers of the same geometry type into a single layer.

How can I ensure that the split layers retain all the attribute information from the original shapefile?

Answer: When you split a shapefile using QGIS tools, all attribute information for the selected features is retained in the split layers. It’s always a good practice to check a few of the split layers’ attribute tables to ensure data integrity. If using scripts or plugins, ensure they are designed to retain all attribute data.

What’s the difference between splitting a shapefile by attributes and filtering a shapefile by attributes?

Answer: Splitting a shapefile by attributes creates new, separate datasets (files) based on the specified attribute values. In contrast, filtering a shapefile by attributes temporarily hides features that don’t meet the specified criteria, allowing you to view a subset of the data within the same file. Filtering doesn’t create new datasets; it merely changes the view within the current dataset.

Can I split a shapefile by multiple attributes based on specific criteria or conditions (e.g., only split where “attribute1” is greater than a certain value)?

Answer: Yes, you can apply specific criteria or conditions when splitting a shapefile. This would typically involve first using the “Select by Expression” tool to select features based on your criteria. Once the desired features are selected, you can then proceed to split the shapefile based on the other attributes as needed.

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.