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

The cartographic rendering of geospatial data – MBTiles and UTFGrid

At this point, you may be wondering, what about the maps? So far, we have not included any geospatial data or visualization. We will be offloading some of the effort in managing and providing geospatial data and services to OpenStreetMap—our favorite public open source geospatial data repository!

Note

Why do we use OpenStreetMap?

  • OSM already provides mirrored map services for quick reproduction in the basemaps
  • OSM provides a very extensive and scalable schema for the kind of geographic features that you might find on a campus
  • Various web, mobile, and desktop clients have already been written to interact with the OSM API
  • OSM provides the databases and other infrastructure, so we don't have to
  • OSM has a granular and reliable way to track changes, using the osm_version and osm_user fields, which complement the osm_id unique ID field

OpenStreetMap to SpatiaLite

To use the OSM data, we need to get it in a format that will be interoperable with other GIS software components. A quick and powerful solution is to store the OSM data in a SQLite SpatiaLite database instance, which, if you remember, is a single file with full spatial and SQL functionality.

To use QGIS to download and convert OSM to SQLite, perform the following steps:

  1. Obtain the OSM data in the same way that we did in Chapter 4, Finding the Best Way to Get There. Use the OpenLayers plugin to zoom into Newark, DE (or use the extent, 39.7009, -75.7195, 39.6542, -75.7784, clockwise from the top of the dialog in the next step):
    1. Navigate to Vector | OpenStreetMap | Download Data to download the OSM data for this extent.
    OpenStreetMap to SpatiaLite
  2. Next, export the XML data in the .osm file to a topological SQLite database. This could potentially be used for routing; although, we will not be doing so here.
    1. Navigate to Vector | OpenStreetMap | Import Topology from XML.
    OpenStreetMap to SpatiaLite
  3. Next, export the topological data to normal geospatial data—polygons in this case.
    1. Navigate to Vector | OpenStreetMap | Export topology to SpatiaLite.
    2. Export type: Polygons (closed ways).
    3. Click on Load from DB to populate the list of fields in the data. Select the fields amenity, building, name, and leisure, as shown in the following screenshot, as fit allowed:
    OpenStreetMap to SpatiaLite
  4. Use DB Manager to display the university buildings.
    1. Navigate to Database | DB Manager | DB Manager.
    2. Highlight the c7 SQLite database.
    3. Execute the following query, ensuring that Load as new layer is selected:
      SELECT * FROM c7_polygons WHERE building = 'yes' and amenity = 'university'
    OpenStreetMap to SpatiaLite
  5. Export the query layer to c7/data/original/delaware-latest-3875/buildings.shp with the EPSG:3857 projection.

To tile and use UTFGrid with TileMill

Although TileMill is no longer under active production by its creator Mapbox, it is still useful for us to produce MBTiles tiled images rendered by Mapnik using CartoCSS and a UTFGrid interaction layer.

Preparing a basemap from OSM

TileMill requires that all the data be rendered and tiled together and, therefore, only supports vector data input, including JSON, shapefile, SpatiaLite, and PostGIS.

