如何使用GDAL库在java中编写输出文件?

时间:2014-08-27 08:53:00

标签: java gdal

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.


      }
     }

这里也是使用GDAL库进行遥感图像处理的java编码的新手。需要一些帮助来编写具有相同尺寸的输入图像和投影的图像。变换。 提前谢谢。

1 个答案:

答案 0 :(得分:2)

看看这个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();