spatial-narrative
A Rust library for modeling, indexing, analyzing, and transforming spatial narratives โ sequences of events anchored in both space and time.
๐ Documentation ยท ๐ Getting Started ยท ๐ฆ Crates.io ยท ๐ฌ Discussions
Overview
spatial-narrative provides a comprehensive toolkit for working with geospatial event data. It enables researchers, journalists, urban planners, and developers to:
- Model events with precise locations, timestamps, and rich metadata
- Index large datasets for efficient spatial and temporal queries
- Analyze patterns, trajectories, clusters, and relationships
- Transform between formats (GeoJSON, CSV, JSON) for interoperability
- Graph event relationships for network analysis
Features
| Module | Description | Status |
|---|---|---|
core |
Location, Timestamp, Event, Narrative types | โ Complete |
io |
Import/export (GeoJSON, CSV, JSON) | โ Complete |
index |
R-tree spatial, B-tree temporal, combined indexes | โ Complete |
graph |
Event relationship graphs with petgraph | โ Complete |
analysis |
Metrics, clustering, trajectory analysis | ๐ง Planned |
parser |
Geoparsing from unstructured text | ๐ง Planned |
Installation
Add to your Cargo.toml:
[]
= "1.0"
Quick Start
use ;
use SpatiotemporalIndex;
use ;
// Create events
let event1 = new;
let event2 = new;
// Build a narrative
let narrative = new
.title
.events
.build;
// Index for fast queries
let mut index = new;
for event in &narrative.events
// Build relationship graph
let mut graph = from_events;
graph.connect_temporal; // Auto-connect by time sequence
graph.connect_spatial; // Connect events within 5km
Core Concepts
Events
An Event is the fundamental unit โ a thing that happened at a specific place and time:
use ;
let event = new
.location
.timestamp
.text
.tag
.tag
.source
.build;
Narratives
A Narrative is an ordered collection of related events:
use ;
let narrative = new
.title
.author
.description
.events
.tag
.build;
// Query capabilities
let chronological = narrative.events_chronological;
let time_span = narrative.time_range;
let geographic_extent = narrative.bounds;
// Filtering
let paris_events = narrative.filter_spatial;
let january_events = narrative.filter_temporal;
Indexing
Efficient queries over large event collections using specialized data structures.
Spatial Index (R-tree)
use SpatialIndex;
use Location;
let mut index: = new;
// Insert events
for event in &events
// Bounding box query
let results = index.query_bbox;
// K-nearest neighbors
let nearest = index.nearest;
// Radius query (approximate, in degrees)
let nearby = index.query_radius;
Temporal Index (B-tree)
use TemporalIndex;
use ;
let mut index: = new;
for event in &events
// Time range query
let range = new;
let january_events = index.query_range;
// Before/after queries
let early_events = index.before;
let recent_events = index.after;
// Chronological iteration
for event in index.chronological
Spatiotemporal Index
Combined space-time queries with heatmap generation:
use ;
use ;
let mut index = new;
for event in &events
// Combined query: events in NYC during January
let bounds = new;
let range = month;
let results = index.query;
// Generate heatmap data for visualization
let grid = new; // 50x50 grid
let heatmap = index.heatmap;
// Export heatmap for visualization (see Visualization section)
for lat_idx in 0..heatmap.grid.lat_cells
Graph Analysis
Model event relationships as directed graphs using petgraph.
Building Graphs
use ;
// Create from events
let mut graph = from_events;
// Automatic connection strategies
graph.connect_temporal; // A โ B if A happens before B
graph.connect_spatial; // Connect events within 10km
graph.connect_thematic; // Connect events sharing tags
// Manual connections
let n1 = graph.get_node.unwrap;
let n2 = graph.get_node.unwrap;
graph.connect;
// Weighted connections
graph.connect_weighted;
Graph Queries
// Path finding
if let Some = graph.shortest_path
// Connectivity
let has_connection = graph.has_path;
// Neighborhood
let following_events = graph.successors;
let preceding_events = graph.predecessors;
// Structure analysis
let entry_points = graph.roots; // Events with no predecessors
let endpoints = graph.leaves; // Events with no successors
// Subgraph extraction
let january_subgraph = graph.subgraph_temporal;
let nyc_subgraph = graph.subgraph_spatial;
Edge Types
| Type | Description | Use Case |
|---|---|---|
Temporal |
Time sequence | A happens before B |
Spatial |
Geographic proximity | Events at same location |
Causal |
Cause and effect | A causes B |
Thematic |
Shared themes/tags | Related topics |
Reference |
Citation/mention | A references B |
Custom |
User-defined | Domain-specific |
I/O Formats
GeoJSON
Industry-standard format for geographic data. Compatible with Leaflet, Mapbox, QGIS, Google Earth.
use ;
// Export to GeoJSON
let format = with_options;
let mut output = Vecnew;
format.export?;
// Import from GeoJSON
let narrative: Narrative = format.import?;
CSV
For spreadsheet analysis and data science workflows:
use ;
let format = with_options;
// Round-trip
format.export?;
let imported: Narrative = format.import?;
Native JSON
Full-fidelity format preserving all metadata:
use ;
let format = pretty; // Human-readable
format.export?;
Visualization Integration
spatial-narrative is a data processing library, not a visualization tool. It produces data structures that integrate with mapping libraries:
Web (JavaScript)
Export to GeoJSON and use with Leaflet or Mapbox:
// Load exported GeoJSON
.
.;
Heatmaps
Convert Heatmap output to visualization format:
// Generate heatmap data
let heatmap = index.heatmap;
// Export as GeoJSON grid for visualization
let features: =
.flat_map
.collect;
Desktop GIS
Export to GeoJSON and open in:
- QGIS โ Full-featured open-source GIS
- ArcGIS โ Professional GIS platform
- Google Earth Pro โ 3D globe visualization
Graph Visualization
Export graph structure for network visualization tools:
// Export to DOT format for Graphviz
Examples
Run included examples:
# Core types and operations
# I/O format handling
# Spatial and temporal indexing
Performance
| Operation | Complexity | Notes |
|---|---|---|
| Spatial bbox query | O(log n + k) | R-tree, k = results |
| Temporal range query | O(log n + k) | B-tree |
| K-nearest neighbors | O(log n + k) | R-tree |
| Graph path finding | O((V + E) log V) | Dijkstra |
| Heatmap generation | O(n) | Single pass |
For datasets exceeding 1M events, consider:
- Streaming imports with
io::StreamingReader(planned) - Spatial partitioning by region
- Temporal partitioning by time period
- Parallel processing with
rayonfeature
API Reference
Full API documentation available at:
Or view online at docs.rs/spatial-narrative.
Use Cases
| Domain | Application |
|---|---|
| Journalism | Track story development across locations and time |
| Historical Research | Model timelines with precise geographic context |
| Urban Planning | Analyze event patterns in urban environments |
| Disaster Response | Correlate incident reports spatiotemporally |
| Travel & Tourism | Build location-aware travel narratives |
| Academic Research | Process geographic and temporal research data |
| Security Analysis | Pattern detection in event sequences |
Contributing
See CONTRIBUTING.md for guidelines.
# Run tests
# Run with all features
# Check formatting
# Run linter
License
MIT License โ see LICENSE for details.
Acknowledgments
Built with: