# Spatial join

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Spatial_join
> Markdown URL: https://mediated.wiki/source/Spatial_join.md
> Source: https://en.wikipedia.org/wiki/Spatial_join
> Source revision: 1339691494
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

This article needs additional citations for verification. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed. Find sources: "Spatial join" – news · newspapers · books · scholar · JSTOR (January 2023) (Learn how and when to remove this message)

GIS tool for merging attribute tables

A **spatial join** is an operation in a [geographic information system](/source/Geographic_information_system) (GIS) or [spatial database](/source/Spatial_database) that combines the attribute tables of two spatial layers based on a desired [spatial relation](/source/Spatial_relation) between their geometries.[1] It is similar to the [table join](/source/Join_(SQL)) operation in relational databases in merging two tables, but each pair of rows is correlated based on some form of matching location rather than a common [key value](/source/Foreign_key).[2] It is also similar to [vector overlay](/source/Vector_overlay) operations common in GIS software such as Intersect and Union in merging two spatial datasets, but the output does not contain a composite geometry, only merged attributes.

Spatial joins are used in a variety of [spatial analysis](/source/Spatial_analysis) and management applications, including [allocating individuals to districts](/source/Location-allocation) and statistical aggregation. Spatial join is found in most, if not all, GIS and spatial database software, although this term is not always used, and sometimes it must be derived indirectly by the combination of several tools.

## Spatial relation predicates

Main articles: [Spatial relation](/source/Spatial_relation) and [Geospatial topology](/source/Geospatial_topology)

Examples of topological spatial relations.

Fundamental to the spatial join operation is the formulation of a spatial relationship between two [geometric primitives](/source/Geometric_primitive) as a [logical predicate](/source/Predicate_(mathematical_logic)); that is, a criterion that can be evaluated as true or false.[3] For example, "A is less than 5km from B" would be true if the distance between points A and B is 3km, and false if the distance is 10km. These relation predicates can be of two types:

- A **[Topological relation](/source/Geospatial_topology)** is a qualitative relationship between two shapes that does not depend on a measurable space (that is, coordinates). Common examples of such predicates include "A is *completely inside* B," "A *overlaps* B," "A is *adjacent* to B" (i.e., sharing a boundary but no interior), and "A is *disjoint* from B" (not touching at all). These are commonly specified according to some form of the [9-Intersection Model](/source/DE-9IM),[4] which is incorporated into the international [Simple Feature Access](/source/Simple_Features) specification (ISO 19125-2).[5]

- A **Metric relation** is a quantitative (measurable) relationship between two shapes in a coordinate space, most commonly a distance or direction. Common examples include "A is *due north* of B" or "A is *less than 5 km* from B." Not all software implementations support metric relations.

Note that some relations are *commutative* (e.g., A overlaps B if and only if B overlaps A) while others are not (e.g., A is within B does not mean B is within A).

The geometric primitives involved in these relations may be of any [dimension](/source/Dimension) (points, lines, or regions), but some relations may only have meaning with certain dimensions. For example, "A is within B" has a clear meaning if A is a point and B is a region, but is meaningless if both A and B are points. Other relations may be vague; for example, the distance between two regions or two lines may be interpreted as the minimal distance between their closest boundaries, or a mean distance between their centroids.[6]

## Operation

As in a [relational table join](/source/Join_(SQL)) as defined in the [relational algebra](/source/Relational_algebra#Joins_and_join-like_operators), two input layers or tables are provided (hereafter *X* and *Y*), and the output is a table containing all of the columns of each of the inputs (or some subset thereof if selected by the user). The rows of the new table are a subset of Cross join or [Cartesian product](/source/Cartesian_product) of the two tables, all possible pairs of rows {X1-Y1, X1-Y2, X1-Y3, X2-Y1, X2-Y2, X2-Y3, X3-Y1, X3-Y2, X3-Y3, ...}. Rather than include all possible combinations, each pair is evaluated according to the given spatial predicate; those for which the predicate is true are considered "matching" and are retained, while those for which the predicate is false are discarded.

For example, consider the following two tables:

Students table StudentID LastName GPA Residence: point 1 Rafferty 3.56 • 2 Jones 2.75 • 3 Heisenberg 3.98 • 4 Robinson 1.56 • 5 Smith 2.67 • 6 Williams 3.46 •

Schools table SchoolID SchoolName District: polygon Building: point 31 Belknap Elementary □ • 33 Parkview Elementary □ • 34 Smith Elementary □ • 35 Central Elementary □ •

When the spatial join is executed, the direction of attachment must be specified, for two reasons: 1) the given spatial predicate may not be commutative, and 2) there is often a many-to-one relationship between the rows (e.g., many students are inside each school district). In the example above, a common goal would be to join the schools table to the students table (the *target table*), with the relation predicate being "student.residence *within* school.district." Assuming that the districts do not overlap, each student point will be in no more than one school district, so the output would have the same rows as the students table, with the corresponding school attributes attached, as:

