Table of Contents for
QGIS: Becoming a GIS Power User

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition QGIS: Becoming a GIS Power User by Alexander Bruy Published by Packt Publishing, 2017
  1. Cover
  2. Table of Contents
  3. QGIS: Becoming a GIS Power User
  4. QGIS: Becoming a GIS Power User
  5. QGIS: Becoming a GIS Power User
  6. Credits
  7. Preface
  8. What you need for this learning path
  9. Who this learning path is for
  10. Reader feedback
  11. Customer support
  12. 1. Module 1
  13. 1. Getting Started with QGIS
  14. Running QGIS for the first time
  15. Introducing the QGIS user interface
  16. Finding help and reporting issues
  17. Summary
  18. 2. Viewing Spatial Data
  19. Dealing with coordinate reference systems
  20. Loading raster files
  21. Loading data from databases
  22. Loading data from OGC web services
  23. Styling raster layers
  24. Styling vector layers
  25. Loading background maps
  26. Dealing with project files
  27. Summary
  28. 3. Data Creation and Editing
  29. Working with feature selection tools
  30. Editing vector geometries
  31. Using measuring tools
  32. Editing attributes
  33. Reprojecting and converting vector and raster data
  34. Joining tabular data
  35. Using temporary scratch layers
  36. Checking for topological errors and fixing them
  37. Adding data to spatial databases
  38. Summary
  39. 4. Spatial Analysis
  40. Combining raster and vector data
  41. Vector and raster analysis with Processing
  42. Leveraging the power of spatial databases
  43. Summary
  44. 5. Creating Great Maps
  45. Labeling
  46. Designing print maps
  47. Presenting your maps online
  48. Summary
  49. 6. Extending QGIS with Python
  50. Getting to know the Python Console
  51. Creating custom geoprocessing scripts using Python
  52. Developing your first plugin
  53. Summary
  54. 2. Module 2
  55. 1. Exploring Places – from Concept to Interface
  56. Acquiring data for geospatial applications
  57. Visualizing GIS data
  58. The basemap
  59. Summary
  60. 2. Identifying the Best Places
  61. Raster analysis
  62. Publishing the results as a web application
  63. Summary
  64. 3. Discovering Physical Relationships
  65. Spatial join for a performant operational layer interaction
  66. The CartoDB platform
  67. Leaflet and an external API: CartoDB SQL
  68. Summary
  69. 4. Finding the Best Way to Get There
  70. OpenStreetMap data for topology
  71. Database importing and topological relationships
  72. Creating the travel time isochron polygons
  73. Generating the shortest paths for all students
  74. Web applications – creating safe corridors
  75. Summary
  76. 5. Demonstrating Change
  77. TopoJSON
  78. The D3 data visualization library
  79. Summary
  80. 6. Estimating Unknown Values
  81. Interpolated model values
  82. A dynamic web application – OpenLayers AJAX with Python and SpatiaLite
  83. Summary
  84. 7. Mapping for Enterprises and Communities
  85. The cartographic rendering of geospatial data – MBTiles and UTFGrid
  86. Interacting with Mapbox services
  87. Putting it all together
  88. Going further – local MBTiles hosting with TileStream
  89. Summary
  90. 3. Module 3
  91. 1. Data Input and Output
  92. Finding geospatial data on your computer
  93. Describing data sources
  94. Importing data from text files
  95. Importing KML/KMZ files
  96. Importing DXF/DWG files
  97. Opening a NetCDF file
  98. Saving a vector layer
  99. Saving a raster layer
  100. Reprojecting a layer
  101. Batch format conversion
  102. Batch reprojection
  103. Loading vector layers into SpatiaLite
  104. Loading vector layers into PostGIS
  105. 2. Data Management
  106. Joining layer data
  107. Cleaning up the attribute table
  108. Configuring relations
  109. Joining tables in databases
  110. Creating views in SpatiaLite
  111. Creating views in PostGIS
  112. Creating spatial indexes
  113. Georeferencing rasters
  114. Georeferencing vector layers
  115. Creating raster overviews (pyramids)
  116. Building virtual rasters (catalogs)
  117. 3. Common Data Preprocessing Steps
  118. Converting points to lines to polygons and back – QGIS
  119. Converting points to lines to polygons and back – SpatiaLite
  120. Converting points to lines to polygons and back – PostGIS
  121. Cropping rasters
  122. Clipping vectors
  123. Extracting vectors
  124. Converting rasters to vectors
  125. Converting vectors to rasters
  126. Building DateTime strings
  127. Geotagging photos
  128. 4. Data Exploration
  129. Listing unique values in a column
  130. Exploring numeric value distribution in a column
  131. Exploring spatiotemporal vector data using Time Manager
  132. Creating animations using Time Manager
  133. Designing time-dependent styles
  134. Loading BaseMaps with the QuickMapServices plugin
  135. Loading BaseMaps with the OpenLayers plugin
  136. Viewing geotagged photos
  137. 5. Classic Vector Analysis
  138. Selecting optimum sites
  139. Dasymetric mapping
  140. Calculating regional statistics
  141. Estimating density heatmaps
  142. Estimating values based on samples
  143. 6. Network Analysis
  144. Creating a simple routing network
  145. Calculating the shortest paths using the Road graph plugin
  146. Routing with one-way streets in the Road graph plugin
  147. Calculating the shortest paths with the QGIS network analysis library
  148. Routing point sequences
  149. Automating multiple route computation using batch processing
  150. Matching points to the nearest line
  151. Creating a routing network for pgRouting
  152. Visualizing the pgRouting results in QGIS
  153. Using the pgRoutingLayer plugin for convenience
  154. Getting network data from the OSM
  155. 7. Raster Analysis I
  156. Using the raster calculator
  157. Preparing elevation data
  158. Calculating a slope
  159. Calculating a hillshade layer
  160. Analyzing hydrology
  161. Calculating a topographic index
  162. Automating analysis tasks using the graphical modeler
  163. 8. Raster Analysis II
  164. Calculating NDVI
  165. Handling null values
  166. Setting extents with masks
  167. Sampling a raster layer
  168. Visualizing multispectral layers
  169. Modifying and reclassifying values in raster layers
  170. Performing supervised classification of raster layers
  171. 9. QGIS and the Web
  172. Using web services
  173. Using WFS and WFS-T
  174. Searching CSW
  175. Using WMS and WMS Tiles
  176. Using WCS
  177. Using GDAL
  178. Serving web maps with the QGIS server
  179. Scale-dependent rendering
  180. Hooking up web clients
  181. Managing GeoServer from QGIS
  182. 10. Cartography Tips
  183. Using Rule Based Rendering
  184. Handling transparencies
  185. Understanding the feature and layer blending modes
  186. Saving and loading styles
  187. Configuring data-defined labels
  188. Creating custom SVG graphics
  189. Making pretty graticules in any projection
  190. Making useful graticules in printed maps
  191. Creating a map series using Atlas
  192. 11. Extending QGIS
  193. Defining custom projections
  194. Working near the dateline
  195. Working offline
  196. Using the QspatiaLite plugin
  197. Adding plugins with Python dependencies
  198. Using the Python console
  199. Writing Processing algorithms
  200. Writing QGIS plugins
  201. Using external tools
  202. 12. Up and Coming
  203. Preparing LiDAR data
  204. Opening File Geodatabases with the OpenFileGDB driver
  205. Using Geopackages
  206. The PostGIS Topology Editor plugin
  207. The Topology Checker plugin
  208. GRASS Topology tools
  209. Hunting for bugs
  210. Reporting bugs
  211. Bibliography
  212. Index

