geopandasepsggeopackage

Losing the crs when writing to .gpkg with geopandas


When I write my .gpkg I am losing the CRS. I have tried setting the CRS with .set_crs, or adding the CRS when writing the .gpkg (which creates a fault - "fiona._env - WARNING - dataset filename.gpkg does not support layer creation option EPSG"

My code

for layername in fiona.listlayers(file):                 
    vector = geopandas.read_file(file, layer=layername)
    vector.set_crs(4326)
    vector.to_file(filename + ".gpkg", layer = layername, driver='GPKG')

or

for layername in fiona.listlayers(file):                 
    vector = geopandas.read_file(file, layer=layername)
    vector.to_file(filename + ".gpkg", layer = layername, driver='GPKG', epsg=4326)

neither works.


Solution

  • vector.set_crs(4326) does not work in place by default. You either need to assign it or specify inplace=True.

    for layername in fiona.listlayers(file):                 
        vector = geopandas.read_file(file, layer=layername)
        # vector.set_crs(4326, inplace=True)  # one option
        vector = vector.set_crs(4326)  # other option
        vector.to_file(filename + ".gpkg", layer = layername, driver='GPKG')
    

    Your second attempt does not work because to_file does not have espg keyword you are trying to lose and that gets lost among arguments passed to Fiona and GDAL (which silently ignores it).