10

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.

  • 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. – landocalrissian Dec 12 '14 at 12:44
  • Linking related feature-request in QGIS.org: issues.qgis.org/issues/15791, so we can keep an eye on it. – Andre Silva May 31 '18 at 14:04
9

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 download it from 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 :).

4

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."))
}
1

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.

1

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.

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.