# Viewing GeoPackage images 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:

{% file src="<https://1655252264-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LjOYqRQStlw99hJWjA4%2Fuploads%2Faae8vc6P94hat25ndhja%2Fobservations.gpkg?alt=media&token=cc13eb99-d995-41da-be50-9c829c46100a>" %}

### Viewing related images in QGIS

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.

<figure><img src="https://1655252264-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LjOYqRQStlw99hJWjA4%2Fuploads%2FNUiluA6CaFVD5NkTsRNW%2Fgpkg-qgis-project-relations.png?alt=media&#x26;token=03fc0828-01f6-47f1-a630-dac3e0619c68" alt=""><figcaption></figcaption></figure>

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.&#x20;

<figure><img src="https://1655252264-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LjOYqRQStlw99hJWjA4%2Fuploads%2FyszTeM1geis5rCmZHLvY%2Fgpkg-related-without-image.png?alt=media&#x26;token=54b49832-ceb2-4cfb-ac92-d7394b667122" alt=""><figcaption></figcaption></figure>

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.&#x20;

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:

```
<script>
    document.write(
        expression.evaluate(
            " '<img height=\"auto\" width=\"auto\" src=' || '\"data:image/png;base64,' || to_base64(\"data\") || '\">' "
        )
    )
</script>
```

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:

```
import QtQuick 2.0

Image {
    width: auto
    height: auto
    fillMode: Image.PreserveAspectFit
    // Use the QGIS expression engine to build the data URI
    source: expression.evaluate("'data:image/png;base64,' || to_base64(\"data\")")
}
```

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

<figure><img src="https://1655252264-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LjOYqRQStlw99hJWjA4%2Fuploads%2FXzA0cDkUJCgRtKpuB9NR%2Fwidget-config.png?alt=media&#x26;token=154da115-5e14-4722-acb7-4caff3e12c93" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://1655252264-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LjOYqRQStlw99hJWjA4%2Fuploads%2F86PVrRwI2CrYdguWT3wU%2Fimage-attribute-table-with-preview.png?alt=media&#x26;token=4b587fb8-48e5-4246-b664-c3f2116695ea" alt=""><figcaption></figcaption></figure>

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:

<figure><img src="https://1655252264-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LjOYqRQStlw99hJWjA4%2Fuploads%2F1BCIBQerpPl4DobQseUF%2Ffeature-attributes-form.png?alt=media&#x26;token=12edef59-1780-483d-91eb-fed979e700f4" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://1655252264-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LjOYqRQStlw99hJWjA4%2Fuploads%2FfB4mnfo6jHh26nyX7Td2%2Ffinal-feature-attributes-with-image.png?alt=media&#x26;token=86c4af89-e37b-431e-ab01-3bb765ebddba" alt=""><figcaption></figcaption></figure>

Reach out to if you're having trouble getting this to work: <support@touchgis.app>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.touchgis.app/import-export/viewing-geopackage-images-in-qgis.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
