Loading vector data from files

In this section, we will talk about loading vector data from GIS formats such as Shapefiles as well as from text files.

We can load vector files using the menu entry by going to Layer | Add vector layer and also by using the Add vector layer toolbar button. If you like shortcuts, use Ctrl + Shift + V. In the Add vector layer dialog, we find a drop-down list that allows us to specify the encoding of the input file. This option is important if we are dealing with files that contain special characters, such as German umlauts or letters from alphabets other than the default Latin one. The following screenshot shows the Add vector layer dialog:

What we are most interested in now is the Browse button, which opens the file-opening dialog. Note the file type filter drop-down list on the bottom-right corner of the dialog. We can open it to see a list of the supported vector file types. This filter is useful to find specific files faster by hiding all files of a different type, but be aware that the filter settings are stored and will be applied again the next time we open the dialog.

This can be a source of confusion if we later try to find a different file and it happens to be hidden by the filter, so remember to check the filter settings if you are having trouble locating a file.

We can load more than one file in one go by selecting multiple files at once (holding down Ctrl on Windows/Ubuntu or Cmd on Mac). Let's give it a try.

  1. We select alaska.shp and airports.shp from the sample data vmap0_shapefiles folder.
  2. Next, we confirm our selection by clicking on Open, and we are taken back to the Add vector layer dialog.
  3. After clicking on Open once more, the selected files are loaded. You will notice that each vector layer is displayed in a random color. Don't worry about that now. We'll deal with layer styles later in this chapter.
    Tip

    There are multiple tricks that make loading data even faster; for example, you can simply drag and drop files from the operating system file browser into QGIS. Another way to quickly access your spatial data is by using QGIS' built-in file browser. If you set up QGIS as shown in Chapter 1, Getting Started with QGIS, you'll find the browser on the left-hand side, just below the layer list. Navigate to your data folder and you can again drag and drop files from the browser to the map. Additionally, you can mark a folder as favorite by right-clicking on the folder and selecting Add to favorites. This way, your data folders will be even faster to access because they are added in the Favorites section right at the top of the browser list.

Another popular source of spatial data are delimited text (CSV) files. QGIS can load CSV files using the Add Delimited Text Layer option available via the menu entry by going to Layers | Add Delimited Text Layer or the corresponding toolbar button. Click on Browse and select elevp.csv from the sample data. CSVs come with all kinds of delimiters. As you can see in the following screenshot, the plugin lets you choose from the most common ones (Comma, Tab, and so on), but you can also specify any other plain or regular expression delimiter. If your CSV contains quotation marks such as " or ', you can use the Quote option to have them removed. The Number of header lines to discard option allows us to skip extra lines at the beginning of the text file. The following Field options include functionality to trim extra spaces from field values or to redefine the decimal separator to a comma. The spatial information itself can be provided either in two columns containing coordinates of points X and Y or by using the Well known text (WKT) format. A WKT field can contain points, lines, or polygons.

Tip

WKT is a very useful and flexible format. For example, a line can be specified by writing LINESTRING (30 10, 10 30, 40 40). If you are unfamiliar with the concept, you can find an introduction with examples at en.wikipedia.org/wiki/Well-known_text.

After clicking on OK, QGIS will prompt us to specify the layer's coordinate reference system (CRS). We will talk about handling coordinate reference systems next.