Loading data into QGIS
The data in the GIS can be submitted as separate files, databases, or external online sources. Moreover, there are different data models used in the GIS to represent the geometry of spatial objects. The vector data model is mostly used to express discrete features, such as points (for example, separately growing trees and points of interest), lines (roads or railways), and polygons (buildings or administrative borders). Raster data is convenient for expressing continuous phenomena that are better represented by coverage than by points, lines, or polygons. The most common raster data sources are remote sensing imagery, digital elevation models, and scanned and georeferenced topographic maps.
QGIS uses the GDAL/OGR spatial data library to read and write multiple vector and raster file formats. In the following sections, we will briefly discuss the most common file formats that you can get your data in. Additionally, we will take a look at the recently widely used data sources, such as CSV files, files collected by GPS-receivers, and OpenStreetMap.
Loading shapefiles
Most of our data is in the ESRI shapefile format, which is one of the most common vector data file formats. There are a few alternative ways of loading a shapefile into QGIS. You can open the dialog window by going to Layer | Add Layer | Add Vector Layer, or by clicking on the relevant button on the Manage Layers toolbar, or by using the Ctrl + Shift + V keyboard shortcut.
In the Add vector layer dialog window, configure the following items:
- Source type, which can be File, Directory, Database, or Protocol. By default, the File source type specified is suitable for loading spatial data represented by separate files, like in our case.
- Consider the Encoding drop-down list options if the data you work with contains special symbols or the charset of its textual attributes differs from the conventional Latin symbology. In this case, you should choose an appropriate encoding type from the drop-down list. Our data does not contain any special symbols, so we accept the default System encoding.
- The Browse button is meant for navigating to the work directory. There, you can choose one or several files (with the Ctrl key) to add after hitting the Open button.
Tip
For the first time, you are likely to be overwhelmed by the amount and diversity of the available files. This is because in the GIS, a dataset is usually represented by several files with different extensions. For example, an ESRI shapefile consists of at least 4 files (
.shp
,.shx
,.dbf
, and.prj
) that share the same name, and among them,.shp
is what you should select to load the data into QGIS. You see all the files because the file type filter in the bottom-right corner of the window is set to All files by default. To hide all unnecessary files, choose the ESRI shapefiles file type. In future, don't forget to adjust the filter according to the desired file type.
Alternatively, you can use the Browser panel to navigate to the working directory. Select the files you want to load (either one or several, by holding down the Ctrl key), and then just drag and drop them onto the map area. If you want to simplify the navigation, add the folder you are currently working with to Favourites from its right-click menu shortcut.
The loaded files are assigned random colors. Don't worry about that as of now; we will cover layer symbology in Chapter 2, Visualizing and Styling the Data.
Loading rasters
The procedure of adding raster files is similar to what was described earlier, so we will briefly go through it for the example of the common raster file format GeoTIFF:
- You can load a raster file by going to Layer | Add Layer | Add Raster Layer, by clicking on the relevant button on the Manage Layers toolbar, or by using the Ctrl + Shift + R keyboard shortcut.
- In the Open a GDAL Supported Raster Data Source window, go to the work directory.
- To see available the GeoTIFF files, remember to adjust a file type filter, select one or more files (by holding down the Ctrl key), and click on Open. Alternatively, you can drag and drop rasters from the Browser panel.
Loading data from the Personal GeoDatabase
ESRI Personal GeoDatabase is an original ArcGIS data format where all of the database's content is held in a single .mdb
Microsoft Access file. It is widely used to store data in a single data file instead of multiple files of different formats.
Adding an ESRI file geodatabase to QGIS is similar to adding any other vector format:
- Access the dialogue window by going to Layer | Add Layer | Add Vector Layer, or by clicking on the relevant button on the Manage Layers toolbar, or with the Ctrl + Shift + V keyboard shortcut.
- Go to the work directory, set the file type filter to ESRI Personal GeoDatabase, and select the database file you want to import data from.
- In the Select vector layers to add... window, you will see the available layers and their characteristics. Click on the layers to be added and hit OK.
Note
Raster layers contained in the database will be interpreted and loaded into QGIS as bounding-box polygons. If the data is stored in tables that do not contain any spatial features, the geometry type will be displayed as Unknown. The tables will be loaded and available for exporting to other formats (for example,
.dbf
) or joining with spatial data layers.
Importing CSV files
A Comma-separated values (CSV) file is another popular data file format. In fact, it is just a spreadsheet with field values delimited by commas. There are various delimiters possible instead of commas (for example, tabs, spaces, colons and so on). Very often, these tables contain spatial data in the form of positional attributes represented by point longitude or latitude (XY) coordinates, or well-known text (WKT) geometry that describes points, lines, or polygons.
In our dataset, we have a CSV file called noise.csv
. It contains the details of noise complaints registered mostly by the New York City Police Department. To add this file as a spatial layer, follow these steps:
- Open the Create a Layer from a Delimited Text File dialog window by going to Layer | Add Layer | Add Delimited Text Layer, or just click on the relevant button in the Manage Layers toolbar.
- After browsing and pointing to your file, QGIS tries to parse it using the specified delimiter. By default, the comma delimiter is used, but you can specify any other delimiter using Custom delimiters (comma, tab, space, and so on) or Regular expression delimiter.
- The dialog also provides access to a number of other useful settings; for example, turning on First record has field names creates headers for fields. After defining the geometry as Point coordinates, X field and Y field containing Longitude and Latitude values will be loaded from the dataset automatically.
- After you have clicked on OK, QGIS reads the data and might show a delimited text file error message similar to Errors in file full_path_to_the_file/filename.csv 100 records discarded due to missing geometry definitions. This means that some points do not contain geographic coordinates, and so they cannot be located properly. After closing the message window, you will be prompted to specify the Coordinate Reference System (CRS) for the layer.
As we can see from the Latitude and Longitude column values, point coordinates were originally registered by the receiving devices of the Global Position System (GPS) in decimal degrees. It is also known that GPS uses the WGS 84 CRS. This is why in the Coordinate Reference System Selector window, we enter the EPSG: 4326 code filter and specify WGS 84 under Geographic Coordinate Systems as the initial CRS. After clicking on OK, you will see that the data is rendered in the map canvas as points.
Note
QGIS uses CRS definitions based on the European Petroleum Search Group's (EPSG) Geodetic Parameter Dataset, which contains detailed structured descriptions of coordinate reference systems and transformations of global, regional, national, and local applications. The database of EPSG identifiers can be used to specify a CRS in QGIS. You can read more about the EPSG Geodetic Parameter Dataset at http://www.epsg-registry.org/.
Loading GPS data
As GPS receivers became portable and relatively cheap, GPS tracking became a ubiquitous and widely used technique of collecting data during a field survey, or simply tracking your own routes while running or cycling. Depending on the receiver's capabilities, in addition to spatial coordinates, a lot of information can be registered and written—for example, time, elevation, and so on. Registered information is stored in points that represent location changes (way, track, or route points), a planned route (if it exists), and a track of movement. There are numerous formats for storing GPS data. As primary data format, QGIS uses the standard interchange GPX (GPS eXchange) format.
We will load the nyc_marathon.gpx
file from our dataset. You can load a .gpx
file as a vector layer by going to Layer | Add Layer | Add Vector Layer, by clicking on the relevant button in the Manage Layers toolbar, or by using the Ctrl + Shift + V keyboard shortcut. Go to the data directory and apply the GPS eXchange format (GPX) file type filter. Select the file (or files) you want to add and click on Open. Alternatively, you can drag and drop files from the Browser panel.
As GPS data consists of points, routes, and tracks, the window pops up where you choose exactly which feature you want to open. You can select one or more features by clicking on a row and holding down the Ctrl key. If you are not sure exactly which feature you need, use Select All. Every feature type will be presented in a separate layer. By turning layers on and off, you can check whether they contain data or not; for example, the file we work with contains only tracks and track points. You can remove unnecessary empty layers by right-clicking on the layer contextual shortcut Remove.
Note
Advanced options for working with GPS data are available through the GPS Tools core plugin. After activation, the plugin functions can be accessed by navigating to Vector | GPS | GPS Tools, or from the button on the Vector toolbar panel.
Getting OpenStreetMap data
OpenStreetMap (OSM) is a crowdsourcing project aimed at the development of an open map of the world. Worldwide, the OSM community uses high-resolution remote sensing imagery, GPS surveying, and local knowledge to make the map as accurate and detailed as possible. As the OSM data is licensed under Open Data Commons' Open Database License (ODbL), it has recently become a widely used source of spatial data. Understanding the importance of unrestricted access to spatial data, QGIS supports OSM, providing integrated access and support for its data.
The QGIS core functionality is available from the OpenStreetMap menu under Vector. Here, you can find all the required tools to download the data and export it into conventional spatial data formats, but if you are new to the QGIS and OSM data concepts, you may find the process tedious. This is why we recommend that you use external QuickOSM plugin capabilities to download the OSM data.
After installation as described in the Extending functionality through plugins section, the plugin's main functionality can be accessed by going to Web | QuickOSM | QuickOSM. The Quick query tab of the main window contains everything you need to perform a query and get the data into QGIS. First, define a key=value
pair from the drop-down lists, or just type it in manually.
Note
It can be quite difficult to select the appropriate tags for the first time if you are not familiar with the OSM tagging system. Each tag is a key=value
pair that describes some point, linear, or polygonal feature. The key describes a broad class (for example, amenities), while the value gives the details (bank, cinema, café, bicycle parking, and so on). Use the Help with key/value button to know more about tagging from the OSM wiki page at http://wiki.openstreetmap.org/wiki/Mapfeatures.
Then, set up the area of interest extent by location name, map canvas, or layer. If the layer drop-down list is inactive, click on the button beside it to refresh it. In the Advanced section, you can turn on/off different geometry types. By default, all geometry types are turned on, so all the relevant points, lines, and polygons will be downloaded. Browse to the directory you want to save the data in. In the following screenshot, you can see the example of the QuickOSM query used to download bicycle parking point data.
Tip
If you are familiar with the Overpass API query language, you can click on Show query or the Query tab to modify the initial query.
After clicking on the Run query button, the shapefile with the data will be loaded into the map canvas.