11from typing import Literal
22
33import cf_xarray # noqa: F401
4+ import geoarrow .rust .core as geoarrow
45import numpy as np
5- import shapely
66import xarray as xr
77from numpy .typing import ArrayLike
88
@@ -13,6 +13,18 @@ def is_meshgrid(coord1: ArrayLike, coord2: ArrayLike):
1313 ) or (np .all (coord1 [:, 0 ] == coord1 [:, 1 ]) and np .all (coord2 [0 , :] == coord2 [1 , :]))
1414
1515
16+ def as_components (boundaries ):
17+ vertices = np .concatenate ([boundaries , boundaries [..., :1 , :]], axis = - 2 )
18+
19+ coords = np .reshape (vertices , (- 1 , 2 ))
20+
21+ coords_per_pixel = vertices .shape [- 2 ]
22+ geom_offsets = np .arange (np .prod (vertices .shape [:- 2 ]) + 1 , dtype = "int32" )
23+ ring_offsets = geom_offsets * coords_per_pixel
24+
25+ return coords , geom_offsets , ring_offsets
26+
27+
1628def infer_grid_type (ds : xr .Dataset ):
1729 # grid types (all geographic):
1830 # - 2d crs (affine transform)
@@ -99,4 +111,4 @@ def infer_cell_geometries(
99111 bound_names = [with_bounds .cf .bounds [name ][0 ] for name in coords ]
100112 boundaries = np .stack ([with_bounds .variables [n ].data for n in bound_names ], axis = - 1 )
101113
102- return shapely .polygons (boundaries )
114+ return geoarrow .polygons (* as_components ( boundaries ), crs = 4326 )
0 commit comments