Spatial analysis turns layers into answers. It asks where water flows, which land is suitable for a crop or a clinic, how far people are from services, how groups are spread across a city and whether a policy changed outcomes in the places it reached. Every one of these questions starts from the vector and raster data of the first two chapters and adds a method built for space.
These 51 notebooks are chapter 03 of the GeoLattice tutorials. They cover terrain and hydrology, multidimensional data cubes, multi-criteria suitability, spatial networks, segregation and spatial causal inference, and finish with six capstone projects, using geolattice.spatial_analysis with real data from the United States and Bangladesh.
GeoLattice
GeoLattice is a Python library for geospatial analysis that brings vector and raster processing, spatial analysis, spatial statistics, geostatistics, spatial databases, web and cloud GIS and remote sensing together under one import. It is built on GDAL, NumPy, pandas, GeoPandas, Shapely and xarray, so its functions take and return objects these libraries already use: GeoDataFrames, xarray DataArrays, NumPy arrays and GDAL-readable files.
| Package | geolattice |
| Repository | github.com/zia207/GeoLattice |
| Python | 3.10 or newer |
| License | MIT |
| Author | Zia Ahmed, Upatta Analytics |
GDAL and PROJ must be installed first, with Python GDAL bindings that match the GDAL library (conda or your system package manager). Then install the spatial statistics option, which includes spatial analysis and the scikit-learn models that spatial causal inference needs:
python3 -m pip install "geolattice[statistics]"The chapter uses four imports:
import geolattice.spatial_analysis as sa
from geolattice.spatial_analysis import network_analysis as sna, segregation as seg
import geolattice.spatial_analysis.spatialcausal as scTerrain and hydrology
A digital elevation model (DEM) is a raster of heights. Its derivatives describe the shape of the land: slope, aspect and curvature, the position of a cell relative to its neighbours and how wet it is likely to be. Hydrology follows water over that surface: fill or breach the pits a DEM always has, give each cell a flow direction, accumulate the area that drains through it, and cut the network into streams and watersheds.
| Task | Functions (sa) |
|---|---|
| Surface derivatives | slope, aspect, hillshade, curvature, plan_curvature, profile_curvature |
| Position and ruggedness | tpi, tri, roughness, twi, landform_classification, multiscale_analysis |
| Visibility and contours | viewshed, contours |
| Condition the DEM | fill_depressions, breach_depressions |
| Flow direction and accumulation | d8_pointer, dinf_pointer, fd8_pointer, d8_flow_accumulation, dinf_flow_accumulation |
| Streams and watersheds | extract_streams, stream_segmentation, snap_pour_points, watershed, subbasins, basins |
| Height above drainage | elevation_above_stream, wetness_index |
The terrain and hydrology tools read a DEM from a path and write their result to another path:
import geolattice.spatial_analysis as sa
sa.slope("dem.tif", "slope.tif") # degrees
sa.hillshade("dem.tif", "hillshade.tif", azimuth=315, altitude=45)
sa.breach_depressions("dem.tif", "dem_breached.tif") # condition the DEM
sa.d8_pointer("dem_breached.tif", "d8.tif") # flow direction
sa.d8_flow_accumulation("dem_breached.tif", "facc.tif") # upslope cells
sa.extract_streams("facc.tif", "streams.tif", threshold=1000) # cells draining > 1000 cells
sa.watershed("d8.tif", "outlets.shp", "watersheds.tif") # watershed of each outletMultidimensional data
Climate and satellite records come as cubes: one raster per variable per time step, stored in NetCDF or Zarr. Multidimensional tools select and aggregate along time, compute anomalies against a baseline, fit a trend to every pixel and reduce many layers to a few principal components.
| Task | Functions (sa) |
|---|---|
| Open and subset cubes | MultidimensionalRaster, subset_multidimensional_raster, make_netcdf_raster_layer |
| Aggregate and compare | aggregate_multidimensional_raster, generate_multidimensional_anomaly, zonal_statistics_as_table |
| Trends and change | generate_trend_raster, predict_using_trend_raster, analyze_changes_using_ccdc, analyze_changes_using_landtrendr |
| Reduce and sample | multidimensional_principal_components, temporal_profile, sample_multidimensional |
| Export | raster_to_netcdf, feature_to_netcdf, table_to_netcdf |
Multi-criteria suitability
Suitability modelling ranks places against several criteria at once: slope, distance to roads, soil, rainfall and so on. Each criterion is put on a common scale, then the criteria are combined, either by hard rules (Boolean), by weights (weighted overlay or weighted linear combination) or by fuzzy membership, which lets a place be partly suitable. The last step tests how much the answer depends on the weights.
| Task | Functions (sa) |
|---|---|
| Common scale | suitability_reclassify, minmax_normalize, percentile_rank_normalize, rescale_by_function |
| Boolean and categorical rules | categorical_and, categorical_or, criteria_count, limiting_criterion |
| Weighted overlay | weighted_overlay, weighted_sum, weighted_linear_combination, score_contributions |
| Fuzzy logic | fuzzy_membership, fuzzy_overlay, weighted_fuzzy_overlay, linear, gaussian, large, small, near |
| Sensitivity and sites | monte_carlo_weights, perturb_weights, locate_regions, top_quantile_mask |
import geolattice.spatial_analysis as sa
# Fuzzy "small" membership: close to 1 for low values, 0.5 at the midpoint
sa.fuzzy_membership("slope.tif", "slope_fz.tif", membership_type="small", midpoint=8, spread=5) # 8 degrees
sa.fuzzy_membership("dist_road.tif", "road_fz.tif", membership_type="small", midpoint=2000, spread=4) # 2 km
# Combine: fuzzy AND keeps the weakest criterion; a weighted overlay trades them off
sa.fuzzy_overlay(["slope_fz.tif", "road_fz.tif"], "suit_fuzzy.tif", overlay_type="and")
sa.weighted_overlay(["slope_class.tif", "road_class.tif"], "suit_weighted.tif", weights=[0.6, 0.4])Network analysis
A road network is a graph: intersections are nodes and road segments are edges with a length and a travel time. Network analysis finds shortest routes, the area reachable within a time budget, how well each place is served by facilities and how goods, people or a disease move along the links.
| Task | Functions (sna) |
|---|---|
| Build graphs | load_road_graph, lines_to_graph, graph_from_edgelist, snap_points_to_nodes |
| Routes and travel times | shortest_routes, route_summary, od_travel_time_matrix, travel_time_matrix |
| Service areas and access | isochrone_polygons, cumulative_accessibility, two_sfca, population_coverage |
| Facility location | nearest_facility_network, evaluate_candidate_sites, closure_impact |
| Flows and structure | fit_gravity_model, flow_communities, flow_pagerank, kansky_indices, percolation_curve |
| Spread on networks | simulate_contagion, metapopulation_sir |
Segregation analysis
Segregation measures describe how two or more groups are spread across the units of a city or region. Indices summarise the whole area, local measures map where groups are concentrated, multiscale profiles show how the answer changes with neighbourhood size, and null models and bootstraps say whether a value is larger than chance.
| Task | Functions (seg) |
|---|---|
| One call for any index | compute |
| Single group and multigroup indices | dissimilarity, isolation, exposure, gini, theil_h, multigroup_dissimilarity, simpson_diversity |
| Local measures | local_quotient, local_entropy, local_significance |
| Neighbourhoods and scale | kernel_weights, knn_weights, network_weights, compute_profile |
| Inference | evenness_null, systematic_null, spatially_constrained_null, spatial_block_bootstrap, panel_trend_test |
| Change over time | decompose_change, compute_panel, areal_interpolate |
Spatial causal inference
Correlation in space is easy to find and hard to interpret. Spatial causal inference estimates the effect of a treatment when nearby places share unmeasured causes (spatial confounding), when treating one place affects its neighbours (interference and spillovers) and when the effect itself varies over space. Quasi-experiments use borders and the timing of policies, and the last two notebooks design spatial experiments and learn causal graphs.
| Task | Functions (sc) |
|---|---|
| Data | SpatialCausalData, SpatialPanel, datasets.simulate_spatial_confounding |
| Spatial confounding | diagnose_spatial_confounding, SpatialPlus, moran_eigenvectors, compare_adjustments |
| Spillovers | ExposureMapping, DistanceDecayExposure, InterferenceEstimator |
| Machine learning estimators | SpatialDML, SpatialDRLearner, GWDML, GWCausalForest |
| Quasi-experiments | GeographicRDD, SpatialDiD, StaggeredSpatialDiD, SpatialSyntheticControl |
| Bayesian models and experiments | BayesianSpatialCausal, ClusterRandomizedDesign, SaturationDesign, power_analysis |
| Causal discovery | SpatialGranger, GCCM, SpatialNOTEARS |
| Inference under dependence | standard_error, conley_variance, spatial_block_bootstrap |
import geolattice.spatial_analysis.spatialcausal as sc
data = sc.datasets.simulate_spatial_confounding(n=400, random_state=0)
print(sc.diagnose_spatial_confounding(data)) # Moran's I on treatment and outcome
print(sc.SpatialDML(se="conley", cutoff=0.2).fit(data).summary())Tutorials
Start with the foundation notebooks. The topics after that can be read in any order, and the capstones combine them.
Foundation
| Tutorial | Topic |
|---|---|
| Spatial analysis | What spatial analysis is and the questions it answers |
| Vector data analysis | Spatial analysis and attribute tools on US data |
| Raster data analysis | Local, focal, zonal and proximity analysis, map algebra |
Digital terrain modeling
| Tutorial | Topic |
|---|---|
| Digital terrain modeling | DEMs and what they measure |
| DEM preprocessing | Metadata, clipping, resampling and terrain visualization |
| Terrain derivatives | Slope, aspect, curvature, TPI, landforms |
| Hydrology modeling | Flow direction, accumulation, streams, watersheds |
Multidimensional data
| Tutorial | Topic |
|---|---|
| Multidimensional raster data | NetCDF and Zarr cubes |
| Data management | NetCDF to multi-band rasters and back |
| Multidimensional analysis | Seasonal and monthly means, anomalies |
| Trend analysis | Per-pixel trends and predictions |
| Principal components | PCA across time slices |
Multi-criteria suitability
| Tutorial | Topic |
|---|---|
| Multi-criteria suitability modeling | The suitability workflow |
| Vector-based suitability | Suitability on polygons |
| Deriving criteria | Criteria for clinic site selection |
| Categorical suitability | Boolean rules |
| Weighted suitability | Weighted overlay and ranking |
| Fuzzy logic foundations | Membership functions and operators |
| Fuzzy suitability for polygons | Fuzzy overlay on vector data |
| Fuzzy suitability for rasters | Fuzzy overlay on grids |
| Advanced fuzzy suitability | Sensitivity, weighting and rule-based inference |
| Sensitivity and uncertainty | How stable the ranking is |
Network analysis
| Tutorial | Topic |
|---|---|
| Spatial network analysis | Graphs in geographic space |
| Geographic embedding | Nodes and edges with locations |
| Routing with OSMnx | Road networks, routing and accessibility |
| Networks from GeoPandas | Graphs from your own lines |
| Service areas and access | Isochrones and facility access |
| Flow networks | Commuting, gravity and contagion |
Segregation analysis
| Tutorial | Topic |
|---|---|
| Segregation analysis | Dimensions of segregation |
| Single-group indices | Dissimilarity, isolation, exposure |
| Multigroup indices | Measures for many groups |
| Local segregation | Where groups concentrate |
| Multiscale profiles | Segregation by neighbourhood size |
| Null models and simulation | What chance alone produces |
| Inference | Tests and confidence intervals |
| Segregation over time | Boundaries, panels and decomposition |
Spatial causal inference
| Tutorial | Topic |
|---|---|
| Spatial causal inference | Data and causal setup |
| Spatial confounding | Diagnose and adjust |
| Interference and spillovers | Direct and spillover effects |
| Spatially varying effects | GWDML and causal forests |
| Geographic RDD | Effects at a boundary |
| Panel designs | DiD with spillovers, staggered adoption, synthetic control |
| Bayesian spatial causal models | ICAR and BYM |
| Designing spatial experiments | Allocation, balance and power |
| Spatial causal discovery | Granger, GCCM and NOTEARS |
Capstone projects
| Tutorial | Topic |
|---|---|
| Flood susceptibility | Sylhet haor basin, Bangladesh |
| Watershed characterization | Onondaga Lake watershed, New York |
| Agricultural suitability | Land suitability for wheat in Bangladesh |
| Landslide susceptibility | Rocky Mountains and Great Plains |
| Habitat suitability | Forest musk deer habitat and connectivity |
| Farmer networks | Information networks and technology diffusion |
Quick reference
Sample data
The notebooks read the sample data in Data/03_spatial_analysis/ and Data/00_shared/ of the repository: DEMs, a NetCDF data cube, New York and Texas suitability layers, Buffalo census blocks for segregation, a US county frame for causal inference, and the data for the six capstones (Sylhet floods, Onondaga Lake, wheat suitability in Bangladesh, Great Plains landslides and forest musk deer).
License
GeoLattice is released under the MIT License. See LICENSE.
Developed by Zia Ahmed, Upatta Analytics