@@ -460,10 +460,11 @@ def shapely_to_cf(
460
460
)
461
461
462
462
# 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 }
467
468
468
469
grid_mapping_varname = None
469
470
if (
@@ -841,7 +842,7 @@ def polygons_to_cf(
841
842
node_count = part_node_count
842
843
elif len (offsets ) >= 2 :
843
844
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 )
845
846
846
847
if len (offsets ) == 3 :
847
848
indices = np .take (indices , offsets [2 ])
@@ -852,29 +853,31 @@ def polygons_to_cf(
852
853
crdX = geom_coords [:, 0 ]
853
854
crdY = geom_coords [:, 1 ]
854
855
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
+ )
855
873
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 ,
863
875
coords = names .coords (x = x , y = y , crdX = crdX , crdY = crdY , dim = dim ),
864
876
)
865
877
866
878
if coord is not None :
867
879
ds = ds .assign_coords ({dim : coord })
868
880
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
878
881
return ds
879
882
880
883
0 commit comments