Opened 14 years ago

Closed 14 years ago

Last modified 14 years ago

#2151 closed bug (fixed)

"Save as shapefile..." truncates field names, sometimes causing duplicates

Reported by: dmorissette Owned by: nobody
Priority: major: does not work as expected Milestone: Version 1.4.0
Component: Vectors Version: Trunk
Keywords: Cc:
Must Fix for Release: No Platform: Debian
Platform Version: Awaiting user input: no

Description

Since the DBF format limits field/attribute names to 10 chars, the "Save as shapefile" command (from the right-click menu on vector layers in legend) has to truncate the name of fields longer than 10 chars. However, this is done without checking for potential duplicates field names.

For instance, if a dataset contains fields "mylongattr1" and "mylongattr2", they are both written as "mylongattr". We should change the dups to have a unique name, presumably by adding a unique number at the end.

I believe QGIS uses OGR for this export (am I right?). If that's the case then the source of the problem is in the OGR shapefile driver where the field name truncation happens. I have filed an OGR ticket about this with a testcase to reproduce, and once it's fixed QGIS should automagically benefit from the fix:

http://trac.osgeo.org/gdal/ticket/3247

However, QGIS could possibly have some smarts to better handle this in the meantime. I'll let you decide if you wait for OGR or implement a fix in QGIS.

Change History (2)

comment:1 by jef, 14 years ago

Resolution: fixed
Status: newclosed

fixed in r12280.

comment:2 by dmorissette, 14 years ago

I see that you produce a fatal error if duplicates are found, which is way better than silently producing an invalid output. However, the day OGR is fixed to automatically rename the fields, QGIS won't be able to benefit from that fix...

If I'm not mistaken (and I'd love to be proven wrong), the current fix (ErrAttributeCreationFailed) forces users to rename the fields in the source before the conversion if they want to export to shapefile, which is not always possible if they connect to a DB or other data source with a schema that they do not control. I believe in the long run it would be better to either support renaming the fields automatically in QGIS (e.g. add a unique number at the end of dups to make them unique), or to be able to let OGR do it for us once http://trac.osgeo.org/gdal/ticket/3247 is fixed.

Note: See TracTickets for help on using tickets.