Writing to GeoPackage fails when two attributes have the same name (ignoring case)


olmo.nietosilleras@...
 

Hi,

Fiona throws a "Failed to write record" RuntimeError when trying to write records to a GeoPackage if two or more attributes have equal names ignoring the case. I understand this is because GeoPackage is a SQLite container and that one should in principle not have two columns with the same name (ignoring case). But would it be possible to include a check and output a more specific error message in this particular case to facilitate debugging?

In my case I ran into this problem while performing a merge with geopandas on two keys that differed only by their case, so that the resulting GeoDataFrame contained two columns with the same name (ignoring the case). A simple solution is clearly to drop one of them or to modify the case before merging the two dataframes. But it would be quite useful if the error message directly pointed one to this problem.

Here is the error message I get with geopandas/fiona (the duplicate attributes are "CODE_OBJ" and "code_obj"):

Traceback (most recent call last):
  File "C:\Continuum\miniconda3\envs\geo\lib\site-packages\geopandas\io\file.py", line 130, in to_file
    colxn.writerecords(df.iterfeatures())
  File "C:\Continuum\miniconda3\envs\geo\lib\site-packages\fiona\collection.py", line 342, in writerecords
    self.session.writerecs(records, self)
  File "fiona/ogrext.pyx", line 1198, in fiona.ogrext.WritingSession.writerecs
RuntimeError: Failed to write record: {'id': '0', 'type': 'Feature', 'properties': {'CODE_OBJ': '000028064CC805B1', 'GWSCOD_H': 202, 'REF_ID': 912884578, 'code_obj': '000028064CC805B1'}, 'geometry': {'type': 'Polygon', 'coordinates': (((247632.21, 160821.25), (247683.44, 160754.28), (247728.75, 160791.5), (247726.5, 160796.37), (247813.0, 160869.0), (247843.21, 160827.61000000002), (247768.44, 160770.21), (247854.5, 160678.0), (247856.6, 160675.93), (247791.64, 160617.36000000002), (247782.0, 160609.0), (247780.41, 160607.24), (247772.44, 160601.25), (247615.69, 160806.28), (247632.21, 160821.25)),)}}

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "fiona/_err.pyx", line 201, in fiona._err.GDALErrCtxManager.__exit__
fiona._err.CPLE_AppDefinedError: b'failed to prepare SQL: SELECT "fid", ST_MinX("geom"), ST_MaxX("geom"), ST_MinY("geom"), ST_MaxY("geom") FROM "layer0" WHERE "geom" NOT NULL AND NOT ST_IsEmpty("geom")'
Exception ignored in: 'fiona._shim.gdal_flush_cache'
Traceback (most recent call last):
  File "fiona/_err.pyx", line 201, in fiona._err.GDALErrCtxManager.__exit__
fiona._err.CPLE_AppDefinedError: b'failed to prepare SQL: SELECT "fid", ST_MinX("geom"), ST_MaxX("geom"), ST_MinY("geom"), ST_MaxY("geom") FROM "layer0" WHERE "geom" NOT NULL AND NOT ST_IsEmpty("geom")'
(This problem does not arise when writing to a shapefile or to GeoJSON.)

As a point of comparison, QGIS for example outputs the following error when trying to add a second column with the same name (ignoring case)

OGR error creating field CODE_OBJ: sqlite3_exec(ALTER TABLE "layer0" ADD COLUMN "CODE_OBJ" TEXT(80)) failed: duplicate column name: CODE_OBJ
Thanks,
Olmo

Join main@fiona.groups.io to automatically receive all group messages.