@@ -854,29 +854,32 @@ def polygons_to_cf(
854
854
crdX = geom_coords [:, 0 ]
855
855
crdY = geom_coords [:, 1 ]
856
856
857
+ data_vars = {names .node_count : (dim , node_count )}
858
+ geometry_attrs = names .geometry_container_attrs
859
+
860
+ # Special case when we have no MultiPolygons and no holes
861
+ if len (part_node_count ) != len (node_count ):
862
+ data_vars [names .part_node_count ] = (names .part_dim , part_node_count )
863
+ geometry_attrs ["part_node_count" ] = names .part_node_count
864
+
865
+ # Special case when we have no holes
866
+ if interior_ring .any ():
867
+ data_vars [names .interior_ring ] = (names .part_dim , interior_ring )
868
+ geometry_attrs ["interior_ring" ] = names .interior_ring
869
+
870
+ data_vars [names .container_name ] = (
871
+ (),
872
+ np .nan ,
873
+ {"geometry_type" : "polygon" , ** geometry_attrs },
874
+ )
857
875
ds = xr .Dataset (
858
- data_vars = {
859
- names .node_count : xr .DataArray (node_count , dims = (dim ,)),
860
- names .container_name : xr .DataArray (
861
- data = np .nan ,
862
- attrs = {"geometry_type" : "polygon" , ** names .geometry_container_attrs },
863
- ),
864
- },
876
+ data_vars = data_vars ,
865
877
coords = names .coords (x = x , y = y , crdX = crdX , crdY = crdY , dim = dim ),
866
878
)
867
879
868
880
if coord is not None :
869
881
ds = ds .assign_coords ({dim : coord })
870
882
871
- # Special case when we have no MultiPolygons and no holes
872
- if len (part_node_count ) != len (node_count ):
873
- ds [names .part_node_count ] = xr .DataArray (part_node_count , dims = names .part_dim )
874
- ds [names .container_name ].attrs ["part_node_count" ] = names .part_node_count
875
-
876
- # Special case when we have no holes
877
- if interior_ring .any ():
878
- ds [names .interior_ring ] = xr .DataArray (interior_ring , dims = names .part_dim )
879
- ds [names .container_name ].attrs ["interior_ring" ] = names .interior_ring
880
883
return ds
881
884
882
885
0 commit comments