Raster data, like vector data, can be stored in numerous
formats. Some formats come from certain software requirements or from
certain satellite image sources. Some are more general while others are
better suited for more complex tasks. In this section, you will see how
to use the gdal_translate raster translation utility and its various parameters to
help you draw the most value out of an image.
Just as ogr2ogr has
several conversion options, so does gdal_translate. The syntax for the command
is slightly different but similar in concept. Here is the
usage:
gdal_translate <options> <input_image> <output_image>
The options available can be listed by running the command without any parameters, as in Example 7-5.
> gdal_translate
Usage: gdal_translate [--help-general]
[-ot {Byte/Int16/UInt16/UInt32/Int32/Float32/Float64/
CInt16/CInt32/CFloat32/CFloat64}] [-not_strict]
[-of format] [-b band] [-outsize xsize[%] ysize[%]]
[-scale [src_min src_max [dst_min dst_max]]]
[-srcwin xoff yoff xsize ysize] [-a_srs srs_def]
[-projwin ulx uly lrx lry] [-co "NAME=VALUE"]*
[-gcp pixel line easting northing]*
[-mo "META-TAG=VALUE"]* [-quiet]
src_dataset dst_dataset
GDAL 1.2.1.0, released 2004/06/23This first part of the output of Example 7-5 shows the various
options. Each item enclosed with brackets [] is optional. Without them, the program
simply converts one image to another, creating the output in GeoTIFF
format by default. Some options are used in the examples of the
following sections and in more depth in other chapters of this book.
Example 7-5 also shows
the software version number and the official release date of the GDAL
project.
Example 7-6 shows a
list of all available image formats. This list will vary depending on
the operating system and (only if you compiled it yourself) the
options you specified during the configuration process. This lists
many formats, though it may actually be able to read more image types.
The list shows only potential output formats that can be used with the
-of option. You supply this option
with the abbreviated name for that format as shown in Example 7-6. For the latest
capabilities, be sure to see the GDAL support formats page http://www.gdal.org/formats_list.html.
The following format drivers are configured and support output: VRT: Virtual Raster GTiff: GeoTIFF NITF: National Imagery Transmission Format HFA: Erdas Imagine Images (.img) ELAS: ELAS AAIGrid: Arc/Info ASCII Grid DTED: DTED Elevation Raster PNG: Portable Network Graphics JPEG: JPEG JFIF MEM: In Memory Raster GIF: Graphics Interchange Format (.gif) XPM: X11 PixMap Format BMP: MS Windows Device Independent Bitmap PCIDSK: PCIDSK Database File PNM: Portable Pixmap Format (netpbm) ENVI: ENVI .hdr Labelled EHdr: ESRI .hdr Labelled PAux: PCI .aux Labelled MFF: Atlantis MFF Raster MFF2: Atlantis MFF2 (HKV) Raster BT: VTP .bt (Binary Terrain) 1.3 Format JPEG2000: JPEG-2000 part 1 (ISO/IEC 15444-1) FIT: FIT Image USGSDEM: USGS Optional ASCII DEM (and CDED)
Keep in mind that any type of image format supported by GDAL can be translated, including digital camera photos or images from a web site. The most basic example of conversion, such as the following example, takes an image in one format and outputs it to another format.
>gdal_translate image1.png image1.tif>gdal_translate -of "PNG" image1.tif image1.png
Even though you specify the output file format, gdal_translate doesn’t automatically
create a filename suffix/extension for you. Remember to add your own
to the end of the filename.
An image can be translated back to the same format too, if creation of that format is supported by GDAL.
In Chapter 6,
gdalinfo was used to show the
details of a RADARSAT image of Canada. The image was a whopping 5,700
× 4,800-pixel image and more than 25 MB in size. Only special
image-handling programs like gdal-related tools can handle files that
large. This isn’t an image you would want to send to a friend as an
email attachment! Not only would the file size be a problem but your
friend probably could not use basic image viewers because it is a very
large GeoTIFF file.
The gdal_translate utility is
a handy tool for many things, one of which is reducing the size of an
image and outputting it to a more usable format for the project at
hand. In Example 7-7,
gdal_translate reduces the size of
the image to 5% and creates a small JPEG format file as output.
Running gdalinfo against this new
file shows that the resulting file is now a mere 285 × 240 pixels and
is a JPEG format image. The file size is now less than 20 KB. This is
easily viewable by the simplest image viewer on most personal
computers.
>gdal_translate -of "JPEG" -outsize 5% 5% canada_mosaic_lcc_1000m.tif can_radar.jpg>gdalinfo can_radar.jpgDriver: JPEG/JPEG JFIF Size is 285, 240
Figure 7-1 shows
what the can_radar.jpg image looks
like after Example 7-13.
gdal_translate can
also be used to specify only a portion of an image to be translated.
The result is a clipped portion of the image being put into a new
file. This is done using the -projwin option to specify geographic
coordinates of the area to clip out, or -srcwin if you know which pixels you want to
include. You can use OpenEV to preview the image and get an idea of
what the upper-left and lower-right coordinates are around your area
of interest. Then do the same type of translation as before, but
provide coordinates in the -projwin
parameter. Example 7-8
shows this kind of clipping as well as a conversion, all in one
command.
> gdal_translate -of "JPEG" -projwin -2281300 7464800 -1812300 7001800 canada_mosaic_lcc_1000m.tif can_radar_clip.jpg
Input file size is 5700, 4800
Computed -srcwin 318 3035 469 463 from projected window.Depending on the image program you use and the image source,
specifying coordinates for parameters like -projwin can be confusing. For some
applications you specify a rectangle using the lower-left coordinate
first and upper-right coordinates last. But in the previous example,
because the origin pixel is in the upper-left corner, upper-left and
lower-right coordinates must be used. If you mix them up, you will
get strange errors.
Figure 7-2 shows the resulting clipped satellite image.