public class Learn {
public static String getFilename(){
String strFilename = "";
Scanner scnr = new Scanner(System.in);
System.out.print("Enter the file path: ");
strFilename = scnr.next();
return strFilename;
}
public static void main(String[] args) {
gdal.AllRegister();
Dataset inputdata = gdal.Open(getFilename(), gdalconstConstants.GA_ReadOnly);
Dataset dataset = null;
Driver driver = null;
driver = gdal.GetDriverByName("GTiff");
driver.Register();
Band band2=null;
Band poBand = null;
int xsize = inputdata.getRasterXSize();
int ysize = inputdata.getRasterYSize();
int bandCount =inputdata.GetRasterCount();
int pixels = xsize*ysize;
int buf_Type = 0, buf_Size = 0, buf_xSize = 0,buf_ySize = 0;
int[] intArray = new int[pixels];
ByteBuffer[] bands = new ByteBuffer[bandCount];
String filename_out = getFilename();
System.out.println(filename_out+" "+xsize+" "+ysize+" ");
dataset = driver.Create(filename_out, xsize, ysize, 1, gdalconst.GDT_Byte);
dataset.SetGeoTransform(inputdata.GetGeoTransform());
dataset.SetProjection(inputdata.GetProjection());
band2 = dataset.GetRasterBand(1); // writable band
for (int band=0; band<bandCount; band++){
poBand = inputdata.GetRasterBand(band+1);
buf_Type = poBand.getDataType();
buf_Size = pixels * gdal.GetDataTypeSize(buf_Type)/8;
buf_xSize = xsize*gdal.GetDataTypeSize(xsize)/8;
buf_ySize = ysize*gdal.GetDataTypeSize(ysize)/8;
System.out.println(buf_Type+","+gdal.GetDataTypeName(poBand.getDataType()));
ByteBuffer data = ByteBuffer.allocateDirect(buf_Size);
data.order(ByteOrder.nativeOrder());
// reading data into "data" buffer
poBand.ReadRaster_Direct(0, 0, poBand.getXSize(), poBand.getYSize(), xsize, ysize, buf_Type, data);
bands[band] = data;
}
//generating indices;
float[] NDVI= new float[xsize*ysize];
Byte[] Binary_pixels= new Byte[xsize*ysize];
for (int i=0; i< xsize*ysize; i++)
{
int Red = bands[3].get(i) & 0xFF;
int NIR = bands[4].get(i) & 0xFF;
//NDVI
if ((NIR+Red) !=0){
NDVI[i]= (float)(NIR-Red)/(NIR+Red);
// System.out.println("NDVI: " + NDVI[i]);
}
else {
NDVI[i]=0;
// System.out.println("NDVI: " + NDVI[i]);
if (NDVI[i] > 0.3 ){
Binary_pixels[i]= 1;
// System.out.println("Binary=1");
}
else{
Binary_pixels[i]=0;
// System.out.println("Binary = 0");
}
}
// writing data into band2.
// Here I want to write a raster file using the data Binary_pixels[] as a raster file with the same projection and Transformations as the input file.
// here my file is ".tif" file with 4 bands in it.
}
}
here am new to java coding that too using a GDAL library for Remote sensing image processing. need some help to write an image with same dimensions of input image and projection & Transforms. Thanks in advance.
byte[]
type? Also, for a boolean raster, you can use the creation optionNBITS=1
to reduce file sizes (if that's an issue). – Mike T Aug 28 '14 at 2:25