
QGIS 2 Cookbook
By :

SpatiaLite is a single file relational database that is built on top of the well-known SQLite database. It can store many layers of various types, including nonspatial tables. Interfaces to the format also allow the ability to run spatial queries of various kinds. It's a highly-flexible and portable format that is great for everyday use, especially when working on standalone projects or with only one user at a time. SpatiaLite works in a similar manner to PostGIS without the need to configure or run a database server.
Pick a vector layer and load it up in QGIS. This step is optional, as you can pick the source layer from the filesystem in a later dialog.
cookbook.db
. The easiest way to do this is with the Browser tab, as shown in the following screenshot:If you have a lot of files listed, this will be quite difficult as the browser doesn't scroll during the drag operation. You can optionally open a second browser window and drag the layer across. Also, note that this defaults to multi-type geometry. If you need to control the options, use the next method.
SQL databases are usually case insensitive, so you can use all lower case characters. Also, never use spaces or special characters in table names; this can just lead to headaches later. An occasional underscore is okay.
This method is more similar to traditional database import and very similar to the PostGIS recipe next in this chapter.
QGIS converts your geometry to a format that is compatible with SpatiaLite and inserts it, along with the attribute table. Afterwards, it updates the metadata tables in SpatiaLite to register the geometry column and build the spatial index on it. These two postprocesses make the database table appear as a spatial layer to QGIS and speed up the loading of data from the table when panning and zooming.
The import dialog contained a few other features that are often useful. You can reproject data as part of the import process if you want, or you can specify the projection if QGIS didn't detect it properly. You can also name the geometry column something different than the default, geom
; for example, utmz10n83
(this is normally not recommended). You can specify the character encoding of the text in the event that it's not handled correctly.
You can even use the dialog to append data to an existing table; for example, you have multiple counties with the same data structure that come as two separate files, but you want them all in one layer.
If, for some reason, the layer didn't import the way that you want, delete it and redo the import. If you delete layers, make sure to learn how to vacuum the database to recover the now empty space in the file and shrink its total size (this is not automatic).
Look for the Vacuum option as a button in many graphical tools. If you don't see it, no worries, just run the SQL, VACUUM;
.
What happens if this fails? Databases can be really picky sometimes. Here are some common issues and solutions:
-nlt PROMOTE_TO_MULTI
, which converts all single items to multi-items to fix this.If you need more advanced settings or can't get the QGIS tool to work, you may need to use the QspatiaLite Plugin (install this with Manage Python Plugins under the Plugins menu), the spatialite-gui (download this from https://www.gaia-gis.it/fossil/spatialite_gui/index) application, or the ogr2ogr command line (this comes with QGIS, which is part of OSGeo4w shell on Windows, or the terminal on Mac or Linux).
Change the font size
Change margin width
Change background colour