I want to be able to change all field names to lower case from a data source when loading them in QGIS (SHP in particular).

  • Is it possible? Via the GUI, via Python.

  • Is it possible to convert field names to lower case when saving a layer with the "Save as" dialog?

Why? I regularly go back and forth between GIS files from OpenData portals and databases like PostGIS or Spatialite. Doing queries with upper case fields require quoting in these database and I want ease the pain. I would like to convert all my SHP field names to lower case.

For info, I've seen it's possible when using Grass e.g the -w option in v.in.ogr. I've seen using bash with ogrinfo is also possible but remains less friendly IMO.


SQLite is actually case-insensitive with table and column names as far as they contain only ASCII-characters. It does honor case in names when they are created but you can't have fields "Test" and "tesT" in the same table and if you have a field "Test" it will be found with SELECT "aTest". But non-ascii characters are case-sensitive in the names of tables and columns which may may you surprised sometimes.

You can test it with these commands:

CREATE TABLE case_test (id);
ALTER TABLE "case_test" ADD COLUMN "a_test"; -- added
ALTER TABLE "case_test" ADD COLUMN "A_test"; -- error, duplicate name
ALTER TABLE "case_test" ADD COLUMN "ä_test"; -- added
ALTER TABLE "case_test" ADD COLUMN "Ä_test"; -- success!

When it comes to your question, as a workaround you could convert all data first into Spatialite with ogr2ogr. Fields are created by default as laundered to lower case http://www.gdal.org/drv_sqlite.html. For updating the names in-place the referenced bash script looks good to me but someone else would probably convert it into a python script.

The Spatialite workaround can be used directly from QGIS. Just open your shapefile and save it into a new Spatialite db through "Save as..".

  • Thanks for your answer but it's not the answer to my question. I do not want to use any database methods here although I may use a database later. I just want to sanitize fields names to lowercase in QGIS natively or via QGIS Python API. It's because I want to use this for beginners. I don't want to ease the pain with another painful solution. – ThomasG77 Nov 5 '15 at 12:24
  • I can delete my answer if you wish, I told it is about a workaround. It gives a partial answer to "Is it possible to lower fields names when saving a layer with the "Save as" dialog?" because if you use Save as.. and save into Spatialite format you will see an option "LAUNDER" which is set to "YES" by default. – user30184 Nov 5 '15 at 12:36
  • 1
    No, keep it. It's not the exact answer I expect but it may help others. Thanks for your feedback anyway. – ThomasG77 Nov 5 '15 at 12:50

At the end, I choose to use QGIS Python API. I'm not sure it's efficient compared with OGR command line use but it does the job.

    from qgis.core import Qgis
except ImportError:
    from qgis.core import QGis as Qgis

layer = qgis.utils.iface.activeLayer()
provider = layer.dataProvider()
fields = provider.fields()
new_fields = QgsFields()
for i in fields:
    new_fields.append(QgsField(i.name().lower(), i.type(), i.typeName(), i.length(), i.precision(), i.comment()))

if int(Qgis.QGIS_VERSION.split('.')[0]) == 2:
    geom_type = provider.geometryType()
    geom_type = provider.wkbType()
writer = QgsVectorFileWriter("new_file2.shp", provider.encoding(), new_fields, geom_type, provider.crs(), "ESRI Shapefile")

for f in provider.getFeatures():
    # add a feature
    fet = QgsFeature()

# delete the writer to flush features to disk
del writer

Edit note: now compatible for QGIS 2.x and 3.x

  • For anyone else coming to this and encountering an error with provider.geometryType(), try replacing it with QgsWkbTypes.MultiPolygon or whichever one suits your dataset. Rest are here - qgis.org/api/classQgsWkbTypes.html. Someone could suggest how to pull this from the dataset - i can't work it out. – Jamie Dec 15 '20 at 17:26
  • 1
    For QGIS 3, you should replace provider.geometryType() with provider.wkbType(). Deduced from "Backwards Incompatible Changes" API notes qgis.org/api/… – ThomasG77 Dec 16 '20 at 19:19
  • Made edit to sample code with the change to support both QGIS 2.x and 3.x. You may try it and tell me if it works for you. – ThomasG77 Dec 16 '20 at 19:29
  • Hero. Thanks so much – Jamie Dec 16 '20 at 19:38

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.