zerometry 0.3.0

Make some zerocopy operations on serialized geo data
Documentation
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
#![doc = include_str!("../README.md")]
#![warn(missing_docs)]

mod bounding_box;
mod coord;
mod coords;
mod relation;
mod segment;
#[cfg(test)]
mod test;
mod zine;
mod zoint;
mod zollection;
mod zolygon;
mod zulti_lines;
mod zulti_points;
mod zulti_polygons;

use std::{io, mem};

pub use bounding_box::BoundingBox;
pub use coord::Coord;
pub(crate) use coord::{COORD_SIZE_IN_BYTES, COORD_SIZE_IN_FLOATS};
pub use coords::Coords;
use geo::LineString;
use geo_types::{Geometry, MultiPolygon, Polygon};
pub use relation::{InputRelation, OutputRelation, RelationBetweenShapes};
pub use segment::Segment;
pub use zine::Zine;
pub use zoint::Zoint;
pub use zollection::Zollection;
pub use zolygon::Zolygon;
pub use zulti_lines::ZultiLines;
pub use zulti_points::ZultiPoints;
pub use zulti_polygons::ZultiPolygons;

/// Main structure of this crate, this is the equivalent of a [`geo_types::Geometry`] but serialized.
#[derive(Debug, Clone, Copy)]
pub enum Zerometry<'a> {
    /// Equivalent of a [`geo_types::Point`]
    Point(Zoint<'a>),
    /// Equivalent of a [`geo_types::MultiPoint`]
    MultiPoints(ZultiPoints<'a>),
    /// Equivalent of a [`geo_types::LineString`]
    Line(Zine<'a>),
    /// Equivalent of a [`geo_types::MultiLineString`]
    MultiLines(ZultiLines<'a>),
    /// Equivalent of a [`geo_types::Polygon`]
    Polygon(Zolygon<'a>),
    /// Equivalent of a [`geo_types::MultiPolygon`]
    MultiPolygon(ZultiPolygons<'a>),
    /// Equivalent of a [`geo_types::GeometryCollection`]
    Collection(Zollection<'a>),
}

impl<'a> Zerometry<'a> {
    /// Create a `Zerometry` from a slice of bytes.
    /// See [`Self::write_from_geometry`] to create the slice of bytes.
    ///
    /// # Safety
    /// The data must be generated from the [`Self::write_from_geometry`] method and be aligned on 64 bits
    #[inline]
    pub unsafe fn from_bytes(data: &'a [u8]) -> Result<Self, std::io::Error> {
        if data.len() < mem::size_of::<u64>() {
            return Err(io::Error::new(
                io::ErrorKind::UnexpectedEof,
                format!(
                    "Was expecting at least {} bytes but found {}",
                    mem::size_of::<u64>(),
                    data.len()
                ),
            ));
        }
        let tag = u64::from_ne_bytes(
            data[..mem::size_of::<u64>()]
                .try_into()
                .map_err(io::Error::other)?,
        );
        let data = &data[mem::size_of::<u64>()..];
        match tag {
            0 => Ok(Zerometry::Point(unsafe { Zoint::from_bytes(data) })),
            1 => Ok(Zerometry::MultiPoints(unsafe {
                ZultiPoints::from_bytes(data)
            })),
            2 => Ok(Zerometry::Polygon(unsafe { Zolygon::from_bytes(data) })),
            3 => Ok(Zerometry::MultiPolygon(unsafe {
                ZultiPolygons::from_bytes(data)
            })),
            // They're located after because it would be a db-breaking to edit the already existing tags
            4 => Ok(Zerometry::Line(unsafe { Zine::from_bytes(data) })),
            5 => Ok(Zerometry::MultiLines(unsafe {
                ZultiLines::from_bytes(data)
            })),
            6 => Ok(Zerometry::Collection(unsafe {
                Zollection::from_bytes(data)
            })),
            _ => Err(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                "Invalid zerometry tag",
            )),
        }
    }

    /// Convert the specified [`geo_types::Geometry`] to a valid [`Zerometry`] slice of bytes in the input buffer.
    /// This is a destructive operation, the original geometry cannot be recreated as-is from the outputted zerometry:
    /// - The Line, Triangle and Rectangle gets converted respectively to Zine and Zolygon
    /// - The collections are flattened to a collection containing one multipoints, one multipolygons and one multilines.
    pub fn write_from_geometry(
        writer: &mut Vec<u8>,
        geometry: &Geometry<f64>,
    ) -> Result<(), std::io::Error> {
        // to stay aligned on 64 bits we must add the tag as a u64
        match geometry {
            Geometry::Point(point) => {
                writer.extend_from_slice(&0_u64.to_ne_bytes());
                Zoint::write_from_geometry(writer, point)?;
            }
            Geometry::MultiPoint(multi_point) => {
                writer.extend_from_slice(&1_u64.to_ne_bytes());
                ZultiPoints::write_from_geometry(writer, multi_point)?;
            }
            Geometry::Polygon(polygon) => {
                writer.extend_from_slice(&2_u64.to_ne_bytes());
                Zolygon::write_from_geometry(writer, polygon)?;
            }
            Geometry::MultiPolygon(multi_polygon) => {
                writer.extend_from_slice(&3_u64.to_ne_bytes());
                ZultiPolygons::write_from_geometry(writer, multi_polygon)?;
            }
            Geometry::LineString(line_string) => {
                writer.extend_from_slice(&4_u64.to_ne_bytes());
                Zine::write_from_geometry(writer, line_string)?;
            }
            Geometry::MultiLineString(multi_line_string) => {
                writer.extend_from_slice(&5_u64.to_ne_bytes());
                ZultiLines::write_from_geometry(writer, multi_line_string)?;
            }
            Geometry::GeometryCollection(collection) => {
                writer.extend_from_slice(&6_u64.to_ne_bytes());
                Zollection::write_from_geometry(writer, collection)?;
            }
            // Should never happens since we're working with geogson in meilisearch
            Geometry::Line(line) => {
                let line = LineString::new(vec![line.start, line.end]);
                Self::write_from_geometry(writer, &line.into())?;
            }
            Geometry::Rect(rect) => {
                Self::write_from_geometry(writer, &rect.to_polygon().into())?;
            }
            Geometry::Triangle(triangle) => {
                Self::write_from_geometry(writer, &triangle.to_polygon().into())?;
            }
        }
        Ok(())
    }

    /// Convert the [`Zerometry`] to a [`Zoint`] if possible. If it was not a point it returns [`None`].
    #[inline]
    pub fn to_point(&self) -> Option<Zoint<'_>> {
        match self {
            Zerometry::Point(a) => Some(*a),
            _ => None,
        }
    }

    /// Convert the [`Zerometry`] to a [`ZultiPoints`] if possible. If it was not a multi point it returns [`None`].
    #[inline]
    pub fn to_multi_points(&self) -> Option<ZultiPoints<'_>> {
        match self {
            Zerometry::MultiPoints(a) => Some(*a),
            _ => None,
        }
    }

    /// Convert the [`Zerometry`] to a [`Zine`] if possible. If it was not a line it returns [`None`].
    #[inline]
    pub fn to_line(&self) -> Option<Zine<'_>> {
        match self {
            Zerometry::Line(a) => Some(*a),
            _ => None,
        }
    }

    /// Convert the [`Zerometry`] to a [`ZultiLines`] if possible. If it was not a multi lines it returns [`None`].
    #[inline]
    pub fn to_zulti_lines(&self) -> Option<ZultiLines<'_>> {
        match self {
            Zerometry::MultiLines(a) => Some(*a),
            _ => None,
        }
    }

    /// Convert the [`Zerometry`] to a [`Zolygon`] if possible. If it was not a polygon it returns [`None`].
    #[inline]
    pub fn to_polygon(&self) -> Option<Zolygon<'_>> {
        match self {
            Zerometry::Polygon(a) => Some(*a),
            _ => None,
        }
    }

    /// Convert the [`Zerometry`] to a [`ZultiPolygons`] if possible. If it was not a multi polygons it returns [`None`].
    #[inline]
    pub fn to_multi_polygon(&self) -> Option<ZultiPolygons<'_>> {
        match self {
            Zerometry::MultiPolygon(a) => Some(*a),
            _ => None,
        }
    }

    /// Convert the [`Zerometry`] to a [`Zollection`] if possible. If it was not a collection it returns [`None`].
    #[inline]
    pub fn to_collection(&self) -> Option<Zollection<'_>> {
        match self {
            Zerometry::Collection(a) => Some(*a),
            _ => None,
        }
    }

    /// Convert the [`Zerometry`] back to a [`geo_types::Geometry`].
    /// Don't forget that converting the geometry to a zerometry was a destructive operation.
    /// This means the geometry you'll get back won't necessarily correspond to your initial geometry.
    pub fn to_geo(&self) -> geo_types::Geometry<f64> {
        match self {
            Zerometry::Point(a) => Geometry::Point(a.to_geo()),
            Zerometry::MultiPoints(a) => Geometry::MultiPoint(a.to_geo()),
            Zerometry::Line(a) => Geometry::LineString(a.to_geo()),
            Zerometry::MultiLines(a) => Geometry::MultiLineString(a.to_geo()),
            Zerometry::Polygon(a) => Geometry::Polygon(a.to_geo()),
            Zerometry::MultiPolygon(a) => Geometry::MultiPolygon(a.to_geo()),
            Zerometry::Collection(zollection) => Geometry::GeometryCollection(zollection.to_geo()),
        }
    }
}

