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

Chapter 2. Identifying the Best Places

In this chapter, we will take a look at how the raster data can be analyzed, enhanced, and used for map production. Specifically, you will learn to produce a grid of the suitable locations based on the criteria values in other grids using raster analysis and map algebra. Then, using the grid, we will produce a simple click-based map. The end result will be a site suitability web application with click-based discovery capabilities. We'll be looking at the suitability for the farmland preservation selection.

In this chapter, we will cover the following topics:

  • Vector data ETL for raster analysis
  • Batch processing
  • Raster analysis concepts
  • Map algebra
  • Additive modeling
  • Proximity analysis
  • Raster data ETL for vector publication
  • Leaflet map application publication with qgis2leaf

Vector data – Extract, Transform, and Load

Our suitability analysis uses map algebra and criteria grids to give us a single value for the suitability for some activity in every place. This requires that the data be expressed in the raster (grid) format. So, let's perform the other necessary ETL steps and then convert our vector data to raster.

We will perform the following actions:

  • Ensure that our data has identical spatial reference systems. For example, we may be using a layer of the roads maintained by the state department of transportation and a layer of land use maintained by the department of natural resources. These layers must have identical spatial reference systems or be transformed to have identical systems.
  • Extract geographic objects according to their classes as defined in some attribute table field if we want to operate on them while they're still in the vector form.
  • If no further analysis is necessary, convert to raster.

Loading data and establishing the CRS conformity

It is important for the layers in this project to be transformed or projected into the same geographic or projected coordinate system. This is necessary for an accurate analysis and for publication to the web formats. Perform the following steps for this:

  1. Disable 'on the fly' projection if it is turned on. Otherwise, 'on the fly' will automatically project your data again to display it with the layers that are already in the Canvas.
    1. Navigate to Settings | Options and configure the settings shown in the following screenshot:
    Loading data and establishing the CRS conformity
  2. Add the project layers:
    1. Navigate to Layer | Add Layer | Vector Layer.
    2. Add the following layers from within c2/data/original.

      Applicants

      County

      Easements

      Land use

      Roads

      Tip

      You can select multiple layers to add by pressing Shift and clicking on the contiguous files or pressing Ctrl and clicking on the noncontiguous files.

  3. Import the Digital Elevation Model from c2/data/original/dem/dem.tif.
    1. Navigate to Layer | Add Layer | Raster Layer.
    2. From the dem directory, select dem.tif and then click on Open.
  4. Even though the layers are in a different CRS, QGIS does not warn us in this case. You must discover the issue by checking each layer individually. Check the CRS of the county layer and one other layer:
    1. Highlight the county layer in the Layers panel.
    2. Navigate to Layer | Properties.
    3. The CRS is displayed under the General tab in the Coordinate reference system section:
    Loading data and establishing the CRS conformity

    Note

    Note that the county layer is in EPSG: 26957, while the others are in EPSG: 2776.

  5. Follow the steps in Chapter 1, Exploring Places – from Concept to Interface, for transformation and projection. We will transform the county layer from EPSG:26957 to EPSG:2776.
    1. Navigate to Layer | Save as | Select CRS.

    Note

    We will save all the output from this chapter in c2/data/output.

To prepare the layers for conversion to raster, we will add a new generic column to all the layers populated with the number 1. This will be translated to a Boolean type raster, where the presence of the object that the raster represents (for example, roads) is indicated by a cell of 1 and all others with a zero. Follow these steps for the applicants, easements, and roads:

  1. Navigate to Layer | Toggle Editing.
  2. Then, navigate to Layer | Open Attribute Table.
  3. Add a column with the button at the top of the Attribute table dialog.
  4. Use value as the name for the new column and the following data format options:
    Loading data and establishing the CRS conformity
  5. Select the new column from the dropdown in the Attribute table and enter 1 into the value box:
    Loading data and establishing the CRS conformity
  6. Click on Update All.
  7. Navigate to Layer | Toggle Editing.
  8. Finally, save.

The extracting (filtering) features

Let's suppose that our criteria includes only a subset of the features in our roads layer—major unlimited access roads (but not freeways), a subset of the features as determined by a classification code (CFCC). To temporarily extract this subset, we will do a layer query by performing the following steps:

  1. Filter the major roads from the roads layer.
    1. Highlight the roads layer.
    2. Navigate to Layer | Query.
    3. Double-click on CFCC to add it to the expression.
    4. Click on the = operator to add to the expression
    5. Under the Values section, click on All to view all the unique values in the CFCC field.
    6. Double-click on A21 to add this to the expression.
    7. Do this for all the codes less than A36. Include A63 for highway on-ramps.
    8. Your selection code will look similar to this:
      "CFCC"  =  'A21'  OR  "CFCC"  =  'A25'  OR  "CFCC"  =  'A31'  OR  "CFCC"  =  'A35'  OR  "CFCC"  =  'A63'
    9. Click on OK, as shown in the following screenshot:
    The extracting (filtering) features
  2. Save the roads layer as a new layer with only the selected features (major_roads) in c2/data/output.

    Tip

    To clear a layer filter, return to the query dialog on the applied layer (highlight it in the Layers pane; navigate to Layer | Query and click on Clear).

  3. Repeat these steps for the developed (LULC1 = 1) and agriculture (LULC1 = 2) land uses (separately) from the landuse layer.

Converting to raster

In this section, we will convert all the needed vector layers to raster. We will be doing this in batch, which will allow us to repeat the same operation many times over multiple layers.

Doing more at once—working in batch

The QGIS Processing Framework provides capabilities to run the same operation many times on different data. This is called batch processing. A batch process is invoked from an operation's context menu in the Processing Toolbox. The batch dialog requires that the parameters for each layer be populated for every iteration. Perform the following steps:

  1. Convert the vector layers to raster.
    1. Navigate to Processing Toolbox.
    2. Select Advanced Interface from the dropdown at the bottom of Processing Toolbox (if it is not selected, it will show as Simple Interface).
    3. Type rasterize to search for the Rasterize tool.
    4. Right-click on the Rasterize tool and select Execute as batch process:
      Doing more at once—working in batch
    5. Fill in the Batch Processing dialog, making sure to specify the parameters as follows:

      Parameter

      Value

      Input layer

      (For example, roads)

      Attribute field

      value

      Output raster size

      Output resolution in map units per pixel

      Horizontal

      30

      Vertical

      30

      Raster type

      Int16

      Output layer

      (For example, roads)

      The following images show how this will look in QGIS:

      Doing more at once—working in batch
    6. Scroll to the right to complete the entry of parameter values.
    Doing more at once—working in batch
  2. Organize the new layers (optional step).
    • Batch sometimes gives unfriendly names based on some bug in the dialog box.
    • Change the layer names by doing the following for each layer created by batch:
      1. Highlight the layer.
      2. Navigate to Layer | Properties.
      3. Change the layer name to the name of the vector layer from which this was created (for example, applicants). You should be able to find a hint for this value in the layer properties in the layer source (name of the .tif file).
      4. Group the layers:

        Press Shift + click on all the layers created by batch and the previous roads raster, in the Layers panel.

        Right-click on the selected layers and click on Group selected.