2
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.

4
  • Why is this tagged Python? Commented Aug 27, 2014 at 8:55
  • did you look over the API for the Java biding for GDAL? gdal.org/java Commented Aug 27, 2014 at 9:47
  • yeah, i wanted to write a output file in a loop because I am storing the values in Binary_pixels array. if i try with this band2.WriteRaster_Direct(0, 0, xsize, ysize, xsize, ysize, buf_Type, Binary_pixels); Data type is getting mismatch.... I dont know how to resolve it ... any suggestions ?? Commented Aug 27, 2014 at 10:18
  • Make your question more clear. How did you attempt to write the data, and what was the error (if any)? What about using the byte[] type? Also, for a boolean raster, you can use the creation option NBITS=1 to reduce file sizes (if that's an issue). Commented Aug 28, 2014 at 2:25

1 Answer 1

2

Look at this example.

    Dataset dataset = null;
    Driver driver = null;
    Band band = null;

    int xsize = 4000;
    int ysize = 400;

    driver = gdal.GetDriverByName("GTiff");

    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4 * xsize);
    byteBuffer.order(ByteOrder.nativeOrder());
    FloatBuffer floatBuffer = byteBuffer.asFloatBuffer();
    int[] intArray = new int[xsize];
    float[] floatArray = new float[xsize];

    dataset = driver.Create(filename, xsize, ysize, 1, gdalconst.GDT_Float32);
    band = dataset.GetRasterBand(1);

    for (int iter = 0; iter < nbIters; iter++)
    {
        if (method == METHOD_DBB)
        {
            for (int i = 0; i < ysize; i++)
            {
                for (int j = 0; j < xsize; j++)
                {
                    floatBuffer.put(j, (float) (i + j));
                }
                band.WriteRaster_Direct(0, i, xsize, 1, gdalconst.GDT_Float32, byteBuffer);
            }
        }
        else
        {
            for (int i = 0; i < ysize; i++)
            {
                for (int j = 0; j < xsize; j++)
                {
                    floatArray[j] = (float) (i + j);
                }
                band.WriteRaster(0, i, xsize, 1, floatArray);
            }
        }
    }

    dataset.delete();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.