Students x Schools StudentID LastName GPA Residence: point SchoolID SchoolName 1 Rafferty 3.56 • 33 Parkview Elementary 2 Jones 2.75 • 34 Smith Elementary 3 Heisenberg 3.98 • 35 Central Elementary 4 Robinson 1.56 • 33 Parkview Elementary 5 Smith 2.67 • 34 Smith Elementary 6 Williams 3.46 • 33 Parkview Elementary

The reverse operation, in this case attaching the student information to the schools table, is not as simple because many rows must be joined to one row. Some GIS software does not allow this operation, but most implementations allow for an *aggregate join*, in which aggregate summaries of the matching rows can be included, such as arrays, counts, sums, or means.[7] For example, the result table might look like:

Schools x Students SchoolID SchoolName District: polygon Building: point Students_COUNT GPA_MEAN 31 Belknap Elementary □ • 0 NULL 33 Parkview Elementary □ • 3 2.86 34 Smith Elementary □ • 2 2.71 35 Central Elementary □ • 1 3.98

Another option when there are multiple matches is to use some criterion to select one of the rows from the matching set, usually a spatial optimization criterion.[2][8] For example, one could join the school building points (not the districts) to the student residents points by selecting the school that is *nearest* to each student. Not all software implements this option directly, although in some cases it can be derived through a combination of tools.

## External links

- [Spatial Join](https://pro.arcgis.com/en/pro-app/latest/tool-reference/analysis/spatial-join.htm) tool in ArcGIS Pro

- [Join attributes by location](https://docs.qgis.org/3.22/en/docs/user_manual/processing_algs/qgis/vectorgeneral.html#join-attributes-by-location) tool in QGIS

- [Join attributes by nearest](https://docs.qgis.org/3.22/en/docs/user_manual/processing_algs/qgis/vectorgeneral.html#join-attributes-by-nearest) tool in QGIS

- [Spatial Join](https://manifold.net/doc/mfd9/join.htm) in Manifold GIS

- [Spatial Joins](https://postgis.net/workshops/postgis-intro/joins.html) in PostGIS

## References

1. **[^](#cite_ref-Longley2011_1-0)** Longley, Paul A.; Goodchild, Michael F.; Maguire, David J.; Rhind, David W. (2011). *Geographic Information Systems & Science* (3rd ed.). Wiley. p. 360.

1. ^ [***a***](#cite_ref-campbell2011_2-0) [***b***](#cite_ref-campbell2011_2-1) Campbell, Jonathan; Shin, Michael (2011). [*Essentials of Geographic Information Systems*](https://saylordotorg.github.io/text_essentials-of-geographic-information-systems/s11-02-multiple-layer-analysis.html). Saylor Foundation. p. 182. [ISBN](/source/ISBN_(identifier)) [9781453321966](https://en.wikipedia.org/wiki/Special:BookSources/9781453321966). Retrieved 5 January 2023.

1. **[^](#cite_ref-qgis_3-0)** ["Join attributes by location tool"](https://docs.qgis.org/3.22/en/docs/user_manual/processing_algs/qgis/vectorgeneral.html#join-attributes-by-location). *QGIS 3.22 Documentation*. OSGeo. Retrieved 4 January 2023.

1. **[^](#cite_ref-sdh1990_4-0)** Egenhofer, M.J.; Herring, J.R. (1990). ["A Mathematical Framework for the Definition of Topological Relationships"](https://web.archive.org/web/20100614161335/http://www.spatial.maine.edu/~max/MJEJRH-SDH1990.pdf) (PDF). Archived from [the original](http://www.spatial.maine.edu/~max/MJEJRH-SDH1990.pdf) (PDF) on 2010-06-14. {{[cite journal](https://en.wikipedia.org/wiki/Template:Cite_journal)}}: Cite journal requires |journal= ([help](https://en.wikipedia.org/wiki/Help:CS1_errors#missing_periodical))

1. **[^](#cite_ref-OGC-SFA_5-0)** Open Geospatial Consortium. ["Simple Feature Access - Part 2: SQL Option"](https://www.ogc.org/standards/sfs). *Open Geospatial Consortium Standards*. Retrieved 4 January 2023.

1. **[^](#cite_ref-worboys2004_6-0)** Worboys, Michael; Duckham, Matt (2004). *GIS: A Computing Perspective* (2nd ed.). Boca Raton, Florida: CRC Press. p. 195. [ISBN](/source/ISBN_(identifier)) [0-415-28375-2](https://en.wikipedia.org/wiki/Special:BookSources/0-415-28375-2).

1. **[^](#cite_ref-7)** ["Spatial Join (Analysis)"](https://pro.arcgis.com/en/pro-app/latest/tool-reference/analysis/spatial-join.htm). *ArcGIS Pro Documentation*. Esri. Retrieved 5 January 2023.

1. **[^](#cite_ref-qgis-nearest_8-0)** ["Join attributes by nearest tool"](https://docs.qgis.org/3.22/en/docs/user_manual/processing_algs/qgis/vectorgeneral.html#join-attributes-by-nearest). *QGIS 3.22 Documentation*. OSGeo. Retrieved 4 January 2023.

---
Adapted from the Wikipedia article [Spatial join](https://en.wikipedia.org/wiki/Spatial_join) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Spatial_join?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
