GeoTIFF file creation with GDAL

From FSDeveloper Wiki
Jump to: navigation, search

Now that the FSX resample tool can also read GeoTIFF files, it has become very interesting to use GeoTIFF files for your photo scenery. The main advantage of using GeoTIFF files is that they contain the placement information of your photo, so that means you don't have to specify all those numbers in the INF file.

But what do you do when you don't have your images in the GeoTIFF format? Or when you have images that are not in the WGS84 projection required by resample? In those cases you need some tools to create the correct GeoTIFF files for you.

In this article I will describe how you can use the GDAL tools to perform such tasks. Of course there are also other GIS tools that can do such tasks for you. But I choose to write about GDAL as I am familiar with these tools from work. And another advantage is that GDAL is an open source project, so you can use the software for free.

Installing GDAL tools

The easiest way to get the GDAL tools is to install OSGeo4W. With this installer you can install multiple open source GIS tools and the GDAL tools are among them.

Another alternative is to install FwTools, but since this package is no longer updated, I would advice you to use OSGeo4W.

Starting the OSGeo4W shell

After you have installed OSGeo4W you can best start the "OSGeo4W shell". The installer should have placed a shortcut for this shell in your start menu. When you use this special shell, you are sure that the path is set correctly for all tools of the toolkit.

A very useful command is the gdalinfo command. When you use this on your image file, it will show all the information stored in the file. So this allows you to see if some geo information is already stored in your image.

 gdalinfo myfile.tif

This is an example of the output you can get when using gdalinfo on your image file:

 Driver: GTiff/GeoTIFF
 Size is 1600, 1200
 Coordinate System is:
 GEOGCS["WGS 84",
     DATUM["WGS_1984",
         SPHEROID["WGS 84",6378137,298.2572235630016,
             AUTHORITY["EPSG","7030"]],
         AUTHORITY["EPSG","6326"]],
     PRIMEM["Greenwich",0],
     UNIT["degree",0.0174532925199433],
     AUTHORITY["EPSG","4326"]]
 Origin = (2.873697280883789,51.239551252582089)
 Pixel Size = (0.000042915344238,-0.000026909535546)
 Metadata:
   AREA_OR_POINT=Area
   TIFFTAG_SOFTWARE=Adobe Photoshop CS2 Windows
   TIFFTAG_DATETIME=2007:03:03 09:28:16
   TIFFTAG_XRESOLUTION=96
   TIFFTAG_YRESOLUTION=96
   TIFFTAG_RESOLUTIONUNIT=2 (pixels/inch)
 Corner Coordinates:
 Upper Left  (   2.8736973,  51.2395513) (  2d52'25.31"E, 51d14'22.38"N)
 Lower Left  (   2.8736973,  51.2072598) (  2d52'25.31"E, 51d12'26.14"N)
 Upper Right (   2.9423618,  51.2395513) (  2d56'32.50"E, 51d14'22.38"N)
 Lower Right (   2.9423618,  51.2072598) (  2d56'32.50"E, 51d12'26.14"N)
 Center      (   2.9080296,  51.2234055) (  2d54'28.91"E, 51d13'24.26"N)
 Band 1 Block=1600x5 Type=Byte, ColorInterp=Red
 Band 2 Block=1600x5 Type=Byte, ColorInterp=Green
 Band 3 Block=1600x5 Type=Byte, ColorInterp=Blue

Adding positional information to an image

One of the most common tasks will be to add position information to an image and thus creating a GeoTIFF file in that way. Let's assume you have the following information about your image:

 [GEOGRAPHIC]
 North=51.2395125258209
 South=51.20725980992728
 West=2.873697280883789
 East=2.942361831665039

To add this information to you image, you need to use the following command:

 gdal_translate -a_srs "+proj=latlong +datum=WGS84" -of GTiff
   -co "INTERLEAVE=PIXEL" -a_ullr 2.873697280883789 51.23955125258209
   2.942361831665039 51.20725980992728 myfile.jpg myfile.tif

The INTERLEAVE=PIXEL option is required to make the GeoTIFF in a format that resample can process. By default FwTools makes them in a band interleaved format, but resample can not read those.

For the input image you can use many different formats, the GDAL tools can read most common image formats. For the output file we will select GeoTIFF of course in this case.

Reprojecting an image

But what to do if the image you have is not yet in the WGS84 projection that resample requires? Of course you can still use the gdal_translate command as shown before to add your positional information, but you need to add it in the projection system that your image uses. This could for example be UTM or some local projection system. This means that you need to specify a different projection after the -s_srs parameter.

Once you have done that, you can reproject your image to the WGS84 projection. You need to use the gdalwarp tool for that:

 gdalwarp  -of GTiff -co "INTERLEAVE=PIXEL"
   -s_srs "+proj=utm +zone=31 +datum=WGS84"
   -t_srs "+proj=latlong +datum=WGS84" -r cubic
   myfile.tif myfile_wgs84.tif

For the source projection you need to enter the projection you used with the gdal_translate command. In this example I showed the projection string for a UTM projection, but of course you need to use the projection that matches your image. The target projection should be WGS84, as that is what resample requires.

For the resampling I have specified that cubic sampling should be used. From the tests with all options I have done this one gives the best results, must better than the default near sampling.

Copying tags

Most programs that you can use to edit your imagery do not understand the geo related tags in the GeoTIFF file, so they will probably save your image back as a normal TIFF image. In that case you will have to insert the geo information again yourself. Of course you could provide the boundaries again as explained above, but you could also save the previous tags and just apply them again. To do this use the listgeo and geotifcp tools.

First you will need to save the current geo tags to a file on your harddisk. To do that we will be using the listgeo tool:

 listgeo myfile_wgs84.tif > myfile_wgs84.gtf

The GTF file is just plain ASCII, so you can open it with your text editor and view the information.

After you have edited your image in your painting program, all you have to do now is put the tags back into the TIFF file, so that it becomes a GeoTIFF again. We will use the geotiffcp tool for that:

 geotifcp -g myfile_wgs84.gtf myfile_geotiff.tif myfile_edited.tif

This will give you a new GeoTIFF file that can again be used by the resample tool to place your imagery in FS. So all you have to do before you start editing in your painting program is make sure yo have save the geo information to a GTF file, then you can always put it back later on.