Using QGIS (4) – Raster Images

September 26th, 2007

Raster Image Sample

Geographic data can be separated into 2 types: vector and raster data. We’ve already introduced vector data (points, lines, and polygons) in our previous posts on QGIS. We now need to briefly discuss raster images and how to use them.

In terms of GIS, raster images (often referred to as just “rasters”) are pixel-based images of places on the earth. They are often captured from cameras on satellites or airplanes and the pictures can be taken in color, black and white, or include infrared wavelengths.

It should be noted that there are numerous methods for creating rasters from non-aerial sources, including spatial interpolation and vector-to-raster conversions.

What is a World File?

Raster images by themselves are are not very useful unless you know where on the earth’s surface it was taken. So most raster files have an accompanying world file that describes both where the image is located and the geographic size of each pixel. A quick demonstration will hopefully illustrate how the world file is used.

First, let’s return to our countries of the world map we setup in QGIS in the last post. The countries symbology was changed to outline each polygon in black and have a transparent interior (select the “No Fill” option in the Fill Patterns box in the layer properties menu).

Countries of the World

What if we wanted to add a raster image of the world to create a better looking map? Fortunately, we already have a raster image we used in previous projects that was downloaded from the Wikipedia Commons. We downloaded the image, resized it to 600×300 and exported it to a TIFF file [click here to download the revised image].

In QGIS, a raster can be added by going to the Layer…Add a Raster Layer menu option so if you were to add the world raster without a world file, this is what happens:

Raster Misaligned in QGIS

Creating the World File

To align the image correctly, we need to create an accompanying world file using a text editor. Create a blank text file with the same name as the image except save it with the “.tfw” file extension (short for tiff world file).

The world file needs 6 lines, each with a specific piece of information (see Wikipedia’s entry or ESRI’s support page for more information).

Line 1: Pixel size in X direction
Line 2: Y-axis rotation
Line 3: X-axis rotation
Line 4: Negative of the Pixel size in Y direction
Line 5: X coordinate of the CENTER of upper-left most pixel
Line 6: Y coordinate of the CENTER of upper-left most pixel

Determining those 6 values requires us to know some information beforehand about our raster image. First, the image coordinates are in longitude (X) and latitude (Y). And since it covers the entire world, we know that in the X direction, there are 360 degrees (-180 to 180) and 180 degrees (-90 to 90) in the Y direction. Mapping this out looks like this:

World Map with Longitude and Latitude

Remembering our image is 600 pixels wide by 300 pixels high, we can now solve for the 6 world file lines.

Line 1: Pixel size in X direction =
        360 degrees / 600 pixels = 0.6 degrees per pixel
Line 2: Y-axis rotation = 0
Line 3: X-axis rotation = 0
Line 4: Negative of the pixel size in Y direction =
        180 degrees / 300 pixels = -0.6 degrees per pixel

It is important to note that lines 5 and 6 ask for the CENTER of the pixel. The upper-left corner of the image is (-180, 90) but we need to know the center of that pixel so a slight calculation needs to occur. Since we already calculated the geographic size of each pixel in lines 1 and 4, we just need to add or subtract half of those values.

Line 5: X coordinate of center of upper-left pixel =
        -180 degrees + (0.6  / 2) = -179.7
Line 6: Y coordinate of center of upper-left pixel =
        90 degrees - (0.6 / 2) = 89.7

So our finished world file looks like this:

0.6
0.0
0.0
-0.6
-179.7
89.7

Click here to download the world file.

Adding the Raster in QGIS

Now with both a raster image and a valid world file in the same directory on your hard drive, remove the misaligned raster and re-add it. QGIS (and most other GIS programs) will automatically locate the world file and perform the necessary adjustments to display the image correctly in the map.

Raster aligned in QGIS

The good news is almost any raster you download will come with a world file so it’s rare you will ever have to actually create one yourself. It is important to know what world files are, how they are named, and the concepts behind the calculations.

World Files and Map Projections

World files are closely related to important concept in GIS called projections. Projections can be confusing and at times difficult to grasp to those less familiar with geography and GIS. All our of examples at Spatial Horizons have used the “Equirectangular projection.” Also called the “geographic projection”, this projection treats longitude and latitude values simply as X and Y, making mapping simple and easy to understand. The trade-off to using this most basic of projections is the distortions produced make distance measurements and area calculations inaccurate. We may address projections in more detail in a future post.

Continue to part 5