@@ -19,7 +19,10 @@ use databend_common_exception::ErrorCode;
1919use databend_common_exception:: Result ;
2020use geo:: BoundingRect ;
2121use geo:: Geometry ;
22+ use geo:: LineString ;
2223use geo:: Point ;
24+ use geo:: Polygon ;
25+ use geo:: Rect ;
2326use geohash:: encode;
2427use geozero:: CoordDimensions ;
2528use geozero:: GeomProcessor ;
@@ -31,6 +34,7 @@ use geozero::ToWkt;
3134use geozero:: geo_types:: GeoWriter ;
3235use geozero:: geojson:: GeoJson ;
3336use geozero:: wkb:: Ewkb ;
37+ use hex:: encode_upper;
3438use serde:: Deserialize ;
3539use serde:: Serialize ;
3640use wkt:: TryFromWkt ;
@@ -185,49 +189,18 @@ pub(crate) fn ewkt_str_to_geo(input: &str) -> Result<(Geometry, Option<i32>)> {
185189 }
186190}
187191
188- pub trait GeometryFormatOutput {
189- fn format ( self , data_type : GeometryDataType ) -> Result < String > ;
190- }
191- impl < B : AsRef < [ u8 ] > > GeometryFormatOutput for Ewkb < B > {
192- fn format ( self , format_type : GeometryDataType ) -> Result < String > {
193- match format_type {
194- GeometryDataType :: WKB => self
195- . to_wkb ( CoordDimensions :: xy ( ) )
196- . map ( |bytes| {
197- bytes
198- . iter ( )
199- . map ( |b| format ! ( "{:02X}" , b) )
200- . collect :: < Vec < _ > > ( )
201- . join ( "" )
202- } )
203- . map_err ( |e| ErrorCode :: GeometryError ( e. to_string ( ) ) ) ,
204- GeometryDataType :: EWKB => Ok ( self
205- . 0
206- . as_ref ( )
207- . iter ( )
208- . map ( |b| format ! ( "{:02X}" , b) )
209- . collect :: < Vec < _ > > ( )
210- . join ( "" ) ) ,
211- GeometryDataType :: WKT => self
212- . to_wkt ( )
213- . map_err ( |e| ErrorCode :: GeometryError ( e. to_string ( ) ) ) ,
214- GeometryDataType :: EWKT => self
215- . to_ewkt ( self . srid ( ) )
216- . map_err ( |e| ErrorCode :: GeometryError ( e. to_string ( ) ) ) ,
217- GeometryDataType :: GEOJSON => self
218- . to_json ( )
219- . map_err ( |e| ErrorCode :: GeometryError ( e. to_string ( ) ) ) ,
220- }
192+ pub fn geometry_format ( ewkb : & [ u8 ] , format_type : GeometryDataType ) -> Result < String > {
193+ let ( geo, srid) = ewkb_to_geo ( & mut Ewkb ( ewkb) ) ?;
194+ let srid = srid. unwrap_or ( 0 ) ;
195+ match format_type {
196+ GeometryDataType :: WKB => geo_to_wkb ( geo) . map ( encode_upper) ,
197+ GeometryDataType :: EWKB => geo_to_ewkb ( geo, Some ( srid) ) . map ( encode_upper) ,
198+ GeometryDataType :: WKT => geo_to_wkt ( geo) ,
199+ GeometryDataType :: EWKT => geo_to_ewkt ( geo, Some ( srid) ) ,
200+ GeometryDataType :: GEOJSON => geo_to_json ( geo) ,
221201 }
222202}
223203
224- pub fn geometry_format < T : GeometryFormatOutput > (
225- geometry : T ,
226- format_type : GeometryDataType ,
227- ) -> Result < String > {
228- geometry. format ( format_type)
229- }
230-
231204/// Convert Geometry object to GEOJSON format.
232205pub fn geo_to_json ( geo : Geometry ) -> Result < String > {
233206 geo. to_json ( )
@@ -258,6 +231,19 @@ pub fn geo_to_ewkt(geo: Geometry, srid: Option<i32>) -> Result<String> {
258231 . map_err ( |e| ErrorCode :: GeometryError ( e. to_string ( ) ) )
259232}
260233
234+ pub fn rect_to_polygon ( rect : Rect < f64 > ) -> Polygon < f64 > {
235+ let min = rect. min ( ) ;
236+ let max = rect. max ( ) ;
237+ let exterior = LineString :: from ( vec ! [
238+ ( min. x, min. y) ,
239+ ( max. x, min. y) ,
240+ ( max. x, max. y) ,
241+ ( min. x, max. y) ,
242+ ( min. x, min. y) ,
243+ ] ) ;
244+ Polygon :: new ( exterior, vec ! [ ] )
245+ }
246+
261247/// Process EWKB input and return SRID.
262248pub fn read_srid < B : AsRef < [ u8 ] > > ( ewkb : & mut Ewkb < B > ) -> Option < i32 > {
263249 let mut srid_processor = SridProcessor :: new ( ) ;
0 commit comments