I'm trying to create new vector layer based on existing. Suddenly long attribute names are truncated. Here is my code:

def CreateNewEmptyLayer(sourceLayer, newLayerFileName):
    provider = sourceLayer.dataProvider()
    writer = QgsVectorFileWriter(newLayerFileName,provider.encoding(), provider.fields(), sourceLayer.wkbType(), provider.crs(),"ESRI Shapefile")
    if writer.hasError() != QgsVectorFileWriter.NoError:
        print("Error when creating shapefile: ", writer.errorMessage())
        return False;

    del writer;
    return True;

Source layer is MapInfo TAB file.

Added code to print info for source layers:

for field in provider.fields():
        print field.name(), field.typeName()

and result is ->source layer atrtribute names are NOT truncated to 11 symbols. So interesting is why QgsVectorFileWriter is truncating them?

  • 2
    Please edit the question to specify the source datatype. If it isn't shapefile, then attribute name truncation for names beyond the dBase limit of 11 characters (bytes) would be expected. – Vince Nov 28 '17 at 13:08

It is a limitation with dbase the .dbf file type of a shapefile. It does not support field names greater than 10 characters.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.