I need to change field names of a shapefile to upper case using ogr. I want to do this automatically, versus calling out each field and replacing the name with upper case. When I try to implement using layer AlterFieldlDefn, I get a TypeError. Here is the code I am using:
Shapefile = r"C:\test.shp" driver = ogr.GetDriverByName('ESRI Shapefile') dataSource = driver.Open(Shapefile, 1) if dataSource is None: print 'Could not open %s' % (Shapefile) else: print 'Opened %s' % (Shapefile) layer = dataSource.GetLayer() layer_defn = layer.GetLayerDefn() for i in range(layer_defn.GetFieldCount()): print layer_defn.GetFieldDefn(i).GetName() uppercase_name = layer_defn.GetFieldDefn(i).GetName().upper() layer.AlterFieldDefn(i, uppercase_name, 1)
The error I get is this:
TypeError: in method 'Layer_AlterFieldDefn', argument 3 of type 'OGRFieldDefnShadow
How do I correctly uses AlterFieldDefn to change the field names to upper case? I do not have ArcGIS 10.2, so AlterField_management is not an option.