11use std:: io:: Cursor ;
22
33use arrow_array:: OffsetSizeTrait ;
4- use byteorder:: { BigEndian , LittleEndian , ReadBytesExt } ;
4+ use byteorder:: ReadBytesExt ;
55
66use crate :: datatypes:: Dimension ;
7- use crate :: geo_traits:: {
8- GeometryCollectionTrait , GeometryTrait , LineStringTrait , MultiLineStringTrait , MultiPointTrait ,
9- MultiPolygonTrait , PointTrait , PolygonTrait ,
10- } ;
7+ use crate :: error:: Result ;
8+ use crate :: geo_traits:: GeometryTrait ;
119use crate :: io:: wkb:: common:: WKBType ;
1210use crate :: io:: wkb:: reader:: geometry_collection:: WKBGeometryCollection ;
1311use crate :: io:: wkb:: reader:: rect:: WKBRect ;
@@ -18,97 +16,66 @@ use crate::io::wkb::reader::{
1816use crate :: scalar:: WKB ;
1917
2018impl < ' a , O : OffsetSizeTrait > WKB < ' a , O > {
19+ /// Convert this WKB scalar to a [WKBGeometry]
20+ ///
21+ /// This "prepares" the WKB input for constant-time coordinate access.
2122 pub fn to_wkb_object ( & ' a self ) -> WKBGeometry < ' a > {
22- let buf = self . arr . value ( self . geom_index ) ;
23+ let buf = self . as_slice ( ) ;
2324 let mut reader = Cursor :: new ( buf) ;
2425 let byte_order = reader. read_u8 ( ) . unwrap ( ) ;
25- let geometry_type_u32 = match byte_order {
26- 0 => reader. read_u32 :: < BigEndian > ( ) . unwrap ( ) ,
27- 1 => reader. read_u32 :: < LittleEndian > ( ) . unwrap ( ) ,
28- _ => panic ! ( "Unexpected byte order." ) ,
29- } ;
30- let geometry_type = WKBType :: try_from ( geometry_type_u32) . unwrap ( ) ;
26+ let wkb_type = self . wkb_type ( ) . unwrap ( ) ;
27+
28+ use Dimension :: * ;
3129
32- match geometry_type {
33- WKBType :: Point => {
34- WKBGeometry :: Point ( WKBPoint :: new ( buf, byte_order. into ( ) , 0 , Dimension :: XY ) )
30+ match wkb_type {
31+ WKBType :: Point => WKBGeometry :: Point ( WKBPoint :: new ( buf, byte_order. into ( ) , 0 , XY ) ) ,
32+ WKBType :: LineString => {
33+ WKBGeometry :: LineString ( WKBLineString :: new ( buf, byte_order. into ( ) , 0 , XY ) )
3534 }
36- WKBType :: LineString => WKBGeometry :: LineString ( WKBLineString :: new (
37- buf,
38- byte_order. into ( ) ,
39- 0 ,
40- Dimension :: XY ,
41- ) ) ,
4235 WKBType :: Polygon => {
43- WKBGeometry :: Polygon ( WKBPolygon :: new ( buf, byte_order. into ( ) , 0 , Dimension :: XY ) )
36+ WKBGeometry :: Polygon ( WKBPolygon :: new ( buf, byte_order. into ( ) , 0 , XY ) )
4437 }
4538 WKBType :: MultiPoint => {
46- WKBGeometry :: MultiPoint ( WKBMultiPoint :: new ( buf, byte_order. into ( ) , Dimension :: XY ) )
39+ WKBGeometry :: MultiPoint ( WKBMultiPoint :: new ( buf, byte_order. into ( ) , XY ) )
40+ }
41+ WKBType :: MultiLineString => {
42+ WKBGeometry :: MultiLineString ( WKBMultiLineString :: new ( buf, byte_order. into ( ) , XY ) )
43+ }
44+ WKBType :: MultiPolygon => {
45+ WKBGeometry :: MultiPolygon ( WKBMultiPolygon :: new ( buf, byte_order. into ( ) , XY ) )
4746 }
48- WKBType :: MultiLineString => WKBGeometry :: MultiLineString ( WKBMultiLineString :: new (
49- buf,
50- byte_order. into ( ) ,
51- Dimension :: XY ,
52- ) ) ,
53- WKBType :: MultiPolygon => WKBGeometry :: MultiPolygon ( WKBMultiPolygon :: new (
54- buf,
55- byte_order. into ( ) ,
56- Dimension :: XY ,
57- ) ) ,
5847 WKBType :: GeometryCollection => WKBGeometry :: GeometryCollection (
59- WKBGeometryCollection :: new ( buf, byte_order. into ( ) , Dimension :: XY ) ,
48+ WKBGeometryCollection :: new ( buf, byte_order. into ( ) , XY ) ,
6049 ) ,
61- WKBType :: PointZ => {
62- WKBGeometry :: Point ( WKBPoint :: new ( buf, byte_order. into ( ) , 0 , Dimension :: XYZ ) )
50+ WKBType :: PointZ => WKBGeometry :: Point ( WKBPoint :: new ( buf, byte_order. into ( ) , 0 , XYZ ) ) ,
51+ WKBType :: LineStringZ => {
52+ WKBGeometry :: LineString ( WKBLineString :: new ( buf, byte_order. into ( ) , 0 , XYZ ) )
6353 }
64- WKBType :: LineStringZ => WKBGeometry :: LineString ( WKBLineString :: new (
65- buf,
66- byte_order. into ( ) ,
67- 0 ,
68- Dimension :: XYZ ,
69- ) ) ,
7054 WKBType :: PolygonZ => {
71- WKBGeometry :: Polygon ( WKBPolygon :: new ( buf, byte_order. into ( ) , 0 , Dimension :: XYZ ) )
55+ WKBGeometry :: Polygon ( WKBPolygon :: new ( buf, byte_order. into ( ) , 0 , XYZ ) )
7256 }
7357 WKBType :: MultiPointZ => {
74- WKBGeometry :: MultiPoint ( WKBMultiPoint :: new ( buf, byte_order. into ( ) , Dimension :: XYZ ) )
58+ WKBGeometry :: MultiPoint ( WKBMultiPoint :: new ( buf, byte_order. into ( ) , XYZ ) )
59+ }
60+ WKBType :: MultiLineStringZ => {
61+ WKBGeometry :: MultiLineString ( WKBMultiLineString :: new ( buf, byte_order. into ( ) , XYZ ) )
62+ }
63+ WKBType :: MultiPolygonZ => {
64+ WKBGeometry :: MultiPolygon ( WKBMultiPolygon :: new ( buf, byte_order. into ( ) , XYZ ) )
7565 }
76- WKBType :: MultiLineStringZ => WKBGeometry :: MultiLineString ( WKBMultiLineString :: new (
77- buf,
78- byte_order. into ( ) ,
79- Dimension :: XYZ ,
80- ) ) ,
81- WKBType :: MultiPolygonZ => WKBGeometry :: MultiPolygon ( WKBMultiPolygon :: new (
82- buf,
83- byte_order. into ( ) ,
84- Dimension :: XYZ ,
85- ) ) ,
8666 WKBType :: GeometryCollectionZ => WKBGeometry :: GeometryCollection (
87- WKBGeometryCollection :: new ( buf, byte_order. into ( ) , Dimension :: XYZ ) ,
67+ WKBGeometryCollection :: new ( buf, byte_order. into ( ) , XYZ ) ,
8868 ) ,
8969 }
9070 }
9171
92- pub fn get_wkb_geometry_type ( & ' a self ) -> WKBType {
93- let buf = self . arr . value ( self . geom_index ) ;
94- let mut reader = Cursor :: new ( buf) ;
95- let byte_order = reader. read_u8 ( ) . unwrap ( ) ;
96- let geometry_type = match byte_order {
97- 0 => reader. read_u32 :: < BigEndian > ( ) . unwrap ( ) ,
98- 1 => reader. read_u32 :: < LittleEndian > ( ) . unwrap ( ) ,
99- _ => panic ! ( "Unexpected byte order." ) ,
100- } ;
101- geometry_type. try_into ( ) . unwrap ( )
102- }
103-
104- pub fn to_wkb_line_string ( & ' a self ) -> WKBLineString < ' a > {
105- match self . to_wkb_object ( ) {
106- WKBGeometry :: LineString ( geom) => geom,
107- _ => panic ! ( ) ,
108- }
72+ /// Access the [WKBType] of this WKB object.
73+ pub fn wkb_type ( & ' a self ) -> Result < WKBType > {
74+ WKBType :: from_buffer ( self . as_ref ( ) )
10975 }
11076}
11177
78+ /// Endianness
11279#[ derive( Debug , Clone , Copy ) ]
11380pub enum Endianness {
11481 BigEndian ,
@@ -226,15 +193,6 @@ impl<'a> WKBGeometry<'a> {
226193 }
227194}
228195
229- impl < ' a > From < WKBGeometry < ' a > > for WKBLineString < ' a > {
230- fn from ( value : WKBGeometry < ' a > ) -> Self {
231- match value {
232- WKBGeometry :: LineString ( geom) => geom,
233- _ => panic ! ( ) ,
234- }
235- }
236- }
237-
238196impl < ' a > GeometryTrait for WKBGeometry < ' a > {
239197 type T = f64 ;
240198 type Point < ' b > = WKBPoint < ' a > where Self : ' b ;
@@ -247,15 +205,7 @@ impl<'a> GeometryTrait for WKBGeometry<'a> {
247205 type Rect < ' b > = WKBRect < ' a > where Self : ' b ;
248206
249207 fn dim ( & self ) -> usize {
250- match self {
251- WKBGeometry :: Point ( g) => PointTrait :: dim ( g) ,
252- WKBGeometry :: LineString ( g) => LineStringTrait :: dim ( g) ,
253- WKBGeometry :: Polygon ( g) => PolygonTrait :: dim ( g) ,
254- WKBGeometry :: MultiPoint ( g) => MultiPointTrait :: dim ( g) ,
255- WKBGeometry :: MultiLineString ( g) => MultiLineStringTrait :: dim ( g) ,
256- WKBGeometry :: MultiPolygon ( g) => MultiPolygonTrait :: dim ( g) ,
257- WKBGeometry :: GeometryCollection ( g) => GeometryCollectionTrait :: dim ( g) ,
258- }
208+ self . dimension ( ) . size ( )
259209 }
260210
261211 fn as_type (
0 commit comments