EXPORT OVERVIEW (PYRAMID) LAYERS AS RASTERS

The memory of raster files may be from few KBs to many GBs. Overview or pyramid layers are very useful for rendering rasters' preview. In GDAL, gdaladdo tool is used for creating these overviews. The taste of overview preview can be experienced here.

In this blog, I will share a java code for exporting overviews of single band raster as separate rasters using gdal which act as a reduced sized images.

We assume that there are external/internal overviews are created, raster is in tif format and band data type is Byte.


package exoverview;

import org.gdal.gdal.*;
import org.gdal.gdalconst.gdalconstConstants;

public class ExportOverviews {
	
    public static void main(String[] args) {
		//Register GDAL
        gdal.AllRegister();
		//Input image
        String tif = "E:\\image.tif";
		//Save input folder location
        String loc = tif.substring(0, tif.lastIndexOf("\\"));
		//Save input image format
        String format = tif.substring(tif.lastIndexOf("."));
		//Open image in read only mode
        Dataset hDataSet = gdal.Open(tif, gdalconstConstants.GA_ReadOnly);
		//Save input image projection details
        String proj = hDataSet.GetProjection();
		//Get input image band
        Band b = hDataSet.GetRasterBand(1);
		//Get input image gdal driver
        Driver driver = hDataSet.GetDriver();
		//Save input image geotransform details
        double[] geo = hDataSet.GetGeoTransform();
        
        //Loop for each overview and export as separate dataset
        for(int x = 0; x<b.GetOverviewCount(); x++){
            //Get input image overview band no. x
            Band bx = b.GetOverview(x);
            //Get input image overview X and Y size
            int xSize = bx.GetXSize();
            int ySize = bx.GetYSize();
			//Create a byte array for storing overview band pixel values
            byte[] regularArrayIn = new byte[xSize*ySize];
            //Read overview band and save pixel values as regularArrayIn array
            bx.ReadRaster(0, 0, xSize, ySize, xSize, ySize, gdalconstConstants.GDT_Byte, regularArrayIn);
            //Create a blank single band raster with same dimension as input image overview no. x
            Dataset outDs = driver.Create(loc+"\\Overview"+x+format, xSize, ySize, 1,  gdalconstConstants.GDT_Byte);
            //provide same geo-information as the input raster
            double[] geos = geo;
            //Double raster pixel size in geotransform as the overview doubles every time
            geos[1] = geos[1]*2;
            geos[5] = geos[5]*2;
			//Save new geotransform in newly created image from overview
            outDs.SetGeoTransform(geos);
            //provide same projection as the input raster
            outDs.SetProjection(proj);
            //write pixel values from regularArrayOut array for each band
            outDs.WriteRaster(0, 0, xSize, ySize, xSize, ySize, gdalconstConstants.GDT_Byte, regularArrayIn, new int[]{1});
            outDs.FlushCache();
        }
        
    }
    
}

The above code is valid for 8-bit raster image which has byte radiometry. For Int16/UInt16/Float32/Float64 etc radiometry use appropriate gdalconstConstants for reading and writing rasters.

QUICK LINKS
Coordinates

Earth Shape

Ellipsoid & Geoid

Datum

Projection

UTM

Rasters

Vectors
GDAL

GDAL Setup

GDAL Rasters

GDAL Vectors

GDAL Tools
WMS

MS4W

Spatial DB

Oracle Spatial

SDO_GEOMETRY

Geo-Crawler
Web Clients

Openlayers

OL3

Closest Distance Map

Openlayers v2.x

ThreeJS

Terrain Viewer

Bluemarble (250m tiff)

JS Tools

Geographiclib

JS2Shapefile

CSV2Shape


GeoSpatialEarth.in Copyright © All rights reserved.