I'm a new user to GIS and am learning as I go. I'm using QGIS 3.2 on a MacBook Pro. I have a delimited text layer with a number of points for which I've set up Map Tips. I want information to come up for each point including a hyperlinked image. The images are pictures of samples taken. The image file could be named in either of two formats, depending on whether or not the sample was sent off for assay. If it was, the point will have a "Tag_Num" and the image will be named with that Tag Number. If it was not, the image will be named for the "Sample_Name", and "Tag_Num" will be NULL. "Tag_Num" and "Sample_Name" are fields in the attribute table, to be clear. The path is the same for all images.

I've tried a couple different ways of doing this, neither works. Here's what I've tried...

<div align="center" style="width:505"><b>[% "Sample Name" %]</b></div>
<br />
<div style="width:450; line-height: 1.2">
<b>Date: </b>[% "Date" %]<br />
<b>Tag Number: </b>[% "Tag_Num" %]<br />
<b>Disposition: </b>[% "Disposition" %]<br />
<b>Location: </b>[% "Latitude" %], [% "Longitude" %]<br />
<b>Sample Type: </b>[% "Sample Type" %]<br />
<br />
<b>Location Description: </b>[% "Loc_Desc" %]<br />
<br />
<b>Sample Description: </b>[% "Sample_Desc" %]<br />
</div><br />
<b>Sample Photo: </b><br />
[% CASE WHEN "Tag_Num" IS NOT NULL THEN '<a href='File:///Users/wsmb/Documents/WCM/Prospecting/Sampling/Photos/Samples/[% "Tag_Num" %].jpg'><img src='File:///Users/wsmb/Documents/WCM/Prospecting/Sampling/Photos/Samples/[% "Tag_Num" %].jpg' width='480' /></a>' ELSE '<a href='File:///Users/wsmb/Documents/WCM/Prospecting/Sampling/Photos/Samples/[% "Sample Name" %].jpg'><img src='File:///Users/wsmb/Documents/WCM/Prospecting/Sampling/Photos/Samples/[% "Sample Name" %].jpg' width='480' /></a>' END CASE %]

Using that route, I get this output in the map tips window:


Date: 08/28/2018

Tag Number: M123456

Disposition: blah

Location: xx.xxxxx, -xxx.xxxxx

Sample Type: bleh

Location Description: blah blah

Sample Description: blah blah blah

Sample Photo: [% CASE WHEN "Tag_Num" IS NOT NULL THEN '' ELSE '' END CASE %]

For whatever reason, the "[% CASE WHEN..." all comes across as text. It does load and properly hyperlink the image(s) if they exist, but it tries to load both. It will just be the empty image frame if the image file does not exist.

I also tried it this way...

<a href="File:///Users/wsmb/Documents/WCM/Prospecting/Sampling/Photos/Samples/[%case when "Tag_Num" is not null then '[% "Tag_Num" %]' else '[% "Sample Name" %]' end %].jpg"><img src="File:///Users/wsmb/Documents/WCM/Prospecting/Sampling/Photos/Samples/[%case when "Tag_Num" is not null then '[% "Tag_Num" %]' else '[% "Sample Name" %]' end %].jpg" width="480"></a>

This way, I get a single blank image frame as though the image is nonexistent, but I do not see any of the "[% case when..." arguments coming over as text...

As I said, I'm new to this, so I'm guessing it's a syntax error, I'm just typing something in a way I'm not supposed to. Or perhaps I cannot nest [% [% %] %] those, or am erring in the way I nest my quotation marks?


I think it's a problem with your syntax -

  1. Perhaps try not having expression delimiters ([% %]) nested within one another, and instead use one expression and concatenators (||) to join up the HTML text and fields.

  2. You might need to escape the single quotation marks in the HTML (e.g. <a href='blah'> ) by using two of them ( <a href=''blah''> )

Try the following - I haven't tested it out exactly:

    '<a href=''File:///Users/wsmb/Documents/WCM/Prospecting/Sampling/Photos/Samples/' ||
              "Tag_Num" || '.jpg''>
     <img src=''File:///Users/wsmb/Documents/WCM/Prospecting/Sampling/Photos/Samples/' || 
              "Tag_Num" || '.jpg'' width=''480'' />
    '<a href=''File:///Users/wsmb/Documents/WCM/Prospecting/Sampling/Photos/Samples/' ||
              "Sample Name" || '.jpg''>
     <img src=''File:///Users/wsmb/Documents/WCM/Prospecting/Sampling/Photos/Samples/' ||
              "Sample Name" || '.jpg'' width=''480'' />
     </a>' END %]

What you can do is pick out the bit between the [% %] and paste it into the Expression window (click the Insert expression... button in the Map Tip options) and see if it gives you any syntax errors in the preview line at the bottom of the window.

  • That worked, thank you :) Note- you left out one ' after the second <img src=
    – Josh
    Sep 13 '18 at 5:30
  • Glad to know it worked, fixed :)
    – she_weeds
    Sep 13 '18 at 5:59

You may use field calculator in QGIS to check the condition and prepare the image URL in a new field. The new field can virtual so no need to be saved, then use the virtual field value as you image URL.
In functions list choose the fields and values and select the fields to be compared against from your attribute table, then write your condition in the expression field.


CASE WHEN "Tag_Num" IS NOT NULL THEN '[% "Tag_Num" %]' ELSE '[% "Sample Name" %]' END

https://docs.qgis.org/2.8/en/docs/user_manual/working_with_vector/field_calculator.html https://docs.qgis.org/2.8/en/docs/user_manual/working_with_vector/expression.html#conditionals

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.