@@ -6,8 +6,9 @@ mod bounding_rect;
66use std:: io:: Write ;
77use std:: sync:: Arc ;
88
9- use geoarrow_array:: GeoArrowArray ;
109use geoarrow_array:: cast:: AsGeoArrowArray ;
10+ use geoarrow_array:: { GeoArrowArray , GeoArrowArrayAccessor , downcast_geoarrow_array} ;
11+ use geoarrow_expr_geo:: util:: to_geo:: geometry_to_geo;
1112use geoarrow_schema:: GeoArrowType ;
1213use geoarrow_schema:: error:: GeoArrowError ;
1314use pyo3:: exceptions:: { PyIOError , PyValueError } ;
@@ -82,12 +83,13 @@ impl PyGeoScalar {
8283 }
8384 }
8485
85- #[ cfg( feature = "geozero " ) ]
86+ #[ cfg( feature = "geojson " ) ]
8687 #[ getter]
8788 fn __geo_interface__ < ' py > ( & ' py self , py : Python < ' py > ) -> PyGeoArrowResult < Bound < ' py , PyAny > > {
88- let json_string = to_json ( & self . 0 ) . map_err ( |err| GeoArrowError :: External ( Box :: new ( err) ) ) ?;
89+ let geojson_geometry = scalar_to_geojson ( & self . 0 ) ?;
90+ let geojson_string = serde_json:: to_string ( & geojson_geometry) ?;
8991 let json_mod = py. import ( intern ! ( py, "json" ) ) ?;
90- Ok ( json_mod. call_method1 ( intern ! ( py, "loads" ) , ( json_string , ) ) ?)
92+ Ok ( json_mod. call_method1 ( intern ! ( py, "loads" ) , ( geojson_string , ) ) ?)
9193 }
9294
9395 #[ cfg( feature = "geozero" ) ]
@@ -182,25 +184,15 @@ fn process_svg_geom<W: Write>(
182184 }
183185}
184186
185- #[ cfg( feature = "geozero" ) ]
186- fn to_json ( arr : & dyn GeoArrowArray ) -> geozero:: error:: Result < String > {
187- use GeoArrowType :: * ;
188- use geozero:: ToJson ;
189- match arr. data_type ( ) {
190- Point ( _) => arr. as_point ( ) . to_json ( ) ,
191- LineString ( _) => arr. as_line_string ( ) . to_json ( ) ,
192- Polygon ( _) => arr. as_polygon ( ) . to_json ( ) ,
193- MultiPoint ( _) => arr. as_multi_point ( ) . to_json ( ) ,
194- MultiLineString ( _) => arr. as_multi_line_string ( ) . to_json ( ) ,
195- MultiPolygon ( _) => arr. as_multi_polygon ( ) . to_json ( ) ,
196- GeometryCollection ( _) => arr. as_geometry_collection ( ) . to_json ( ) ,
197- Geometry ( _) => arr. as_geometry ( ) . to_json ( ) ,
198- Rect ( _) => arr. as_rect ( ) . to_json ( ) ,
199- Wkb ( _) => arr. as_wkb :: < i32 > ( ) . to_json ( ) ,
200- LargeWkb ( _) => arr. as_wkb :: < i64 > ( ) . to_json ( ) ,
201- WkbView ( _) => arr. as_wkb_view ( ) . to_json ( ) ,
202- Wkt ( _) => arr. as_wkt :: < i32 > ( ) . to_json ( ) ,
203- LargeWkt ( _) => arr. as_wkt :: < i64 > ( ) . to_json ( ) ,
204- WktView ( _) => arr. as_wkt_view ( ) . to_json ( ) ,
205- }
187+ #[ cfg( feature = "geojson" ) ]
188+ fn scalar_to_geojson ( scalar : & dyn GeoArrowArray ) -> PyGeoArrowResult < geojson:: Geometry > {
189+ downcast_geoarrow_array ! ( scalar, impl_to_geojson)
190+ }
191+
192+ #[ cfg( feature = "geojson" ) ]
193+ fn impl_to_geojson < ' a > (
194+ array : & ' a impl GeoArrowArrayAccessor < ' a > ,
195+ ) -> PyGeoArrowResult < geojson:: Geometry > {
196+ let geo_geom = geometry_to_geo ( & array. value ( 0 ) ?) ?;
197+ Ok ( ( & geo_geom) . into ( ) )
206198}
0 commit comments