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

How to merge multiple vector layers in QGIS

merging multiple vector layers

To merge multiple vector layers into one layer in QGIS, you can use the Merge Vector Layers tool. Here’s how:

  1. Open QGIS and add the vector layers that you want to merge to the map canvas.
  2. Click on the Processing menu and select Toolbox.
  3. In the toolbox window, search for the Merge Vector Layers tool and double-click on it to open the tool.
  4. In the Merge Vector Layers dialog, select the input layers that you want to merge. You can select multiple layers by holding the Ctrl key while clicking on the layer names.
  5. Specify the output layer name and the output format.
  6. Click Run to execute the tool.

The Merge Vector Layers tool will create a new layer that is the result of merging the selected input layers. The new layer will contain all of the features from the input layers, and the attributes from the input layers will be combined into a single attribute table.

Note: If the input layers have different attributes or geometry types, the merged layer may have null values for some attributes or may have a mixed geometry type.

more ways of merging multiple vector layers in QGIS

But just in case that doesn’t work for you here are a few more ways of merging multiple vector layers in QGIS

  1. Using the Union tool: This tool combines the features of two or more input layers into a single output layer. It creates a new attribute for each input layer, and the attributes from the input layers are preserved in the output layer.
  2. Using the Data Management Tools > Merge Shapefiles to One tool: This tool merges multiple Shapefiles into a single Shapefile. It does not preserve the attributes from the input layers, so the output Shapefile will only have a single attribute table.
  3. Using the Merge Selected Features tool: This tool merges multiple selected features from the same layer into a single feature. It is useful for combining small, adjacent polygons or line segments into a single larger feature.
  4. Using the Merge Attributes by Location tool: This tool merges the attributes of features that intersect or are within a specified distance of each other. It is useful for combining attributes from multiple layers that have a spatial relationship.
  5. Using the Join Attributes by Location tool: This tool creates a new layer with attributes from two input layers that intersect or are within a specified distance of each other. The input layers are not merged together, but the attributes are combined in the output layer.
  6. Using the Save As... tool: This tool allows you to save a layer as a new layer, and you can choose to merge the selected features from multiple layers into a single layer when saving.

Note: Some of these tools may require the Processing plugin to be installed in QGIS. You can install this plugin by going to the Plugins menu and selecting Manage and Install Plugins.

Using the command line to merge vector layers in QGIS

You can use the ogr2ogr command line tool to merge vector layers in QGIS. ogr2ogr is part of the GDAL library, which is included with QGIS.

Here are the steps to merge vector layers using ogr2ogr:

  1. Open a command prompt or terminal window.
  2. Navigate to the directory where your vector layers are stored.
  3. Use the ogr2ogr tool to merge the vector layers. The basic syntax is as follows:
ogr2ogr -f "output_format" output_file.ext input_file_1.ext input_file_2.ext

Replace output_format with the format of the output file (e.g., ESRI Shapefile, GeoJSON, etc.), output_file.ext with the name and file extension of the output file, and input_file_1.ext and input_file_2.ext with the names and file extensions of the input files.

For example, to merge two Shapefiles called layer1.shp and layer2.shp into a single Shapefile called merged.shp, you would run the following command:

ogr2ogr -f "ESRI Shapefile" merged.shp layer1.shp layer2.shp

This will create a new Shapefile called merged.shp that contains all of the features from the input Shapefiles.

Note: ogr2ogr can only merge vector layers that are in the same format. If you have vector layers in different formats, you can use the ogr2ogr tool to first convert them to the same format, and then merge them using the steps above.

Using use Python to merge vector layers in QGIS

One way to do this is by using the ogr2ogr command line tool in a Python script. Here’s an example of how to use ogr2ogr to merge two Shapefiles using Python:

import subprocess

input_file_1 = "layer1.shp"
input_file_2 = "layer2.shp"
output_file = "merged.shp"

cmd = "ogr2ogr -f 'ESRI Shapefile' {} {} {}".format(output_file, input_file_1, input_file_2)
subprocess.call(cmd, shell=True)

This script will merge the two Shapefiles layer1.shp and layer2.shp into a single Shapefile called merged.shp.

Another way to merge vector layers using Python in QGIS is by using the QGIS Python API (also known as PyQGIS). Here’s an example of how to merge two vector layers using PyQGIS:

from qgis.core import QgsVectorLayer, QgsProject

# Load the input layers
layer1 = QgsVectorLayer("layer1.shp", "Layer 1", "ogr")
layer2 = QgsVectorLayer("layer2.shp", "Layer 2", "ogr")

# Add the input layers to the map
QgsProject.instance().addMapLayers([layer1, layer2])

# Create a list of the input layers
layers = [layer1, layer2]

# Merge the input layers
output_layer = QgsVectorLayer("Polygon?crs=epsg:4326", "Merged", "memory")
output_layer_data_provider = output_layer.dataProvider()
for current_layer in layers:
    output_layer_data_provider.addFeatures(current_layer.getFeatures())

# Add the merged layer to the map
QgsProject.instance().addMapLayer(output_layer)

This script will load the two input layers layer1.shp and layer2.shp, add them to the map, and then merge them into a single memory layer called Merged. The merged layer is then added to the map.

Note: These examples assume that the input layers are in the same format and have the same CRS (Coordinate Reference System). If the input layers are in different formats or have different CRSs, you will need to handle the conversion and reprojection appropriately.

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