impl<'a> From<Zoint<'a>> for Zerometry<'a> {
    #[inline]
    fn from(point: Zoint<'a>) -> Self {
        Zerometry::Point(point)
    }
}

impl<'a> From<ZultiPoints<'a>> for Zerometry<'a> {
    #[inline]
    fn from(points: ZultiPoints<'a>) -> Self {
        Zerometry::MultiPoints(points)
    }
}

impl<'a> From<Zolygon<'a>> for Zerometry<'a> {
    #[inline]
    fn from(polygon: Zolygon<'a>) -> Self {
        Zerometry::Polygon(polygon)
    }
}

impl<'a> From<ZultiPolygons<'a>> for Zerometry<'a> {
    #[inline]
    fn from(polygon: ZultiPolygons<'a>) -> Self {
        Zerometry::MultiPolygon(polygon)
    }
}

impl<'a> RelationBetweenShapes<Zoint<'a>> for Zerometry<'a> {
    fn relation(&self, other: &Zoint, relation: InputRelation) -> OutputRelation {
        match self {
            Zerometry::Point(a) => a.relation(other, relation),
            Zerometry::MultiPoints(a) => a.relation(other, relation),
            Zerometry::Line(a) => a.relation(other, relation),
            Zerometry::MultiLines(a) => a.relation(other, relation),
            Zerometry::Polygon(a) => a.relation(other, relation),
            Zerometry::MultiPolygon(a) => a.relation(other, relation),
            Zerometry::Collection(a) => a.relation(other, relation),
        }
    }
}

