getting_started/
getting_started.rs1use spatio::{Point3d, Spatio, TemporalPoint};
2use spatio_types::geo::Point;
3use std::time::{Duration, SystemTime, UNIX_EPOCH};
4
5fn main() -> Result<(), Box<dyn std::error::Error>> {
6 env_logger::init();
8
9 println!("=== Spatio - Getting Started ===\n");
10
11 let db = Spatio::memory()?;
13 println!("✓ Created in-memory database\n");
14
15 println!("1. Spatial Point Storage (Hot State)");
17 println!("------------------------------------");
18
19 let nyc = Point3d::new(-74.0060, 40.7128, 0.0);
21 let london = Point3d::new(-0.1278, 51.5074, 0.0);
22 let paris = Point3d::new(2.3522, 48.8566, 0.0);
23
24 db.upsert(
25 "cities",
26 "nyc",
27 nyc.clone(),
28 serde_json::json!({"data": "New York"}),
29 None,
30 )?;
31 println!(" Stored nyc at ({}, {})", nyc.x(), nyc.y());
32
33 db.upsert(
34 "cities",
35 "london",
36 london.clone(),
37 serde_json::json!({"data": "London"}),
38 None,
39 )?;
40 println!(" Stored london at ({}, {})", london.x(), london.y());
41
42 db.upsert(
43 "cities",
44 "paris",
45 paris.clone(),
46 serde_json::json!({"data": "Paris"}),
47 None,
48 )?;
49 println!(" Stored paris at ({}, {})", paris.x(), paris.y());
50 println!(" Stored 3 cities with automatic spatial indexing");
51
52 let nearby = db.query_radius("cities", &london, 500_000.0, 10)?;
55 println!(" Found {} cities within 500km of London:", nearby.len());
56 for (loc, dist) in &nearby {
57 println!(
58 " - {} ({:.1}m away): {:?}",
59 loc.object_id, dist, loc.metadata
60 );
61 }
62 println!();
63
64 println!("2. Trajectory Tracking (Cold State)");
66 println!("-----------------------------------");
67
68 let vehicle_path = vec![
69 TemporalPoint {
70 point: Point::new(-74.0060, 40.7128),
71 timestamp: UNIX_EPOCH + Duration::from_secs(1640995200),
72 },
73 TemporalPoint {
74 point: Point::new(-74.0040, 40.7150),
75 timestamp: UNIX_EPOCH + Duration::from_secs(1640995260),
76 },
77 TemporalPoint {
78 point: Point::new(-74.0020, 40.7172),
79 timestamp: UNIX_EPOCH + Duration::from_secs(1640995320),
80 },
81 ];
82
83 db.insert_trajectory("logistics", "vehicle:truck001", &vehicle_path)?;
84 println!(
85 " Stored vehicle trajectory with {} waypoints",
86 vehicle_path.len()
87 );
88
89 let path_segment = db.query_trajectory(
91 "logistics",
92 "vehicle:truck001",
93 UNIX_EPOCH + Duration::from_secs(1640995200),
94 UNIX_EPOCH + Duration::from_secs(1640995320),
95 100,
96 )?;
97 println!(
98 " Retrieved {} waypoints from trajectory\n",
99 path_segment.len()
100 );
101
102 println!("3. Historical Data Ingestion");
104 println!("----------------------------");
105
106 let past_time = SystemTime::now() - Duration::from_secs(3600);
108 let past_pos = Point3d::new(10.0, 10.0, 0.0);
109
110 db.upsert(
111 "fleet",
112 "old_truck",
113 past_pos,
114 serde_json::json!({"data": "Historical Data"}),
115 Some(spatio::SetOptions::with_timestamp(past_time)),
116 )?;
117 println!(" Ingested historical data point\n");
118
119 println!("4. Database Statistics");
121 println!("----------------------");
122
123 let stats = db.stats();
124 println!(" Stats available: {:?}", stats);
125
126 println!("=== Getting Started Complete! ===");
127 println!("\nKey Features Demonstrated:");
128 println!(" • Real-time location updates (Hot State)");
129 println!(" • Spatial radius queries");
130 println!(" • Trajectory tracking (Cold State)");
131 println!(" • Historical data ingestion");
132
133 Ok(())
134}