Understand various types of spatial join relationships
Recognize different types of spatial joins
Understand how table relationships can affect a spatial join
To spatially join one dataset to another, attributes from the first dataset (join feature) are appended to the attributes in the second dataset (target feature) based on the relative spatial relationship between the two datasets’ geometries. 1, 2
Unlike table joins by attributes, we’re not really concerned with the tables having a primary key (a column, or columns, that uniquely identifies each record in a table) to conduct a join. Instead, the relative spatial location between the features in two datasets will determine what gets joined.
This chapter will cover spatial join relationships, spatial join types, and attribute table relationships.
Spatial Join Relationships#
There are multiple spatial join relationships that we can specify. Only the features that meet the specified spatial relationship criteria will be joined together. 1, 2 The spatial arrangement necessary to satisfy a specified spatial relationship depends on the vector types (i.e., point, line, polygon) of the join feature and the target feature. Note that some spatial relationships are not possible for certain combinations of vector types.
Below is a description of the spatial relationships available in the
geopandas module (this list is not necessarily exhaustive). [^gpd_meerge] We also provide diagrams visualizing the spatial arrangement needed for various combinations of vector types to satisfy the spatial relationship criteria and allow for a join.
One feature contains another if the geometry’s points are not to the exterior of the other’s geometry and the geometry’s interior contains at least one point of the other geometry’s interior.
The geometry’s interior crosses that of the other geometry, provided that the geometry does not contain the other and the dimension of the intersection is less than the dimension of either geometry.
A feature intersects another if the geometry’s boundary or interior has any part in common with the other geometry.
A feature overlaps another if the geometry shares at least one point, but not all points, with the other geometry, provided that the geometries and the intersection of their interiors all have the same dimensions.
A feature touches another if the geometry shares at least one point with the other geometry, provided that no part of the geometry’s interior intersects with the other geometry.
A feature is within another if the geometry is enclosed in the other geometry (geometry’s boundary and interior intersects with the other geometry’s interior only).
Spatial Join Types#
There are four types of spatial joins: outer join, inner join, left join, and right join. These spatial join types determine which features from both datasets are kept in the resulting output dataset.
Only the inner, left, and right join types are available in the
geopandas module and are identical to those in
All features from both datasets are kept, regardless if the features meet the specified spatial relationship criteria for a join. As all attribute fields are combined, rows that do not have a match may have null values in the fields that originated from the other dataset. 3
Only features from both datasets that meet the spatial relationship for the joined are kept. The geometries from the first or left dataset are used for the join. 3
All features from the first or left dataset are kept, regardless if the features meet the specified spatial relationship criteria for a join. As all attribute fields are combined, rows that do not have a match with the right dataset may have null values in the fields that originated from the right dataset.
All features from the second or right dataset are kept, regardless if the features meet the specified spatial relationship criteria for a join. As all attribute fields are combined, rows that do not have a match with the left dataset may have null values in the fields that originated from the left dataset.
For more details on join types, see the chapter on joining by attributes.
Table relationships, or cardinality, specify the direction of the join and the relationship between the two datasets. There are four types of table relationships: one-to-one, one-to-many, many-to-one, and many-to-many. While these relationships are not specified as a parameter in the
geopandas module when conducting a spatial join, it is important to have a general idea of the table relationship between the two datasets. Generally, a many-to-one join and a many-to-many join should be avoided because it can create ambiguity and unpredictability when analyzing and manipulating the resulting attribute table. 3
Each feature in one geometry is linked to one (and only one) feature in the second geometry (and vice versa). Each feature in both datasets will appear only once at most. 3
One-to-many relationship or many-to-one relationship#
Each feature in one geometry is linked to one or more features in the second geometry. The output geometry may see the “one” feature duplicated across multiple rows to accommodate its multiple linkages in the other dataset (the “many”). A one-to-many or many-to-one relationship can be dependent on the spatial join type (for example, if a left join results in a one-to-many relationship, then a right join will result in a many-to-one relationship). 3
These two table relationships—while similar—are not the same! In a one-to-many relationship, the “many” geometry is the dataset that is being joined to. Alternatively, in a many-to-one relationship, the “one” geometry is the dataset being joined to. Thus, the many-to-one relationship can result in duplicate “one” geometries and create uncertainty, which is why a many-to-one join should be avoided. 3
One or more features in one geometry are linked to one or more features in the second geometry. The output geometry may have multiple rows of each target and join feature to accommodate the multiple linkages, which can create ambiguity—hence why a many-to-many join should be avoided. 3
To ensure you’re doing a one-to-one or one-to-many join, it may be helpful to envision your spatial join before performing a spatial join. Think about what geometries you will be joining to (the target feature) and what geometry or geometries from the join feature might be joined to one geometry in the target feature.