impl<'a> RelationBetweenShapes<ZultiPoints<'a>> for Zerometry<'a> {
    fn relation(&self, other: &ZultiPoints, relation: InputRelation) -> OutputRelation {
        match self {
            Zerometry::Point(a) => a.relation(other, relation),
            Zerometry::MultiPoints(a) => a.relation(other, relation),
            Zerometry::Line(a) => a.relation(other, relation),
            Zerometry::MultiLines(a) => a.relation(other, relation),
            Zerometry::Polygon(a) => a.relation(other, relation),
            Zerometry::MultiPolygon(a) => a.relation(other, relation),
            Zerometry::Collection(a) => a.relation(other, relation),
        }
    }
}

impl<'a> RelationBetweenShapes<Zine<'a>> for Zerometry<'a> {
    fn relation(&self, other: &Zine, relation: InputRelation) -> OutputRelation {
        match self {
            Zerometry::Point(a) => a.relation(other, relation),
            Zerometry::MultiPoints(a) => a.relation(other, relation),
            Zerometry::MultiLines(a) => a.relation(other, relation),
            Zerometry::Line(a) => a.relation(other, relation),
            Zerometry::Polygon(a) => a.relation(other, relation),
            Zerometry::MultiPolygon(a) => a.relation(other, relation),
            Zerometry::Collection(a) => a.relation(other, relation),
        }
    }
}

impl<'a> RelationBetweenShapes<ZultiLines<'a>> for Zerometry<'a> {
    fn relation(&self, other: &ZultiLines, relation: InputRelation) -> OutputRelation {
        match self {
            Zerometry::Point(a) => a.relation(other, relation),
            Zerometry::MultiPoints(a) => a.relation(other, relation),
            Zerometry::MultiLines(a) => a.relation(other, relation),
            Zerometry::Line(a) => a.relation(other, relation),
            Zerometry::Polygon(a) => a.relation(other, relation),
            Zerometry::MultiPolygon(a) => a.relation(other, relation),
            Zerometry::Collection(a) => a.relation(other, relation),
        }
    }
}

