I am trying to link a PDF for a shapefile, but I'm unable to set a relative path. How can I set a relative path in QGIS? I am only able to open the PDF in my PC, whereas when share this file to others, they are able to open the file only when they change the path in the attributes. Note: The attcachment and shapefile are in two different subfolders. The path for shapefile is "F:\GIS\SHP\POZZETTI_.shx" whereas path for PDF is "F:\GIS\PDF\PDF_POZZETTI\POZZETTO_1.pdf"
1 Answer
Solution
Using QGIS expression with function file_path()
and variable @layer
, you get the path of the current layer: file_path (@layer)
. Using this, the file you want to access must be stored in the same folder as the layer.
Concatenate this with the filename to get what you want, using function concatenate()
or even easier using pipes ||
as operator to concatenate: file_path (@layer) || '/filename.pdf'
Be sure to avoid paths containing special characters or spaces and be sure that the the filename stored in the Attribute called Fieldname
comes with the extension .pdf
at the end. Otherwise, add '.pdf'
as a string at the end of the expression:
file_path (@layer) || '/' || "Fieldname" || '.pdf'
Example
So if you have an attribute field called file
where you store the filename (e.g. my_file.pdf
), use this expression to create the raltive path to this file:
file_path (@layer) || '/' || "file"
Depending on Operation System, you might have to replace backslashes \
with forward slashes /
. To do so, use this expression (be aware that in QGIS, backslahes are double masked \\
):
replace (
file_path (@layer) || '/' || "file",
'\\',
'/'
)
Screenshot: the expression creates the path to the folder where the layer is saved and looks for a file named like the value stored in the attribute field "file"
:
-
Hello , Thank you your response, but it's not working for me , so i will give my project details. So that could you please write the exact condition which will be helpful for me. My Qgis project folder is D:\learn\qgis and the shapfile layer name is trail . Whereas my pdf is the same folder of the project as 1.pdf . So could you please write me the condition in my case that could set me a relative path. Thank you Commented Apr 1, 2023 at 18:58
-
It depends where exactly you want to have the path and for what? You want to create an attribute value as a text string?– BabelCommented Apr 1, 2023 at 19:03
-
Yes i created an attribute value as text and assigned action to it. So when send this project to my friend in his computer the pdf is not opening. Because of the path difference. Commented Apr 1, 2023 at 20:58
-
-
F:\Akwa Dropbox\Lavori\02-F\2022-5 Consulenza Frana Petralia Sottana\2023_FOGNATURA_PETRALIA\GIS\PDF\PDF_POZZETTI\POZZETTO_1.pdf only wirth this path i am able to open PDF on my computer not others. I tried the above expression given by babel , but i dont know what the problem is it is not working. Commented Apr 3, 2023 at 8:00
.shp
,.shx
, and.dbf
(plus other optional files with the same prefix). The correct way to reference it is by the.shp
.