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
//! This crate provides functions to convert `rust-geo` geometry types to and from Well Known
//! Binary format.
//!
//! # Examples
//!
//! ```rust
//! # extern crate geo;
//! # extern crate wkb;
//! # fn main() {
//! use geo::*;
//! use wkb::*;
//!
//! let p: Geometry<f64> = Geometry::Point(Point::new(2., 4.));
//! let res = geom_to_wkb(&p);
//! assert_eq!(res, vec![1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 16, 64]);
//! # }
//! ```
//!
//! You can also 'read' a Geometry from a `std::io::Read`:
//!
//! ```rust
//! # extern crate geo;
//! # extern crate wkb;
//! # fn main() {
//! use geo::*;
//! use wkb::*;
//!
//! let bytes: Vec<u8> = vec![1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 16, 64];
//! let p: Geometry<f64> = wkb_to_geom(&mut bytes.as_slice()).unwrap();
//! assert_eq!(p, Geometry::Point(Point::new(2., 4.)));
//! # }
//! ```
//!
//! Adding proper `*Ext` traits is planned.
//!
//!

#![allow(unused_must_use)]
extern crate geo;
extern crate byteorder;
extern crate num_traits;

use std::io::prelude::*;
use std::io;

use geo::*;
use num_traits::Float;
use byteorder::{WriteBytesExt, ReadBytesExt};
use byteorder::{LittleEndian};

#[derive(Debug)]
pub enum WKBReadError {
    WrongType,
    IOError(io::Error),
}

impl From<io::Error> for WKBReadError {
    fn from(err: io::Error) -> WKBReadError {
        WKBReadError::IOError(err)
    }
}


fn read_point<I: Read>(mut wkb: I) -> Result<Point<f64>, WKBReadError> {
    let x: f64 = wkb.read_f64::<LittleEndian>()?;
    let y: f64 = wkb.read_f64::<LittleEndian>()?;
    Ok(Point::new(x, y))
}

fn write_point<W: Write, T: Into<f64>+Float>(p: &Point<T>, out: &mut W) {
    out.write_f64::<LittleEndian>(p.x().into());
    out.write_f64::<LittleEndian>(p.y().into());
}

fn read_many_points<I: Read>(mut wkb: I) -> Result<Vec<Point<f64>>, WKBReadError> {
    let num_points = wkb.read_u32::<LittleEndian>()? as usize;
    let mut res: Vec<Point<f64>> = Vec::with_capacity(num_points);
    for _ in 0..num_points {
        res.push(read_point(&mut wkb)?);
    }

    Ok(res)
}

fn write_many_points<W: Write, T: Into<f64>+Float>(mp: &[Point<T>], mut out: &mut W) {
    out.write_u32::<LittleEndian>(mp.len() as u32);
    for p in mp.iter() {
        write_point(p, &mut out);
    }
}

/// Convert a Geometry into WKB bytes.
pub fn geom_to_wkb<T: Into<f64>+Float>(geom: &geo::Geometry<T>) -> Vec<u8> {
    let mut result: Vec<u8> = Vec::new();
    write_geom_to_wkb(geom, &mut result);
    result
}