impl<'a> RelationBetweenShapes<Zolygon<'a>> for Zerometry<'a> {
    fn relation(&self, other: &Zolygon, relation: InputRelation) -> OutputRelation {
        match self {
            Zerometry::Point(a) => a.relation(other, relation),
            Zerometry::MultiPoints(a) => a.relation(other, relation),
            Zerometry::MultiLines(a) => a.relation(other, relation),
            Zerometry::Line(a) => a.relation(other, relation),
            Zerometry::Polygon(a) => a.relation(other, relation),
            Zerometry::MultiPolygon(a) => a.relation(other, relation),
            Zerometry::Collection(a) => a.relation(other, relation),
        }
    }
}

impl<'a> RelationBetweenShapes<ZultiPolygons<'a>> for Zerometry<'a> {
    fn relation(&self, other: &ZultiPolygons, relation: InputRelation) -> OutputRelation {
        match self {
            Zerometry::Point(a) => a.relation(other, relation),
            Zerometry::MultiPoints(a) => a.relation(other, relation),
            Zerometry::MultiLines(a) => a.relation(other, relation),
            Zerometry::Line(a) => a.relation(other, relation),
            Zerometry::Polygon(a) => a.relation(other, relation),
            Zerometry::MultiPolygon(a) => a.relation(other, relation),
            Zerometry::Collection(a) => a.relation(other, relation),
        }
    }
}

impl<'a> RelationBetweenShapes<Zollection<'a>> for Zerometry<'a> {
    fn relation(&self, other: &Zollection, relation: InputRelation) -> OutputRelation {
        match self {
            Zerometry::Point(a) => a.relation(other, relation),
            Zerometry::MultiPoints(a) => a.relation(other, relation),
            Zerometry::MultiLines(a) => a.relation(other, relation),
            Zerometry::Line(a) => a.relation(other, relation),
            Zerometry::Polygon(a) => a.relation(other, relation),
            Zerometry::MultiPolygon(a) => a.relation(other, relation),
            Zerometry::Collection(a) => a.relation(other, relation),
        }
    }
}

impl<'a> RelationBetweenShapes<Zerometry<'a>> for Zerometry<'a> {
    fn relation(&self, other: &Zerometry, relation: InputRelation) -> OutputRelation {
        match other {
            Zerometry::Point(a) => self.relation(a, relation),
            Zerometry::MultiPoints(a) => self.relation(a, relation),
            Zerometry::Line(a) => a.relation(other, relation),
            Zerometry::MultiLines(a) => self.relation(a, relation),
            Zerometry::Polygon(a) => self.relation(a, relation),
            Zerometry::MultiPolygon(a) => self.relation(a, relation),
            Zerometry::Collection(a) => self.relation(a, relation),
        }
    }
}

impl<'a> RelationBetweenShapes<Geometry<f64>> for Zerometry<'a> {
    fn relation(&self, other: &Geometry<f64>, relation: InputRelation) -> OutputRelation {
        let mut buffer = Vec::new();
        Zerometry::write_from_geometry(&mut buffer, other).unwrap();
        let other = unsafe { Zerometry::from_bytes(&buffer).unwrap() };
        self.relation(&other, relation)
    }
}

impl<'a> RelationBetweenShapes<Zerometry<'a>> for Geometry<f64> {
    fn relation(&self, other: &Zerometry<'a>, relation: InputRelation) -> OutputRelation {
        let mut buffer = Vec::new();
        Zerometry::write_from_geometry(&mut buffer, self).unwrap();
        let this = unsafe { Zerometry::from_bytes(&buffer).unwrap() };
        this.relation(other, relation)
    }
}

impl<'a> RelationBetweenShapes<Polygon<f64>> for Zerometry<'a> {
    fn relation(&self, other: &Polygon<f64>, relation: InputRelation) -> OutputRelation {
        let mut buffer = Vec::new();
        Zerometry::write_from_geometry(&mut buffer, &Geometry::Polygon(other.clone())).unwrap();
        let other = unsafe { Zerometry::from_bytes(&buffer).unwrap() };
        self.relation(&other, relation)
    }
}

impl<'a> RelationBetweenShapes<MultiPolygon<f64>> for Zerometry<'a> {
    fn relation(&self, other: &MultiPolygon<f64>, relation: InputRelation) -> OutputRelation {
        let mut buffer = Vec::new();
        Zerometry::write_from_geometry(&mut buffer, &Geometry::MultiPolygon(other.clone()))
            .unwrap();
        let other = unsafe { Zerometry::from_bytes(&buffer).unwrap() };
        self.relation(&other, relation)
    }
}

impl PartialEq<Geometry> for Zerometry<'_> {
    fn eq(&self, other: &Geometry) -> bool {
        match (self, other) {
            (Zerometry::Point(zoint), Geometry::Point(point)) => zoint.eq(point),
            (Zerometry::MultiPoints(zulti_points), Geometry::MultiPoint(multi_point)) => {
                zulti_points.eq(multi_point)
            }
            (Zerometry::Line(zine), Geometry::LineString(line_string)) => zine.eq(line_string),
            (Zerometry::MultiLines(zulti_lines), Geometry::MultiLineString(multi_line_string)) => {
                zulti_lines.eq(multi_line_string)
            }
            (Zerometry::Polygon(zolygon), Geometry::Polygon(polygon)) => zolygon.eq(polygon),
            (Zerometry::MultiPolygon(zulti_polygon), Geometry::MultiPolygon(multi_polygon)) => {
                zulti_polygon.eq(multi_polygon)
            }
            _ => false,
        }
    }
}

#[cfg(test)]
mod zerometry_test {
    use geo_types::geometry;

    use crate::Zerometry;

    #[test]
    fn naive_point_roundtrip() {
        let point = geometry::Geometry::Point(geometry::Point::new(45.0, 65.0));
        let mut buf = Vec::new();
        Zerometry::write_from_geometry(&mut buf, &point).unwrap();
        let zoint = unsafe { Zerometry::from_bytes(&buf).unwrap() };
        assert_eq!(zoint, point);
    }

    #[test]
    fn naive_multi_point_roundtrip() {
        let multi_point = geometry::Geometry::MultiPoint(geometry::MultiPoint::new(vec![]));
        let mut buf = Vec::new();
        Zerometry::write_from_geometry(&mut buf, &multi_point).unwrap();
        let zulti_point = unsafe { Zerometry::from_bytes(&buf).unwrap() };
        assert_eq!(zulti_point, multi_point);

        let multi_point = geometry::Geometry::MultiPoint(geometry::MultiPoint::new(vec![
            geometry::Point::new(45.0, 65.0),
            geometry::Point::new(46.0, 66.0),
            geometry::Point::new(44.0, 64.0),
            geometry::Point::new(45.0, 65.0),
        ]));
        let mut buf = Vec::new();
        Zerometry::write_from_geometry(&mut buf, &multi_point).unwrap();
        let zulti_point = unsafe { Zerometry::from_bytes(&buf).unwrap() };
        assert_eq!(zulti_point, multi_point);
    }

    #[test]
    fn naive_line_string_roundtrip() {
        let line_string = geometry::Geometry::LineString(geometry::LineString::new(vec![]));
        let mut buf = Vec::new();
        Zerometry::write_from_geometry(&mut buf, &line_string).unwrap();
        let zine_string = unsafe { Zerometry::from_bytes(&buf).unwrap() };
        assert_eq!(zine_string, line_string);

        let line_string = geometry::Geometry::LineString(geometry::LineString::new(vec![
            geometry::Coord { x: 45.0, y: 25.0 },
            geometry::Coord { x: 46.0, y: 24.0 },
            geometry::Coord { x: 45.0, y: 25.0 },
        ]));
        let mut buf = Vec::new();
        Zerometry::write_from_geometry(&mut buf, &line_string).unwrap();
        let zine_string = unsafe { Zerometry::from_bytes(&buf).unwrap() };
        assert_eq!(zine_string, line_string);
    }

