Spatex.GeoFrame (Spatex v0.2.1)

Copy Markdown View Source

Adapter between 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:

MethodGeometryRequired option
:queen, :rookpolygons or multipolygons (WKB)-
:knnany; polygons use centroidsk: pos_integer
:distance_bandany; polygons use centroidsthreshold: number
:kernelany; polygons use centroids- (see Spatex.Graph.build_kernel/2)
:triangulationany; 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

Summary

Functions

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

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

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

Functions

graph(frame, method, opts \\ [])

@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(frame, result, opts \\ [])

@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(frame, column)

@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.