A dynamic web application – OpenLayers AJAX with Python and SpatiaLite

In this section, we will produce our web application, which, unlike any so far, involves a dynamic interaction between client and server. We will also use a different web map client API—OpenLayers. OpenLayers has long been a leader in web mapping; however, it has been overshadowed by smaller clients, such as Leaflet, as of late. With its latest incarnation, OpenLayers 3, OpenLayers has been slimmed down but still retains a functionality advantage in most areas over its newer peers.

Server side – CGI in Python

Common Gateway Interface (CGI) is perhaps the simplest way to run a server-side code for dynamic web use. This makes it great for doing proof of concept learning. The most typical use of CGI is in data processing and passing it onto the database from the web forms received through HTTP POST. The most common attack vector is the SQL injection. Going a step further, dynamic processing similar to CGI is often implemented through a minimal framework, such as Bottle, CherryPy, or Flask, to handle common tasks such as routing and sometimes templating, thus making for a more secure environment.

Don't forget that Python is sensitive to indents. Indents are always expressed as spaces with a uniform number per hierarchy level. For example, an if block may contain lines prefixed by four spaces. If the if block falls within a for loop, the same lines should be prefaced by 8 spaces.

Python CGI development

Next, we will start up a CGIHTTPServer hosting instance via a separate Windows console session. Then, we will work on the development of our server-side code—primarily through the QGIS Python Console.

