Tutorial: Working with Rasters in QGIS - Mosaicing and Subseting

Tutorial moved to  http://www.qgistutorials.com/en/docs/raster_mosaicing_and_clipping.html


QGIS has basic raster functionality via the GdalTools plugin. This plugin is on by default and available via with ‘Raster’ menu. If you do not see the options under Raster menu, please follow this tutorial to find and enable the plugin.  In this tutorial, I will explain some basics of rasters captured by satellite sensors and show you how to mosaic and subset imagery within Quantum GIS.
The data we will be using is from NASA public domain NASA/GSFC, Rapid Response imagery. This satellite imagery is from the Moderate-resolution Imaging Spectroradiometer (MODIS) sensor onboard the Terra satellite. I downloaded several FAS 2km subset geotiffs in southern Africa and saved in on my hard drive.
  • Load these images in QGIS. Click on Layers → Add Raster Layer.
  • Browse to the directory with the individual images. Hold down the ‘SHIFT’ key and click on the image files to make a multiple selection. Click ‘Open’. You will see the images load up in the Table of Content on the left panel.
  • You will see there are 5 individual satellite images. Right click on any one of the images and click Properties.
  • In the Properties dialog, you can examine the details about the image in the General and Metadata tabs. Each raster can be displayed using upto 3 bands - one for each of Red, Green and Blue (RGB) channels. This particular image is a 3-6-7 band combination of the MODIS sensor. This means that the Band 3 is displayed in Red channel, Band 6 is displayed in Green channel and Band 7 is displayed in Blue channel. Since the bands 3-6-7 do not represent the real R G and B sensors, this image is called a False Color Composite (FCC). There is also a True Color version of these images available to download as well.
  • Click OK and return to the main QGIS window. Now let us create a single ‘Mosaic’ image from all these individual images. Click on Raster → Miscellaneous → Merge. This menu option uses a GDAL utility in the backend called ‘gdal_merge’.
  • Click Select next to Input files and browse to the directory containing all the individual geotiffs. Keep holding ‘SHIFT’ key and select all of them. Click OK. Next, click ‘Select’ next to Output file and name the output file as ‘mosaic.tif’. At the bottom, check the box next to ‘Load into canvas when finished’. Click OK. You will get a pop-up message saying ‘Processing complete’, once the mosaic is created and loaded to the Canvas. Click ‘Close’.
  • You will see that the individual images and now combined and mosaiced into a single mosaic.tif
  • Another Raster operation you can do is to subset or ‘crop’ an image. Let us create a subset of this mosaic. Click on Raster → Extraction → Clipper.
  • In the Clipper dialog, choose the ‘mosaic’ as the input file from the dropdown box. In the Output file, click Select and name the output file as ‘subset.tif’. Now we have to choose the exxtent of our subset by draing a selection in the canvas window.
  • Switch to the Canvas window and draw a rectangle selection in the image.
  • Now switch back to the Clipper dialog and you will see that the coordinate fields are populated based on your selection. If you need to subset using have specific bounding box, you can also manually enter the coordinates here. At the bottom, check the box next to ‘Load into canvas when finished’. Click OK.
  • Once the processing is complete, you will see a new layer named ‘subset’ loaded in the canvas. This is your subset image created in the last step.
  • Pro Tip: The Clipper process uses gdal_translate utility at the backend to carry out the subsetting task. If you look at the documentation of gdal_translate, there are lots of advanced options. If you wish to use any of these option in the above task, you can click ‘Edit’ button at the bottom of the Clipper window and enter the desired option by typing in. This will be useful if you want to have access to some advanced gdal_translate functionality.

