0

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?

enter image description here

3
  • 1
    If 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-geek May 29 '20 at 21:42
  • But there is not possible to click on "Data defined override" is like not active – Olgierd Halski Jun 1 '20 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-geek Jun 1 '20 at 14:32
1

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.

1
2

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')

enter image description here

6
  • 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... – Olgierd Halski Jun 1 '20 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 – Olgierd Halski Jun 1 '20 at 18:11
  • can you share a few records for your data – kartoza-geek Jun 1 '20 at 18:55
  • 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!! – Olgierd Halski Jun 2 '20 at 11:38
  • i have updated the answer to add a filter – kartoza-geek Jun 3 '20 at 10:28
0

This can be done by data-defining the image paths, and is documented in the Creating a Report section in the QGIS User Manual:

https://docs.qgis.org/3.10/en/docs/user_manual/print_composer/create_reports.html#including-pictures-in-a-report

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.