    #[test]
    fn naive_multi_line_string_roundtrip() {
        let multi_line_string =
            geometry::Geometry::MultiLineString(geometry::MultiLineString::new(vec![]));
        let mut buf = Vec::new();
        Zerometry::write_from_geometry(&mut buf, &multi_line_string).unwrap();
        let zulti_line_string = unsafe { Zerometry::from_bytes(&buf).unwrap() };
        assert_eq!(zulti_line_string, multi_line_string);

        let multi_line_string =
            geometry::Geometry::MultiLineString(geometry::MultiLineString::new(vec![
                geometry::LineString::new(vec![
                    geometry::Coord { x: 45.0, y: 25.0 },
                    geometry::Coord { x: 46.0, y: 24.0 },
                    geometry::Coord { x: 45.0, y: 25.0 },
                ]),
                geometry::LineString::new(vec![]),
                geometry::LineString::new(vec![
                    geometry::Coord { x: 66.0, y: 46.0 },
                    geometry::Coord { x: 47.0, y: 34.0 },
                    geometry::Coord { x: 66.0, y: 26.0 },
                ]),
            ]));
        let mut buf = Vec::new();
        Zerometry::write_from_geometry(&mut buf, &multi_line_string).unwrap();
        let zulti_line_string = unsafe { Zerometry::from_bytes(&buf).unwrap() };
        assert_eq!(zulti_line_string, multi_line_string);

        let multi_line_string =
            geometry::Geometry::MultiLineString(geometry::MultiLineString::new(vec![
                geometry::LineString::new(vec![
                    geometry::Coord { x: 45.0, y: 25.0 },
                    geometry::Coord { x: 46.0, y: 24.0 },
                    geometry::Coord { x: 45.0, y: 25.0 },
                ]),
                geometry::LineString::new(vec![
                    geometry::Coord { x: 55.0, y: 25.0 },
                    geometry::Coord { x: 46.0, y: 34.0 },
                    geometry::Coord { x: 55.0, y: 25.0 },
                ]),
                geometry::LineString::new(vec![
                    geometry::Coord { x: 66.0, y: 46.0 },
                    geometry::Coord { x: 47.0, y: 34.0 },
                    geometry::Coord { x: 66.0, y: 26.0 },
                ]),
            ]));
        let mut buf = Vec::new();
        Zerometry::write_from_geometry(&mut buf, &multi_line_string).unwrap();
        let zulti_line_string = unsafe { Zerometry::from_bytes(&buf).unwrap() };
        assert_eq!(zulti_line_string, multi_line_string);
    }

    #[test]
    fn naive_polygon_roundtrip() {
        let polygon = geometry::Geometry::Polygon(geometry::Polygon::new(
            geometry::LineString::new(vec![]),
            vec![],
        ));
        let mut buf = Vec::new();
        Zerometry::write_from_geometry(&mut buf, &polygon).unwrap();
        let zolygon = unsafe { Zerometry::from_bytes(&buf).unwrap() };
        assert_eq!(zolygon, polygon);

        let polygon = geometry::Geometry::Polygon(geometry::Polygon::new(
            geometry::LineString::new(vec![
                geometry::Coord { x: 66.0, y: 46.0 },
                geometry::Coord { x: 47.0, y: 34.0 },
                geometry::Coord { x: 66.0, y: 26.0 },
            ]),
            vec![],
        ));
        let mut buf = Vec::new();
        Zerometry::write_from_geometry(&mut buf, &polygon).unwrap();
        let zolygon = unsafe { Zerometry::from_bytes(&buf).unwrap() };
        assert_eq!(zolygon, polygon);
    }

    #[test]
    fn naive_multi_polygon_roundtrip() {
        let multi_polygon = geometry::Geometry::MultiPolygon(geometry::MultiPolygon::new(vec![]));
        let mut buf = Vec::new();
        Zerometry::write_from_geometry(&mut buf, &multi_polygon).unwrap();
        let zulti_polygon = unsafe { Zerometry::from_bytes(&buf).unwrap() };
        assert_eq!(zulti_polygon, multi_polygon);

        let multi_polygon = geometry::Geometry::MultiPolygon(geometry::MultiPolygon::new(vec![
            geometry::Polygon::new(
                geometry::LineString::new(vec![
                    geometry::Coord { x: 66.0, y: 46.0 },
                    geometry::Coord { x: 47.0, y: 34.0 },
                    geometry::Coord { x: 66.0, y: 26.0 },
                ]),
                vec![],
            ),
            geometry::Polygon::new(
                geometry::LineString::new(vec![
                    geometry::Coord { x: 86.0, y: 48.0 },
                    geometry::Coord { x: 67.0, y: 36.0 },
                    geometry::Coord { x: 86.0, y: 28.0 },
                ]),
                vec![],
            ),
        ]));
        let mut buf = Vec::new();
        Zerometry::write_from_geometry(&mut buf, &multi_polygon).unwrap();
        let zulti_polygon = unsafe { Zerometry::from_bytes(&buf).unwrap() };
        assert_eq!(zulti_polygon, multi_polygon);
    }

    #[test]
    fn naive_geometry_collection_roundtrip() {
        /*
        let geometry_collection =
            geometry::Geometry::GeometryCollection(geometry::GeometryCollection::new_from(todo!()));
        let mut buf = Vec::new();
        Zerometry::write_from_geometry(&mut buf, &geometry_collection).unwrap();
        let zeometry_collection = Zerometry::from_bytes(&buf).unwrap();
        assert_eq!(zeometry_collection, geometry_collection);
        */
    }
}