Reads and writes spatial weights in the GeoDa/PySAL GAL and GWT text formats.
The writers reproduce libpysal.graph.Graph.to_gal/1 and to_gwt/1 byte for byte; the
readers accept the files written by libpysal, GeoDa and legacy libpysal.io.
GAL
Adjacency only, no weights:
4
a 2
b c
b 1
a
...The first line is the node count n (legacy files use 0 n shapefile id_var; the reader
takes n from the second token whenever the header has more than one). Each node then
takes two lines: id cardinality and its neighbor ids (an empty line for an island).
The declared cardinality is ignored when reading. A graph read from GAL has binary
weights (1.0); weights are lost when writing GAL, and a weighted graph writes the
same file as its binary version. Ids must not contain whitespace.
GWT
One edge per line, focal neighbor weight, after a one-line header (written as
0 n Unknown Unknown; any first line is skipped when reading). Weights are written with
seven decimals (%.7f, as libpysal does), so a roundtrip preserves them to within
5.0e-8. libpysal writes integer-typed weights (for example binary contiguity graphs) as
1 rather than 1.0000000; Spatex stores floats only and always writes seven decimals (both
forms read back identically). Whitespace in ids is replaced by _ when writing, and islands are written as
a zero-weight self row id id 0.0000000.
Ids
Both writers accept integer, binary and atom ids and write them with to_string/1.
The readers take an :ids option:
:auto(default) - integers when every id parses as one, strings otherwise, matching libpysal's lossless integer roundtrip;:integer- integers; raises when an id is not an integer;:string- always strings.
Node order is the file order: GAL row order, or first appearance in a GWT file.
Readers take a path (read_gal/2, read_gwt/2) or already-loaded text (parse_gal/2,
parse_gwt/2). All functions raise ArgumentError on malformed input.
Examples
iex> g = Spatex.Graph.from_arrays([1, 2], [2, 1], [0.5, 2.0])
iex> g |> Spatex.Graph.IO.to_gal() |> IO.iodata_to_binary()
"2\n1 1\n2\n2 1\n1\n"
iex> g |> Spatex.Graph.IO.to_gwt() |> IO.iodata_to_binary()
"0 2 Unknown Unknown\n1 2 0.5000000\n2 1 2.0000000\n"
iex> g |> Spatex.Graph.IO.to_gwt() |> Spatex.Graph.IO.parse_gwt() |> Spatex.Graph.to_adjacency()
[{1, 2, 0.5}, {2, 1, 2.0}]
Summary
Functions
Parses GAL text (a binary or iodata).
Parses GWT text (a binary or iodata); the first line is a header and is skipped.
Reads a GAL file. See the module documentation for the :ids option.
Reads a GWT file. See the module documentation for the :ids option.
Renders graph as GAL text (weights are dropped, see the module documentation).
Renders graph as GWT text: header, then focal neighbor weight per edge with seven
decimals.
Writes graph to path in GAL format. Returns :ok.
Writes graph to path in GWT format. Returns :ok.
Types
Functions
@spec parse_gal(iodata(), keyword()) :: Spatex.Graph.t()
Parses GAL text (a binary or iodata).
@spec parse_gwt(iodata(), keyword()) :: Spatex.Graph.t()
Parses GWT text (a binary or iodata); the first line is a header and is skipped.
@spec read_gal(Path.t(), keyword()) :: Spatex.Graph.t()
Reads a GAL file. See the module documentation for the :ids option.
@spec read_gwt(Path.t(), keyword()) :: Spatex.Graph.t()
Reads a GWT file. See the module documentation for the :ids option.
@spec to_gal(Spatex.Graph.t()) :: iodata()
Renders graph as GAL text (weights are dropped, see the module documentation).
@spec to_gwt(Spatex.Graph.t()) :: iodata()
Renders graph as GWT text: header, then focal neighbor weight per edge with seven
decimals.
@spec write_gal(Spatex.Graph.t(), Path.t()) :: :ok
Writes graph to path in GAL format. Returns :ok.
@spec write_gwt(Spatex.Graph.t(), Path.t()) :: :ok
Writes graph to path in GWT format. Returns :ok.