3

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.

4

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
1

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.

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()))

writer = QgsVectorFileWriter("new_file2.shp", provider.encoding(), new_fields, provider.geometryType(), provider.crs(), "ESRI Shapefile")

for f in provider.getFeatures():
    # add a feature
    fet = QgsFeature()
    fet.setGeometry(f.geometry())
    fet.setAttributes(f.attributes())
    writer.addFeature(fet)

# delete the writer to flush features to disk
del writer

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.