QGIS Geopackage – Can QGIS Geopackage Files with Multiple Layers Exist as a Single .gpkg File?

layersqgis

My question is about the basic structure of a .gpkg file. When I make a .gpkg file, they only ever contain one layer, and I cannot add additional layers to them. However, sometimes when I have added and then exported the vector layers as .qgz files and the completed map as .qgt files, all left in the same directory as the .qpkg file, they will show up in the layers or associations of the .gpkg file, IF I have saved the .gpkg file after performing all these operations.

As an example, following the above to create these files and saving them all in the same directory, would show two layers when I opened the .gpkg file:
X.gpkg – contains polygons [layer 1]
Y.gqz – contains points [layer 2]
Y.gqt – contains themes for map made [not seen as layer, but accessible via SHOW LAYOUT MANAGER]

Is there a way that I could save all this so that it was just in one .gpkg file? Or instead is the .gpkg file always found with its corresponding layer files as separate files?

I've watched numerous videos on YouTube, read the documentation but am still fundamentally confused about this.

Best Answer

Geopackage is in fact a container format (like zip files). It as based on SQLite, thus it has database functionality. See more about Geopackage here: https://www.geopackage.org/

You can save several layers (vector and raster) as well as styles, QGIS projects etc. to the same Geopackage file.

To make an example (download the file):

  1. I loaded the pre-installed vector base-map (type world in the coordinates field at the very bottom of your QGIS window), than applied a style to it. With geomtry by expression, I created the centroid of the countries (point layer) and the boundary (line layer), applying different styles for each.

  2. Than I saved the first layer: right click layer / Export / Save Features As... and created a new Geopackage.

  3. Now, I saved the two other layers to the same Geopackage: right click layer / Export / Save Features As... an select the same Geopackage, but be sure to define another Layername in the dialog window so that the already existing layer will not be overwritten.

  4. Now, save the project to the same Geopackage: Menu Project / Save as [second entry] / Geopackage (see screenshot). In the dialog that opens, for connection select the same Geopackage file, give a name to the project and click OK. enter image description here

  5. Click the following link to download the Geopackage created this way. To open it, in your QGIS main window go to the Browser panel and navigate to the folder where you stored the Geopackage. You can expand the Geopackage entry to see what it contains - compare the screenshot: you see the different layers as well as the QGIS project (red arrow). Double click the project to open the whole project or one of the layers to open just this one. When opened, it should look like this (if you just open the Geopackage, e.g. by drag and drop, only the layers contained will be loaded, but not the project - thus settings in the project like styles will not be loaded):enter image description here

In this way, you can add additional layers etc. to the Geopackage. Click the curved double arrow icon at the top of the Browser panel to refresh the display of the Geopackage's content.

In this way, you can share whole projects, including the data by sending just a single file. However, be aware that many settings (project!) are QGIS-specific and may also vary from version to version of QGIS. So for long-term archiving, I would not recommend to save everything in one large Geopackage - it's also difficult to keep an overview of what you have and as a complex binary format, you need special software (like QGIS) to access Geopackages.