@@ -460,25 +460,38 @@ impl Default for Config {
460
460
}
461
461
462
462
fn to_geoarrow_record_batch ( mut record_batch : RecordBatch , config : Config ) -> Result < RecordBatch > {
463
- if !config. convert_wkb {
464
- Ok ( record_batch)
465
- } else if let Some ( ( index, _) ) = record_batch. schema ( ) . column_with_name ( "geometry" ) {
466
- let geometry_column = record_batch. remove_column ( index) ;
467
- let binary_array: GenericByteArray < GenericBinaryType < i32 > > =
468
- geometry_column. as_binary :: < i32 > ( ) . clone ( ) ;
469
- let wkb_array = WKBArray :: new ( binary_array, Default :: default ( ) ) ;
470
- let geometry_array = geoarrow:: io:: wkb:: from_wkb (
471
- & wkb_array,
472
- NativeType :: Geometry ( CoordType :: Interleaved ) ,
473
- false ,
474
- ) ?;
475
- let mut columns = record_batch. columns ( ) . to_vec ( ) ;
476
- let mut schema_builder = SchemaBuilder :: from ( & * record_batch. schema ( ) ) ;
477
- schema_builder. push ( geometry_array. extension_field ( ) ) ;
478
- let schema = schema_builder. finish ( ) ;
479
- columns. push ( geometry_array. to_array_ref ( ) ) ;
480
- let record_batch = RecordBatch :: try_new ( schema. into ( ) , columns) ?;
481
- Ok ( record_batch)
463
+ if let Some ( ( index, field) ) = record_batch. schema ( ) . column_with_name ( "geometry" ) {
464
+ if config. convert_wkb {
465
+ let geometry_column = record_batch. remove_column ( index) ;
466
+ let binary_array: GenericByteArray < GenericBinaryType < i32 > > =
467
+ geometry_column. as_binary :: < i32 > ( ) . clone ( ) ;
468
+ let wkb_array = WKBArray :: new ( binary_array, Default :: default ( ) ) ;
469
+ let geometry_array = geoarrow:: io:: wkb:: from_wkb (
470
+ & wkb_array,
471
+ NativeType :: Geometry ( CoordType :: Interleaved ) ,
472
+ false ,
473
+ ) ?;
474
+ let mut columns = record_batch. columns ( ) . to_vec ( ) ;
475
+ let mut schema_builder = SchemaBuilder :: from ( & * record_batch. schema ( ) ) ;
476
+ schema_builder. push ( geometry_array. extension_field ( ) ) ;
477
+ let schema = schema_builder. finish ( ) ;
478
+ columns. push ( geometry_array. to_array_ref ( ) ) ;
479
+ let record_batch = RecordBatch :: try_new ( schema. into ( ) , columns) ?;
480
+ Ok ( record_batch)
481
+ } else {
482
+ let mut metadata = field. metadata ( ) . clone ( ) ;
483
+ metadata. insert (
484
+ "ARROW:extension:name" . to_string ( ) ,
485
+ "geoarrow.wkb" . to_string ( ) ,
486
+ ) ;
487
+ let field = field. clone ( ) . with_metadata ( metadata) ;
488
+ let mut schema_builder = SchemaBuilder :: from ( & * record_batch. schema ( ) ) ;
489
+ let field_ref = schema_builder. field_mut ( index) ;
490
+ * field_ref = field. into ( ) ;
491
+ let schema = schema_builder. finish ( ) ;
492
+ let record_batch = record_batch. with_schema ( schema. into ( ) ) ?;
493
+ Ok ( record_batch)
494
+ }
482
495
} else {
483
496
Ok ( record_batch)
484
497
}
@@ -676,5 +689,8 @@ mod tests {
676
689
. search_to_arrow_table ( "data/100-sentinel-2-items.parquet" , Search :: default ( ) )
677
690
. unwrap ( ) ;
678
691
assert_eq ! ( table. len( ) , 100 ) ;
692
+ let geometry = table. geometry_column ( None ) . unwrap ( ) ;
693
+ dbg ! ( geometry. extension_field( ) . metadata( ) ) ;
694
+ assert ! ( false ) ;
679
695
}
680
696
}
0 commit comments