
QGIS Spatial Join: Basics and Practice
What you'll learn
- The basic concept of spatial joins in GIS
- How to perform a spatial join in QGIS
Recommended for
- Anyone who wants to understand the concept of spatial joins
- Anyone who wants to learn the steps for running a spatial join in QGIS
- Anyone who has struggled with complex spatial relationships and could not get a spatial join to work
Introduction
When you analyze data in GIS, you sometimes need to combine data from different layers. For example, when you want to count the hospitals in each municipality, you need to join layers based on location.
A spatial join combines layers by using this kind of spatial relationship. Unlike a simple join on attribute values, it uses the spatial relationship between features (such as intersect, contain, or equal) as the criterion.
This article covers spatial joins from the basic concept to the steps in QGIS, with a worked example.
What is a spatial join?
A spatial join is an operation that checks the spatial relationship between two features and joins the attributes of the features that meet a condition.
Suppose you want to count the hospitals in each municipality. You have an administrative area layer that represents the shape of each municipality as a polygon, and a layer of medical institutions represented as points. The medical institution layer, however, has no attribute for the municipality each institution is in. A spatial join solves this. It checks whether each medical institution point falls inside an administrative area polygon and adds the name of the municipality to the attributes of the medical institution layer. You can then see at a glance which medical institutions are in which municipality.

Types of spatial relationships
This section explains spatial relationships, the key concept in spatial joins.
A spatial join joins layer attributes by checking how two features are positioned relative to each other. QGIS calls this criterion, the relationship between features, a spatial relationship.
It is easy to mix up which layer is joined to which, so this section uses the terms from the QGIS dialog: Join to features in and By comparing to. The Join to features in layer is the layer you want to add attributes to, and the By comparing to layer is the layer you get the attributes from.
For example, to add municipality names from an administrative area layer to a medical institution layer, specify the medical institution layer as Join to features in and the administrative area layer as By comparing to.

When you run a spatial join in QGIS, you can choose from the following eight spatial relationships.
Intersect
The intersect relationship is satisfied when a feature in the Join to features in layer and a feature in the By comparing to layer overlap even partially, or touch each other. It includes the contain, are within, touch, overlap, and cross relationships.

Contain and are within
The contain relationship is satisfied when the area of a feature in the Join to features in layer contains a feature in the By comparing to layer.
Conversely, the are within relationship applies when a feature in the Join to features in layer is contained in a feature in the By comparing to layer. The roles of the Join to features in and By comparing to layers are reversed compared with contain.
For example, suppose you have a library layer and an administrative area layer. To add the municipality each library is in to the library layer, use are within. To add information about the libraries in each municipality to the administrative area layer, use contain.

Disjoint
The disjoint relationship is satisfied when the features are apart from each other.

Equal
The equal relationship is satisfied when the features have the same shape and the same position.

Touch
The touch relationship is satisfied when a feature in the Join to features in layer and a feature in the By comparing to layer meet along a boundary line. This relationship is useful when you want to use administrative area data to add adjacent municipalities or prefectures to the attributes.

Overlap
The overlap relationship is satisfied when two features of the same dimension partly overlap each other.
For example, you can use it to analyze only the buildings that overlap multiple municipalities.

Cross
The cross relationship is satisfied when parts of two features overlap. It is mainly used to compare lines with lines, or lines with polygons. For example, you can use it to answer questions such as “What are the names of the rivers that cross the road, and how many are there?”

Compared with intersect, cross is a more restrictive relationship. For two lines, the intersection must be a point. For a line and a polygon, the intersection must be a line.

Tips for understanding spatial relationships
A simple way to understand spatial relationships is to ask:
“How is the Join to features in layer positioned relative to the By comparing to layer?”
For example, to add administrative area information to medical institutions, ask, “Is the medical institution point within the administrative area polygon?” The spatial relationship to choose in this case is are within.
For details on each spatial relationship, see the official documentation.
Spatial join in practice
As a worked example, this article adds municipality values to railway station data and then filters the stations of a specific municipality.
Get the data
First, get the data and add it to QGIS. Both the municipality data and the railway station data come from the National Land Numerical Information Download Service: the “Administrative Area Data (Tokyo)” and the “Railway Data.”
The Railway Data contains several Shapefiles, so use the railway station file that ends in _Station.shp. In National Land Numerical Information, stations are represented as lines, so this article uses data converted to points with the Centroids Processing tool.
For how to add National Land Numerical Information data to QGIS, see the following article.
Once you have added the data and your project looks like the image below, you are ready to go.

Join administrative area data to railway station data
First, check the attribute table of the railway station layer. It has attributes such as the line name, operating company, and station name, but no attribute for the municipality each station is in.

Next, check the attribute table of the administrative area layer. The municipality names are stored in the N03_004 field.

So use a spatial join to add the municipality information to the attributes of the railway station layer. You can open the spatial join dialog in either of these ways:
- From the menu bar, select Vector → Data Management Tools → Join attributes by location.
- From the Processing Toolbox, select Vector general → Join attributes by location.

In the dialog, set the options as follows:
- Join to features in: select the
鉄道駅layer (railway stations). You want to add the attributes of the administrative area layer to the attributes of the railway station layer. - Where the features (geometric predicate): check are within. The condition you want is whether a railway station point is within an administrative area polygon.
- By comparing to: select the
行政区域layer (administrative areas). - Fields to add (leave empty to use all fields): click the ... button and select N03_004. Of the attributes in the administrative area layer, you want to join the municipality name (
N03_004). - Join type: select Take attributes of the first matching feature only (one-to-one). This example uses railway stations converted to points, so no station belongs to more than one administrative area.
- When the settings are complete, click the Run button.

Open the attribute table of the joined layer to check the result. The attribute for the municipality name (the field you selected in step 4) has been added to the railway station data.

The spatial join completed successfully.
Finally, use the filter to show only the stations in a specific municipality. For example, the filter condition "N03_004" = '大田区' extracts only the stations located in Ota ward. For more on how to use filters, see the following article.

Join types
The Join type option has three choices. The result differs depending on which you select, so choose the one that fits your analysis.
Create separate feature for each matching feature (one-to-many)
When a feature in the Join to features in layer matches several features in the By comparing to layer, this method outputs each match as a separate new feature. As a result, each feature in the Join to features in layer is created as many times as it has matches in the By comparing to layer, so the number of output features can increase.

Take attributes of the first matching feature only (one-to-one)
For each feature in the Join to features in layer, this method joins only the attributes of the first matching feature in the By comparing to layer (the first one QGIS finds). As a result, the number of output features equals the number of features in the Join to features in layer.
Be careful when there are multiple possible matches: you cannot predict which feature in the By comparing to layer is joined.
Take attributes of the feature with largest overlap only (one-to-one)
For each feature in the Join to features in layer, this method joins only the attributes of the feature in the By comparing to layer that overlaps it the most spatially (the largest overlapping area or length). As a result, the number of output features equals the number of features in the Join to features in layer.
Conclusion
This article explained spatial joins, from the basic concept to the actual steps.
A spatial join is an important operation that underlies many kinds of GIS analysis. By building on the basic steps in this article, you can take on more complex spatial analysis. Once you master spatial joins, the possibilities for analyzing data based on location expand greatly.
Give it a try.


QGIS LAB is a comprehensive information hub for QGIS, the open-source GIS software. Under the concept of “Geospatial for Greater Good,” we share the knowledge and skills to open up the world through location data.