/// Write a geometry to the underlying writer.
pub fn write_geom_to_wkb<W: Write, T: Into<f64>+Float>(geom: &geo::Geometry<T>, mut result: &mut W) {
    // FIXME replace type signature with Into<Geometry<T>>
    
    // little endian
    result.write_u8(1);
    match geom {
        &Geometry::Point(p) => {
            result.write_u32::<LittleEndian>(1);
            write_point(&p, &mut result);
        },
        &Geometry::LineString(ref ls) => {
            result.write_u32::<LittleEndian>(2);
            write_many_points(&ls.0, &mut result);
        },
        &Geometry::Line(ref l) => {
            result.write_u32::<LittleEndian>(2);
            write_many_points(&[l.start, l.end], &mut result);
        },
        &Geometry::Polygon(ref p) => {
            result.write_u32::<LittleEndian>(3);
            result.write_u32::<LittleEndian>(1 + p.interiors.len() as u32);
            write_many_points(&p.exterior.0, &mut result);
            for i in p.interiors.iter() {
                write_many_points(&i.0, &mut result);
            }
        }
        &Geometry::MultiPoint(ref mp) => {
            result.write_u32::<LittleEndian>(4);
            write_many_points(&mp.0, &mut result);
        },
        &Geometry::MultiLineString(ref mls) => {
            result.write_u32::<LittleEndian>(5);
            result.write_u32::<LittleEndian>(mls.0.len() as u32);
            for ls in mls.0.iter() {
                // I tried to have this call write_geom_to_wkb again, but I couldn't get the types
                // working.
                result.write_u8(1);
                result.write_u32::<LittleEndian>(2);
                write_many_points(&ls.0, &mut result);
            }
        },
        &Geometry::MultiPolygon(ref mp) => {
            result.write_u32::<LittleEndian>(6);
            result.write_u32::<LittleEndian>(mp.0.len() as u32);
            for poly in mp.0.iter() {
                result.write_u8(1);
                result.write_u32::<LittleEndian>(3);
                result.write_u32::<LittleEndian>(1 + poly.interiors.len() as u32);

                write_many_points(&poly.exterior.0, &mut result);
                for int in poly.interiors.iter() {
                    write_many_points(&int.0, &mut result);
                }
            }
        },
        &Geometry::GeometryCollection(ref _gc) => {
            // FIXME implement, don't want to duplicate all the above
            unimplemented!();
        }
    }

}
/// Read a Geometry from a reader. Converts WKB to a Geometry.
pub fn wkb_to_geom<I: Read>(mut wkb: &mut I) -> Result<geo::Geometry<f64>, WKBReadError> {
    match wkb.read_u8()? {
        0 => unimplemented!(),
        1 => { },  // LittleEndian, OK
        _ => panic!(),
    };

    match wkb.read_u32::<LittleEndian>()? {
        1 => {
            // Point
            Ok(Geometry::Point(read_point(&mut wkb)?))
        },
        2 => {
            // LineString
            let points = read_many_points(&mut wkb)?;
            Ok(Geometry::LineString(LineString(points)))
        },
        3 => {
            // Polygon
            let num_rings = wkb.read_u32::<LittleEndian>()? as usize;
            let exterior = read_many_points(&mut wkb)?;
            let mut interiors = Vec::with_capacity(num_rings-1);
            for _ in 0..(num_rings-1) {
                interiors.push(LineString(read_many_points(&mut wkb)?));
            }
            Ok(Geometry::Polygon(Polygon::new(LineString(exterior), interiors)))
        },
        4 => {
            // MultiPoint
            let points = read_many_points(&mut wkb)?;
            Ok(Geometry::MultiPoint(MultiPoint(points)))
        },
        5 => {
            // MultiLineString
            let num_linestrings = wkb.read_u32::<LittleEndian>()? as usize;
            let mut linestrings = Vec::with_capacity(num_linestrings);
            for _ in 0..num_linestrings {
                let linestring: LineString<f64> = match wkb_to_geom(wkb)? {
                    Geometry::LineString(l) => l,
                    _ => { return Err(WKBReadError::WrongType); },
                };

                linestrings.push(linestring);
            }
            Ok(Geometry::MultiLineString(MultiLineString(linestrings)))
        },
        6 => {
            // MultiPolygon
            let num_polygons = wkb.read_u32::<LittleEndian>()? as usize;
            let mut polygons = Vec::with_capacity(num_polygons);
            for _ in 0..num_polygons {
                let polygon: Polygon<f64> = match wkb_to_geom(wkb)? {
                    Geometry::Polygon(p) => p,
                    _ => { return Err(WKBReadError::WrongType); },
                };

                polygons.push(polygon);
            }

            Ok(Geometry::MultiPolygon(MultiPolygon(polygons)))
        },
        _ => unimplemented!(),
    }

}


#[cfg(test)]
mod tests {
    use super::*;

    fn assert_two_f64<R: Read, I: Into<f64>>(reader: &mut R, a: I, b: I) {
        assert_eq!(reader.read_f64::<LittleEndian>().unwrap(), a.into());
        assert_eq!(reader.read_f64::<LittleEndian>().unwrap(), b.into());
    }

