1
1
use types as postgis;
2
- use ewkb:: { AsEwkbPoint , EwkbPoint , AsEwkbLineString , EwkbLineString } ;
2
+ use ewkb;
3
3
use std:: io:: prelude:: * ;
4
4
use std:: mem;
5
5
use std:: fmt;
@@ -217,9 +217,9 @@ impl TwkbGeom for Point {
217
217
}
218
218
}
219
219
220
- impl < ' a > AsEwkbPoint < ' a > for Point {
221
- fn as_ewkb ( & ' a self ) -> EwkbPoint < ' a > {
222
- EwkbPoint { geom : self , srid : None , point_type : postgis:: PointType :: Point }
220
+ impl < ' a > ewkb :: AsEwkbPoint < ' a > for Point {
221
+ fn as_ewkb ( & ' a self ) -> ewkb :: EwkbPoint < ' a > {
222
+ ewkb :: EwkbPoint { geom : self , srid : None , point_type : postgis:: PointType :: Point }
223
223
}
224
224
}
225
225
@@ -246,6 +246,23 @@ impl TwkbGeom for LineString {
246
246
}
247
247
}
248
248
249
+ impl < ' a > postgis:: LineString < ' a > for LineString {
250
+ type ItemType = Point ;
251
+ type Iter = Iter < ' a , Self :: ItemType > ;
252
+ fn points ( & ' a self ) -> Self :: Iter {
253
+ self . points . iter ( )
254
+ }
255
+ }
256
+
257
+ impl < ' a > ewkb:: AsEwkbLineString < ' a > for LineString {
258
+ type PointType = Point ;
259
+ type Iter = Iter < ' a , Point > ;
260
+ fn as_ewkb ( & ' a self ) -> ewkb:: EwkbLineString < ' a , Self :: PointType , Self :: Iter > {
261
+ ewkb:: EwkbLineString { geom : self , srid : None , point_type : postgis:: PointType :: Point }
262
+ }
263
+ }
264
+
265
+
249
266
impl TwkbGeom for Polygon {
250
267
fn read_twkb_body < R : Read > ( raw : & mut R , twkb_info : & TwkbInfo ) -> Result < Self , Error > {
251
268
// nrings uvarint
@@ -281,6 +298,24 @@ impl TwkbGeom for Polygon {
281
298
}
282
299
}
283
300
301
+ impl < ' a > postgis:: Polygon < ' a > for Polygon {
302
+ type ItemType = LineString ;
303
+ type Iter = Iter < ' a , Self :: ItemType > ;
304
+ fn rings ( & ' a self ) -> Self :: Iter {
305
+ self . rings . iter ( )
306
+ }
307
+ }
308
+
309
+ impl < ' a > ewkb:: AsEwkbPolygon < ' a > for Polygon {
310
+ type PointType = Point ;
311
+ type PointIter = Iter < ' a , Point > ;
312
+ type ItemType = LineString ;
313
+ type Iter = Iter < ' a , Self :: ItemType > ;
314
+ fn as_ewkb ( & ' a self ) -> ewkb:: EwkbPolygon < ' a , Self :: PointType , Self :: PointIter , Self :: ItemType , Self :: Iter > {
315
+ ewkb:: EwkbPolygon { geom : self , srid : None , point_type : postgis:: PointType :: Point }
316
+ }
317
+ }
318
+
284
319
285
320
impl TwkbGeom for MultiPoint {
286
321
fn read_twkb_body < R : Read > ( raw : & mut R , twkb_info : & TwkbInfo ) -> Result < Self , Error > {
@@ -312,6 +347,23 @@ impl TwkbGeom for MultiPoint {
312
347
}
313
348
}
314
349
350
+ impl < ' a > postgis:: MultiPoint < ' a > for MultiPoint {
351
+ type ItemType = Point ;
352
+ type Iter = Iter < ' a , Self :: ItemType > ;
353
+ fn points ( & ' a self ) -> Self :: Iter {
354
+ self . points . iter ( )
355
+ }
356
+ }
357
+
358
+ impl < ' a > ewkb:: AsEwkbMultiPoint < ' a > for MultiPoint {
359
+ type PointType = Point ;
360
+ type Iter = Iter < ' a , Point > ;
361
+ fn as_ewkb ( & ' a self ) -> ewkb:: EwkbMultiPoint < ' a , Self :: PointType , Self :: Iter > {
362
+ ewkb:: EwkbMultiPoint { geom : self , srid : None , point_type : postgis:: PointType :: Point }
363
+ }
364
+ }
365
+
366
+
315
367
impl TwkbGeom for MultiLineString {
316
368
fn read_twkb_body < R : Read > ( raw : & mut R , twkb_info : & TwkbInfo ) -> Result < Self , Error > {
317
369
// nlinestrings uvarint
@@ -350,6 +402,25 @@ impl TwkbGeom for MultiLineString {
350
402
}
351
403
}
352
404
405
+ impl < ' a > postgis:: MultiLineString < ' a > for MultiLineString {
406
+ type ItemType = LineString ;
407
+ type Iter = Iter < ' a , Self :: ItemType > ;
408
+ fn lines ( & ' a self ) -> Self :: Iter {
409
+ self . lines . iter ( )
410
+ }
411
+ }
412
+
413
+ impl < ' a > ewkb:: AsEwkbMultiLineString < ' a > for MultiLineString {
414
+ type PointType = Point ;
415
+ type PointIter = Iter < ' a , Point > ;
416
+ type ItemType = LineString ;
417
+ type Iter = Iter < ' a , Self :: ItemType > ;
418
+ fn as_ewkb ( & ' a self ) -> ewkb:: EwkbMultiLineString < ' a , Self :: PointType , Self :: PointIter , Self :: ItemType , Self :: Iter > {
419
+ ewkb:: EwkbMultiLineString { geom : self , srid : None , point_type : postgis:: PointType :: Point }
420
+ }
421
+ }
422
+
423
+
353
424
impl TwkbGeom for MultiPolygon {
354
425
fn read_twkb_body < R : Read > ( raw : & mut R , twkb_info : & TwkbInfo ) -> Result < Self , Error > {
355
426
// npolygons uvarint
@@ -401,27 +472,29 @@ impl TwkbGeom for MultiPolygon {
401
472
}
402
473
}
403
474
404
-
405
- impl < ' a > AsEwkbLineString < ' a > for LineString {
406
- type PointType = Point ;
407
- type Iter = Iter < ' a , Point > ;
408
- fn as_ewkb ( & ' a self ) -> EwkbLineString < ' a , Self :: PointType , Self :: Iter > {
409
- EwkbLineString { geom : self , srid : None , point_type : postgis:: PointType :: Point }
475
+ impl < ' a > postgis:: MultiPolygon < ' a > for MultiPolygon {
476
+ type ItemType = Polygon ;
477
+ type Iter = Iter < ' a , Self :: ItemType > ;
478
+ fn polygons ( & ' a self ) -> Self :: Iter {
479
+ self . polygons . iter ( )
410
480
}
411
481
}
412
482
413
-
414
- impl < ' a > postgis:: LineString < ' a > for LineString {
415
- type ItemType = Point ;
483
+ impl < ' a > ewkb:: AsEwkbMultiPolygon < ' a > for MultiPolygon {
484
+ type PointType = Point ;
485
+ type PointIter = Iter < ' a , Point > ;
486
+ type LineType = LineString ;
487
+ type LineIter = Iter < ' a , Self :: LineType > ;
488
+ type ItemType = Polygon ;
416
489
type Iter = Iter < ' a , Self :: ItemType > ;
417
- fn points ( & ' a self ) -> Self :: Iter {
418
- self . points . iter ( )
490
+ fn as_ewkb ( & ' a self ) -> ewkb :: EwkbMultiPolygon < ' a , Self :: PointType , Self :: PointIter , Self :: LineType , Self :: LineIter , Self :: ItemType , Self :: Iter > {
491
+ ewkb :: EwkbMultiPolygon { geom : self , srid : None , point_type : postgis :: PointType :: Point }
419
492
}
420
493
}
421
494
422
495
423
496
#[ cfg( test) ]
424
- use ewkb:: EwkbWrite ;
497
+ use ewkb:: { EwkbWrite , AsEwkbPoint , AsEwkbLineString , AsEwkbPolygon , AsEwkbMultiPoint , AsEwkbMultiLineString , AsEwkbMultiPolygon } ;
425
498
426
499
#[ cfg( test) ]
427
500
fn hex_to_vec ( hexstr : & str ) -> Vec < u8 > {
@@ -517,3 +590,39 @@ fn test_write_line() {
517
590
assert_eq ! ( format!( "{:?}" , line. as_ewkb( ) ) , "$ewkbtype" ) ;
518
591
assert_eq ! ( line. as_ewkb( ) . to_hex_ewkb( ) , "010200000002000000000000000000244000000000000034C00000000000000000000000000000E0BF" ) ;
519
592
}
593
+
594
+ #[ test]
595
+ fn test_write_polygon ( ) {
596
+ let twkb = hex_to_vec ( "03000205000004000004030000030514141700001718000018" ) ; // SELECT encode(ST_AsTWKB('POLYGON ((0 0, 2 0, 2 2, 0 2, 0 0),(10 10, -2 10, -2 -2, 10 -2, 10 10))'::geometry), 'hex')
597
+ let polygon = Polygon :: read_twkb ( & mut twkb. as_slice ( ) ) . unwrap ( ) ;
598
+ assert_eq ! ( format!( "{:?}" , polygon. as_ewkb( ) ) , "$ewkbtype" ) ;
599
+ assert_eq ! ( polygon. as_ewkb( ) . to_hex_ewkb( ) , "010300000002000000050000000000000000000000000000000000000000000000000000400000000000000000000000000000004000000000000000400000000000000000000000000000004000000000000000000000000000000000050000000000000000002440000000000000244000000000000000C0000000000000244000000000000000C000000000000000C0000000000000244000000000000000C000000000000024400000000000002440" ) ;
600
+ }
601
+
602
+ #[ test]
603
+ fn test_write_multipoint ( ) {
604
+ let twkb = hex_to_vec ( "04000214271326" ) ; // SELECT encode(ST_AsTWKB('MULTIPOINT ((10 -20), (0 -0.5))'::geometry), 'hex')
605
+ let multipoint = MultiPoint :: read_twkb ( & mut twkb. as_slice ( ) ) . unwrap ( ) ;
606
+ assert_eq ! ( format!( "{:?}" , multipoint. as_ewkb( ) ) , "$ewkbtype" ) ;
607
+ //assert_eq!(multipoint.as_ewkb().to_hex_ewkb(), "0104000000020000000101000000000000000000244000000000000034C001010000000000000000000000000000000000E0BF");
608
+ // "MULTIPOINT(10 -20,0 -1)"
609
+ assert_eq ! ( multipoint. as_ewkb( ) . to_hex_ewkb( ) , "0104000000020000000101000000000000000000244000000000000034C001010000000000000000000000000000000000F0BF" ) ;
610
+ }
611
+
612
+ #[ test]
613
+ fn test_write_multiline ( ) {
614
+ let twkb = hex_to_vec ( "05000202142713260200020400" ) ; // SELECT encode(ST_AsTWKB('MULTILINESTRING ((10 -20, 0 -0.5), (0 0, 2 0))'::geometry), 'hex')
615
+ let multiline = MultiLineString :: read_twkb ( & mut twkb. as_slice ( ) ) . unwrap ( ) ;
616
+ assert_eq ! ( format!( "{:?}" , multiline. as_ewkb( ) ) , "$ewkbtype" ) ;
617
+ //assert_eq!(multiline.as_ewkb().to_hex_ewkb(), "010500000002000000010200000002000000000000000000244000000000000034C00000000000000000000000000000E0BF0102000000020000000000000000000000000000000000000000000000000000400000000000000000");
618
+ // "MULTILINESTRING((10 -20,0 -1),(0 0,2 0))"
619
+ assert_eq ! ( multiline. as_ewkb( ) . to_hex_ewkb( ) , "010500000002000000010200000002000000000000000000244000000000000034C00000000000000000000000000000F0BF0102000000020000000000000000000000000000000000000000000000000000400000000000000000" ) ;
620
+ }
621
+
622
+ #[ test]
623
+ fn test_write_multipoly ( ) {
624
+ let twkb = hex_to_vec ( "060002010500000400000403000003010514141700001718000018" ) ; // SELECT encode(ST_AsTWKB('MULTIPOLYGON (((0 0, 2 0, 2 2, 0 2, 0 0)), ((10 10, -2 10, -2 -2, 10 -2, 10 10)))'::geometry), 'hex')
625
+ let multipoly = MultiPolygon :: read_twkb ( & mut twkb. as_slice ( ) ) . unwrap ( ) ;
626
+ assert_eq ! ( format!( "{:?}" , multipoly. as_ewkb( ) ) , "$ewkbtype" ) ;
627
+ assert_eq ! ( multipoly. as_ewkb( ) . to_hex_ewkb( ) , "010600000002000000010300000001000000050000000000000000000000000000000000000000000000000000400000000000000000000000000000004000000000000000400000000000000000000000000000004000000000000000000000000000000000010300000001000000050000000000000000002440000000000000244000000000000000C0000000000000244000000000000000C000000000000000C0000000000000244000000000000000C000000000000024400000000000002440" ) ;
628
+ }
0 commit comments