In the following steps, we will render a cartographically pleasing map as a .mbtiles (single-file-based) tile cache:

  1. Install and open TileMill.
  2. Download the Delaware data from the North America section of the Geofabrik OSM extracts site (http://download.geofabrik.de/north-america.html) as a shapefile. Alternatively, you can directly download it from http://download.geofabrik.de/north-america/us/delaware-latest.shp.zip. Ensure that you expand and copy the zip archive to your project directory after you've downloaded it.
  3. Reproject all the data from EPSG:4326 to :3875. If you remember, QGIS can do this in batch as with other Processing Toolbox algorithms, as you learned in Chapter 2, Identifying the Best Places, making this process a bit quicker.

    Output all the layers to c7/data/original/delaware-latest-3875.

    Preparing a basemap from OSM
  4. Copy the DC example to a new project.
    • You will find it in C:\Program Files (x86)\TileMill-v0.10.1\tilemill\examples\open-streets-dc
    • Copy it to C:\Users\[YOURUSERNAME]\Documents\MapBox\project\c7
  5. Delete all the files from the layers directory.
  6. Copy and extract all the shapefiles from c7/data/original/delaware-latest-3875 into the layers directory in the project directory of c7, which can be found at C:\Users\[YOURUSERNAME]\Documents\MapBox\project\c7\layers.
  7. Edit the project.mml file.
    1. Change all the instances of the open-streets-dc string to c7.
    2. Change the single instance of Open Streets, DC to c7.
    3. Substitute the following bounds and center:
        "bounds": [
          -75.7845,
          39.6586,
          -75.7187,
          39.71
        ],
        "center": [
          -75.7538,
          39.6827,
          14
      ],
    4. Change the following layer references to files:

      Land usages: Change this layer from osm-landusages.shp to landuse.shp

      ocean: Remove this layer or ignore

      water: Change this layer from osm-waterareas.shp to waterways.shp

      tunnels: Change this layer from osm-roads.shp to roads.shp

      roads: Change this layer from osm-roads.shp to roads.shp

      mainroads: Change this layer from osm-mainroads.shp to roads.shp

      motorways: Change this layer from osm-motorways.shp to roads.shp

      bridges: Change this layer from osm-roads.shp to roads.shp

      places: Change this layer from osm-places.shp to places.shp

      road-label: Change this layer from osm-roads.shp to roads.shp

  8. Open TileMill and select the c7 project from the Projects dialog, as shown in the following screenshot:
    Preparing a basemap from OSM

Preparing the operational layer in TileMill

  1. Open the Layers panel from the bottommost button in the bottom-left corner. Refer to the next image.
  2. Click on + Add layer.
  3. Populate the parameters with the following values:
    • ID: buildings.
    • Datasource: c7/data/original/delaware-latest-3875/buildings.shp.
    • Click on Save & Style. You can return to this dialog later by clicking on the Editor button (pencil icon) in the Layers panel, by the #c7 layer, as shown in the next image.
  4. If you don't yet see your layer, ensure that you have some style defined in the tab on the right that will be applied to the layer (this should be populated by default with a minimal style). Then, click on Save in the top-right corner.
  5. Use the CartoCSS syntax to change the style in style.mss. TileMill provides a color picker, which we can access by clicking on a swatch color at the bottom of the CartoCSS/style pane. After changing a color, you can view the hex code down there. Just pick a color, place the hex code in your CartoCSS, and save it. For example, consider the following code:
    #buildings {
      line-color:#eb8f65;
      line-width:0.5;
      polygon-opacity:1;
      polygon-fill:#fdedc9;
    }
  6. Click on Save (with the pencil icon) in the upper-right corner of the main screen (above the CartoCSS input) to view the changes, as shown in the following screenshot:
    Preparing the operational layer in TileMill
  7. Go to the Templates tab by clicking on the topmost button in the lower-left corner and change the Teaser and Full interaction types to use {{{id}}} from buildings, as shown in the following screenshot:
    Preparing the operational layer in TileMill

Exporting MBTiles

MBTiles is a format developed by Mapbox to store geographic information. There are two compelling aspects of this format, besides interaction with a small but impressive suite of software and services developed by Mapbox: firstly, MBTiles stores a whole tile store in a single file, which is easy to transfer and maintain and secondly, UTFGrid, which is the use of UTF characters for highly performant data interaction, is enabled by this format.

Uploading to Mapbox

Perform the following steps:

  1. Create an account on mapbox.com.
  2. Access the Export dialog from the Export button in the upper-right corner. Select Upload from this menu.
  3. Sign in to your Mapbox account by clicking on the button at the top of the dialog.
  4. Press Shift, click on it, and drag to define an extent in the map.
  5. Zoom to one level above your intended minimum zoom to preview the extent.
  6. Fill in the descriptive information in the export dialog.
    • Name: c7
    • Zoom: 11 to 16
  7. Click on the map to establish a Center coordinate.
  8. Select Save settings to project.
  9. Upload, as shown in the following screenshot:
    Uploading to Mapbox

The MBTiles file

The steps for exporting directly to an MBTiles file are similar to the previous procedure. This format can be uploaded to mapbox.com or served with software that supports the format, such as TileStream. Of course, no sign-on is needed.

The MBTiles file