My layer "users_print_view" contains image path. I would like to display this as image in print composer. I can copy path and "Adds a new picture to the layout" and paste it but I would like it to be automatic. Can I there use Variable? Choose there like layer and column? Or maybe other way... Do you have some ideas?
-
1If you click on data defined you can select fields and check if the field containing your path is listed there. If you select it then all the paths should be data driven.– kartoza-geekCommented May 29, 2020 at 21:42
-
But there is not possible to click on "Data defined override" is like not active– Olgierd HalskiCommented Jun 1, 2020 at 11:29
-
Because the image is not associated with any layer that is why it is greyed out. Can you try to click on edit and then use an aggregate expression to retrieve the path from the layer you need. ie aggregate(layer:='images_3b4a4039_611f_4628_9cd4_fd7db5e2637e',aggregate:='concatenate', expression:="path", concatenator:=','). I haven't tested this yet– kartoza-geekCommented Jun 1, 2020 at 14:32
3 Answers
You are trying to add an image to the map but the map item (picture) has no relationship with the layer items. So what you need to do is the following.
- Add an image to the map.
- In data defined settings select
Edit
- Enter the following formulae
aggregate(layer:='New_scratch_layer_34fb05c7_8585_4ab4_ac73_d29eb398f3c2',aggregate:='concatenate', expression:="path", concatenator:=',')
Replace the layername with yourlayer name.
NB I tested this with a layer with a single record. You would need to modify the expression to fetch the correct row if you have multiple entries.
If you have multiple entries you can use a filter just like below
aggregate(layer:='images_72aff17a_57df_4cf2_82ac_21400778cbc0',aggregate:='concatenate', expression:="path", filter:="name"='test')
-
Thanks for the help;-). In output preview the path is displayed after your instruction. however you are right I have multiple entries and for now the image is not display yet in composer... Commented Jun 1, 2020 at 17:43
-
Maybe a good solution would be to use a filter. My .qgs project name contains the image name. e.g: abcd_1234A_efgh.qgs And the name of the image is 1234A Commented Jun 1, 2020 at 18:11
-
-
Now is working I used regexp_substr. One more question just for feature. "NB I tested this with a layer with a single record. You would need to modify the expression to fetch the correct row if you have multiple entries." How I can using your formulae that it will return only one record. As you know in my table i have multiple entires I would like tu use for example the first record only. I try use filter but no result or like "set one" Thx one more time!! Commented Jun 2, 2020 at 11:38
-
To build on @kartoza-geek's answer, you can use all kinds of variables and values to determine the image filepath. Use their answer if you are relying on another layer to get the filename.
If your image filename is based on the project filename, you can use the project filename as a variable and extract the image name from it using regexp.
So in your example if the project file is abcd_1234A_efgh.qgs
the following regexp expression will extract anything between the first and second instances of an underscore from the project name (@project_basename
).
regexp_substr(@project_basename,'_([^_]+)_')
Then just concatenate the relevant image filetype extension (e.g. .jpg
, .png
) at the end, and at the beginning, the path to your image (if it's relative to your project file location, you can use @project_folder
as a variable).
For example if the file is 1234A.jpg in the images
subfolder of where your qgs file is and you are using Windows...
@project_folder||'\\images\\'||regexp_substr(@project_basename,'_([^_]+)_')||'.jpg'
Which would return something like
C:\Path_To_Project\images\1234A.jpg
You can of course add other values from your atlas features, other layer features, etc. to this string if required.
This can be done by data-defining the image paths, and is documented in the Creating a Report section in the QGIS User Manual: