[Gdal-dev] How to extract sub-image

Frank Warmerdam warmerdam at pobox.com
Tue May 25 17:32:10 EDT 2004


Clay, Bruce wrote:
> I know how to open a file and read the bands and thus the data so I can
> get data for a sub-image but I don't see how to create a file of the
> same format that is a particular sub-image of the overall image.
> Actually this would be a subset of GTOPO30 data but I believe that is
> treated the same as an actual image.
> 
> Since this seems to be a very common function, I was wondering if there
> isn't already a tool written to do this.  That is a not Python tool.

Bruce,

The gdal_translate utility can produce subsets of the source image.  You
can supply the source window in pixel/line coordinates or in georeferenced
coordinates.   In pixel line you might do something like this to take a
300x300 chunk out of gtopo.dat starting a line 600 and 500 pixels in from
the left:

   gdal_translate -srcwin 500 600 300 300 gtopo.dat  subset.tif

Use -projwin to do then same but with upper left and lower right projected
coordinates instead.

Note that gdal_translate implements this by creating an in-memory virtual
file referring to the desired areas of the source image.

> The other thing I have been trying to do is to create a single geotiff
> color image or sub-image from three separate geotiff images treating
> each one as one of R, G or B as would be the case for creating a
> composite color image from landsat data.  This function also seems like
> it would already exist and I have asked before but I wanted to ask one
> more time before I start writing one.  Again it needs to be a non-python
> method to be able to build it into a larger program and because I don't
> know python.  When I try to run the python scripts in the distribution I
> get errors that I don't relate to.  I tried to use MicroDem to do this
> but it created a grey scale image not a color one

It can be done interactively using the new composite tool Andrey wrote in
OpenEV.  But if you want to incorporate it into a program you would need to
reimplement.  Once again, it is creating a virtual file image in memory that
points off to the red, green and blue input rasters.   I would suggest you read
over the virtual file tutorial:

   http://www.remotesensing.org/gdal/gdal_vrttut.html

Best regards,
-- 
---------------------------------------+--------------------------------------
I set the clouds in motion - turn up   | Frank Warmerdam, warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | Geospatial Programmer for Rent




More information about the Gdal-dev mailing list