# `GeoFrame.SpatialJoin`
[🔗](https://github.com/CountlinkX-Solutions/geoframe/blob/v0.3.4/lib/geo_frame/spatial_join.ex#L1)

Spatial joins (`sjoin/3`, `sjoin_nearest/3`) between two `GeoFrame.GeoDataFrame`s with GeoPandas semantics.

The matching pairs come from `GeoFrame.GeoSeries.sindex_query/4` (R-tree probe, exact
refinement, computed in Rust). Rust also lays out the result rows (join order, unmatched
rows) and hands the row indices over as native-endian `u32` binaries that Explorer reads
with `Series.from_binary/3`, so a join of millions of pairs never builds a list of indices
on the BEAM heap, and geometry never leaves Rust memory.

## Result

  * `how: :inner` (default) keeps matched pairs; `:left` also keeps unmatched left rows
    once, with nulls on the right; `:right` is the mirror image.
  * The geometry is the left geometry for `:inner` and `:left`, the right geometry for
    `:right` (with that frame's geometry column name).
  * Columns follow GeoPandas: left attributes, then `index_right` (the zero-based row
    position in the right frame, a nullable `s64`), then right attributes (never the right
    geometry). For `how: :right` it is `index_left`, left attributes, right attributes.
    An attribute name present on both sides becomes `name_left` / `name_right`
    (`:lsuffix`, `:rsuffix`), and the index column is `index_<rsuffix>` (`index_<lsuffix>`
    for a right join), as in GeoPandas. The geometry is always the last column, whichever side it
    comes from (GeoPandas puts the right geometry in the middle of a right join; a
    `GeoFrame.GeoDataFrame` keeps its geometry last).
  * Row order: `:inner` and `:left` by left row then right row; `:right` by right row then
    left row.

## Secondary geometry columns

The secondary geometry columns of both frames (`GeoFrame.GeoDataFrame.put_geometry/3`) are
attributes of the result, like the other geometry-dtype columns of a GeoPandas frame: each
keeps its own CRS, a name present on both sides gets the suffixes, and rows without a
partner hold a null geometry. They follow the active geometry (left columns first), whereas
GeoPandas leaves each at its original column position.

## Nulls on the unmatched side

Explorer cannot `take` with missing indices, so the unmatched side is gathered from the
frame with one all-null row appended (`concat_rows`) and `nil` indices pointing at that
row. That keeps column dtypes and costs one copy of the unmatched side's attributes.

Null and empty geometries never match, but still appear in `:left` / `:right` results.
Frames whose CRS are both set and differ raise `ArgumentError`.

# `sjoin`

```elixir
@spec sjoin(GeoFrame.GeoDataFrame.t(), GeoFrame.GeoDataFrame.t(), keyword()) ::
  GeoFrame.GeoDataFrame.t()
```

Joins `left` and `right` on a spatial relation.

Options:

  * `:how` - `:inner` (default), `:left` or `:right`.
  * `:predicate` - any predicate of `GeoFrame.GeoSeries.sindex_query/4` (default
    `:intersects`), read as `left.predicate(right)`.
  * `:distance` - required for `:dwithin`.
  * `:lsuffix` / `:rsuffix` - suffixes for colliding attribute names (`"left"` / `"right"`).

Raises `ArgumentError` for bad or unknown options, a CRS mismatch, or when the result would contain
two columns with the same name (including an existing `index_right` / `index_left`).

# `sjoin_nearest`

```elixir
@spec sjoin_nearest(GeoFrame.GeoDataFrame.t(), GeoFrame.GeoDataFrame.t(), keyword()) ::
  GeoFrame.GeoDataFrame.t()
```

Joins each `left` row with its nearest `right` geometry (planar euclidean distance,
geometry to geometry), like `geopandas.sjoin_nearest`.

Every right geometry at exactly the minimum distance is returned, so a left row can
appear several times (ties); ties are ordered by right row. For `how: :right` the roles
swap: each right row is joined with its nearest left geometries.

Options:

  * `:how` - `:inner` (default; rows without a match are dropped), `:left` or `:right`.
  * `:max_distance` - non-negative; farther candidates do not match (inclusive limit).
  * `:distance_col` - name of an extra `f64` column holding the distance (omitted by
    default; `nil` for unmatched rows).
  * `:exclusive` - when `true`, geometries equal to the query one are ignored
    (default `false`).
  * `:lsuffix` / `:rsuffix` - suffixes for colliding attribute names (`"left"` / `"right"`).

Column order and nulls follow `sjoin/3`; the distance column comes after the right
attributes. Null and empty geometries never match. Raises `ArgumentError` for bad or unknown options,
a CRS mismatch or duplicate result column names.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