26 comments :

  1. Fantastic tutorial! You're very clear! Any tips to turning a bunch of rasters into a pyramided mosaic that could be served out to another program?

    ReplyDelete
  2. Which program you had in mind? I am not sure if the qgis/gdal pyramids would work in Arc/ERDAS.

    ReplyDelete
  3. Can you tell me why my raster pull down only has the raster calculator option.

    ReplyDelete
  4. Hi. You must be missing the GdalTools plugin. Please fetch that plugin and enable it. Here's a tutorial on how to do that. http://qgis.spatialthoughts.com/2010/10/using-plugins-in-quantum-gis_05.html

    ReplyDelete
  5. Nice job. I've create a similar tutorial using Layer Stack for Landsat seven bands - http://t.co/Bgo8jg5A

    ReplyDelete
  6. Hi ,Ujaval Gandhi : I got some doubt regarding Qgis ... i am familier with Mapinfo . i got 30 shapefiles having location information and facility available at Health Centers , but when i open all the layers and click (i) identity button on every health center location only top layer is able to display attribute ..

    ReplyDelete
    Replies
    1. Hi Sandy. Good point. There is a setting that will allow you to identify across all shapefiles. By default the selection is only the top layer, but you can change it. Go to Settings -> Options -> Map Tools -> Identify and change the mode to 'Top Down'.

      Delete
  7. Is there a way to subset an image in an irregular shape( other than a rectangle or square) ?

    ReplyDelete
    Replies
    1. There is a 'Mask layer' option in Clipper tool that you can use. Here are the detailed instructions http://qgistips.spatialthoughts.com/2012/11/clip-raster-in-irregular-shape-using.html

      Delete
  8. Hello Ujaval,
    I'm trying to clipp a lot of rasters from big mosaic. I've done export of my frames (*.shp-masks) for as many files as many clipping shapes were inside (each shape has small common space with the neigbours to avoid diffrent float info). Do You know any automatic way to clipp it instead of clipping every single shp file?
    I'm not good in Python's things.
    Best regards!

    ReplyDelete
  9. nice one boss, the tutorial is really informative. but before i can do all that i have a peculiar problem. when i load my raster images they take too long to load. panning and zooming also take too long. is there anything i am not doing. ASAP

    ReplyDelete
    Replies
    1. You can right-click on the Raster layer, go to Properties. Under 'Pyramids' tab, build the pyramids. Once that is done, you should be able to Pan and Zoom much faster.

      Delete
  10. Does anybody know how to fill gaps in Landsat7 imagery using Qgis 1.8.0-Lisboa?

    ReplyDelete
    Replies
    1. You can use the Raster -> Analysis -> Fill Nodata tool to fill the gaps using QGIS.

      See this for more details http://gis.stackexchange.com/questions/30250/filling-gaps-in-landsat-etm-images-with-qgis

      Delete
  11. Hi, I am trying to see if it is possible to depict the growth of a city over a period of 10 years (or 20, depending on if imagery is available) using sat images? I am very new at this, so i would appreciate it if you could direct me.

    Thanks

    ReplyDelete
    Replies
    1. Hi Sirus, for the urban growth application that you are considering, Landsat imagery is very useful. Landsat data is free to download and goes back 30 years. Check out http://landsat.usgs.gov/Landsat_Search_and_Download.php

      Also, if you want to see some examples of what is possible with Landsat imagery, check out the Timelapse videos at http://earthengine.google.org/#intro

      Delete
  12. Hi Ujaval,
    I have a raster in ascii grid format and when I use the clipper, it outputs a file in an unreadable geotiff format. I need the clipped ascii grid file (to be read in php) for my application. I tried using Translate in Raster conversion but still results to a geotiff file. Any suggestion? Thanks.

    ReplyDelete
  13. Hi i'm getting an error like The -projwin option was used, but the geotransform is
    rotated. This configuration is not supported. what shall i do?Plz help me....

    ReplyDelete
  14. Hey Ujaval,

    Great job with tutorials. Not directly related to QGIS but I wanted to know how to combine landsat images with google earth images in either ERDAS/ArcMAP or eCognition. The problem with google earth images is that u can't see different bands that u see in landsat images. On the contrary google images are way better in terms of resolution compared to landsat one s. So I am trying to figure out to use both together. Any suggestions? ideas?

    Thanks! :)

    Tejas

    ReplyDelete
  15. Hi Ujaval,

    Good Tutorial.

    I am missing some concept when it comes to importing raster datasets. I have download and unzipped a DEM dataset from the USGS NED Clearinghouse. I can easily add this dataset in ArcGIS as a single file name appears in the Add File window. If I try in Qgis, I go to the Add Raster tool, a window opens and I see all of the individual files and two folders for the raster dataset. The two files have aux and rrd suffixes. The two folders contain files with adf, dat, nit, and dir suffixes. I've tried adding the aux and the rrd file. The aux opens as a single gray band file with no apparent attributes, and the rr file is not a supported format. Am I missing some pre-processing step, or is this dataset format not supported?

    ReplyDelete
    Replies
    1. Hi Rick, the file that you are working is Arc/Info Binary Grid Format. QGIS uses GDAL library to read raster data and it does have limited support of it. This link might help http://pvanb.wordpress.com/2010/03/15/esri-rasters-in-grass-or-qgis/

      Delete
    2. Ujaval,

      Thanks for the prompt reply. It got me half way through the process. I'm now hung up on the projection. My project shapefiles are all in the Michigan GeoRef (EPSG:3078 - NAD83 / Michigan Oblique Mercator), with units in meters (6 digits to left of decimal). The DEM raster files are in EPSG: 4269 - NAD83, and the units are decimal degrees (2 digits to left of decimal). In ArcGIS, I can import the raster and it does use the projection and coordinate system of the project so that the raster plots where it should. In Qgis, it's as if no projection information for the raster file is read, or recognized. The raster plots very near the origin, well to the lower left of my project shapefiles. I recognize that I need to transform the coordinates of the raster file so that it is spatially registered to my project. When I open the tool to "Set CRS of Layer(s)" a message tells me that there is no projection associated with the raster file and that I can accept the projection of the project, or define a projection. I've done it both ways with no positive results. I shouldn't have to georeference this raster should I?

      Delete
    3. As a follow-up, if I re-project the raster file from NAD83 to NAD83/Michigan Oblique Mercator within ArcGIS, and save the raster file as an img file, I can import the re-projected raster file (NAD83/Michigan Oblique Mercator) into Qgis with absolutley no problems, with the raster perfectly registered to the remaining vector shapefiles in my project. I could transform all of these rasters in ArcGIS and save them so that I can use them in Qgis, but I'd prefer to figure out whether it is possible to properly transform the data within Qgis. Eventually I will be losing access to ArcGIS.

      Delete
    4. You've run into a problem that has long frustrated QGIS users. As far as I know there is still not a clear way to deal with this projection in PROJ4 library (which QGIS uses). Try this link for more info on how to convert it to a supported projection using other open-source tools.

      http://www.townshipgis.com/resources/problems-and-workarounds/converting-michigan-georef-to-state-plane-using-open-source-tools

      Delete
  16. Hello ujaval gandhi plz help me..ur tutorial is useful a lot and easy too,i tried doin it this way but in the last when its finished...the mosaic file just show the first pic only,not combined properly at all..plz help me i have project

    ReplyDelete