geopandasgdalarcpyfiona

'InvalidVersion' error returned when writting an ESRI shp file geopandas.GeoDataFrame.to_file


When writing my GeoDataFrame to file an issue with GDAL version encountered.

My GeoDataFrame: geoPoliDF is (12,2):


geoPoliDF

    pol_id  pol_geometry
0   14      POLYGON ((9.000 0.000, 9.000 2.000, 10.000 2.0...
1   15      POLYGON ((9.000 3.000, 9.000 5.000, 10.000 5.0...
2   17      POLYGON ((9.000 9.000, 9.000 11.000, 10.000 11...
3   18      POLYGON ((9.000 12.000, 9.000 14.000, 10.000 1...
4   24      POLYGON ((3.000 0.000, 3.000 2.000, 5.000 2.00...
5   25      POLYGON ((3.000 3.000, 3.000 5.000, 5.000 5.00...
6   27      POLYGON ((3.000 9.000, 3.000 11.000, 5.000 11....
7   28      POLYGON ((3.000 12.000, 3.000 14.000, 5.000 14...
8   29      POLYGON ((0.000 0.000, 0.000 2.000, 2.000 2.00...
9   30      POLYGON ((0.000 3.000, 0.000 5.000, 2.000 5.00...
10  32      POLYGON ((0.000 9.000, 0.000 11.000, 2.000 11....
11  33      POLYGON ((0.000 12.000, 0.000 14.000, 2.000 14...

It contains polygon ids (pol_id) and their geometry (pol_geometry):

geoPoliDF['pol_id'].dtype, geoPoliDF['pol_geometry'].dtype

(dtype('int32'), <geopandas.array.GeometryDtype at 0x22017b8d0a0>)

Directories for input/output of files:

base_dir = r'C:\Users\ucesemu\Documents\code\AutomatedPhase2\AutomatedPhase2.gdb'

polyOutFc = os.path.join(base_dir,r'wFlatPoly.shp')

Writing the geoDataFrame to ESRI shapefile:

geoPoliDF.to_file(polyOutFc, driver='ESRI Shapefile',crs='EPSG:27700')

Issue encountered:

---------------------------------------------------------------------------
InvalidVersion                            Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_4624\2995218618.py in <cell line: 2>()
      1 polyOutFc = os.path.join(base_dir,r'wFlatPoly.shp')
----> 2 geoPoliDF.to_file(polyOutFc, driver='ESRI Shapefile',crs='EPSG:27700')

~\AppData\Local\ESRI\conda\envs\arcgispro-clone\lib\site-packages\geopandas\geodataframe.py in to_file(self, filename, driver, schema, index, **kwargs)
   1201         from geopandas.io.file import _to_file
   1202 
-> 1203         _to_file(self, filename, driver, schema, index, **kwargs)
   1204 
   1205     def set_crs(self, crs=None, epsg=None, inplace=False, allow_override=False):

~\AppData\Local\ESRI\conda\envs\arcgispro-clone\lib\site-packages\geopandas\io\file.py in _to_file(df, filename, driver, schema, index, mode, crs, engine, **kwargs)
    543 
    544     if engine == "fiona":
--> 545         _to_file_fiona(df, filename, driver, schema, crs, mode, **kwargs)
    546     elif engine == "pyogrio":
    547         _to_file_pyogrio(df, filename, driver, schema, crs, mode, **kwargs)

~\AppData\Local\ESRI\conda\envs\arcgispro-clone\lib\site-packages\geopandas\io\file.py in _to_file_fiona(df, filename, driver, schema, crs, mode, **kwargs)
    566         except AttributeError:
    567             gdal_version = "2.0.0"  # just assume it is not the latest
--> 568         if Version(gdal_version) >= Version("3.0.0") and crs:
    569             crs_wkt = crs.to_wkt()
    570         elif crs:

~\AppData\Local\ESRI\conda\envs\arcgispro-clone\lib\site-packages\packaging\version.py in __init__(self, version)
    195         match = self._regex.search(version)
    196         if not match:
--> 197             raise InvalidVersion(f"Invalid version: '{version}'")
    198 
    199         # Store the parsed out pieces of the version

InvalidVersion: Invalid version: '3.4.0e'

I checked if the crs is present and correct, error persists.

I tried different paths and shorter shapefile names, error persists.

The environment I am working is a clone of ArcGIS-pro. Most of the libraries are built when cloning i.e.:

gdal 3.4.0 arcgispro_py39_17150 [arcgispro] esri

[conda list:] (https://i.sstatic.net/Tn4dk.png)


Solution

  • I had the same Error. My solution was to uninstall geopandas version 0.12.2 in the ArcGIS Pro Clone and install geopandas version 0.9.0

    I suppose it has something to do with Python 3.9