Loading vector data from files
In this section, we will talk about loading vector data from GIS file formats, such as shapefiles, as well as from text files.
We can load vector files by going to Layer | Add Layer | Add Vector Layer and also using the Add Vector Layer toolbar button. If you like shortcuts, use Ctrl + Shift + V. In the Add vector layer dialog, which is shown in the following screenshot, 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 different from the default Latin ones.
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 in the bottom-right corner of the dialog. We can open it to see a list of supported vector file types. This filter is useful to find specific files faster by hiding all the files of a different type, but be aware that the filter settings are stored and will be applied again the next time you open the file opening dialog. This can be a source of confusion if you try to find a different file later 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:
- First, we select
alaska.shp
andairports.shp
from theshapefiles
sample data folder. - Next, we confirm our selection by clicking on Open, and we are taken back to the Add vector layer dialog.
- After we've clicked on Open once more, the selected files are loaded. You will notice that each vector layer is displayed in a random color, which is most likely different from the color that you see in the following screenshot. Don't worry about this now; we'll deal with layer styles later in this chapter.
Even without us using any spatial analysis tools, these simple steps of visualizing spatial datasets enable us to find, for example, the southernmost airport on the Alaskan mainland.
Tip
There are multiple tricks that make loading data even faster; for example, you can simply drag and drop files from the operating system's file browser into QGIS.
Another way to quickly access your spatial data is by using QGIS's built-in file browser. If you have 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 it and selecting Add as a favorite. In this way, you can access your data folders even faster, because they are added in the Favorites section right at the top of the browser list.
Another popular source of spatial data is delimited text (CSV) files. QGIS can load CSV files using the Add Delimited Text Layer option available via the menu entry by going to Layer | Add Layer | Add Delimited Text Layer or the corresponding toolbar button. Click on Browse and select elevp.csv
from the sample data. CSV files 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 file 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 any potential extra lines at the beginning of the text file. The following Field options include functionality for trimming extra spaces from field values or redefine the decimal separator to a comma. The spatial information itself can be provided either in the two columns that contain the coordinates of points X and Y, or using the Well known text (WKT) format. A WKT field can contain points, lines, or polygons. For example, a point can be specified as POINT (30 10)
, a simple line with three nodes would be LINESTRING (30 10, 10 30, 40 40)
, and a polygon with four nodes would be POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))
.
Note
Note that the first and last coordinate pair in a polygon has to be identical.
WKT is a very useful and flexible format. If you are unfamiliar with the concept, you can find a detailed introduction with examples at http://en.wikipedia.org/wiki/Well-known_text.
After we've clicked on OK, QGIS will prompt us to specify the layer's coordinate reference system (CRS). We will talk about handling CRS next.