In addition to the traditional raster and vector files QGIS 3 also allows the implementation of Spatial Databases such as PostGIS, as well as light alternatives such as Spatialite and the Geopackage format.
Spatial databases optimize management by allowing various spatial and non-spatial data types to be stored and manipulated in a single file. The following examples show how to integrate a set of Shapefile layers, with their styles, into a Saptialite and Geopackage file.
The database administrator is a module that allows to manage the different database formats supported by QGIS 3, among its functions are to connect a database, view its content, edit it, SQL queries, import and export files.
Create a Spatialite Database:
1.- First the Shapefile of the project are loaded, in this case the free access layers of a hydrogeological and gravimetric study of Utah, FORGE, were used, obtained from the web https://gis.utah.gov/
2.- Creating a Spatialite database: There are several ways to create a Spatialite layer:
- Layer menu > New Spatialite layer.
- Navigation Pane > Spatialite > right click Create Database.
- Using the sheet-shaped button on the “Manage Layers” toolbar.
The first option displays a dialog box similar to the one used to create Shapefile, from top to bottom it allows to give a name and location to the Database file (in the example Utah_well.sqlite), followed by options to define the first layer that will contain the Database: name, geometry type, cartographic reference system, fields.
In the Navigation Panel and the Database Manager you can see the new layer created:
3.- To import vector layers, activate the Database Administrator, then click on the down arrow button (“Input“). In the dialog box that appears, in the upper tab select the layer to import. Then click on “Update Options“, the name of the layer appears in the tab “Table” we can edit this name, be sure not to leave spaces between characters. All other options are left by default.
The option “Create Spatial Index” must be activated. Then click Ok, a message will appear announcing that the import was successful.
6.- To add the imported Layers to the Map, select the Layer in the Database Manager right click “Add to Canvas“, repeat with all the layers, you will have a duplicate of each layer, the Shapefiles and those contained in the Spatialite database, the latter will not have a defined style.
7.- Setting Styles: you can edit or copy the styles defined for the Shapefile layers, select a layer right click Style > Copy Style, repeat the process on the Database layer and choose the Paste Style option.
8.- Saving Styles in the Database: right click on the layer > Properties, in the symbology tab, in the lower part select Styles > Save > Save to Database (Spatialite), a dialog box appears in which you must assign a name to the style and optionally a description.
In the Database Administrator you can verify that a layer of styles (layer_styles) has been added, when consulting it in Table mode we can see the saved Styles:
Creating a Geopackage Layer:
This format is an option to the Spatialite format, to add layers a slightly different procedure must be performed.
1.- Go to menu Layer > Create Layer > New Geopackage Layer, in the dialog box you configure the location and name of the database, the properties of the first layer it will contain.
2.- For the Database Administrator to recognize the new layer, select the option Geopackage > right button “New Connection…”. If you want to create a Geopackage file, locate and select the newly created Geopackage file.
3.- The integration of layers to the database is done by selecting the layers Shapefile > right button “Save as…”, in the upper tab we make sure that the format is Geopackage, in File Name locate and select the layer Geopackage, in Layer Name write a name for the layer to be added, then click Ok. It is repeated for the other layers.
4.- In the Layers Panel and in the Database Administrator it is verified that the vectorial layers have been added to the Geopackage.
5.- To create and save the styles, repeat the procedure performed with SpatiaLite.
Add Raster files to our databases:
Although the Spatialite and Geopackage formats support raster files (images, Grids, digital models, among others) that can be displayed in QGIS 3, currently the Database Manager can only handle vector formats, in future versions it is expected to improve the support to other types of data.