Starting a CGI hosting

Starting a CGI session is simple— you just need to use the –m command line switch directly with Python, which loads the module as you might load a script. The following code starts CGIHTTPServer in port 8000. The current working directory will be served as the public web directory; in this case, this is C:\packt\c6\data\web.

In a new Windows console session, run the following:

cd C:\packt\c6\data\web
python -m CGIHTTPServer 8000

Testing the CGI hosting

Python (.py) CGI files can only run out of directories named either cgi or cgi-bin. This is a precaution to ensure that we intend the files in this directory to be publically executable.

To test this, create a file at c6/data/web/cgi-bin/simple_test.py with the following content:

Note

The first line is our shebang, which allows this file to be independently executable through the interpreter listed in the path on Unix systems. While this has no effect on Windows systems, where execution is handled through file associations, we will leave this here for interoperability.

#!/usr/bin/python

# Import the cgi, and system modules
import cgi, sys

# Required header that tells the browser how to render the HTML.
print "Content-Type: text/html\n\n"
print "Hello world"

You should now see the "Hello world" message when you visit http://localhost:8000/cgi-bin/simple_test.py on your browser. To debug on the client side, make sure you are using a browser-based web development view, plugin, or extension, such as Chrome's Developer Tools toolbar or Firefox's Firebug extension.

Debugging server-side code

Here are a few ways through which you can debug during Python CGI development:

  • Use the Python Console in QGIS (navigate to Plugins | Python Console). You can run the Python code from your Python CGI Scripts here directly; however, this will fail for the scripts that rely on information passed through HTTP, but you can at least catch syntax errors, and you can populate it with the expected values to compare the result to what you're getting on a web browser. Sometimes, you'll want to quit QGIS to clear out the memory of the Python interpreter.
  • A quicker way of doing this is to run your script in the command line with –d (verbose debugging). This will catch any issues that may not come up in interactive use, avoiding the variables that may have inadvertently been set in the same interactive session (substitute index.py with the name of your Python script). Run the following command from your command line shell:
    python -d C:\packt\c6\data\web\cgi-bin\index.py
    
  • If your Python CGI script is interacting with a database, you definitely need to test the queries through DB Manager SQL Window (or whichever database interface you prefer). It is often helpful to populate the queries with the expected values.
  • Go to the following location in our web browser (substitute index.py with the name of your Python script):

    localhost:8000/cgi-bin/index.py

Our Python server-side, database-driven code

Now, let's create a Python code to provide dynamic web access to our SQLite database.

PySpatiaLite

The PySpatiaLite module provides dbapi2 access to SpatiaLite databases. Dbapi2 is a standard library for interacting with databases from Python. This is very fortunate because if you use the dbapi2 connector from the sqlite3 module alone, any query using spatial types or functions will fail. The sqlite3 module was not built to support SpatiaLite.

Add the following to the preceding code. This will perform the following functions:

  • Import the PySpatiaLite module and connect to our sqlite3/SpatiaLite database
  • Use the connection as a context manager, which automatically commits the executed queries and rolls back in case of an error
  • To test that the connection is working, use the SQLITE_VERSION() function in a SELECT query and print the result

The following code, appended to the preceding one, can be found at c6/data/web/cgi-bin/db_test.py. Make sure that the path in the code for the SQLite database file matches the actual location on your system.

# Import the pySpatiaLite module
from pySpatiaLite import dbapi2 as sqlite3
conn = sqlite3.connect('C:\packt\c6\data\web\cgi-bin\c6.sqlite')

# Use connection handler as context
with conn:    
  c = conn.cursor() 
  c.execute('SELECT SQLITE_VERSION()')

  data = c.fetchone()
  print data
  print 'SQLite version:{0}'.format(data[0])

You can view the following results in a web browser at http://localhost:8000/cgi-bin/db_test.py.

(u'3.7.17',) SQLite version:3.7.17

The first time that the data is printed, it is preceded by a u and wrapped in single quotes. This tells us that this is a unicode string (as our database uses unicode encoding). If we access element 0 in this string, we get a nonwrapped result.

The Python code for web access to SQLite through JSON

The following code performs the following functions:

  • It connects to the database
  • It issues a query to find the minimum distance location and field where the specified location and date are given
  • It returns JSON with field value pairs based on the database result field names and values

You can find the code at c6/data/web/cgi-bin/get_json.py:

#!/usr/bin/python

import cgi, cgitb, json, sys
from pySpatiaLite import dbapi2 as sqlite3

