Reprojecting a raster

A core requirement for all geospatial analysis is the ability to change the map projection of data in order to allow different layers to be open on the same map. Reprojection can be challenging, but QGIS makes it a snap of the fingers. Starting with this recipe, we will begin using the powerful QGIS Processing Toolbox. The Processing Toolbox wraps over 600 algorithms into a highly consistent API, available to Python and also as interactive tools. This toolbox was originally a third-party plugin named SEXTANTE, but is now a standard plugin distributed with QGIS.

Getting ready

As with many recipes in this chapter, we will use the SatImage raster available at https://geospatialpython.googlecode.com/files/SatImage.zip.

Place this raster in your /qgis_data/rasters directory.

How to do it...

In this recipe, we will use the gdal warp algorithm of the processing module to reproject our image from EPSG 4326 to 3722. To do this, we need to perform the following steps:

  1. Start QGIS.
  2. From the Plugins menu, select Python Console.
  3. The first line of code is used to import the processing module:
    import processing
    
  4. Next, we load our raster layer and validate it:
    rasterLyr = QgsRasterLayer("/qgis_data/rasters/SatImage.tif", "Reproject")
    rasterLyr.isValid()
    
  5. Finally, we run the gdal warp algorithm by inserting the correct parameters, including the layer reference, current projection, desired projection, None for changes to the resolution, 0 to represent nearest neighbor resampling, None for additional parameters, 0 –Byte output raster data type (1 for int16), and an output name for the reprojected image:
    processing.runalg("gdalogr:warpreproject", rasterLyr, "EPSG:4326", "EPSG:3722", None, 0, None, "/0, qgis_data/rasters/warped.tif")
    
  6. Verify that the output image, warped.tif, was properly created in the filesystem.

How it works...

The Processing Toolbox is essentially a wrapper for command-line tools. However, unlike the tools it accesses, the toolbox provides a consistent and mostly predictable API. Users familiar with Esri's ArcGIS ArcToolbox will find this approach familiar. Besides consistency, the toolbox adds additional validation of parameters and logging, making these tools more user friendly. It is important to remember that you must explicitly import the processing module. PyQGIS automatically loads the QGIS API, but this module is not yet included. Remember that it was a third-party plugin until fairly recently.

There's more...

The runalg() method, short for the run algorithm, is the most common way to run processing commands. There are other processing methods that you can use though. If you want to load the output of your command straight into QGIS, you can swap runalg() for the runandload() method. All arguments to the method remain the same. You can also get a list of processing algorithms with descriptions by running processing.alglist(). For any given algorithm, you can run the alghelp() command to see the types of input it requires, such as processing.alghelp("gdalogr:warpproject"). You can also write your own processing scripts based on combinations of algorithms and add them to the processing toolbox. There is also a visual modeler for chaining processing commands together.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset