Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.


Numberedheadings
h3, h4start-numbering-with
number-formatdecimal
skip-headingsHNaN
start-numbering-with2
h1[h1.decimal]2
h2.[h1.decimal].[h2.upper-latin]
h3
h4
h5
enabledtrue
h6
start-numbering-atH1

Vector Data in QGIS

Introduction

Vector data provides a way to represent real world features within the GIS environment. A feature is anything you can see on the landscape.

QGIS can open around 54 different over 60 file-based data formats (using the OGR library), including ESRI shapefile and MapInfo TAB, and can save around 23 of these. It can also connect to four spatial database sources natively (including PostgreSQL, SpatiaLite, Oracle, and MSSQL), with others available using ODBC, and to Web Feature Service (WFS) and Point Cloud data among other formats.The , and to Arc REST services. QGIS’s default file format used by QGIS when creating data OGC is GeoPackage, a file-based database. For databases the default connection is to PostgreSQL/PostGIS. Most QGIS functionality is available for all vector data sources, though some formats can only be used in are read-only mode and need to be saved to another format for editing.

File-Based Vector Data

GeoPackage

GeoPackage is relatively new - it was first published as an OGC standard in 2014data format. It has a .gpkg file extension, and although it can be used for single layers, it is implemented using the SQLite database standard, so can hold multiple layers in a single file as well. It is supported by current versions of most desktop GIS packages, and although it has not been widely used until now, it should be set to take over from shapefile as the default file-based storage and interchange format across applications and platforms. It will hold raster as well as vector data.

To load a GeoPackage layer:

