5

I reorganized my shapefiles folders. Now the layer file paths are broken. I have tried the solutions shown in the QGIS User guide (chapter 6.2), but:

  • I don't want to process my layers individually since there are far too many of them.
  • The "Auto-Find" in the "Handle Unavailable Layers" dialogue, also the scan in the "Repair Data Source" tool ("QGIS scans through all other broken paths and tries to auto-fix those that have the same broken file path") don't work properly. It makes some mistakes, and then it's a total mess to fix it.

I have a list of the before/after paths, which I could use in a "replace layers file path" tool, which would be the simplest way. But it seems not to exist in QGIS, does it? Or is there another tool for that?

I have also seen that post, but my python skills don't allow me to generalise it to solve my problem. Do you know a code that allows me to batch-replace the path on all concerned layers? Like X to X', Y to Y', Z to Z' etc.

2
  • I suggest you load your layers anew if you don't want to fix them manually.
    – Erik
    Commented Mar 14, 2023 at 14:45
  • It's a very big map, that would take me hours ; and i have serveral maps to repair. Therefore the Babel's solution is better for my case. Thanks anyway
    – greg
    Commented Mar 15, 2023 at 15:06

2 Answers 2

6

Save your project as QGIS files (.qgs), not the default (zipped) .qgz file - or unzip the .qgz project file. Then you're able to open the project in a text editor in a human-readable format. There, you can find and replace the paths to the broken layers.

You have to change the path/layer name in two places. Look for:

  1. Layer tree, looking something like this: <layer-tree-layer [...] source="./layer_1.gpkg|layername=points_start">

  2. Datasource, looking something like this: <datasource>./layer_1.gpkg|layername=line</datasource>

In both cases, the name of the file is layer_1.gpkg, and the path is relative - the file is in the same folder as the project file: ./. Change both instances accordingly.

1
  • 3
    It works well, thanks ! I just had to understand the way the relative paths are anotated, which is quite simple: each descent of a level in the tree structure is is indicated by "../". With the use of find+replace, I did'nt have to pay attentoin the two distinct places mentioned above.
    – greg
    Commented Mar 15, 2023 at 14:59
0

Install the changeDataSource plugin from the repository. Enter values in the Find and Replace fields then select the Replace button to bulk update layer file paths. Any affected strings turn yellow. Then select Apply and they'll turn green if the new connection is valid or red if it isn't. Alternatively, you can just type over/into the connection strings by double clicking them and then selecting Apply:enter image description here

1
  • 1
    We're looking for detailed Answers that not only suggest a solution, but explain why it should be used, and shows how it helps.
    – Vince
    Commented May 16, 2023 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.