    fn write_two_f64<W: Write, F: Into<f64>>(writer: &mut W, a: F, b: F) {
        writer.write_f64::<LittleEndian>(a.into());
        writer.write_f64::<LittleEndian>(b.into());
    }

    #[test]
    fn point_to_wkb() {
        let p: Geometry<f64> = Geometry::Point(Point::new(2., 4.));
        let res = geom_to_wkb(&p);
        let mut res = res.as_slice();
        assert_eq!(res.read_u8().unwrap(), 1);
        assert_eq!(res.read_u32::<LittleEndian>().unwrap(), 1);
        assert_two_f64(&mut res, 2, 4);

        assert_eq!(wkb_to_geom(&mut geom_to_wkb(&p).as_slice()).unwrap(), p);
    }

    #[test]
    fn wkb_to_point() {
        let mut bytes = Vec::new();
        bytes.write_u8(1);
        bytes.write_u32::<LittleEndian>(1);
        bytes.write_f64::<LittleEndian>(100.);
        bytes.write_f64::<LittleEndian>(-2.);

        let geom = wkb_to_geom(&mut bytes.as_slice()).unwrap();
        // TODO need a geom.is_point()
        if let Geometry::Point(p) = geom {
            assert_eq!(p.x(), 100.);
            assert_eq!(p.y(), -2.);
        } else {
            assert!(false);
        }

        assert_eq!(geom_to_wkb(&wkb_to_geom(&mut bytes.as_slice()).unwrap()), bytes);
    }

    #[test]
    fn linestring_to_wkb() {
        let mut ls = LineString(vec![]);
        ls.0.push(Point::new(0., 0.));
        ls.0.push(Point::new(1., 0.));
        ls.0.push(Point::new(1., 1.));
        ls.0.push(Point::new(0., 1.));
        ls.0.push(Point::new(0., 0.));
        let ls = Geometry::LineString(ls);

        let res = geom_to_wkb(&ls);
        let mut res = res.as_slice();
        assert_eq!(res.read_u8().unwrap(), 1);  // LittleEndian
        assert_eq!(res.read_u32::<LittleEndian>().unwrap(), 2);  // 2 = Linestring
        assert_eq!(res.read_u32::<LittleEndian>().unwrap(), 5);  // num points

        assert_two_f64(&mut res, 0, 0);
        assert_two_f64(&mut res, 1, 0);
        assert_two_f64(&mut res, 1, 1);
        assert_two_f64(&mut res, 0, 1);
        assert_two_f64(&mut res, 0, 0);

        assert_eq!(wkb_to_geom(&mut geom_to_wkb(&ls).as_slice()).unwrap(), ls);
    }

    #[test]
    fn wkb_to_linestring() {
        let mut bytes = Vec::new();
        bytes.write_u8(1);

        bytes.write_u32::<LittleEndian>(2);
        bytes.write_u32::<LittleEndian>(2);

        write_two_f64(&mut bytes, 0, 0);
        write_two_f64(&mut bytes, 1000, 1000);

        let geom = wkb_to_geom(&mut bytes.as_slice()).unwrap();
        if let Geometry::LineString(ls) = geom {
            assert_eq!(ls.0.len(), 2);
            assert_eq!(ls.0[0].x(), 0.);
            assert_eq!(ls.0[0].y(), 0.);
            assert_eq!(ls.0[1].x(), 1000.);
            assert_eq!(ls.0[1].y(), 1000.);
        } else {
            assert!(false);
        }

        assert_eq!(geom_to_wkb(&wkb_to_geom(&mut bytes.as_slice()).unwrap()), bytes);
    }


