Skip to content

Commit d8d3de8

Browse files
committed
Speed up geometry encoding
Closes #534
1 parent acc1eb0 commit d8d3de8

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

Diff for: cf_xarray/geometry.py

+24-21
Original file line numberDiff line numberDiff line change
@@ -460,10 +460,11 @@ def shapely_to_cf(
460460
)
461461

462462
# Get all types to call the appropriate translation function.
463-
types = {
464-
geom.item().geom_type if isinstance(geom, xr.DataArray) else geom.geom_type
465-
for geom in geometries
466-
}
463+
geometries = (
464+
geometries.data if isinstance(geometries, xr.DataArray) else geometries
465+
) #
466+
# types = {geom.geom_type for geom in geometries}
467+
types = {geometries[0].geom_type}
467468

468469
grid_mapping_varname = None
469470
if (
@@ -841,7 +842,7 @@ def polygons_to_cf(
841842
node_count = part_node_count
842843
elif len(offsets) >= 2:
843844
indices = np.take(offsets[0], offsets[1])
844-
interior_ring = np.isin(offsets[0], indices, invert=True)[:-1].astype(int)
845+
interior_ring = np.isin(offsets[0], indices, invert=True)[:-1].view(np.int8)
845846

846847
if len(offsets) == 3:
847848
indices = np.take(indices, offsets[2])
@@ -852,29 +853,31 @@ def polygons_to_cf(
852853
crdX = geom_coords[:, 0]
853854
crdY = geom_coords[:, 1]
854855

856+
data_vars = {names.node_count: (dim, node_count)}
857+
858+
# Special case when we have no MultiPolygons and no holes
859+
if len(part_node_count) != len(node_count):
860+
data_vars[names.part_node_count] = (names.part_dim, part_node_count)
861+
names.geometry_container_attrs["part_node_count"] = names.part_node_count
862+
863+
# Special case when we have no holes
864+
if (interior_ring != 0).any():
865+
data_vars[names.interior_ring] = (names.part_dim, interior_ring)
866+
names.geometry_container_attrs["interior_ring"] = names.interior_ring
867+
868+
data_vars[names.container_name] = (
869+
(),
870+
np.nan,
871+
{"geometry_type": "polygon", **names.geometry_container_attrs},
872+
)
855873
ds = xr.Dataset(
856-
data_vars={
857-
names.node_count: xr.DataArray(node_count, dims=(dim,)),
858-
names.container_name: xr.DataArray(
859-
data=np.nan,
860-
attrs={"geometry_type": "polygon", **names.geometry_container_attrs},
861-
),
862-
},
874+
data_vars=data_vars,
863875
coords=names.coords(x=x, y=y, crdX=crdX, crdY=crdY, dim=dim),
864876
)
865877

866878
if coord is not None:
867879
ds = ds.assign_coords({dim: coord})
868880

869-
# Special case when we have no MultiPolygons and no holes
870-
if len(part_node_count) != len(node_count):
871-
ds[names.part_node_count] = xr.DataArray(part_node_count, dims=names.part_dim)
872-
ds[names.container_name].attrs["part_node_count"] = names.part_node_count
873-
874-
# Special case when we have no holes
875-
if (interior_ring != 0).any():
876-
ds[names.interior_ring] = xr.DataArray(interior_ring, dims=names.part_dim)
877-
ds[names.container_name].attrs["interior_ring"] = names.interior_ring
878881
return ds
879882

880883

0 commit comments

Comments
 (0)