Quantum GIS (QGIS, pronounced kyü'-jis) is a free and open-source desktop GIS package analogous to Esri's ArcMap. Because of its support for viewing PostGIS data and strong cartographic capabilities, the two are often found paired together. (OpenJUMP is another desktop application often used in combination with PostGIS, though its strengths are in spatial querying and geoprocessing.)
A. Add PostGIS data to QGIS
Let's see how QGIS can be used to view the tables we created and populated in the previous section.
- Open QGIS. It'll be the QGIS Desktop 2.x choice from the QGIS folder in your Start menu.
The basic elements of the application GUI are similar to ArcMap's. A narrow pane on the left side of the window lists the project layers and their symbology, while a wider pane to the right displays the layer features themselves. Across the top is a set of toolbars that can be moved to custom positions by the user.
The Manage Layers toolbar (docked vertically on the left side of the window by default) provides access to a set of buttons that are used to add data to the project (the buttons with plus signs). Moving from top to bottom:
- Add Vector Layer - for data that is supported by the OGR library, an open-source library that allows for working with vector data in many different formats including Esri shapefiles and geodatabases
- Add Raster Layer - for data that is supported by the GDAL library, the raster counterpart to the OGR library
- Add SpatiaLite Layer - for data stored in a SpatiaLite database (another free and open-source spatial database extension similar to PostGIS, built to add spatial functionality to an RDBMS called SQLite)
- Add PostGIS Layers - as the name implies, for data stored in PostGIS tables
- Add MSSQL Spatial Layer - for data stored in Microsoft SQL Server spatial databases
- Add DB2 Spatial Layer
- Add Oracle Spatial Layer - for vector data stored in Oracle Spatial databases
- Add WMS/WMTS Layer - for data streamed via a Web Mapping Service (WMS) or a Web Map Tile Service (WMTS)
- Add ArcGIS Mapserver Layer
- Add WCS Layer - for data streamed via a Web Coverage Service (WCS)
- Add WFS Layer - for data streamed via a Web Feature Service (WFS)
- Add ArcGIS FeatureServer Layer
- Add Delimited Text Layer - for data stored as delimited text
- Add/Edit Virtual Layer - the result of an advanced query, they do not carry data by themselves and can be seen as views to other layers
- New Shapefile Layer - for Esri shapefile data
(This tool enables the creation of a new shapefile dataset.)
- New SpatialLite Layer
- New GeoPackage Layer
- New Temporary Scratch Layer
- Click the Add PostGIS Layers button.
- In the Add PostGIS Tables dialog, click the New button.
- In the Create a New PostGIS connection dialog supply the following information. (See Figure 3.1, below.)
- Name: Lesson3
- Service - (leave blank)
- Host: localhost
- Port: 5432
- Database: Lesson3db
- SSL mode: disable
- Username: postgres
- Password: <the password you established when you installed the software>
- You should click the Test Connection button to make sure you have typed things correctly.
- Click OK to create the connection and dismiss the dialog.
The Lesson3 name should appear in the pick list in the Connections section of the Add PostGIS Table(s) dialog. (see it in Figure 3.2, below)
- Now, click the Connect button.
If an Enter Credentials dialog pops up just supply the postgres user name and the password you established for it.
- Expand the list of layers available in the nyc_poi schema, if necessary. You should see something that looks like the following, that includes nyc_poi in the Schema list.
- You should see the pts, lines and polys tables created early in the last section and also three layers that reference the mixed table created later (one layer for each geometry type). One of the convenient features of QGIS is that it automatically creates separate layers for each geometry type in a multi-geometry table like our mixed table.
- Click and drag to highlight all 6 nyc_poi layer rows, and click Add. You should see your data displayed in the map display area and the layers listed in the Layers Panel on the left side of the window (the equivalent to ArcMap's Table of Contents).
Let's take a quick tour of how basic functions are performed in QGIS.
B. Explore basic functions of QGIS
- Zoom to the full extent of all the layers by selecting View > Zoom Full.
- Click-hold and drag to rearrange the layers. Put them in the following order, from top to bottom: pts, lines, polys, and then the mixed pts, lines, and polys layers.
Note the various features.
- The pts, lines and polys layers are redundant with the three layers based on the mixed table, so turn off pts, lines and polys by clicking the x next to each layer.
While playing with layer visibility, you may note that the polys layer contains a hole in the Central Park polygon, whereas the "mixed" version of that polygon does not. This is to be expected since we didn't bother to create that inner ring in the mixed version.
- Each layer based on the mixed table has the same name. Go ahead and re-name these layers: mixed - lines, mixed - pts and mixed - polys by right-clicking on the layers one at a time and selecting the Rename command.
Now let's see how we can restrict the features that are displayed in a layer by setting a property analogous to the "definition query" in ArcMap.
- Double-click on the mixed - lines layer to open its Layer Properties dialog. You should see tabs, along the left side, similar to what you would find in the same dialog in ArcMap.
The Style tab is where you'd go to change the way a layer is symbolized. Note the pick list at the top of the Style dialog which provides Single Symbol, Categorized and Graduated options, etc.
The Actions tab provides functionality similar to ArcMap's Hyperlink settings for launching external applications to view data found in the attribute table such as images and URLs.
The Joins tab is where you'd go if you need to join data from another table to the layer's attribute table.
The Diagrams tab provides settings for creating pie chart and histogram (bar) chart overlays from numeric data in the attribute table.
You can investigate the other elements of the Layer Properties at your leisure.
- Now, still in the dialog for the mixed - lines Layer Properties, note the locations of the three line features, then go to the General tab. Recall that the three line features represent two tunnels (the longer line features) and a bridge. We are going to restrict the layer to showing just tunnels by doing the following:
- Click on the Query Builder button at the lower right of the Layer Properties dialog.
- In the Provider specific filter expression box compose the expression shown below, or just copy-paste it.
"name" LIKE '%Tunnel'
- Click the Test button to verify the veracity of your expression, then OK after confirming that it returns 2 rows.
- Click OK to dismiss the Query Builder dialog.
You'll see the expression mirrored in the Provider feature filter box of the Layer Properties dialog.
- Click OK to dismiss the Layer Properties dialog.
You should see that the Brooklyn Bridge is no longer displayed as part of the layer.
- An intuitive set of zoom/pan tools can be found on the Map Navigation Toolbar (the bar containing the "hand" icon). As in ArcMap, clicking on the Pan tool activates it. After doing so you can then click-and-drag in the map display area to alter the visible extent.
- To the right of the Pan tool, you'll see a number of useful tools for zooming: in/out, to the full extent, to the extent of a layer, to the extent of the selected features, and backward and forward in the extent history, along with tools associated with map tips and bookmarks.
- To the right of the Map Navigation toolbar is the Attributes Toolbar. There you'll find the Identify Features tool. The other controls on this toolbar include tools for selecting (the layer needs to be highlighted first)/unselecting features, for opening the attribute table of the layer selected in the Layers Panel, a Field Calculator, etc.
- Right-click on the mixed - pts layer and select Open Attribute Table. Again, you should find the table dialog has functionality that is very similar to what is found in ArcMap.
- Find and click the Select features using an expression button (it has a script capital E on it) and enter the following example expression:
gid < 4Note that you can build the expression graphically by expanding the Fields and Values and Operators lists, then double-clicking on items in those lists.
- (Don't have the Select by expression dialog covering the table of contents pane.) Click Select to evaluate the selection expression, then click Close to dismiss the Select by expression dialog and note the selected features in the attribute table and on the map.
- Close the Attribute table.
- Finally, select Project > Project Properties.
Under the General tab, you can: set the selection and background colors, specify whether references to data sources should be stored with relative or absolute paths and set the display units of the project.
Under the CRS (Coordinate Reference System) tab, you can specify on-the-fly coordinate system transformation settings similar to what Esri offers at the data frame level in ArcMap. Let's re-project our data into the New York East State Plane system.
- Click on the CRS tab.
- Check the Enable 'on the fly' CRS transformation (OTF) box.
- Scroll to the top of the Coordinate reference system of the world list and click the minus sign [-] sign boxes to collapse the lists. Note that the options are categorized into Geographic Coordinate Systems and Projected Coordinate Systems.
QGIS doesn't include a State Plane sub-category in its Projected Coordinate System list. All of the state plane projections can be found in either the Lambert Conformal Conic or Transverse Mercator sub-categories.
- Scrolling through those long lists can be tedious, so go to the Filter box near the top of the dialog and enter new york. The coordinate system listing should now only include those with 'new york' in the name.
- Find NAD83 / New York East EPSG: 32115 under the Transverse Mercator category, select it.
Arrange the windows on your screen so that you can see as much of them both as possible
At the bottom of the main QGIS interface, take note of the values in the Coordinate readout slot.
Keep your eye on the spatial data.
Now, on the Project Properties dialog, click OK.
- If your data are no longer visible, select View > Zoom Full. You should note that the Coordinate readout at the bottom of the application window now reports values in the ballpark of 190000,210000.
- Finally, save your project by clicking the Save button. Give the project a name of Lesson3 and save it to an appropriate folder. Note that the file extension for QGIS project files is .qgs.
That completes our quick tour of QGIS. In the next section, we'll return to pgAdmin to see how queries can be saved for later re-use.