    #[test]
    fn polygon_to_wkb() {
        let mut ls = LineString(vec![]);
        ls.0.push(Point::new(0., 0.));
        ls.0.push(Point::new(10., 0.));
        ls.0.push(Point::new(10., 10.));
        ls.0.push(Point::new(0., 10.));
        ls.0.push(Point::new(0., 0.));

        let mut int = LineString(vec![]);
        int.0.push(Point::new(2., 2.));
        int.0.push(Point::new(2., 4.));
        int.0.push(Point::new(4., 4.));
        int.0.push(Point::new(4., 2.));
        int.0.push(Point::new(2., 2.));
        let p = Geometry::Polygon(Polygon::new(ls, vec![int]));

        let res = geom_to_wkb(&p);
        let mut res = res.as_slice();
        assert_eq!(res.read_u8().unwrap(), 1);
        assert_eq!(res.read_u32::<LittleEndian>().unwrap(), 3);
        assert_eq!(res.read_u32::<LittleEndian>().unwrap(), 2);

        // Exterior Ring
        assert_eq!(res.read_u32::<LittleEndian>().unwrap(), 5);

        assert_two_f64(&mut res, 0, 0);
        assert_two_f64(&mut res, 10, 0);
        assert_two_f64(&mut res, 10, 10);
        assert_two_f64(&mut res, 0, 10);
        assert_two_f64(&mut res, 0, 0);

        // interior ring
        assert_eq!(res.read_u32::<LittleEndian>().unwrap(), 5);

        assert_two_f64(&mut res, 2, 2);
        assert_two_f64(&mut res, 2, 4);
        assert_two_f64(&mut res, 4, 4);
        assert_two_f64(&mut res, 4, 2);
        assert_two_f64(&mut res, 2, 2);

        assert_eq!(wkb_to_geom(&mut geom_to_wkb(&p).as_slice()).unwrap(), p);
    }

    #[test]
    fn wkb_to_polygon() {
        let mut bytes = Vec::new();
        bytes.write_u8(1);
        bytes.write_u32::<LittleEndian>(3);
        bytes.write_u32::<LittleEndian>(1);
        bytes.write_u32::<LittleEndian>(3);

        write_two_f64(&mut bytes, 0, 0);
        write_two_f64(&mut bytes, 1, 0);
        write_two_f64(&mut bytes, 0, 1);

        let geom = wkb_to_geom(&mut bytes.as_slice()).unwrap();
        if let Geometry::Polygon(p) = geom {
            assert_eq!(p.interiors.len(), 0);
            assert_eq!(p.exterior.0.len(), 3);
            assert_eq!(p.exterior.0[0], Point::new(0., 0.));
            assert_eq!(p.exterior.0[1], Point::new(1., 0.));
            assert_eq!(p.exterior.0[2], Point::new(0., 1.));
        } else {
            assert!(false);
        }

        assert_eq!(geom_to_wkb(&wkb_to_geom(&mut bytes.as_slice()).unwrap()), bytes);
    }

    #[test]
    fn multipoint_to_wkb() {
        let p: Geometry<f64> = Geometry::MultiPoint(MultiPoint(vec![Point::new(0., 0.), Point::new(10., -2.)]));
        let res = geom_to_wkb(&p);
        let mut res = res.as_slice();
        assert_eq!(res.read_u8().unwrap(), 1);
        assert_eq!(res.read_u32::<LittleEndian>().unwrap(), 4);
        assert_eq!(res.read_u32::<LittleEndian>().unwrap(), 2);
        assert_two_f64(&mut res, 0, 0);
        assert_two_f64(&mut res, 10, -2);

        assert_eq!(wkb_to_geom(&mut geom_to_wkb(&p).as_slice()).unwrap(), p);
    }

    #[test]
    fn wkb_to_multipoint() {
        let mut bytes = Vec::new();
        bytes.write_u8(1);
        bytes.write_u32::<LittleEndian>(4);
        bytes.write_u32::<LittleEndian>(1);
        write_two_f64(&mut bytes, 100, -2);

        let geom = wkb_to_geom(&mut bytes.as_slice()).unwrap();
        if let Geometry::MultiPoint(mp) = geom {
            assert_eq!(mp.0.len(), 1);
            assert_eq!(mp.0[0].x(), 100.);
            assert_eq!(mp.0[0].y(), -2.);
        } else {
            assert!(false);
        }

        assert_eq!(geom_to_wkb(&wkb_to_geom(&mut bytes.as_slice()).unwrap()), bytes);
    }

