REMOVE INTERNAL PYRAMID/OVERVIEW FROM RASTER

Pyramids or Overviews are created for fast rendering of images in low zoom levels. These pyramid layers can be created created externally or internally using GDAL command gdaladdo.

Creation of external pyramid results in the creation of an external file with same name as the image with .ovr or .aux (USE_RRD) extension. Whereas, creation of internal pyramids increases the size of the image as these pyramids are integrated with image. For example the following image is 305 MB and after creation of 8 pyramid levels the size increases to 408 MB. The main disadvantage of internal pyramids is that they cannot be removed from the image once created. Hence the external overviews are considered most suitable.

Multi-spectral image8 overview layers
A multi-spectral image
8 Pyramid layers

One simple solution for removing the internal pyramids from image is to copy full image to create a new image with same parameters except the pyramids. Following is the Java code for recreating image without pyramids. We assume that band data type of image is Byte.


package pan2mx;

public class RemovePyramid {
    public static void main(String[] args) {
        gdal.AllRegister();
        //String[] args = {"C:\\Users\\XYZ\\Desktop\\img\\mxpyramid.tif","C:\\Users\\XYZ\\Desktop\\img\\mxonly.tif"};
        if(args.length!=2){
            Usage();
        }
        //Read Input raster
        Dataset dataset = gdal.Open(args[0], gdalconstConstants.GA_ReadOnly);
		//Exit if unable to open image
        if (dataset==null){
            System.out.println ("Could not open image file");
            Usage();
        }
        //Count number of Raster Bands
        int cBand = dataset.GetRasterCount();
        //Create a iBandMap as array
        int[] iBandMap = new int[cBand];
        
        for(int b=0; b<cBand;){
            iBandMap[b] = ++b;
        }
        //Count number of pixels along and width and height of raster
        int rows = dataset.GetRasterXSize();
        int cols = dataset.GetRasterYSize();
		
		//Get raster projection
        String proj = dataset.GetProjection();
		        
        // Save the input raster geo-information
        double[] geo = dataset.GetGeoTransform();

		// Get driver of input raster for creating output raster
        Driver driver = dataset.GetDriver();
		
		//Count total number of input raster pixels
        int totalpixelperband = cols*rows;
		
        //Create a byte array for input raster pixel values
        byte[] regularArrayIn = new byte[cBand*totalpixelperband];
		
        //Read the input raster and save pixel values in byte arrayregularArrayIn for all bands
        dataset.ReadRaster(0, 0, rows, cols, rows, cols, gdalconstConstants.GDT_Byte, regularArrayIn, iBandMap);
		
        
        //Create a blank mxband band raster with same dimension as input raster
        Dataset outDs = driver.Create(args[1], rows, cols, cBand,  gdalconstConstants.GDT_Byte);
			
        //provide same geo-information as the input raster
        outDs.SetGeoTransform(geo);
			
        //provide same projection as the input raster
        outDs.SetProjection(proj);
			
        //write pixel values from regularArrayOut array into multi-spectral image
        outDs.WriteRaster(0, 0, rows, cols, rows, cols, gdalconstConstants.GDT_Byte, regularArrayIn, iBandMap);
        outDs.FlushCache();
        
      dataset.delete();
    }
    private static void Usage(){
        System.out.println ("Please provide input image location as first argument and saving image location as second argument/n"+
                    "eg: java -jar RemovePyramid.jar \"D:\\Area\\mxpyramid.tif\" \"D:\\OutputFolder\\onlymx.tif"");
            System.exit(1);
    }
    }

The above code is valid for raster image with 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.