Viewing GeoPackage images in QGIS

Our GeoPackage exports use the Related Tables extension of the GeoPackage spec to store photo attachments. Learn how to view them in QGIS.

The GeoPackage Related Tables extension establishes a relationship between three different tables:

  • A Feature table, with features that you want to store extra data on

  • A Related table, which includes photos or other kinds of data related to features in the table above

  • A Mapping table, which points to individual items in the Feature table and the Related table to tell the software that the two items are connected.

For example, say we have a GeoPackage with one point feature class (Observations) that stores photos of noteworthy field conditions. When exporting from Touch GIS the GeoPackage will have three tables for this feature class and its images:

  • Observation (the Feature table)

  • Observation_images (the Related table with all of your photo data)

  • Obervation_Observation_images_map (the Mapping table)

Here's a test GeoPackage file if you would like to follow the examples below exactly:

If you have QGIS version 3.44 or higher, QGIS will automatically detect the relationships between the Feature table, the Related table, and the Mapping table. You can confirm whether this is the case after loading your GeoPackage export into a QGIS project by opening the Project menu, selecting Properties, and then opening the Relations section. You should see something like the following, keeping our example GeoPackage from above.

It shows that the Mapping table has a relationship to both the Observation and Observation_images layers. If you're running an older version of QGIS and these relationships weren't automatically loaded, you can create them yourself with the "Add Relation" button. One relation runs from the Mapping table's base_id field to the Feature table's primary key (Touch GIS uses "gpkg_id" for this field, but other apps may use something else), and the other relation runs from the Mapping table's related_id field to the Related table's primary key ("id", most likely).

Once you have the project's relationships established, you'll be able to see related records when you open the Attributes table for a feature layer.

In the above screenshot, the bottom panel labeled "Observation_images_media_forward" was automatically added, because the Observation feature has relations, in fact it has 3 related images.

We need to make two more adjustments to see the images rendered directly in the Attributes table for each Feature.

For the first, we need to tell QGIS that the Related table has image data, and how to display it. Right click on the Related images table (Observation_images, in this example) and open the Properties panel, then go to the attributes form.

At the top, change the "Autogenerate" form to "Drag and Drop Designer". Then add an HTML Widget by dragging it from the "Available Widgets" column to the "Form Layout" column.

GeoPackages store images in their binary object form, so the following code tells QGIS how to process that data into an image. Paste this into the "Configure HTML Widget" window that opens:

This will render the photo in its full size, but if you would like to adjust it, change the width and height properties above from "auto" to some number. To retain aspect ratios but make the default images smaller or larger, keep one of them as "auto" and adjust the other by inserting a number like 300. Note that "data" is the name of the column in the Related Images table which contains the raw photo data.

Aside: You can also accomplish this with the QML widget, if you're more comfortable with that syntax. In that case, here is the corresponding code:

With either of those done successfully, you should see the image preview in the widget configuration screen like so:

And if you open the Attributes table for the Related Images table, you'll see the images inline like so:

The second adjustment we need to make to get these images to show in the Feature Attributes table is to tell QGIS to display the Related table record instead of the Mapping table record in the Attributes form.

Right click on the Feature layer (Observation in this example), choose Properties, and again navigate to the Attributes Form tab. Change the type from "Autogenerate" to "Drag and Drop Designer" and drag the Relations widget from the Available Widgets column to the Form Layout column. Then in the Widget Display section, change the Cardinality dropdown from "Many to one relation" to the other option which will be the Related images table, in this case "Observation_images (id)". You can also give the field a label like "Related Images" to make it clear. Here's what it should look like in the end:

Click "OK", and with that final adjustment we should now see related images directly in the Feature Attribute form:

Reach out to if you're having trouble getting this to work: [email protected]

Last updated