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

标签 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.


      }
     }

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

最佳答案

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

关于java - 如何使用GDAL库在java中编写输出文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25522833/

相关文章:

c++ - ESRI shapefile 选择哪个 C++ 库?

java - 转换为 JAR 文件时未找到 UCanAccess 驱动程序

Java-rest 调用 : Population of value within variable should work for two variables name

java - 无法使用Spring MVC打开WEB-INF子文件夹中的jsp

ios - NSTask 或 iPhone 的同等版本

tomcat - tomcat 中的 native 库

python - 将 GeoTIFF 图像划分为规则网格并计算平均值

django-models - GeoDjango/GDAL : Wrong data field being stored

java - 如果某事为真则跳过几行

java - AWS S3 上传陷入 WAITING 状态