Skip to content

Commit 3e04f0e

Browse files
committed
wip
1 parent 07cb9ce commit 3e04f0e

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

examples/04.rs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
use std::collections::hash_map::Entry;
2+
use std::collections::HashMap;
3+
use std::error::Error;
4+
use std::ffi::{c_char, CString};
5+
use std::fs::File;
6+
use std::ptr::null_mut;
7+
8+
// todo;; safe interface
9+
use meos_sys::*;
10+
11+
const INST_WKT: &str = "POINT(1 1)@2000-01-01";
12+
13+
#[derive(Debug, serde::Deserialize)]
14+
struct AisRecord {
15+
t: String,
16+
mmsi: i64,
17+
latitude: f64,
18+
longitude: f64,
19+
sog: f64,
20+
}
21+
22+
struct TripRecord {
23+
mmsi: i64,
24+
trip: *mut TSequence,
25+
}
26+
27+
const NO_INSTANTS_BATCH: i32 = 1000;
28+
29+
fn main() -> Result<(), Box<dyn Error>> {
30+
unsafe {
31+
meos_initialize(null_mut(), None);
32+
}
33+
let file = File::open("tests/data/ais_instants.csv")?;
34+
let mut rdr = csv::Reader::from_reader(file);
35+
36+
let mut i = 0;
37+
let mut trips: HashMap<i64, TripRecord> = HashMap::new();
38+
for result in rdr.deserialize() {
39+
print!("{i} [");
40+
i += 1;
41+
let record: AisRecord = result?;
42+
let mmsi = record.mmsi;
43+
let t_ptr = CString::new(record.t)?;
44+
let ts = unsafe { pg_timestamp_in(t_ptr.as_ptr(), -1) };
45+
46+
unsafe {
47+
let t_out = pg_timestamp_out(ts);
48+
let t_out = CString::from_raw(t_out);
49+
let point_buffer = format!(
50+
"SRID=4326;Point({} {})@{}+00",
51+
record.longitude,
52+
record.latitude,
53+
t_out.to_str()?
54+
);
55+
print!("{mmsi} - {point_buffer}");
56+
let gp_ptr = CString::new(point_buffer)?;
57+
match trips.entry(record.mmsi) {
58+
Entry::Occupied(t) => {
59+
let r = t.get();
60+
let inst = tgeompoint_in(gp_ptr.as_ptr());
61+
if !inst.is_null() && !r.trip.is_null() {
62+
tsequence_append_tinstant(
63+
r.trip,
64+
inst as *mut TInstant,
65+
0.0,
66+
null_mut(),
67+
true,
68+
);
69+
print!(".");
70+
} else {
71+
print!("x")
72+
}
73+
}
74+
Entry::Vacant(t) => {
75+
// TInstant *inst = (TInstant *) tgeogpoint_in(point_buffer);
76+
let inst = tgeompoint_in(gp_ptr.as_ptr());
77+
78+
let arr = [inst];
79+
let trip = tsequence_make_exp(
80+
arr.as_ptr() as *mut *const TInstant,
81+
1,
82+
NO_INSTANTS_BATCH * 1000,
83+
true,
84+
true,
85+
interpType_LINEAR,
86+
false,
87+
);
88+
89+
let r = TripRecord { mmsi, trip };
90+
tsequence_append_tinstant(trip, inst as *mut TInstant, 0.0, null_mut(), true);
91+
92+
t.insert(r);
93+
}
94+
}
95+
}
96+
println!("]");
97+
}
98+
99+
println!("Total trips: {}", trips.len());
100+
101+
Ok(())
102+
}

0 commit comments

Comments
 (0)