# Enables some debugging functionality
cgitb.enable()

# Creating row factory function so that we can get field names
# in dict returned from query
def dict_factory(cursor, row):
  d = {}
  for idx, col in enumerate(cursor.description):
    d[col[0]] = row[idx]
  return d

# Connect to DB and setup row_factory
conn = sqlite3.connect('C:\packt\c6\data\web\cgi-bin\c6.sqlite')
conn.row_factory = dict_factory

# Print json headers, so response type is recognized and correctly decoded
print 'Content-Type: application/json\n\n'

# Use CGI FieldStorage object to retrieve data passed by HTTP GET
# Using numeric datatype casts to eliminate special characters
fs = cgi.FieldStorage()
longitude = float(fs.getfirst('longitude'))
latitude = float(fs.getfirst('latitude'))
day = int(fs.getfirst('day'))

# Use user selected location and days to find nearest location (minimum distance) 
# and correct date column
query = 'SELECT pk, calc{2} as index_value, min(Distance(PointFromText(\'POINT ({0} {1})\'),geom)) as min_dist FROM vulnerability'.format(longitude, latitude, day)

# Use connection as context manager, output first/only result row as json
with conn:
  c = conn.cursor()
  c.execute(query)
  data = c.fetchone()
  print json.dumps(data)

You can test the preceding code by commenting out the portion that gets arguments from the HTTP request and setting these arbitrarily.

The full code is available at c6/data/web/cgi-bin/json_test.py:

# longitude = float(fs.getfirst('longitude'))
# latitude = float(fs.getfirst('latitude'))
# day = int(fs.getfirst('day'))
longitude = -75.28075
latitude = 39.47785
day = 15

If you browse to http://localhost:8000/cgi-bin/json_test.py, you'll see the literal JSON printed to the browser. You can also do the equivalent by browsing to the following URL, which includes these arguments: http://localhost:8000/cgi-bin/get_json.py?longitude=-75.28075&latitude= 39.47785&day=15.

{"pk": 260, "min_dist": 161.77454362713507, "index_value": 7}

The OpenLayers/jQuery client-side code

Now that our backend code and dependencies are all in place, it's time to move on to integrating this into our frontend interface.

Exporting the OpenLayers 3 map using QGIS

QGIS helps us get started on our project by allowing us to generate a working OpenLayers map with all the dependencies, basic HTML elements, and interaction event handler functionality. Of course, as with qgis2leaf, this can be extended to include the additional leveraging of the map project layers and interactivity elements.

The following steps will produce an OpenLayers 3 map that we will modify to produce our database-interactive map application:

  1. Start a new QGIS map or remove all the layers from the current one.
  2. Add delaware_boundary.shp to the map. Pan and zoom to the Delaware geographic boundary object if QGIS does not do so automatically.
  3. Convert the delaware_boundary polygon layer to lines by navigating to Vector | Geometry Tools | Polygons to lines. Nonfilled polygons are not supported by Export to OpenLayers. The following image shows these inputs populated:
    Exporting the OpenLayers 3 map using QGIS
  4. After you add the line boundaries, brighten them up and increase the size as well. Clicking on anything outside this boundary may not return a valid result. Rename the layer Delaware Boundary in the Layers panel.
  5. Install the Export to OpenLayers 3 plugin if it isn't already installed.
  6. Navigate to Web | Export to OpenLayers | Create OpenLayers Map.
  7. In the Export to OpenLayers 3 dialog, use the following parameter values (refer to the following image for clarification):
    • Ensure that your stylized line boundary for Delaware (which we created in step 4) is checked and visible with no popup. Otherwise, this might obscure the interaction that we will create.
    • Delete the unused fields. This is an important step, so uncheck this. Otherwise, you may see an error.
    • If you've already zoomed and panned your canvas to the Delaware Boundary layer, you can ignore the Extent parameter. Otherwise, set the extent parameter to Fit to Layers extent.
    • Max zoom level: 14.
    • Min zoom level: 8.
    • Select Restrict to extent.
    • Unselect Use layer scale dependent visibility.
    • Base layer: MapQuest, as shown in the following screenshot:
    Exporting the OpenLayers 3 map using QGIS

Modifying the exported OpenLayers 3 map application

Now that we have the base code and dependencies for our map application, we can move on to modifying the code so that it interacts with the backend, providing the desired information upon click interaction.

Remember that the backend script will respond to the selected date and location by finding the closest "regular point" and the calculated interpolated index for that date.

Adding an interactive HTML element

Add the following to c6/data/web/index.html in the body, just above the div#id element.

