14

I have a huge list of layers in a project that are named in a foreign language. I want to translate and add the English title to the name.

When I right click on a layer and choose Properties -> General and change the layer name, it is visualized in the layer tree but the original file stays the same.

Is there a way to change the name of the original file from QGIS?

There are so many that searching them all in my folders would take too long.

2
  • 1
    I think you would have to right click and choose save as to change the name of the file if you don't want to use windows explorer. Dec 12, 2014 at 12:44
  • Linking related feature-request in QGIS.org: issues.qgis.org/issues/15791, so we can keep an eye on it. May 31, 2018 at 14:04

6 Answers 6

15

QGIS 3.22

In QGIS 3.22 you have an option to rename layers using the context menu in QGIS Browser:

enter image description here

QGIS 2.x

For doing so, you would need to i) remove the layer from the ToC, ii) rename the files that conform the Shapefile (i.e., shp, dbf, shx, prj, and the like), and iii) load the renamed layer to QGIS. But we do need to automatize such workflow!

If you look at the steps, they're similar to what the Table Manager plugin does. So, I adapted such plugin's code to rename Shapefiles, you can find it here.

You can use it in this way (first try with a small backup project to see how it works):

  1. Rename your QGIS layers in the ToC, these new names will be taken to overwrite your corresponding Shapefiles names.

  2. Save your QGIS project in the same folder as the script rename_shapefiles.py

  3. Open the QGIS Python console.

  4. Copy the next code snippet there:

     from rename_shapes import RenameShapefiles
     for lyr in iface.mapCanvas().layers():
         rn = RenameShapefiles( iface, lyr )
         rn.doSave()
    

That's it! I've tested it on GNU/Linux, QGIS 2.6. The order of layers in the ToC varies after running the code.

Let me know if you have any issue.

If you need the layer order to be preserved, ask a new question, perhaps I can look at it :).

7

For R users, I wrote a quick R function that batch renames the .shp file and all files associated. It's pretty bare bones, and there is no error checking, etc., so if it fails you'll need to do a little investigation.

# Function to rename shapefile (.shp) and all associated files
# fpath = full file path of any of the associated files (character)
# newName = new name for the files (character)

renameShp <- function(fpath, newName) {
  dir <- dirname(fpath)
  base <- basename(fpath)
  fname <- strsplit(base, '[.]')[[1]][1]
  ls <- list.files(path=dir, pattern=fname, full.names=TRUE)
  sapply(ls, FUN=function(p){
    file.rename(from=p, to=sub(pattern=fname,replacement=newName, p))
  })
  print(paste("Renamed", length(ls), "files."))
}
2

If you have a consistent schema for renaming them, you could do this in a python script. One method in python is os.rename(src, dest). Specific details would depend on what the algorithm for renaming the files is. This is for renaming the actual files, not just the layer names in the TOC.

Otherwise, I don't know of an alternative to renaming them manually, since you have to translate them. You could, in theory, build a dictionary of translations in python, but that would probably take as long to code as doing it manually. Also, be aware that renaming files themselves will break data sources. Renaming just the layer names in the TOC should be ok, but I'm not as familiar with QGIS as Arc.

2

I suggest to use this program to rename your files http://www.bulkrenameutility.co.uk/Main_Intro.php. It's open and easy to use. After you have renamed your file, you must open, with a text editor, your qgis project file (try notepad++) and make the same find&replace.

1

I think this issue could be fixed relatively easily by using/creating a tool that has a batch processing option and that does essentially nothing more than save the output. In batch processing, once one has chosen the folder where to save the files (and given them a general prefix), then one can use the Autofill option and choose 'fill with parameter values'. This will use the layer name.

0

If you've already renamed the layers in QGIS you can rename the files using pyqgis with os module based on the layername:

import os

shapefile_folder = r'/home/bera/GIS/Data/LMV-data_2021-10/sve_1milj_Sweref_99_TM_shape/svk/riks (copy 1)/' #The folder where you have the shapefiles

#Create a dictionary of filename without the extension as key and layername as value
to_rename = {os.path.basename(layer.source()).split('.')[0]:layer.name() for layer in QgsProject.instance().mapLayers().values()}
to_rename = {k:v for k,v in to_rename.items() if k!=v} #Remove those where old and new name are the same
#{'al_riks': 'al_riks_abcdef', 'an_riks': 'an_riks_newname', 'bs_riks': 'bs_riks_newname'}
#{old_name:newname, ...}

for root, folder, files in os.walk(shapefile_folder):
    for file in files: #For all files in shapefile folder
        if file.split('.')[0] in to_rename: #Check if it's in to_rename dictionary
            oldfile = os.path.join(root, file) #If it is create a full path and filename
            newfile = os.path.join(root, to_rename[file.split('.')[0]]+'.'+file.split('.')[1]) #Replace filename with the layername + file extension
            #print(oldfile) #'/home/bera/GIS/Data/LMV-data_2021-10/sve_1milj_Sweref_99_TM_shape/svk/riks (copy 1)/al_riks.prj'
            #print(newfile) #'/home/bera/GIS/Data/LMV-data_2021-10/sve_1milj_Sweref_99_TM_shape/svk/riks (copy 1)/al_riks_abcdef.prj'
            os.rename(oldfile, newfile) #Rename the file

enter image description here

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.