Skip to content

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

julia
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
julia
contiguity_adjacency(geoms; rule = :queen)

Build a binary contiguity adjacency matrix W for polygonal geometries.

  • geoms: Vector of geometries. Accepts LibGEOS.Geometry or GeoInterface-compatible objects that LibGEOS can wrap via LibGEOS.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.

source
julia
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).

source

See Also