1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
use crate::temporal::tinstant::TInstant;
use crate::temporal::JSONCVariant;
use crate::{
boxes::STBox,
factory,
temporal::{number::tfloat::TFloat, temporal::Temporal},
};
use core::fmt;
use geos::{CoordDimensions, Geom, Geometry, WKBWriter};
use meos_sys::GSERIALIZED;
use std::{
ffi::{c_void, CStr, CString},
ptr, slice,
};
#[derive(Clone, Copy)]
pub struct Point(f64, f64, Option<f64>);
fn point_to_gserialize(point: Point, geodetic: bool) -> *mut meos_sys::GSERIALIZED {
let cstring = CString::new(point.to_string()).unwrap();
unsafe {
if geodetic {
meos_sys::geom_in(cstring.as_ptr().cast_mut(), -1)
} else {
meos_sys::geog_in(cstring.as_ptr().cast_mut(), -1)
}
}
}
pub(super) fn geometry_to_gserialized(geometry: &Geometry) -> *mut GSERIALIZED {
let mut writer = WKBWriter::new().expect("Failed to create WKBWriter");
writer.set_output_dimension(CoordDimensions::ThreeD);
let wkb: Vec<u8> = writer.write_wkb(geometry).unwrap();
let wkb_len = wkb.len();
unsafe {
meos_sys::geo_from_ewkb(
wkb.as_ptr(),
wkb_len,
geometry.get_srid().unwrap_or_default(),
)
}
}
pub(super) fn gserialized_to_geometry(
gs: *mut meos_sys::GSERIALIZED,
) -> Result<Geometry, geos::Error> {
let mut size = 0;
let endian = CString::new("xdr").unwrap();
let bytes = unsafe { meos_sys::geo_as_ewkb(gs, endian.as_ptr(), ptr::addr_of_mut!(size)) };
Geometry::new_from_wkb(unsafe { slice::from_raw_parts(bytes, size) })
}
pub(super) fn create_set_of_geometries(values: &[Geometry]) -> *mut meos_sys::Set {
let mut cgeos: Vec<_> = values.iter().map(geometry_to_gserialized).collect();
unsafe { meos_sys::geoset_make(cgeos.as_mut_ptr(), values.len() as i32) }
}
impl fmt::Display for Point {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if let Some(z) = self.2 {
f.write_fmt(format_args!("Point({}, {}, {})", self.0, self.1, z))
} else {
f.write_fmt(format_args!("Point({}, {})", self.0, self.1))
}
}
}
pub trait TPointTrait<const IS_GEODETIC: bool>: Temporal {
/// Returns the temporal point as a WKT string.
///
/// ## Arguments
///
/// * `precision` - The precision of the returned geometry.
///
/// ## Returns
///
/// A `String` representing the temporal point.
///
/// ## MEOS Functions
///
/// `tpoint_out`
fn as_wkt(&self, precision: i32) -> String {
let out_str = unsafe { meos_sys::tspatial_as_text(self.inner(), precision) };
let c_str = unsafe { CStr::from_ptr(out_str) };
let str = c_str.to_str().unwrap().to_owned();
unsafe { libc::free(out_str.cast::<c_void>()) };
str
}
/// Returns the temporal point as an EWKT string.
///
/// ## Arguments
///
/// * `precision` - The precision of the returned geometry.
///
/// ## Returns
///
/// A `String` representing the temporal point.
///
/// ## MEOS Functions
///
/// `tpoint_as_ewkt`
fn as_ewkt(&self, precision: i32) -> String {
let out_str = unsafe { meos_sys::tspatial_as_ewkt(self.inner(), precision) };
let c_str = unsafe { CStr::from_ptr(out_str) };
let str = c_str.to_str().unwrap().to_owned();
unsafe { libc::free(out_str.cast::<c_void>()) };
str
}
/// Returns the trajectory of the temporal point as a `GeoJSON` string.
///
/// ## Arguments
///
/// * `option` - The option to use when serializing the trajectory.
/// * `precision` - The precision of the returned geometry.
/// * `srs` - The spatial reference system of the returned geometry.
///
/// ## Returns
///
/// A `String` representing the trajectory of the temporal point.
///
/// ## MEOS Functions
///
/// `gserialized_as_geojson`
fn as_geojson(&self, variant: JSONCVariant, srs: &str) -> Option<String> {
let cstring = CString::new(srs).unwrap();
let trajectory = unsafe { meos_sys::tpoint_trajectory(self.inner(), false) };
let out_str =
unsafe { meos_sys::geo_as_geojson(trajectory, variant as i32, 5, cstring.as_ptr()) };
let c_str = unsafe { CStr::from_ptr(out_str) };
let str = c_str.to_str().map_err(|_| std::fmt::Error).ok()?;
let result = str.to_owned();
unsafe { libc::free(out_str.cast::<c_void>()) };
Some(result)
}
/// Returns the length of the trajectory.
///
/// ## Returns
///
/// A `f64` with the length of the trajectory.
///
/// ## MEOS Functions
///
/// `tpoint_length`
fn length(&self) -> f64 {
unsafe { meos_sys::tpoint_length(self.inner()) }
}
/// Returns the cumulative length of the trajectory.
///
/// ## Returns
///
/// A `TFloat` with the cumulative length of the trajectory.
///
/// ## MEOS Functions
///
/// `tpoint_cumulative_length`
fn cumulative_length(&self) -> TFloat {
factory::<TFloat>(unsafe { meos_sys::tpoint_cumulative_length(self.inner()) })
}
/// Returns the speed of the temporal point.
///
/// ## Returns
///
/// A `TFloat` with the speed of the temporal point.
///
/// ## MEOS Functions
///
/// `tpoint_speed`
fn speed(&self) -> TFloat {
factory::<TFloat>(unsafe { meos_sys::tpoint_speed(self.inner()) })
}
/// Returns the x coordinate of the temporal point.
///
/// ## Returns
///
/// A `TFloat` with the x coordinate of the temporal point.
///
/// ## MEOS Functions
///
/// `tpoint_get_x`
fn x(&self) -> TFloat {
factory::<TFloat>(unsafe { meos_sys::tpoint_get_x(self.inner()) })
}
/// Returns the y coordinate of the temporal point.
///
/// ## Returns
///
/// A `TFloat` with the y coordinate of the temporal point.
///
/// ## MEOS Functions
///
/// `tpoint_get_y`
fn y(&self) -> TFloat {
factory::<TFloat>(unsafe { meos_sys::tpoint_get_y(self.inner()) })
}
/// Returns the z coordinate of the temporal point.
///
/// ## Returns
///
/// A `TFloat` with the z coordinate of the temporal point.
///
/// ## MEOS Functions
///
/// `tpoint_get_z`
fn z(&self) -> Option<TFloat> {
if self.has_z() {
Some(factory::<TFloat>(unsafe {
meos_sys::tpoint_get_z(self.inner())
}))
} else {
None
}
}
/// Returns whether the temporal point has a z coordinate.
///
/// ## Returns
///
/// A `bool` indicating whether the temporal point has a z coordinate.
///
/// ## MEOS Functions
///
/// `tpoint_start_value`
fn has_z(&self) -> bool {
let ptr = unsafe { meos_sys::tpoint_get_z(self.inner()) };
!ptr.is_null()
}
/// Returns a `STBox` representing the bounding box of the temporal point.
///
/// ## Returns
///
/// A `STBox` with the bounding box.
///
/// ## MEOS Functions
///
/// `tspatial_to_stbox`
fn stbox(&self) -> STBox {
STBox::from_inner(unsafe { meos_sys::tspatial_to_stbox(self.inner()) })
}
/// Returns a collection of bounding boxes representing the segments of the temporal point.
///
/// ## Returns
///
/// A `Vec<STBox>` with the bounding boxes.
///
/// ## MEOS Functions
///
/// `tpoint_stboxes`
fn stboxes(&self) -> Vec<STBox> {
let mut count = 0;
let result = unsafe { meos_sys::tgeo_stboxes(self.inner(), ptr::addr_of_mut!(count)) };
unsafe {
std::slice::from_raw_parts(result, count as usize)
.iter()
.map(|&stbox| {
let mut boxed_stbox = Box::new(stbox);
let ptr: *mut meos_sys::STBox = &raw mut *boxed_stbox;
STBox::from_inner(ptr)
})
.collect()
}
}
/// Returns whether the temporal point is simple (i.e., does not self-intersect).
///
/// ## Returns
///
/// A `bool` indicating whether the temporal point is simple.
///
/// ## MEOS Functions
///
/// `tpoint_is_simple`
fn is_simple(&self) -> bool {
unsafe { meos_sys::tpoint_is_simple(self.inner()) }
}
/// Returns the temporal bearing between the temporal point and another point.
///
/// ## Arguments
///
/// * `other` - A `BaseGeometry` or `TPoint` to check the bearing to.
///
/// ## Returns
///
/// A `TFloat` indicating the temporal bearing between the temporal point and `other`.
///
/// ## MEOS Functions
///
/// `bearing_tpoint_point`, `bearing_tpoint_tpoint`
fn bearing(&self, other: &Self::Enum) -> TFloat {
factory::<TFloat>(unsafe { meos_sys::bearing_tpoint_tpoint(self.inner(), other.inner()) })
}
/// Returns the temporal bearing between the temporal point and another point.
///
/// ## Arguments
///
/// * `other` - A `BaseGeometry` or `TPoint` to check the bearing to.
///
/// ## Returns
///
/// A `TFloat` indicating the temporal bearing between the temporal point and `other`.
///
/// ## MEOS Functions
///
/// `bearing_tpoint_point`, `bearing_tpoint_tpoint`
fn bearing_geometry(&self, geometry: &Geometry) -> TFloat {
let geo = geometry_to_gserialized(geometry);
factory::<TFloat>(unsafe { meos_sys::bearing_tpoint_point(self.inner(), geo, false) })
}
/// Returns the temporal azimuth of the temporal point.
///
/// ## Returns
///
/// A `TFloatSequenceSet` indicating the temporal azimuth of the temporal point.
///
/// ## MEOS Functions
///
/// `tpoint_azimuth`
fn azimuth(&self) -> Option<TFloat> {
let result = unsafe { meos_sys::tpoint_azimuth(self.inner()) };
if result.is_null() {
None
} else {
Some(factory::<TFloat>(result))
}
}
/// Returns the angular difference of the temporal point.
///
/// ## Returns
///
/// A `TFloatSequenceSet` indicating the temporal angular difference of the temporal point.
///
/// ## MEOS Functions
///
/// `tpoint_angular_difference`
fn angular_difference(&self) -> Option<TFloat> {
let result = unsafe { meos_sys::tpoint_angular_difference(self.inner()) };
if result.is_null() {
None
} else {
Some(factory::<TFloat>(result))
}
}
/// Returns the time-weighted centroid of the temporal point.
///
/// ## Arguments
///
/// * `precision` - The precision of the returned geometry.
///
/// ## Returns
///
/// A `BaseGeometry` indicating the time-weighted centroid of the temporal point.
///
/// ## MEOS Functions
///
/// `tpoint_twcentroid`
fn time_weighted_centroid(&self) -> Result<Geometry, geos::Error> {
let gs = unsafe { meos_sys::tpoint_twcentroid(self.inner()) };
gserialized_to_geometry(gs)
}
/// Returns the trajectory of the temporal point as a geos geometry.
///
/// ## Arguments
///
/// * `unary_union` - True when the `ST_UnaryUnion` function is applied to
/// the result to remove redundant geometry components. Note that applying the
/// `ST_UnaryUnion` function is EXTREMELY slow as reported by Issue MobilityDB#679.
///
/// ## Returns
///
/// A `Geometry` representing the trajectory.
///
/// ## MEOS Functions
///
/// `gserialized_to_geos_geometry`
fn trajectory(&self, unary_union: bool) -> Result<Geometry, geos::Error> {
let gs = unsafe { meos_sys::tpoint_trajectory(self.inner(), unary_union) };
gserialized_to_geometry(gs)
}
// ------------------------- Spatial Reference System ----------------------
/// Returns the SRID.
///
/// MEOS Functions:
/// `tpoint_srid`
fn srid(&self) -> i32 {
unsafe { meos_sys::tspatial_srid(self.inner()) }
}
/// Returns a new `TPoint` with the given SRID.
///
/// MEOS Functions:
/// `tpoint_set_srid`
fn with_srid(&self, srid: i32) -> Self {
Self::from_inner_as_temporal(unsafe { meos_sys::tspatial_set_srid(self.inner(), srid) })
}
// ------------------------- Transformations -------------------------------
/// Round the coordinate values to a number of decimal places.
///
/// Returns:
/// A new `TGeomPoint` object.
///
/// MEOS Functions:
/// `tpoint_round`
fn round(&self, max_decimals: i32) -> Self {
Self::from_inner_as_temporal(unsafe {
meos_sys::temporal_round(self.inner(), max_decimals)
})
}
/// Split the temporal point into a collection of simple temporal points.
///
/// Returns:
/// A `Vec<Self::Enum>`.
///
/// MEOS Functions:
/// `tpoint_make_simple`
fn make_simple(&self) -> Vec<Self::Enum> {
let mut count = 0;
let result =
unsafe { meos_sys::tpoint_make_simple(self.inner(), ptr::addr_of_mut!(count)) };
unsafe {
std::slice::from_raw_parts(result, count as usize)
.iter()
.map(|&temporal| factory::<Self::Enum>(temporal))
.collect()
}
}
/// Expands `self` with `other`.
/// The result is equal to `self` but with the spatial dimensions
/// expanded by `other` in all directions.
///
/// Args:
/// other: The object to expand `self` with.
///
/// Returns:
/// A new `STBox` instance.
///
/// MEOS Functions:
/// `tpoint_expand_space`
fn expand(&self, distance: f64) -> STBox {
STBox::from_inner(unsafe { meos_sys::stbox_expand_space(self.stbox().inner(), distance) })
}
/// Returns a new `TPoint` of the same subclass of `self` transformed to another SRID.
///
/// Args:
/// srid: The desired SRID
///
/// Returns:
/// A new `TPoint` instance.
///
/// MEOS Functions:
/// `tpoint_transform`
fn transform(&self, srid: i32) -> Self {
Self::from_inner_as_temporal(unsafe { meos_sys::tspatial_transform(self.inner(), srid) })
}
// ------------------------- Restrictions ----------------------------------
/// Returns a new temporal object with the values of `self` restricted to `other`.
///
/// Args:
/// other: An object to restrict the values of `self` to.
///
/// Returns:
/// A new `TPoint` with the values of `self` restricted to `other`.
///
/// MEOS Functions:
/// `tpoint_at_value`, `tpoint_at_stbox`, `temporal_at_values`,
/// `temporal_at_timestamp`, `temporal_at_tstzset`, `temporal_at_tstzspan`, `temporal_at_tstzspanset`
fn at_point(&self, point: Point) -> Self::Enum {
let geo = point_to_gserialize(point, IS_GEODETIC);
factory::<Self::Enum>(unsafe { meos_sys::tpoint_at_value(self.inner(), geo) })
}
/// Returns a new temporal object with the values of `self` restricted to `other`.
///
/// Args:
/// other: An object to restrict the values of `self` to.
///
/// Returns:
/// A new `TPoint` with the values of `self` restricted to `other`.
///
/// MEOS Functions:
/// `tpoint_at_value`, `tpoint_at_stbox`, `temporal_at_values`,
/// `temporal_at_timestamp`, `temporal_at_tstzset`, `temporal_at_tstzspan`, `temporal_at_tstzspanset`
fn at_geometry(&self, geometry: &Geometry) -> Self::Enum {
let geo = geometry_to_gserialized(geometry);
factory::<Self::Enum>(unsafe { meos_sys::tpoint_at_value(self.inner(), geo) })
}
/// Returns a new temporal object with the values of `self` restricted to `other`.
///
/// Args:
/// other: An object to restrict the values of `self` to.
///
/// Returns:
/// A new `TPoint` with the values of `self` restricted to `other`.
///
/// MEOS Functions:
/// `tpoint_at_value`, `tpoint_at_stbox`, `temporal_at_values`,
/// `temporal_at_timestamp`, `temporal_at_tstzset`, `temporal_at_tstzspan`, `temporal_at_tstzspanset`
fn at_geometries(&self, geometries: &[Geometry]) -> Self::Enum {
let mut pointers: Vec<_> = geometries
.iter()
.map(|g| {
let bytes = g.to_wkb().unwrap();
let bytes_len = bytes.len();
unsafe {
meos_sys::geo_from_ewkb(
bytes.as_ptr(),
bytes_len,
g.get_srid().expect("No SRID"),
)
}
})
.collect();
let geoset = unsafe { meos_sys::geoset_make(pointers.as_mut_ptr(), pointers.len() as i32) };
factory::<Self::Enum>(unsafe { meos_sys::temporal_at_values(self.inner(), geoset) })
}
/// Returns a new temporal object with the values of `self` restricted to the complement of `other`.
///
/// Args:
/// other: An object to restrict the values of `self` to the complement of.
///
/// Returns:
/// A new `TPoint` with the values of `self` restricted to the complement of `other`.
///
/// MEOS Functions:
/// `tpoint_minus_value`, `tpoint_minus_stbox`, `temporal_minus_values`,
/// `temporal_minus_timestamp`, `temporal_minus_tstzset`, `temporal_minus_tstzspan`, `temporal_minus_tstzspanset`
fn minus_point(&self, point: Point) -> Self::Enum {
let geo = point_to_gserialize(point, IS_GEODETIC);
factory::<Self::Enum>(unsafe { meos_sys::tpoint_minus_value(self.inner(), geo) })
}
/// Returns a new temporal object with the values of `self` restricted to the complement of `other`.
///
/// Args:
/// other: An object to restrict the values of `self` to the complement of.
///
/// Returns:
/// A new `TPoint` with the values of `self` restricted to the complement of `other`.
///
/// MEOS Functions:
/// `tpoint_minus_value`, `tpoint_minus_stbox`, `temporal_minus_values`,
/// `temporal_minus_timestamp`, `temporal_minus_tstzset`, `temporal_minus_tstzspan`, `temporal_minus_tstzspanset`
fn minus_geometry(&self, geometry: &Geometry) -> Self::Enum {
let geo = geometry_to_gserialized(geometry);
factory::<Self::Enum>(unsafe { meos_sys::tpoint_minus_value(self.inner(), geo) })
}
/// Returns a new temporal object with the values of `self` restricted to the complement of `other`.
///
/// Args:
/// other: An object to restrict the values of `self` to the complement of.
///
/// Returns:
/// A new `TPoint` with the values of `self` restricted to the complement of `other`.
///
/// MEOS Functions:
/// `tpoint_minus_value`, `tpoint_minus_stbox`, `temporal_minus_values`,
/// `temporal_minus_timestamp`, `temporal_minus_tstzset`, `temporal_minus_tstzspan`, `temporal_minus_tstzspanset`
fn minus_geometries(&self, geometries: &[Geometry]) -> Self::Enum {
let mut pointers: Vec<_> = geometries
.iter()
.map(|g| {
let bytes = g.to_wkb().unwrap();
let bytes_len = bytes.len();
unsafe {
meos_sys::geo_from_ewkb(
bytes.as_ptr(),
bytes_len,
g.get_srid().expect("No SRID"),
)
}
})
.collect();
let geoset = unsafe { meos_sys::geoset_make(pointers.as_mut_ptr(), pointers.len() as i32) };
factory::<Self::Enum>(unsafe { meos_sys::temporal_minus_values(self.inner(), geoset) })
}
// ------------------------- Position Operations ---------------------------
/// Returns whether the bounding box of `self` is below to the bounding box of `other`.
///
/// Args:
/// other: A box or a temporal object to compare to `self`.
///
/// Returns:
/// True if below, False otherwise.
///
/// See Also:
/// `TsTzSpan::is_before`
fn is_below(&self, other: &Self::Enum) -> bool {
unsafe { meos_sys::below_tspatial_tspatial(self.inner(), other.inner()) }
}
/// Returns whether the bounding box of `self` is over or below to the bounding box of `other`.
///
/// Args:
/// other: A box or a temporal object to compare to `self`.
///
/// Returns:
/// True if over or below, False otherwise.
///
/// See Also:
/// `TsTzSpan::is_over_or_before`
fn is_over_or_below(&self, other: &Self::Enum) -> bool {
unsafe { meos_sys::overbelow_tspatial_tspatial(self.inner(), other.inner()) }
}
/// Returns whether the bounding box of `self` is above to the bounding box of `other`.
///
/// Args:
/// other: A box or a temporal object to compare to `self`.
///
/// Returns:
/// True if above, False otherwise.
///
/// See Also:
/// `TsTzSpan::is_after`
fn is_above(&self, other: &Self::Enum) -> bool {
unsafe { meos_sys::above_tspatial_tspatial(self.inner(), other.inner()) }
}
/// Returns whether the bounding box of `self` is over or above to the bounding box of `other`.
///
/// Args:
/// other: A box or a temporal object to compare to `self`.
///
/// Returns:
/// True if over or above, False otherwise.
///
/// See Also:
/// `TsTzSpan::is_over_or_before`
fn is_over_or_above(&self, other: &Self::Enum) -> bool {
unsafe { meos_sys::overabove_tspatial_tspatial(self.inner(), other.inner()) }
}
/// Returns whether the bounding box of `self` is front to the bounding box of `other`. Both must have 3rd dimension
///
/// Args:
/// other: A box or a temporal object to compare to `self`.
///
/// Returns:
/// True if front, False otherwise.
fn is_front(&self, other: &Self::Enum) -> Option<bool> {
if self.has_z() {
Some(unsafe { meos_sys::front_tspatial_tspatial(self.inner(), other.inner()) })
} else {
None
}
}
/// Returns whether the bounding box of `self` is over or front to the bounding box of `other`.
///
/// Args:
/// other: A box or a temporal object to compare to `self`.
///
/// Returns:
/// True if over or front, False otherwise.
///
/// See Also:
/// `TsTzSpan::is_over_or_before`
fn is_over_or_front(&self, other: &Self::Enum) -> Option<bool> {
if self.has_z() {
Some(unsafe { meos_sys::overfront_tspatial_tspatial(self.inner(), other.inner()) })
} else {
None
}
}
/// Returns whether the bounding box of `self` is behind to the bounding box of `other`.
///
/// Args:
/// other: A box or a temporal object to compare to `self`.
///
/// Returns:
/// True if behind, False otherwise.
fn is_behind(&self, other: &Self::Enum) -> Option<bool> {
if self.has_z() {
Some(unsafe { meos_sys::back_tspatial_tspatial(self.inner(), other.inner()) })
} else {
None
}
}
/// Returns whether the bounding box of `self` is over or behind to the bounding box of `other`.
///
/// Args:
/// other: A box or a temporal object to compare to `self`.
///
/// Returns:
/// True if over or behind, False otherwise.
fn is_over_or_behind(&self, other: &Self::Enum) -> Option<bool> {
if self.has_z() {
Some(unsafe { meos_sys::overback_tspatial_tspatial(self.inner(), other.inner()) })
} else {
None
}
}
/// Returns a new temporal boolean indicating whether the temporal point is contained by `container`.
///
/// # Arguments
///
/// * `container` - An object to check for containing `self`.
///
/// # Returns
///
/// A new `TBool` indicating whether the temporal point is contained by `container`.
///
/// # MEOS Functions
///
/// * `tcontains_geo_tgeo`
fn is_spatially_contained_in_geometry(&self, container: &Geometry) -> Self::TBoolType {
let geo = geometry_to_gserialized(container);
Self::TBoolType::from_inner_as_temporal(unsafe {
meos_sys::tcontains_geo_tgeo(geo, self.inner(), false, false)
})
}
/// Returns a new temporal boolean indicating whether the temporal point intersects `geometry`.
///
/// # Arguments
///
/// * `geometry` - An object to check for intersection with.
///
/// # Returns
///
/// A new `TBool` indicating whether the temporal point intersects `geometry`.
///
/// # MEOS Functions
///
/// * `tintersects_tgeo_geo`
fn is_disjoint_to_geometry(&self, geometry: &Geometry) -> Self::TBoolType {
let geo = geometry_to_gserialized(geometry);
Self::TBoolType::from_inner_as_temporal(unsafe {
meos_sys::tdisjoint_tgeo_geo(self.inner(), geo, false, false)
})
}
/// Returns a new temporal boolean indicating whether the temporal point is within `distance` of `other`.
///
/// # Arguments
///
/// * `other` - An object to check the distance to.
/// * `distance` - The distance to check in units of the spatial reference system.
///
/// # Returns
///
/// A new `TBool` indicating whether the temporal point is within `distance` of `other`.
///
/// # MEOS Functions
///
/// * `tdwithin_tgeo_geo`, `tdwithin_tgeo_tgeo`
fn is_within_distance(&self, other: &Self::Enum, distance: f64) -> Self::TBoolType {
Self::TBoolType::from_inner_as_temporal(unsafe {
meos_sys::tdwithin_tgeo_tgeo(self.inner(), other.inner(), distance, false, false)
})
}
/// Returns a new temporal boolean indicating whether the temporal point is within `distance` of `geometry`.
///
/// # Arguments
///
/// * `geometry` - An object to check the distance to.
/// * `distance` - The distance to check in units of the spatial reference system.
///
/// # Returns
///
/// A new `TBool` indicating whether the temporal point is within `distance` of `geometry`.
///
/// # MEOS Functions
///
/// * `tdwithin_tgeo_geo`, `tdwithin_tgeo_tgeo`
fn within_distance_of_geometry(&self, geometry: &Geometry, distance: f64) -> Self::TBoolType {
let geo = geometry_to_gserialized(geometry);
Self::TBoolType::from_inner_as_temporal(unsafe {
meos_sys::tdwithin_tgeo_geo(self.inner(), geo, distance, false, false)
})
}
/// Returns a new temporal boolean indicating whether the temporal point intersects `geometry`.
///
/// # Arguments
///
/// * `geometry` - An object to check for intersection with.
///
/// # Returns
///
/// A new `TBool` indicating whether the temporal point intersects `geometry`.
///
/// # MEOS Functions
///
/// * `tintersects_tgeo_geo`
fn intersects_geometry(&self, geometry: &Geometry) -> Self::TBoolType {
let geo = geometry_to_gserialized(geometry);
Self::TBoolType::from_inner_as_temporal(unsafe {
meos_sys::tintersects_tgeo_geo(self.inner(), geo, false, false)
})
}
/// Returns a new temporal boolean indicating whether the temporal point touches `other`.
///
/// # Arguments
///
/// * `other` - An object to check for touching with.
///
/// # Returns
///
/// A new `TBool` indicating whether the temporal point touches `other`.
///
/// # MEOS Functions
///
/// * `ttouches_tgeo_geo`
fn touches_geometry(&self, geometry: &Geometry) -> Self::TBoolType {
let geo = geometry_to_gserialized(geometry);
Self::TBoolType::from_inner_as_temporal(unsafe {
meos_sys::ttouches_tgeo_geo(self.inner(), geo, false, false)
})
}
/// Returns the temporal distance between the temporal point and `other`.
///
/// # Arguments
///
/// * `other` - An object to check the distance to.
///
/// # Returns
///
/// A new `TFloat` indicating the temporal distance between the temporal point and `other`.
///
/// # MEOS Functions
///
/// * `distance_tgeo_point`, `distance_tgeo_tgeo`
fn distance(&self, other: &Self::Enum) -> TFloat {
factory::<TFloat>(unsafe { meos_sys::tdistance_tgeo_tgeo(self.inner(), other.inner()) })
}
/// Returns the temporal distance between the temporal point and `other`.
///
/// # Arguments
///
/// * `geometry` - An object to check the distance to.
///
/// # Returns
///
/// A new `TFloat` indicating the temporal distance between the temporal point and `geometry`.
///
/// # MEOS Functions
///
/// * `distance_tgeo_point`, `distance_tgeo_tgeo`
fn distance_to_point(&self, point: Point) -> TFloat {
let point = point_to_gserialize(point, IS_GEODETIC);
factory::<TFloat>(unsafe { meos_sys::tdistance_tgeo_geo(self.inner(), point) })
}
/// Returns the nearest approach distance between the temporal point and `other`.
///
/// # Arguments
///
/// * `other` - An object to check the nearest approach distance to.
///
/// # Returns
///
/// A `f64` indicating the nearest approach distance between the temporal point and `other`.
///
/// # MEOS Functions
///
/// * `nad_tgeo_geo`, `nad_tgeo_stbox`, `nad_tgeo_tgeo`
fn nearest_approach_distance(&self, other: &Self::Enum) -> f64 {
unsafe { meos_sys::nad_tgeo_tgeo(self.inner(), other.inner()) }
}
/// Returns the nearest approach distance between the temporal point and `other`.
///
/// # Arguments
///
/// * `geometry` - An object to check the nearest approach distance to.
///
/// # Returns
///
/// A `f64` indicating the nearest approach distance between the temporal point and `geometry`.
///
/// # MEOS Functions
///
/// * `nad_tgeo_geo`, `nad_tgeo_stbox`, `nad_tgeo_tgeo`
fn nearest_approach_distance_to_geometry(&self, geometry: &Geometry) -> f64 {
let geo = geometry_to_gserialized(geometry);
unsafe { meos_sys::nad_tgeo_geo(self.inner(), geo) }
}
/// Returns the nearest approach instant between the temporal point and `other`.
///
/// # Arguments
///
/// * `other` - An object to check the nearest approach instant to.
///
/// # Returns
///
/// A new temporal instant indicating the nearest approach instant between the temporal point and `other`.
///
/// # MEOS Functions
///
/// * `nai_tgeo_geo`, `nai_tgeo_tgeo`
fn nearest_approach_instant(&self, other: &Self::Enum) -> Self::TI {
Self::TI::from_inner(unsafe { meos_sys::nai_tgeo_tgeo(self.inner(), other.inner()) })
}
/// Returns the nearest approach instant between the temporal point and `other`.
///
/// # Arguments
///
/// * `other` - An object to check the nearest approach instant to.
///
/// # Returns
///
/// A new temporal instant indicating the nearest approach instant between the temporal point and `other`.
///
/// # MEOS Functions
///
/// * `nai_tgeo_geo`
fn nearest_approach_instant_to_geometry(&self, geometry: &Geometry) -> Self::TI {
let geo = geometry_to_gserialized(geometry);
Self::TI::from_inner(unsafe { meos_sys::nai_tgeo_geo(self.inner(), geo) })
}
/// Returns the shortest line between the temporal point and `other`.
///
/// # Arguments
///
/// * `other` - An object to check the shortest line to.
///
/// # Returns
///
/// A new `BaseGeometry` indicating the shortest line between the temporal point and `other`.
///
/// # MEOS Functions
///
/// * `shortestline_tgeo_geo`, `shortestline_tgeo_tgeo`
fn shortest_line(&self, other: &Self::Enum) -> Result<Geometry, geos::Error> {
let gs = unsafe { meos_sys::shortestline_tgeo_tgeo(self.inner(), other.inner()) };
gserialized_to_geometry(gs)
}
/// Returns the shortest line between the temporal point and `other`.
///
/// # Arguments
///
/// * `other` - An object to check the shortest line to.
///
/// # Returns
///
/// A new `BaseGeometry` indicating the shortest line between the temporal point and `other`.
///
/// # MEOS Functions
///
/// * `shortestline_tgeo_geo`, `shortestline_tgeo_tgeo`
fn shortest_line_to_geometry(&self, geometry: &Geometry) -> Result<Geometry, geos::Error> {
let geo = geometry_to_gserialized(geometry);
let gs = unsafe { meos_sys::shortestline_tgeo_geo(self.inner(), geo) };
gserialized_to_geometry(gs)
}
// /// Split the temporal point into segments following the tiling of the bounding box.
// ///
// /// # Arguments
// ///
// /// * `size` - The size of the spatial tiles. If `self` has a spatial dimension and this argument is not provided, the tiling will be only temporal.
// /// * `duration` - The duration of the temporal tiles. If `self` has a time dimension and this argument is not provided, the tiling will be only spatial.
// /// * `origin` - The origin of the spatial tiling. If not provided, the origin will be (0, 0, 0).
// /// * `start` - The start time of the temporal tiling. If not provided, the start time used by default is Monday, January 3, 2000.
// /// * `remove_empty` - If True, remove the tiles that are empty.
// ///
// /// # Returns
// ///
// /// A list of `TPoint` objects.
// ///
// /// # See Also
// ///
// /// `STBox::tile`
// fn tile(
// &self,
// size: f64,
// duration: Option<&str>,
// origin: Option<&impl Geometry>,
// start: Option<&str>,
// remove_empty: bool,
// ) -> Vec<Self::Enum> {
// let bbox = STBox::from_tpoint(self);
// let tiles = bbox.tile(size, duration, origin, start);
// if remove_empty {
// tiles.iter().filter_map(|tile| self.at(tile)).collect()
// } else {
// tiles.iter().map(|tile| self.at(tile)).collect()
// }
// }
// /// Splits `self` into fragments with respect to space buckets.
// ///
// /// # Arguments
// ///
// /// * `xsize` - Size of the x dimension.
// /// * `ysize` - Size of the y dimension.
// /// * `zsize` - Size of the z dimension.
// /// * `origin` - The origin of the spatial tiling. If not provided, the origin will be (0, 0, 0).
// /// * `bitmatrix` - If True, use a bitmatrix to speed up the process.
// /// * `include_border` - If True, include the upper border in the box.
// ///
// /// # Returns
// ///
// /// A list of temporal points.
// ///
// /// # MEOS Functions
// ///
// /// * `tpoint_value_split`
// fn space_split(
// &self,
// xsize: f64,
// ysize: Option<f64>,
// zsize: Option<f64>,
// origin: Option<&impl Geometry>,
// bitmatrix: bool,
// include_border: bool,
// ) -> Vec<Temporal> {
// let ysz = ysize.unwrap_or(xsize);
// let zsz = zsize.unwrap_or(xsize);
// let gs = match origin {
// Some(geo) => geo_to_gserialized(geo, self.is_geog_point()),
// None => {
// if self.is_geog_point() {
// pgis_geography_in("Point(0 0 0)", -1)
// } else {
// pgis_geometry_in("Point(0 0 0)", -1)
// }
// }
// };
// let (fragments, values, count) =
// tpoint_space_split(self.inner(), xsize, ysz, zsz, gs, bitmatrix, include_border);
// (0..count).map(|i| Temporal::new(fragments[i])).collect()
// }
// /// Splits `self` into fragments with respect to space and tstzspan buckets.
// ///
// /// # Arguments
// ///
// /// * `xsize` - Size of the x dimension.
// /// * `duration` - Duration of the tstzspan buckets.
// /// * `ysize` - Size of the y dimension.
// /// * `zsize` - Size of the z dimension.
// /// * `origin` - The origin of the spatial tiling. If not provided, the origin will be (0, 0, 0).
// /// * `time_start` - Start time of the first tstzspan bucket. If None, the start time used by default is Monday, January 3, 2000.
// /// * `bitmatrix` - If True, use a bitmatrix to speed up the process.
// /// * `include_border` - If True, include the upper border in the box.
// ///
// /// # Returns
// ///
// /// A list of temporal floats.
// ///
// /// # MEOS Functions
// ///
// /// * `tfloat_value_time_split`
// fn space_time_split(
// &self,
// xsize: f64,
// duration: &str,
// ysize: Option<f64>,
// zsize: Option<f64>,
// origin: Option<&impl Geometry>,
// time_start: Option<&str>,
// bitmatrix: bool,
// include_border: bool,
// ) -> Vec<Temporal> {
// let ysz = ysize.unwrap_or(xsize);
// let zsz = zsize.unwrap_or(xsize);
// let dt = pg_interval_in(duration, -1);
// let gs = match origin {
// Some(geo) => geo_to_gserialized(geo, self.is_geog_point()),
// None => {
// if self.is_geog_point() {
// pgis_geography_in("Point(0 0 0)", -1)
// } else {
// pgis_geometry_in("Point(0 0 0)", -1)
// }
// }
// };
// let st = match time_start {
// Some(start) => pg_timestamptz_in(start, -1),
// None => pg_timestamptz_in("2000-01-03", -1),
// };
// let (fragments, points, times, count) = tpoint_space_time_split(
// self.inner(),
// xsize,
// ysz,
// zsz,
// dt,
// gs,
// st,
// bitmatrix,
// include_border,
// );
// (0..count).map(|i| Temporal::new(fragments[i])).collect()
// }
}
macro_rules! impl_tpoint_traits {
($type:ty, $temporal_type:ident, $is_geodetic:expr, $tpoint_type:ident) => {
paste::paste! {
impl Collection for $type {
impl_collection!(tspatial, Geometry);
fn contains(&self, element: &Self::Type) -> bool {
unsafe { meos_sys::contains_tspatial_stbox(self.inner(), meos_sys::geo_to_stbox(geometry_to_gserialized(element))) }
}
}
impl_simple_traits_for_temporal!($type, with_drop);
impl fmt::Debug for $type {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.as_wkt(5))
}
}
impl SimplifiableTemporal for $type {}
impl Temporal for $type {
type TI = [<T $tpoint_type PointInstant>];
type TS = [<T $tpoint_type PointSequence>];
type TSS = [<T $tpoint_type PointSequenceSet>];
type TBB = STBox;
type Enum = [<T $tpoint_type Point>];
type TBoolType = [<TBool $temporal_type>];
impl_always_and_ever_value_equality_functions!(geo, geometry_to_gserialized);
fn from_inner_as_temporal(inner: *mut meos_sys::Temporal) -> Self {
Self {
#[allow(clippy::cast_ptr_alignment)]
_inner: ptr::NonNull::new(inner.cast::<meos_sys::[<T $temporal_type>]>()).expect("Null pointers not allowed"),
}
}
fn inner(&self) -> *const meos_sys::Temporal {
self._inner.as_ptr() as *const meos_sys::Temporal
}
fn bounding_box(&self) -> Self::TBB {
STBox::from_inner(unsafe { meos_sys::tspatial_to_stbox(self.inner()) })
}
fn values(&self) -> Vec<Self::Type> {
let mut count = 0;
unsafe {
let values = meos_sys::tgeo_values(self.inner(), ptr::addr_of_mut!(count));
std::slice::from_raw_parts(values, count as usize)
.into_iter()
.map(|&gs| gserialized_to_geometry(gs))
.map(Result::unwrap)
.collect()
}
}
fn start_value(&self) -> Self::Type {
gserialized_to_geometry(unsafe { meos_sys::tgeo_start_value(self.inner()) }).unwrap()
}
fn end_value(&self) -> Self::Type {
gserialized_to_geometry(unsafe { meos_sys::tgeo_end_value(self.inner()) }).unwrap()
}
fn value_at_timestamp<Tz: TimeZone>(
&self,
timestamp: DateTime<Tz>,
) -> Option<Self::Type> {
let mut result: mem::MaybeUninit<*mut meos_sys::GSERIALIZED> = mem::MaybeUninit::uninit();
unsafe {
let success = meos_sys::tgeo_value_at_timestamptz(
self.inner(),
to_meos_timestamp(×tamp),
true,
result.as_mut_ptr(),
);
if success {
Some(gserialized_to_geometry(result.assume_init()).unwrap())
} else {
None
}
}
}
fn at_value(&self, value: &Self::Type) -> Option<Self::Enum> {
let result = unsafe { meos_sys::tpoint_at_value(self.inner(), geometry_to_gserialized(value)) };
if !result.is_null() {
Some(factory::<Self::Enum>(result))
} else {
None
}
}
fn at_values(&self, values: &[Self::Type]) -> Option<Self::Enum> {
unsafe {
let mut cgeos: Vec<_> = values.into_iter().map(|geo| geometry_to_gserialized(&geo)).collect();
let set = meos_sys::geoset_make(cgeos.as_mut_ptr(), values.len() as i32);
let result = meos_sys::temporal_at_values(self.inner(), set);
if !result.is_null() {
Some(factory::<Self::Enum>(result))
} else {
None
}
}
}
fn minus_value(&self, value: Self::Type) -> Self::Enum {
factory::<Self::Enum>(unsafe {
meos_sys::tpoint_minus_value(self.inner(), geometry_to_gserialized(&value))
})
}
fn minus_values(&self, values: &[Self::Type]) -> Self::Enum {
factory::<Self::Enum>(unsafe {
let mut cgeos: Vec<_> = values.into_iter().map(|geo| geometry_to_gserialized(&geo)).collect();
let set = meos_sys::geoset_make(cgeos.as_mut_ptr(), values.len() as i32);
meos_sys::temporal_minus_values(self.inner(), set)
})
}
fn temporal_equal_value(&self, value: &Self::Type) -> Self::TBoolType {
Self::TBoolType::from_inner_as_temporal(unsafe {
meos_sys::teq_tgeo_geo(self.inner(), geometry_to_gserialized(value))
})
}
fn temporal_not_equal_value(&self, value: &Self::Type) -> Self::TBoolType {
Self::TBoolType::from_inner_as_temporal(unsafe {
meos_sys::tne_tgeo_geo(self.inner(), geometry_to_gserialized(value))
})
}
}
}
};
}
pub(super) use impl_tpoint_traits;