@@ -377,8 +377,6 @@ def to_crs(
377
377
_obj = _obj .drop_indexes (variable_crs_solved .keys ())
378
378
379
379
for key , crs in variable_crs_solved .items ():
380
- if crs :
381
- _obj [key ].attrs ["crs" ] = CRS .from_user_input (crs )
382
380
_obj = _obj .set_xindex ([key ], GeometryIndex , crs = crs )
383
381
384
382
return _obj
@@ -507,8 +505,6 @@ def set_crs(
507
505
_obj = _obj .drop_indexes (variable_crs_solved .keys ())
508
506
509
507
for key , crs in variable_crs_solved .items ():
510
- if crs :
511
- _obj [key ].attrs ["crs" ] = CRS .from_user_input (crs )
512
508
_obj = _obj .set_xindex ([key ], GeometryIndex , crs = crs )
513
509
514
510
return _obj
@@ -698,12 +694,19 @@ def set_geom_indexes(
698
694
_obj = _obj .drop_indexes (coord_names )
699
695
700
696
for coord in coord_names :
701
- if crs :
702
- _obj [coord ].attrs ["crs" ] = CRS .from_user_input (crs )
703
697
_obj = _obj .set_xindex (coord , GeometryIndex , crs = crs , ** kwargs )
704
698
705
699
return _obj
706
700
701
+ def _get_crs (self , name ) -> CRS | None :
702
+ if name in self ._geom_indexes :
703
+ crs = self ._obj .xindexes [name ].crs
704
+ elif name in self ._geom_coords_all :
705
+ crs = self ._obj [name ].attrs .get ("crs" , None )
706
+ else :
707
+ crs = None
708
+ return crs
709
+
707
710
def to_geopandas (self ) -> GeoDataFrame | pd .DataFrame :
708
711
"""Convert this array into a GeoPandas :class:`~geopandas.GeoDataFrame`
709
712
@@ -777,13 +780,13 @@ def to_geopandas(self) -> GeoDataFrame | pd.DataFrame:
777
780
# ensure CRS of all columns is preserved
778
781
for c in gdf .columns :
779
782
if c in self ._geom_coords_all :
780
- gdf [c ] = gpd .GeoSeries (gdf [c ], crs = self ._obj [ c ]. attrs . get ( "crs" , None ))
783
+ gdf [c ] = gpd .GeoSeries (gdf [c ], crs = self ._get_crs ( c ))
781
784
782
785
# if geometry is an index, reset and assign as active
783
786
index_name = gdf .index .name
784
- if index_name in self ._geom_coords_all :
787
+ if index_name in self ._geom_indexes :
785
788
return gdf .reset_index ().set_geometry (
786
- index_name , crs = self ._obj [ index_name ]. attrs . get ( "crs" , None )
789
+ index_name , crs = self ._get_crs ( index_name )
787
790
) # type: ignore
788
791
789
792
warnings .warn (
@@ -896,12 +899,10 @@ def to_geodataframe(
896
899
# ensure CRS of all columns is preserved
897
900
for c in df .columns :
898
901
if c in self ._geom_coords_all :
899
- df [c ] = gpd .GeoSeries (df [c ], crs = self ._obj [ c ]. attrs . get ( "crs" , None ))
902
+ df [c ] = gpd .GeoSeries (df [c ], crs = self ._get_crs ( c ))
900
903
901
904
if geometry is not None :
902
- return df .set_geometry (
903
- geometry , crs = self ._obj [geometry ].attrs .get ("crs" , None )
904
- ) # type: ignore
905
+ return df .set_geometry (geometry , crs = self ._get_crs (geometry )) # type: ignore
905
906
906
907
warnings .warn (
907
908
"No active geometry column to be set. The resulting object "
0 commit comments