# `Spatex.GeoFrame`
[🔗](https://github.com/CountlinkX-Solutions/spatex/blob/v0.2.1/lib/spatex/geo_frame.ex#L1)

Adapter between [GeoFrame](https://github.com/CountlinkX-Solutions/geoframe) and Spatex.

Spatex does not depend on GeoFrame. This module calls GeoFrame's public API at runtime
(`GeoFrame.GeoDataFrame` and `GeoFrame.GeoSeries`), and raises an `ArgumentError` saying so
when GeoFrame is not loaded. GeoFrame is an optional integration that is not on Hex yet: to
use this module, add a local GeoFrame checkout to your own dependencies, for example
`{:geoframe, path: "../geoframe"}`. Spatex's own GeoFrame tests and notebooks find the
checkout through the `SPATEX_GEOFRAME_PATH` environment variable.

## Building graphs

`graph/3` takes a `GeoFrame.GeoDataFrame` or a `GeoFrame.GeoSeries` and a method:

| Method | Geometry | Required option |
|---|---|---|
| `:queen`, `:rook` | polygons or multipolygons (WKB) | - |
| `:knn` | any; polygons use centroids | `k: pos_integer` |
| `:distance_band` | any; polygons use centroids | `threshold: number` |
| `:kernel` | any; polygons use centroids | - (see `Spatex.Graph.build_kernel/2`) |
| `:triangulation` | any; polygons use centroids | - (see `Spatex.Graph.build_triangulation/2`) |

Point methods always work on the centroid of each geometry; the centroid of a point is the
point itself. Remaining options go to the matching `Spatex.Graph` builder. The graph's ids
are the row positions unless `ids:` is a list of unique ids or the name of an attribute
column of a `GeoDataFrame`.

### Geographic coordinates

The distance methods are Euclidean, so on longitude/latitude data they measure degrees.
When the geometry reports a geographic CRS (`GeoSeries.is_geographic/1`), `graph/3` raises
`ArgumentError` unless `allow_geographic: true` (proceed silently) or
`allow_geographic: :warn` (proceed and log a warning) is given. Project the frame first
(`GeoFrame.GeoDataFrame.to_crs/2`) for meaningful distances. Contiguity methods are not
affected.

## Results as columns

`values/2` pulls an attribute column as an `Explorer.Series`, which every Spatex ESDA
function accepts. `put_results/3` writes per-site result columns back onto the frame; see
`Spatex.Explorer.to_dataframe/2` for the columns.

## Examples

    frame = GeoFrame.GeoDataFrame.from_wkb!(df, "geometry")
    graph = Spatex.GeoFrame.graph(frame, :queen)
    lisa = Spatex.ESDA.moran_local(Spatex.GeoFrame.values(frame, "crime"), graph, seed: 1)
    Spatex.GeoFrame.put_results(frame, lisa)
    #=> a GeoDataFrame with lisa_is, lisa_q, lisa_p_sim, lisa_cluster, ... columns

# `graph`

```elixir
@spec graph(struct(), atom(), keyword()) :: Spatex.Graph.t()
```

Builds a `Spatex.Graph` from a `GeoFrame.GeoDataFrame` or `GeoFrame.GeoSeries`.

See the module documentation for the methods and options. Raises `ArgumentError` for an
unknown method, missing required options, geometries a method cannot handle (a non-polygon
with `:queen`/`:rook`, an empty or null geometry for point methods) or geographic data for
a distance method.

# `put_results`

```elixir
@spec put_results(struct(), struct(), keyword()) :: struct()
```

Adds the per-site columns of an ESDA or classification result to a `GeoDataFrame`.

Accepts the results `Spatex.Explorer.to_dataframe/2` accepts, including `Spatex.Region`
results (a `labels` column). Returns a new frame. Column names are the ones
`Spatex.Explorer.to_dataframe/2` produces,
prefixed (the `id` column is not added). The default prefix depends on the result:
`"lisa_"` for `Spatex.ESDA.MoranLocal`, `"g_"` for `Spatex.ESDA.GLocal`, `"class_"` for
`Spatex.Classify`, otherwise the snake-cased result module name followed by `_`. Existing
columns with the same name are replaced.

## Options

  * `:prefix` - column name prefix.
  * `:crit` - significance level for LISA cluster labels (default `0.05`).

Raises `ArgumentError` when any result column's length differs from the frame's rows, or
when the result has no per-site columns (instead of returning the frame unchanged).

# `values`

```elixir
@spec values(struct(), String.t() | atom()) :: struct()
```

The attribute column `column` of a `GeoFrame.GeoDataFrame` as an `Explorer.Series`.

Raises `ArgumentError` for a geometry column.

---

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