    #[test]
    fn multilinestring_to_wkb() {
        let ls = Geometry::MultiLineString(MultiLineString(vec![
                    LineString(vec![Point::new(0., 0.), Point::new(1., 1.)]),
                    LineString(vec![Point::new(10., 10.), Point::new(10., 11.)]),
                   ]));

        let res = geom_to_wkb(&ls);
        let mut res = res.as_slice();
        assert_eq!(res.read_u8().unwrap(), 1);
        assert_eq!(res.read_u32::<LittleEndian>().unwrap(), 5); // 5 - MultiLineString
        assert_eq!(res.read_u32::<LittleEndian>().unwrap(), 2); // 2 linestrings

        assert_eq!(res.read_u8().unwrap(), 1);
        assert_eq!(res.read_u32::<LittleEndian>().unwrap(), 2);  // 2 = Linestring
        assert_eq!(res.read_u32::<LittleEndian>().unwrap(), 2);  // num points
        assert_two_f64(&mut res, 0, 0);
        assert_two_f64(&mut res, 1, 1);

        assert_eq!(res.read_u8().unwrap(), 1);
        assert_eq!(res.read_u32::<LittleEndian>().unwrap(), 2);  // 2 = Linestring
        assert_eq!(res.read_u32::<LittleEndian>().unwrap(), 2);  // num points
        assert_two_f64(&mut res, 10, 10);
        assert_two_f64(&mut res, 10, 11);

        assert_eq!(wkb_to_geom(&mut geom_to_wkb(&ls).as_slice()).unwrap(), ls);
    }

    #[test]
    fn wkb_to_multilinestring() {
        let mut bytes = Vec::new();
        bytes.write_u8(1);
        bytes.write_u32::<LittleEndian>(5);
        bytes.write_u32::<LittleEndian>(1);

        bytes.write_u8(1);
        bytes.write_u32::<LittleEndian>(2);
        bytes.write_u32::<LittleEndian>(3);
        write_two_f64(&mut bytes, 0, 0);
        write_two_f64(&mut bytes, 1, 0);
        write_two_f64(&mut bytes, 0, 1);

        let geom = wkb_to_geom(&mut bytes.as_slice()).unwrap();
        if let Geometry::MultiLineString(mls) = geom {
            assert_eq!(mls.0.len(), 1);
            assert_eq!(mls.0[0].0.len(), 3);
            assert_eq!(mls.0[0].0[0], Point::new(0., 0.));
            assert_eq!(mls.0[0].0[1], Point::new(1., 0.));
            assert_eq!(mls.0[0].0[2], Point::new(0., 1.));
        } else {
            assert!(false);
        }

        assert_eq!(geom_to_wkb(&wkb_to_geom(&mut bytes.as_slice()).unwrap()), bytes);
    }


    #[test]
    fn multipolygon_to_wkb() {
        let mut ls = LineString(vec![]);
        ls.0.push(Point::new(0., 0.));
        ls.0.push(Point::new(10., 0.));
        ls.0.push(Point::new(10., 10.));
        ls.0.push(Point::new(0., 10.));
        ls.0.push(Point::new(0., 0.));

        let mut int = LineString(vec![]);
        int.0.push(Point::new(2., 2.));
        int.0.push(Point::new(2., 4.));
        int.0.push(Point::new(2., 2.));
        let p1 = Polygon::new(ls, vec![]);
        let p2 = Polygon::new(int, vec![]);
        let p = Geometry::MultiPolygon(MultiPolygon(vec![p1, p2]));

        let res = geom_to_wkb(&p);
        let mut res = res.as_slice();
        assert_eq!(res.read_u8().unwrap(), 1);
        assert_eq!(res.read_u32::<LittleEndian>().unwrap(), 6);  // Multipolygon
        assert_eq!(res.read_u32::<LittleEndian>().unwrap(), 2);  // num polygons

        // polygon 1
        assert_eq!(res.read_u8().unwrap(), 1);
        assert_eq!(res.read_u32::<LittleEndian>().unwrap(), 3);  // polygon
        assert_eq!(res.read_u32::<LittleEndian>().unwrap(), 1); // only one ring

        assert_eq!(res.read_u32::<LittleEndian>().unwrap(), 5); // 5 points in ring #1
        assert_two_f64(&mut res, 0, 0);
        assert_two_f64(&mut res, 10, 0);
        assert_two_f64(&mut res, 10, 10);
        assert_two_f64(&mut res, 0, 10);
        assert_two_f64(&mut res, 0, 0);

        // polygon 2
        assert_eq!(res.read_u8().unwrap(), 1);
        assert_eq!(res.read_u32::<LittleEndian>().unwrap(), 3);  // polygon
        assert_eq!(res.read_u32::<LittleEndian>().unwrap(), 1); // one ring
        assert_eq!(res.read_u32::<LittleEndian>().unwrap(), 3); // 3 points in ring #1

        assert_two_f64(&mut res, 2, 2);
        assert_two_f64(&mut res, 2, 4);
        assert_two_f64(&mut res, 2, 2);

        assert_eq!(wkb_to_geom(&mut geom_to_wkb(&p).as_slice()).unwrap(), p);
    }

