QGIS – Vector Data Connection

Update – April 2017

Many things have changed with QGIS over the last few years since this post was written. I thought it worth the time to revisit the topic and expand its scope. Please take a look at my new post: QGIS – Loading Data

—————–

One of the major differences between many COTS applications and Open-source is in the area of adding layers and data to your project.  While these programs share many of the same technologies on the back end to actually connect with the various data sources, for example, the GDAL/OGR libary, the user-interface is very different.
ESRI products have the place where you connect to a data source, separate from the interface to add data to a project.  For those products, ArcCatalog is the browser where you load in various data layers, and the data connections are set up under a different menu.  Once you have established a particular data connection, then any layers available through that connection are integrated into the ArcCatalog window, or the “Add Data” window in ArcGIS.
The interface that qGIS uses to connect to data sources varies depending on what source you are connecting to.  There is not a single data connection button.  Instead, there are several, one for vector layers, raster layers, PostGIS, SpatiaLite, WMS, and WFS layers.  There are also buttons to create a new ESRI Shapefile layer, remove a layer, and for GPS tools.

QGIS Data Connection Toolbar

This post will focus on “Add Vector Layer” dialog.  Future posts will address more obscure features of some of the other options.

QGIS Add Vector Layer Dialog

Unlike in the ESRI products, the dialog in QGIS where you add a layer is also where you define connections to new data sources and choose layers from those data sources to be included.

We will review each of the major connection types, with samples of connections that worked and didn’t work.  Each connection window gives you the option to test the connection.  I will include a couple of failed connection tests to help interpret the errors so you know what to look for in the future.

Add ESRI Shape

QGIS Add Vector - ESRI Shape

Adding an ESRI Shapefile to QGIS is about as straight-forward as you can get it.  Open up the add vector data dialog, leave it on the default selection which is “file”, then browse for the shapefile.

Add ESRI Personal Geodatabase Layer

Add ESRI Personal Geodatabase Layers

Adding a layer from an ESRI Personal Geodatabase layer is also relatively simple.  Simply open up the add vector layer dialog, leave on the default of “file”, browse to the desired database.  If the database is not showing up in the browse window, change the file type filter to “.mdb”.  Once you select open, a dialog will come up that lists all of the layers present in the PGDB, with a count of the features in each layer.

Select Layer from ESRI PGDB

Select one or more of the layers that you want and click “OK”.  The layers will be loaded.

Add ArcINFO Coverage

Add Vector Layer - ArcINFO Coverage

For an ArcINFO Coverage, select the “Directory” radio button.  Under Source, Type, choose the “ArcINFO Binary Coverage” option.  Then browse and select the coverage name.  This will then open the list of vector layers within the coverage, the point, line and polygon representations of the features.

QGIS Add Vector Layer - ArcINFO

Add MySQL Spatial Layer

Of the data types that I have discussed adding so far, the MySQL process is the most complex.  This is evident by looking at the changes to the dialog box when you select the “database” option:

Add MySQL Vector Layer

The dialog has expanded to show a section for making connections.  There is an existing connection here, named “socalurisa”.  I am going to set up a new one.  First step is select “New” connection.   This brings up the following dialog, which I have already filled a number of values into.

QGIS - New Database Connection

Stepping through these values one by one, we have:

  • Type:  Pulldown options include MySQL, PostGreSQL, ODBC, etc.  I have chosen “MySQL” for this example.  One item to note here is that the option for an ESRI Personal Geodatabase seems to be implemented incorrectly.  This is the second place where you can add a PGDB.  The first is through the “File” selection option discussed above.  That option works simply.  As yet, I have not been able to successfully set up a database connection to the PGDB.
  • Name:  Choose the name for this connection.  Connections are made to individual databases, and may be local or remote, so make sure it is relatively descriptive.  It is easy enough to simply view the connection to review the details, but a descriptive name is always beneficial.
  • Host:  This describes where the database is hosted.  In this case, the MySQL installation is on the local machine, so the name is “localhost”.  If you were loading a database from a remote server, like a website, the host name might look something like this: “somedatabase.db.3111.somedomain.com”.  It can be a challenge to find the correct name of a remote database, but it is usually listed on the administration page of your remote host.
  • Database:  The name of your database.  In this case, “socalurisa”.  From a quick test, it seems that the database name is NOT case sensitive.  I do not, however know if this would raise problems when trying to execute other queries to that database from within QGIS if you do not match cases correctly.  Better to be safe and match the database name exactly.
  • Port:  3306 – Short answer – Computers have virtual ports open through the operating system that different types of data are allowed to pass through.  Some programs use a specific port to pass their data through.  RDBMS systems are embedded into the system to an extent that you actually access them by going out of the computer and creating a connection back in, through this port.  Hence, the reason for a data connection and a host and port name.  This is basically a WAG (Wild Ass Guess) on my part as operating system inner workings are definitely not a specialty of mine.
  • Username:  testuser
  • Password:  usertest

Let’s use the test connection to see if this connection is successful

QGIS - Failed Database Connection Test

There seems to be a problem with this connection.  It looks like I put in the wrong password.  Of course, the only way I know this is that I purposefully did this.  There really isn’t that much in the way of explanation as to why the connection failed, just “Access denied” in the last line.  Let’s fix the password and try the connection again:

QGIS - Successful database connection

This time, the connection was successful because the password was fixed.  This now will bring up a layer selection dialog similar to those seen previously where you may choose which database layers to load.  I have one comment about connecting to databases that is very important for people to consider.  The passwords are NOT encrypted.  If you load in a database layer, and go to the layer properties in the table of contents, the database connection properties are clearly listed.  This seems like a rather Glaring security lapse, but there may be a reason for it.  I will do some research and post a follow up.

This winds up the major vector data types you can load in to QGIS.  There are a couple of database types that I didn’t walk through here, but their connection format is going to be very similar to MySQL.  There seems to be a lack of documentation in the manual about setting up data types.  The only ones discussed there are SpatiaLite and PostGIS.  I suppose most of the others are self explanatory, but a primer never hurts.

5 thoughts on “QGIS – Vector Data Connection”

  1. Very good and helpful information here especially on the fields of ‘port’ and ‘host’. After that I connected without much effort only that the ‘password’ i entered ‘test’. Thanks

  2. Hi there,
    when I try to open a personal geodatabase the software give me a message error “not valid or unknown data source” (I am traslating from Italian).
    Have you anny suggestion?
    thanks
    Marco

  3. HI;
    I tried connecting my SDE database to QGIS, But am getting the below error

    [Microsoft][ODBC SQL Server Driver]Cannot generate SSPI context [Microsoft][ODBC Driver Manager] Driver’s SQLSetConnectAttr failed QODBC3: Unable to connect

Leave a Comment