Click Open Data Source Manager 
  • Browse to the .gpkg file you want to open (this will be in .../Data/Vector under root directory for the course)

  • Double-click or drag and drop to open the layer in QGIS

  • Layer metadata

    Right-click > Properties on the layer in the browser to see its metadata - CRS, number of features, fields and so on.

    one or many layers, including non-spatial and raster data, and can also store QGIS projects.

    Load GeoPackage

    1. All vector data is in ...\QGIS Training\Data\vector - right-click on the Favorites icon in the Browser panel to add this location for quick access

    2. Open the project from the previous exercise

    3. Using the Data Source Manager button, add file-based vector data by clicking the Vector tab on the left and browsing to the file

    4. Click Add to load the aonb_england.gpkg GeoPackage layer

    5. Click Close to close the dialog and view the layer

    ESRI Shapefile

    Shapefile is a widely-used legacy format , developed by ESRI (used in for example ArcGIS), and is used on many download sites. Shapefiles comprise four or more individual files on disk - all must be available for the file to open.

    To filter directory listings to shapefile (or any file vector format):

  • Click Open Data Source Manager

  • Click the Vector tab on the left of the dialog

  • Click the three dots (...) to the right of the Vector Dataset(s) field, then use the dropdown to the right of the File name: field to filter to ESRI Shapefiles (*.shp)

  • The list of files will be filtered to .shp only

    Load Shapefile

    1. Use the Browser to navigate to wind_turbines_point.shp in the vector folder (it’s in D:/PhotonUser/My Files/Home Folder/QGIS Training/Data)

    2. Double-click on the file to load it into the Layers panel, then right-click > Zoom to Layer(s) to show the layer on the map

    3. Right-click on the layer in the Browser to see the other options available

    MapInfo

    QGIS can open both MIF and TAB files - these will appear in the browser with other file-based data. Note that MapInfo files can contain multiple geometry types, so QGIS may present a dialog with a choice of geometry types - i.e. line, polygon, point - to load. You can filter to MapInfo files in the same way as described for shapefiles above.

    Editing MapInfo files

    QGIS can edit .tab but not .mif files - if you need to edit these, you will need to save them to a different format (for example GeoPackage), then save back when you are finished.

    Other

    The process above can be used to load most vector data from file, including for example GML and GeoJSON. QGIS can also open ESRI GeoDatabases, though these will be read-only.

    Spreadsheet Data

    Load MapInfo

    1. Load the aonb_wales.mif file

    Excel

    QGIS will open an Excel file as vector data, and for the most part correctly will normally recognise data types (for example string, integer and so on, date). If the data includes coordinates, creating there is an extra step you need to create a geometry column which QGIS can use to represent the data spatially requires an extra step, described under Excel below. Alternatively, use the Spreadsheet Layers plugin, or load the data as CSV using the Delimited Text option .

    Load Excel

    1. Add RTC Location 2017_0.xlsx, using the Vector tab in the Data Source Manager

    described in Delimited Text Data below.

    Excel

    To load data from Excel:

    1. Click Open Data Source Manager

    2. Click Vector and browse to the Excel file

    3. Click Add to add the layer to QGIS

    4. Select the worksheet(s) to add

    At this stage, the layer will show in the Layers panel as a table (for example ), but it won't appear on the map. If the data doesn't contain coordinates, this may be what you want. However if the data represents points, and you have an x and y column, you will need to add a geometry column to the data so that QGIS can add it to the map.
  • In the search bar, search for  the Create Points from Table process

  • Open the process, and complete the dialogue, making sure to set the X and Y fields correctly, and to use the CRS that the coordinate data is in

  • Click Run

  • The layer will appear in the Layers panel as Points from table, and the points will appear on the map

  • Delimited Text Data (e.g. CSV)

    If you need more control over the process of loading spreadsheet data, or if it's already held in delimited text such as CSV, you can use the Add delimited text layer tool.

  • Click Open Data Source Manager

  • Select the Delimited Text tab to bring up the Delimited Text dialog

  • Adjust the settings shown below if necessary - note that in this instance, only the Geometry CRS setting was changed from the default - QGIS derived all other settings from the data

  • Click Add to add the layer to the map

  • Saving CSV in a vector format

    At this point you may want to save the CSV as a standard vector format (for example GeoPackage) - there are some things which can't be done while it's still held as CSV. On the other hand, if you are using a CSV which is regularly updated from an external source, you may want to keep it as CSV, so that QGIS is always looking at the latest version of the data.

    If the data has no coordinate data, and is just going to be used in QGIS as a table, check the Geometry definition > No geometry option in the dialog above. This is useful, for example, if there is a layer that needs to be joined.

    If the file has a column containing Well Known Text (WKT) data (e.g. "SRID=27700;POINT(507417 163699)" ) instead of coordinates, check the Geometry definition > Well known text option.

    Database Layers

    Loading database vector layers follows a slightly different process to loading a file-based layer, as
    1. Note that the data appears as a table in the Layers panel at this point - nothing is on the map

    2. To turn the coordinates into points, use the Search box (in the lower left corner) to find the Create points layer from table process

    3. Complete the dialog as follows and run the process - this will create a new spatial layer on the map

    Database Layers

    To load vector layers from a database, you first need to establish a database connection. The process is similar for all databases, but in this example we will use data in a PostGreSQL PostgreSQL database.

    Database layers

    You can load either spatial or non-spatial data from a database - the non-spatial data will be shown in QGIS as a table, which you can join to spatial data as shown below.

    To load data from a PostGreSQL database:

  • Click Open Data Source Manager

  • Click the PostgreSQL tab

  • If you haven't connected to the database before, click New to create a new database connection

  • Complete the dialog:

    1. Name: your name for the connection

    2. Host: the name or IP address of the server the database is running on

    3. Port: the port the database is running on - by default in PostGreSQL this is 5432, but it could be different

    4. Database: the name of the database you want to connect to

  • Under Authentication, click the Add button to add your credentials

  • The first time you do this, you will be prompted for a Master password - make sure this is something you can safely store or remember, as it cannot be retrieved later

  • Once you have done this, you will be able to set the credentials for this connection:
  • Click Save to save these credentials, then click Test Connection to make sure the connection is OK

  • If all is well, click OK, then click Connect to establish the connection with the database
  • Expand the database schemas, and select a table and click Add to add it to the map

  • Check Also list tables with no geometry to view non-spatial data tables

  • Once the data is loaded in the Layers panel, it will behave in the same way as file-based vector layers. You only need to set up the connection once - after that, you can connect to the database from the PostGreSQL table in the Data Source Manager, or from the Browser tab.

    1. Create a connection to the training_qgis database on the appstream_data.astuntechnology.com server using Data Source Manager > PostgreSQL > New

    2. Complete the connection details as shown in the screenshot below, using the Basic tab under Authentication - note the user credentials are qgis/qgis (click Store)

    3. Click Test Connection to make sure you can connect to the database - if successful, you will see a confirmation message

    4. Click OK, then Connect to open the connection

    5. Close the Data Source Manager dialog, then using the Browser pane, navigate to PostGIS > QGIS Training > boundaryline > county_region and double-click on the layer to add to the map

    Web Feature Service (WFS) Layers

    Data available in OGC Web Feature Service (WFS) format can be loaded in QGIS and used in a similar way to any other vector layer. However as the data is online, , as long as you have an Internet connection is required, although once the connection has been made it data can be saved locally . WFS data is supplied as GML, and features can be selected, styled and queried in the same way as any other vector data. 

    Create a new saved WFS Connection 

    Open

    The first time you use a WFS source, you will once you are connected. So first of all you need to create a connection:

    Click

    .

    Load WFS

    1. Create a new WFS connection using Data Source Manager

    , then the WFS tabClick New, then in the Create a New WFS Connection dialog, add the connection details.
  • Name: your name for the connection

  • URL: the URL of the WFS, for example http://essex.astuntechnology.com
    1. > WFS > New

    2. Open Chrome in AppStream, and go to https://data.gov.uk and search for conservation areas in Carlisle, then right click on Download to copy the link, and paste to the URL field in the dialogue below  - it should be http://maps.carlisle.gov.uk/getows.ashx?mapsource=

    Essex
    1. CCC/

    Inspire_Colchester
    1. inspire&TypeName=

    allotment
    1. conservation_areas&version=1.1.0&request=GetFeature&service=WFS

  • Authentication: complete as required

  • Click OK.

  • Connect to the WFS 

  • Select the saved WFS

  • Click Connect to show the layers available in the WFS
  • Select the layer(s) required - you can select multiple layers

  • Click Add to load the layer(s) into the map

  • Note that with large datasets, it can take some time for the layers to load

  • WFS Queries

    • By default, only features within the current view extent will be downloaded - uncheck Only request features overlapping the view extent to load all features from the source

    • To load only a subset of features (other than by extent), click Build query to bring up the SQL Query Composer, and add a query to select the features you need

    Once WFS layers are loaded, they can be managed in the same way as other vector layers.

    Transactional Web Feature Service (WFS-T) layers, which are editable, are also supported in QGIS.

    Navigating Vector Layers

    There are different approaches to navigating around the layers you have loaded, for example using toolbars, mouse actions and menus - some of the key ones are listed here.

    The buttons in the Map Navigation Toolbar will also get you around the map, and change the action of your mouse and cursor - these will help you identify the data and views you need.

    View Layer Extents

    When a layer has been added to the Layers panel, the features will be visible in the map frame on the right. To see the whole layer in the frame:

    1. Click on the layer to select it

    2. Right-click > Zoom to Layer (or click the icon on the toolbar)

    Attribute Table

    You can browse the attribute data for each layer by right-clicking on the layer and selecting Open Attribute Table, (or click the  icon in the toolbar). 

    Manage the Layer List

    Normally, layers at the top of the list in the Layers panel appear on the map in front of layers further down the list. You can change the order of layers in the list by dragging and dropping layers to a new position.

    Group Layers 

    Layers can be grouped to create a hierarchical layer list, or table of contents.

    Click the Add Group button at the top of the Layers panel 
  • Right-click and Rename the new group

  • Drag and drop layers into the group 

  • Remove Layers

    To remove a layer from the list, select the layer, then Right-click > Remove. This does not delete the data, just removes it from the map. However you will lose any styling or queries you have applied to the layer.

    Joining Layers

    If you want to use data in your map which has no spatial references
    1. (you can also paste from here)

    2. Click OK then Connect

    3. Click on the layer you want to load, then Add and Close

    4. The layer will be added to the map

    Attribute Table

    Attribute tables hold the data which sits behind every layer.

    Open Attribute Tables

    1. Right-click > Open Attribute Table on one of the layers you have open, and see how the data in the attribute tables relates to the features on the map (you may need to use the Panel Switcher to find the table)

    2. Select an feature in the attribute table by clicking on the row number  on the left - it will be highlighted ('selected') in the table, and will also be selected in yellow on the map

    3. When you have a feature selected, use the Zoom to Selection button in the attribute table toolbar to navigate to the feature on the map

    4. Click on a column header to sort the table using that column

    Join Layer to Non-Spatial data

    If you want to use data which has no spatial references in the map, you will need to join this to a corresponding spatial dataset. For In this example, you might have population we have Index of Multiple Deprivation (IMD) data for census areas (LLSOAs), and a polygon layer of the census areas themselves, and you want to LLSOAs themselves - we will join the two in order to show the population IMD figures for each areas as a label.

  • Add the non-spatial data table, and the spatial layer you are joining it to, as described earlier

  • The new table will appear in the Layers panel, but not on the map

  • Double-click the spatial layer to bring up the Properties dialog, and select the Joins tab

  • Click the Add button bottom left to add a join

    In the Add Vector Join dialog, use the dropdowns to specify:

  • Join layer: the layer you want to join to the current one

  • Join field: the field in the target layer (the one you are joining to) that is going to be used for the join (this might be ID, for example, or name, or in the case below, neighbourhood)

  • Target field: the field in the current layer (the spatial one) which is going to be used for the join - this must contain the same data as the join field
  • Click OK and OK again, then check the attribute table of the spatial layer - it will have an expanded number of columns to include the data from the non-spatial table

  • Join definition

    The join definition

    Join Layers

    1. Open IMD2019_Index_of_Multiple_Deprivation.xlsx  selecting the IMD2019 sheet

    2. If it’s not already loaded, load the llsoa_dec_2011_england_wales_ne layer from the LLSOA_Dec_2011_England_Wales GeoPackage

    3. Open the attribute tables for both IMD2019  and llsoa_dec_2011_england_wales_ne and compare the data

    4. Notice that LSOA code (2011) in the the spreadsheet layer uses the same data as code in the spatial layer

    5. Select  llsoa_dec_2011_england_wales_ne and right-click > Properties > Joins, and press the green + button to add a join

    6. Select the target spreadsheet layer and the correct columns to match the two layers, then click OK

    7. Open the attribute table of llsoa_dec_2011_england_wales_ne to ensure the data has been joined correctly

    8. If you have time and you know how, add the IMD Rank as a label to each LLSOA, and apply a Graduated style to the joined data using the Rank attribute, or a Categorised style using the Decile attribute (this will be covered in more detail in a later module)

    9. Save the project

    The join you have just created is saved in memory, and is saved in the QGIS project - it does not affect the source data

    . It will therefore reflect

    , but it will be updated with the current data each time the project is loaded.

    You can edit the join after it has been created - for example to exclude fields from the joined data - in the same dialog you used to create it.

    Exporting Vector Data

    To export a layer or part of a layer:

    Right

    Exporting Vector Data

    Any of the vector formats you have been using can be exported into most other formats, including CSV and GeoPackage.

    Export Data

    1. Click on the layer to be exported, then right-click > Export > Save Features as

    from the layer
    1. In the dialog,

    you can simply save the file as a different name - however you can also use other options
    1. options include:

      1. Format: save the data in a different vector format

      2. File name: browse to the location where you want the file to be saved, and give it a name

      3. Layer name: for a GeoPackage, by default this will be the same as the file name, but you can change it (GeoPackages can contain many layers)

      4. CRS: reproject the data on save to a different CRS

      5. Save only selected features: save a subset of the features

    CRS: reproject the data on save to a different CRS
      1. based on a selection you have already made

      2. Select fields to export: save a subset of fields from the attribute table

      3. Extent: save only features visible in the current extent

    Exporting a joined layer

    1. Use the remaining time in the exercise to try a few of these options

    Note that If you export a layer with a join, the exported data will include the fields in the join - this is a way of creating a permanent new dataset from a joined layer, although from this point on it won’t be updated if the source layers change.