EXPORT PANCHROMATIC TO MULTI-SPECTRAL RASTER

Mosaic is a process of merging multiple images into single images with full, some or none overlapping area. You would have experienced the problem of mosaicking two or more rasters having different number of bands. This is a very common problem of mosaicking panchromatic raster with multi-spectral one.

This problem is due to the fact that mosaic process merges rasters band-wise. If there is any band missing, then the process stops.

The solution was figured out when the panchromatic image is converted to multi-spectral. How is this possible? What will be the values of other bands? What if I copy the same single band into a new dataset the same number of times as the band count of multi-spectral raster. All the bands will have same value and image with still be visible in grey scale. Grey scale... Why? Think about it.

Multi-spectral rasterPanchromatic raster
A multi-spectral image
A Panchromatic image

Following is the Java code for converting panchromatic image to multi-spectral. We assume that band data type of panchromatic image is Byte.


package pan2mx;

public class Pan2MX3band {
    public static void main(String[] args) {
        gdal.AllRegister();
        //String[] args = {"C:\\Users\\XYZ\\Desktop\\img\\pan.tif","C:\\Users\\XYZ\\Desktop\\img\\", "3"};
        if(args.length<1 || args.length>2){
            Usage();
        }
        //Read Input raster
        Dataset dataset = gdal.Open(args[0], gdalconstConstants.GA_ReadOnly);
		//Save image format
        String imgFormat = args[0].substring(args[0].lastIndexOf("."));
		//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();
		//If input image is not panchromatic, then exit.
        if(cBand!=1){
            System.out.println ("Input image file is not panchromatic.");
            Usage();
        }
        //Number of bands required in multi-spectral image i.e, third argument
        int mxband = Integer.parseInt(args[2]);
		//Create a iBandMap as array
        int[] iBandMap = new int[mxband];
        
        for(int b=0; b<mxband;){
            iBandMap[b] = ++b;
        }
        //Count number of pixels along and width and height of raster
        int rows = dataset.GetRasterXSize();
        int cols = dataset.GetRasterYSize();
		
        //Count total number of input raster pixels
        int totalpixelperband = cols*rows;
		
        //Get raster projection
        String proj = dataset.GetProjection();
		
        //Create a byte array for input raster pixel values
        byte[] regularArrayIn = new byte[totalpixelperband];
		
        //Create a byte array to save the pixel values of each band of input raster
        byte[] regularArrayOut = new byte[mxband*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, new int[] {1});
		
        // Save the input raster geo-information
        double[] geo = dataset.GetGeoTransform();

        //Save pixel values of raster from arrayregularArrayIn to each regularArrayOut array
        for(int i=0; i<mxband; i++){
            for (int j=0; j<regularArrayIn.length; j++){
                    regularArrayOut[(regularArrayIn.length*i)+j] = regularArrayIn[j];
            }
        }
        // Get driver of input raster for creating output raster
        Driver driver = dataset.GetDriver();
		
        //for each regularArrayOut array, create a multi-spectral raster file using the above driver
        
            //Create a blank mxband band raster with same dimension as input raster
            Dataset outDs = driver.Create(args[1]+"pan2mx."+imgFormat, rows, cols, 3,  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, regularArrayOut, iBandMap);
            outDs.FlushCache();
        
     dataset.delete();
    }
    private static void Usage(){
        System.out.println ("Please provide input pan image location as first argument, saving location as second argument and number of bands required as third/n"+
                    "eg: java -jar Pan2MX3band.jar \"D:\\Area\\Clipped.tif\" \"D:\\OutputFolder\" 3");
            System.exit(1);
    }
    }

Mosaicked raster
A Mosaic of Panchromatic with multi-spectral image

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.