3.1. Lesson: Working with Vector Data¶
Vector data is arguably the most common kind of data you will find in the daily use of GIS. The vector model represents the location and shape of geographic features using points, lines and polygons (and for 3D data also surfaces and volumes), while their other properties are included as attributes (often presented as a table in QGIS). It is usually used to store discrete features, like roads and city blocks. The objects in a vector dataset are called features, and contain data that describe their location and properties.
The goal for this lesson: To learn about the structure of vector data, and how to load vector datasets into a map.
3.1.1. Follow Along: Viewing Layer Attributes¶
It’s important to know that the data you will be working with does not only represent where objects are in space, but also tells you what those objects are.
From the previous exercise, you should have the protected_areas
layer
loaded in your map. If it is not loaded, then you can find the
protected_areas.shp
ESRI Shapefile format dataset in directory
exercise_data/shapefile
.
The polygons representing the protected areas constitute the spatial data, but we can learn more about the protected areas by exploring the attribute table.
In the Layers panel, click on the
protected_areas
layer to select it.In the Attributes Toolbar click the Open Attribute Table button. This will open a new window showing the attribute table of the
protected_areas
layer.A row is called a record and is associated with a feature in the Canvas Map, such as a polygon. A column is called a field (or an attribute), and has a name that helps describe it, such as
name
orid
. Values in the cells are known as attribute values. These definitions are commonly used in GIS, so it is good to become familiar with them.In the
protected_areas
layer, there are two features, which are represented by the two polygons we see on the Map Canvas.Note
In order to understand what the fields and attribute values represent, one may need to find documentation (or metadata) describing the meaning of the attribute values. This is usually available from the creator of the data set.
Next, let’s see how a record in the attribute table is linked to a polygon feature that we see on the Map Canvas.
Go back to the main QGIS window.
In the Attributes Toolbar, click on the Select Feature button.
Make sure the
protected_areas
layer is still selected in the Layers panel.Move your mouse to the Map Canvas and left click on the smaller of the two polygons. The polygon will turn yellow indicating it is selected.
Go back to the Attribute Table window, and you should see a record (row) highlighted. These are the attribute values of the selected polygon.
You can also select a feature using the Attribute Table.
In the Attribute Table window, on the far left, click on the row number of the record that is currently not selected.
Go back to the main QGIS window and look at the Map Canvas. You should see the larger of the two polygons colored yellow.
To deselect the feature, go to the Attribute Table window and click on Deselect All button.
Sometimes there are many features shown on the Map Canvas and it might be difficult to see which feature is selected from the Attribute Table. Another way to identify the location of a feature is to use the Flash Feature tool.
In the Attribute Table, right-click on any cell in the row that has the attribute value
r2855697
for the fieldfull_id
.In the context menu, click on Flash Feature and watch the Map Canvas.
You should see the polygon flash red a few times. If you missed it, try it again.
Another useful tool is the Zoom to Feature tool, that tells QGIS to zoom to the feature of interest.
In the Attribute Table, right-click on any cell in the row that has the attribute value
r2855697
for the fieldfull_id
.In the context menu, click on Zoom to Feature
Look at the Map Canvas. The polygon should now occupy the extent of the Map Canvas area.
You may now close the attribute table.
3.1.2. Try Yourself Exploring Vector Data Attributes¶
How many fields are available in the rivers layer?
Tell us a bit about the
town
places in your dataset.
3.1.3. Follow Along: Loading Vector Data From GeoPackage Database¶
Databases allow you to store a large volume of associated data in one file. You may already be familiar with a database management system (DBMS) such as Libreoffice Base or MS Access. GIS applications can also make use of databases. GIS-specific DBMSes (such as PostGIS) have extra functions, because they need to handle spatial data.
The GeoPackage open format is a container that
allows you to store GIS data (layers) in a single file.
Unlike the ESRI Shapefile format (e.g. the protected_areas.shp
dataset
you loaded earlier), a single GeoPackage file can contain various data (both
vector and raster data) in different coordinate reference systems, as well as
tables without spatial information; all these features allow you to share data
easily and avoid file duplication.
In order to load a layer from a GeoPackage, you will first need to create the connection to it:
Click on the New button and browse to the
training_data.gpkg
file in theexercise_data
folder you downloaded before.Select the file and press Open. The file path is now added to the Geopackage connections list, and appears in the drop-down menu.
You are now ready to add any layer from this GeoPackage to QGIS.
Click on the Connect button. In the central part of the window you should now see the list of all the layers contained in the GeoPackage file.
Select the roads layer and click on the Add button.
A roads layer is added to the Layers panel with features displayed on the map canvas.
Click on Close.
Congratulations! You have loaded the first layer from a GeoPackage.
3.1.4. Follow Along: Loading Vector Data From a SpatiaLite Database with the Browser¶
QGIS provides access to many other database formats. Like GeoPackage, the SpatiaLite database format is an extension of the SQLite library. And adding a layer from a SpatiaLite provider follows the same rules as described above: Create the connection –> Enable it –> Add the layer(s).
While this is one way to add SpatiaLite data to your map, let’s explore another powerful way to add data: the Browser.
In this tab you can see all the storage disks connected to your computer as well as entries for most of the tabs in the left. These allow quick access to connected databases or folders.
For example, click on the drop-down icon next to the GeoPackage entry. You’ll see the
training-data.gpkg
file we previously connected to (and its layers, if expanded).Right-click the SpatiaLite entry and select New Connection….
Navigate to the
exercise_data
folder, select thelanduse.sqlite
file and click Open.Notice that a landuse.sqlite entry has been added under the SpatiaLite one.
Double-click the landuse layer or select and drag-and-drop it onto the map canvas. A new layer is added to the Layers panel and its features are displayed on the map canvas.
Tip
Enable the Browser panel in and use it to add your data. It’s a handy shortcut for the tab, with the same functionality.
Note
Remember to save your project frequently! The project file doesn’t contain any of the data itself, but it remembers which layers you loaded into your map.
3.1.5. Try Yourself Load More Vector Data¶
Load the following datasets from the exercise_data
folder into your map
using any of the methods explained above:
buildings
water
3.1.6. Follow Along: Reordering the Layers¶
The layers in your Layers list are drawn on the map in a certain order. The layer at the bottom of the list is drawn first, and the layer at the top is drawn last. By changing the order that they are shown on the list, you can change the order they are drawn in.
Note
You can alter this behavior using the Control rendering order checkbox beneath the Layer Order panel. We will however not discuss this feature yet.
The order in which the layers have been loaded into the map is probably not logical at this stage. It’s possible that the road layer is completely hidden because other layers are on top of it.
For example, this layer order…
… would result in roads and places being hidden as they run underneath the polygons of the landuse layer.
To resolve this problem:
Click and drag on a layer in the Layers list.
Reorder them to look like this:
You’ll see that the map now makes more sense visually, with roads and buildings appearing above the land use regions.
3.1.7. In Conclusion¶
Now you’ve added all the layers you need from several different sources.
3.1.8. What’s Next?¶
Using the random palette automatically assigned when loading the layers, your current map is probably not easy to read. It would be preferable to assign your own choice of colors and symbols. This is what you’ll learn to do in the next lesson.