This is the HTML for the select element, which will pass a day. You would probably want to change the code here to scale with your application—this one is limited to days in a single month (and as it requires 10 days of retrospective data, it is limited to days from 6/10 to 6/30):

  <select id="day">
    <option value="10">2013-06-10</option>
    <option value="11">2013-06-11</option>
    <option value="12">2013-06-12</option>
    <option value="13">2013-06-13</option>
    <option value="14">2013-06-14</option>
    <option value="15">2013-06-15</option>
    <option value="16">2013-06-16</option>
    <option value="17">2013-06-17</option>
    <option value="18">2013-06-18</option>
    <option value="19">2013-06-19</option>
    <option value="20">2013-06-20</option>
    <option value="21">2013-06-21</option>
    <option value="22">2013-06-22</option>
    <option value="23">2013-06-23</option>
    <option value="24">2013-06-24</option>
    <option value="25">2013-06-25</option>
    <option value="26">2013-06-26</option>
    <option value="27">2013-06-27</option>
    <option value="28">2013-06-28</option>
    <option value="29">2013-06-29</option>
    <option value="30">2013-06-30</option>
  </select>

This element will then be accessed by jQuery using the div#id reference.

AJAX – the glue between frontend and backend

AJAX is a loose term applied specifically to an asynchronous interaction between client and server software using XML objects. This makes it possible to retrieve data from the server without the classic interaction of a submit button, which will take you to a page built on the result. Nowadays, AJAX is often used with JSON instead of XML to the same affect; it does not require a new page to be generated to catch the result from the server-side processing.

jQuery is a JavaScript library which provides many useful cross-browser utilities, particularly focusing on the DOM manipulation. One of the useful features that jQuery is known for is sending, receiving, and rendering results from AJAX calls. AJAX calls used to be possible from within OpenLayers; however, in OpenLayers 3, an external library is required. Fortunately for us, jQuery is included in the exported base OpenLayers 3 web application from QGIS.

Adding an AJAX call to the singleclick event handler

To add a jQuery AJAX call to our CGI script, add the following code to the "singleclick" event handler on SingleClick. This is our custom function that is triggered when a user clicks on the frontend map.

This AJAX call references the CGI script URL. The data object contains all the parameters that we wish to pass to the server. jQuery will take care of encoding the data object in a URL query string. Execute the following code:

jQuery.ajax({ 
  url: http://localhost:8000/cgi-bin/get_json.py,
  data: {"longitude": newCoord[0], "latitude": newCoord[1], "day": day}
})
Add a callback function to the jquery ajax call by inserting the following lines directly after it.
.done(function(response) {
popupText = 'Vulnerability Index (1=Least Vulnerable, 10=Most Vulnerable): ' + response.index_value;
Populating and triggering the popup from the callback function

Now, to get the script response to show in a popup after clicking, comment out the following lines:

/* var popupField;

  var currentFeature;
  var currentFeatureKeys;
  map.forEachFeatureAtPixel(pixel, function(feature, layer) {
    currentFeature = feature;
    currentFeatureKeys = currentFeature.getKeys();
    var field = popupLayers[layersList.indexOf(layer) - 1];
    if (field == NO_POPUP){
    }
    else if (field == ALL_FIELDS){
      for ( var i=0; i<currentFeatureKeys.length;i++) {
        if (currentFeatureKeys[i] != 'geometry') {
          popupField = currentFeatureKeys[i] + ': '+ currentFeature.get(currentFeatureKeys[i]);
          popupText = popupText + popupField+'<br>';
        }
      }
    }
    else{
      var value = feature.get(field);
      if (value){
        popupText = field + ': '+ value;
      }
    }
  }); */

Finally, copy and paste the portion that does the actual triggering of the popup in the .done callback function. The .done callback is triggered when the AJAX call returns a data response from the server (the data response is stored in the response object variable). Execute the following code:

.done(function(response) {
  popupText = 'Vulnerability Index (1=Least Vulnerable, 10=Most Vulnerable): ' + response.index_value;  
  if (popupText) {
    overlayPopup.setPosition(coord);
    content.innerHTML = popupText;
    container.style.display = 'block';
  } else {
    container.style.display = 'none';
   closer.blur();
  }

Testing the application

Now, the application should be complete. You will be able to view it in your browser at http://localhost:8000.

You will want to test by picking a date from the Select menu and clicking on different locations on the map. You will see something similar to the following image, showing a susceptibility score for any location on the map within the study extent (Delaware).

Testing the application