Spatial Utilities
Helpers for constructing spatial structures frequently used in latent models.
Contiguity Adjacency from Polygons
Build a binary adjacency matrix for polygon features using queen contiguity (shared boundary point). Accepts either a vector of geometries or a LibGEOS GeometryCollection
. A Shapefile convenience method is provided via package extension. It requires you to load Shapefile.jl first.
Example
using GaussianMarkovRandomFields, LibGEOS
g1 = readgeom("POLYGON((0 0,1 0,1 1,0 1,0 0))")
g2 = readgeom("POLYGON((1 0,2 0,2 1,1 1,1 0))")
W = contiguity_adjacency([g1, g2]) # 2×2 symmetric, ones off-diagonal
GaussianMarkovRandomFields.contiguity_adjacency Function
contiguity_adjacency(geoms; rule = :queen)
Build a binary contiguity adjacency matrix W
for polygonal geometries.
geoms
: Vector of geometries. AcceptsLibGEOS.Geometry
or GeoInterface-compatible objects that LibGEOS can wrap viaLibGEOS.Geometry(geom)
.rule
: Contiguity definition. Currently supports:queen
(share any boundary point).
Returns a sparse, symmetric SparseMatrixCSC{Float64,Int}
with zero diagonal, where W[i,j] = 1.0
iff areas i and j are contiguous under the chosen rule.
Notes
Complexity is O(n²) over the number of polygons. For large
n
, consider spatial indexing or pre-filtering by bounding boxes before calling this utility.Rook contiguity (shared edge only) can be added later;
:queen
covers most use cases.
contiguity_adjacency(gc::LibGEOS.GeometryCollection; rule = :queen)
Build contiguity adjacency from a LibGEOS GeometryCollection
by extracting its member geometries via LibGEOS.getGeometries(gc)
and delegating to contiguity_adjacency(::Vector)
.
See Also
- The BYM + fixed effects Poisson tutorial uses this to build
W
from a shapefile: Advanced GMRF modelling for disease mapping