    #[test]
    fn postgis_wkb_to_multipolygon() {
        let bytes: Vec<u8> = vec![
            0x01, 0x06, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x03, 0x00, 0x00, 0x00,
            0x01, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x24, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x24, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x24, 0x40,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x24, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
            0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40,   
        ];

        let geom = wkb_to_geom(&mut bytes.as_slice()).unwrap();

        if let Geometry::MultiPolygon(mp) = geom {
            assert_eq!(mp.0.len(), 2);
            assert_eq!(mp.0[0].exterior.0.len(), 5);
            assert_eq!(mp.0[0].exterior.0[0], Point::new(0., 0.));
            assert_eq!(mp.0[0].exterior.0[1], Point::new(10., 0.));
            assert_eq!(mp.0[0].exterior.0[2], Point::new(10., 10.));
            assert_eq!(mp.0[0].exterior.0[3], Point::new(0., 10.));
            assert_eq!(mp.0[0].exterior.0[4], Point::new(0., 0.));

            assert_eq!(mp.0[0].interiors.len(), 0);

            assert_eq!(mp.0[1].exterior.0.len(), 4);
            assert_eq!(mp.0[1].exterior.0[0], Point::new(2., 2.));
            assert_eq!(mp.0[1].exterior.0[1], Point::new(2., 4.));
            assert_eq!(mp.0[1].exterior.0[2], Point::new(4., 2.));
            assert_eq!(mp.0[1].exterior.0[0], Point::new(2., 2.));
            assert_eq!(mp.0[1].interiors.len(), 0);
        } else {
            assert!(false);
        }

    }

    #[allow(dead_code)]
    fn geometrycollection_to_wkb() {
        // FIXME finish
        let p: Geometry<_> = Point::new(0., 0.).into();
        let l: Geometry<_> = LineString(vec![Point::new(10., 0.), Point::new(20., 0.)]).into();
        let gc: Geometry<_> = Geometry::GeometryCollection(GeometryCollection(vec![p, l]));

        let res = geom_to_wkb(&gc);
        let mut res = res.as_slice();
        assert_eq!(res.read_u8().unwrap(), 1);
    }

    #[test]
    fn test_simple_multilinestring1() {
        let wkb: Vec<u8> = vec![1, 5, 0, 0, 0, 1, 0, 0, 0, 1, 2, 0,
            0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 240, 63, 0, 0, 0, 0, 0, 0, 240, 63];

        let geom = wkb_to_geom(&mut wkb.as_slice()).unwrap();
        assert_eq!(geom, Geometry::MultiLineString(MultiLineString(vec![
              LineString(vec![Point::new(0., 0.), Point::new(1., 1.)]),
              ])));
    }

    #[test]
    fn test_simple_multilinestring2() {

        let wkb: Vec<u8> = vec![
            0x01, 0x05, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00, 0x00,
            0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f, 0x01, 0x02, 0x00, 0x00, 0x00, 0x02,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x40, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40
        ];

        let geom = wkb_to_geom(&mut wkb.as_slice()).unwrap();
        assert_eq!(geom, Geometry::MultiLineString(MultiLineString(vec![
              LineString(vec![Point::new(0., 0.), Point::new(1., 1.)]),
              LineString(vec![Point::new(2., 2.), Point::new(3., 2.)]),
              ])));
    }

}