Update:
As of GDAL 2.1, you can use the gdal.Translate()
function and pass the bands in the bandList
argument (list of arguments).
from osgeo import gdal
infn='in.tif'
outfn='out.tif'
gdal.Translate(outfn, infn, bandList=[1,2,3], **other_kwargs)
Original answer:
gdal_translate
does this by building a VRT from scratch, copying all the metadata from the source dataset (unless everything matches, then it just uses CreateCopy).
You can do this in python but it's a bit fiddly. An easier way is use the VRT driver to create a VRT copy, then modify the VRT XML to remove the bands you aren't interested in before writing the modified VRT out to new file. See example below:
from osgeo import gdal
from lxml import etree
import os,sys
def read_vsimem(fn):
'''Read GDAL vsimem files'''
vsifile = gdal.VSIFOpenL(fn,'r')
gdal.VSIFSeekL(vsifile, 0, 2)
vsileng = gdal.VSIFTellL(vsifile)
gdal.VSIFSeekL(vsifile, 0, 0)
return gdal.VSIFReadL(1, vsileng, vsifile)
def write_vsimem(fn,data):
'''Write GDAL vsimem files'''
vsifile = gdal.VSIFOpenL(fn,'w')
size = len(data)
gdal.VSIFWriteL(data, 1, size, vsifile)
return gdal.VSIFCloseL(vsifile)
infn='test.tif'
outfn='test2.tif'
#List of bands to retain, output bands will be reordered to match
bands=[4,3,2]
ds = gdal.Open(os.path.abspath(infn)) #Ensure path is absolute not relative path
vrtdrv=gdal.GetDriverByName('VRT')
tifdrv=gdal.GetDriverByName('GTIFF')
#Create an in memory VRT copy of the input raster
vfn='/vsimem/test.vrt'
vrtds=vrtdrv.CreateCopy(vfn,ds)
#Read the XML from memory and parse it
#Could also write the VRT to a temp file on disk instead of /vsimem
#and used etree.parse(vrtfilepath) instead
#i.e.
#vfn='some/path/on/disk/blah.vrt'
#vrtds=vrtdrv.CreateCopy(vfn,ds)
#tree=etree.parse(vfn)
#os.unlink(vfn)
vrtds=vrtdrv.CreateCopy(vfn,ds)
vrtxml=read_vsimem(vfn)
tree=etree.fromstring(vrtxml)
#Ignore bands not in band list
#And put bands we want to keep in the correct order
for band in tree.findall('VRTRasterBand'):
try:
i=bands.index(int(band.attrib['band']))
band.attrib['band']=str(i+1)
bands[i]=band
except ValueError:pass
finally:
tree.remove(band)
for band in bands:
tree.append(band)
#Get the XML string from the tree
vrtxml=etree.tostring(tree, pretty_print=True)
#Write it to the in memory file
#Could also just write to a file on disk
write_vsimem(vfn,vrtxml)
#Open the VRT containing only the selected bands
vrtds=gdal.Open(vfn)
#Write to a new raster
tifds=tifdrv.CreateCopy(outfn,vrtds, 0, ['COMPRESS=JPEG','PHOTOMETRIC=YCBCR','TFW=YES'])
#Close dataset to ensure cache is properly flushed
del tifds
Best Answer
What is GDAL?
GDAL (Geospatial Data Abstraction Library) is a library for reading and processing raster and vector geographical data. It's the backbone of many GIS software, including ArcGIS and QGIS. In fact, there is a GDAL Package for R, in which you have access to many of the GDAL functionalities.
GDAL is written in a programming language called C++, which is great for building systems and big software, but not so practical when dealing with day-to-day analysis. Hence, to facilitate usage of the GDAL tools, there are bindings to other platforms which are better suited to simpler tasks. The aforementioned R-GDAL package is one such binding.
To further facilitate usage, there are also special programs called GDAL Utilities, which are basically specific tools built with GDAL for doing specific tasks. Many GDAL installations (such as Tamas Szekeres's binaries) already come with these, so you don't really have to do anything else.
How to use the GDAL Utilities?
You can call them directly from a command-line console, such as CMD in Windows or Terminal in Linux/Mac. Just pop the console open, type the name of the utility, and you'll be using it. As an example, try using the simplest of utilities, gdalinfo. Navigate to a folder you have a raster, say the raster is called "my_raster.tiff", then just type:
You'll see printed on the console all sorts of metadata on that raster, including Spatial Reference System, extension, statistics, etc. Different utilities may require different flags, which are those letters following a slash, such as -co, -r, or --config in your examples. These allow you to configure special behaviour to the utility. But they all work the same way, you call them directly from the command line.
Taking another example, from one you have posted: say you want to create a compressed and tiled version of a geotiff you have (let's say the same from above), and call it "my_compressed_raster.tiff". You'd use gdal_translate, which is the utility for converting raster into other types of raster. Then you'd go:
It'd go beyond the scope of this answer to specify the behaviour of each utility, as well as its many options, but hopefully this give you an idea of what's going on. You can always research it more in the GDAL main website.
UPDATE
To use the utilities from anywhere, you need first set a few system variables. In Windows, you go to Control Panel -> System -> Advanced System Settings -> Environment Variables (shortcut: Windows key + Break).
Then, in the System Variables frame, you should see a variable called Path. Click on it and edit it, adding a new path:
Or wherever else your gdal install would be. Then you'll be able to call GDAL utilities from any folder in the cmd.