4

In pyQGIS i tried this code (contains also debug prints):

def joinLayers(inputlayer, layer, index_field, name = "JOINED", dataProvider = "ESRI Shapefile"):
 a = []
 b = []
 outpath = getPath(inputlayer.source(), name)
 inputfields = inputlayer.pendingFields()
 nameoflayer = layer.name()
 toappend = layer.pendingFields()
 for fild in toappend:
    a.append(fild.name() + "j")
    fname = fild.name()
    newfield = QgsField(unicode(fname+ "j"), fild.type(), fild.typeName(), fild.length(), fild.precision(), fild.comment())
    b.append(newfield.name())
    inputfields.append(newfield, QgsFields.OriginJoin)
 print a
 print b
 output = QgsVectorFileWriter(outpath, dataProvider, inputfields, inputlayer.crs(), layer.wkbType(), inputlayer.dataProvider().encoding())
 del output

Main problem is renaming fields in for loop over QgsFields.

a

  • [u'UWIj', u'Wellj', u'UBHIj', u'Boreholej', u'Statej', u'Jurisdictij', u'Offshorej', u'Basinj', u'Sub-basinj', u'Latitudej', u'Longitudej', u'Legislatioj', u'Titlej', u'Operatorj', u'Classj', u'Structurej', u'Seismic loj', u'Drilled byj', u'Rigsj', u'Spud/Kickj', u'TD Datej', u'Rig releasj', u'Start deptj', u"Driller'sj", u'Oil showj', u'Gas showj', u'Condensatej', u'Oil fieldj', u'Discoveryj', u'Statusj', u'Well headj', u'Basic relej', u'WCRj']

b

  • [u'UWIj', u'Wellj', u'UBHIj', u'Boreholej', u'Statej', u'Jurisdictij', u'Offshorej', u'Basinj', u'Sub-basinj', u'Latitudej', u'Longitudej', u'Legislatioj', u'Titlej', u'Operatorj', u'Classj', u'Structurej', u'Seismic loj', u'Drilled byj', u'Rigsj', u'Spud/Kickj', u'TD Datej', u'Rig releasj', u'Start deptj', u"Driller'sj", u'Oil showj', u'Gas showj', u'Condensatej', u'Oil fieldj', u'Discoveryj', u'Statusj', u'Well headj', u'Basic relej', u'WCRj']

After write this fields into QgsVectorFileWriter, some fields doesn´t have "j" suffix.

For example attribute Condensate, see attribute table in qgis: Attribute table with some not renamed fields

0

1 Answer 1

3

The attribute field names only allow a maximum of 10 characters due to a limitation in the .dbf storage file (thanks to @MatthiasKuhn for highlighting this).

Your code adds the letter "j" to the end of each name, some of which contain exactly 10 or more characters (such as "Condensate" as you mentioned). Hence why you are not seeing it in the attribute table. You would need to change the for loop to add the "j" letter after the 9th character.

Try adding [:9] to fname when creating a new field:

newfield = QgsField(unicode(fname[:9] + "j"), fild.type(), fild.typeName(), fild.length(), fild.precision(), fild.comment())

The letter "j" will still be added to the end of field names which have less characters (e.g. "Well" will still become "Wellj").

3
  • @MarcelGJS - Most welcome buddy! Hope you get the results you're looking for :)
    – Joseph
    Commented Dec 18, 2015 at 11:52
  • 1
    This limitation is caused by shapefiles (or rather the .dbf format it uses for attribute storage) and not a QGIS per se. Commented Dec 19, 2015 at 22:10
  • @MatthiasKuhn - Thanks buddy, edited post to reflect your comment :)
    – Joseph
    Commented Dec 21, 2015 at 10:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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