说明:
功能如题所示。
geotiff文件 -> JAVA 浮点二维数组float[][] :- 输入: geotiff文件路径,
- 输出: geotiff
import org.gdal.gdal.Dataset;
import org.gdal.gdal.Driver;
import org.gdal.gdal.gdal;
import org.gdal.gdal.Band;
import org.gdal.gdalconst.gdalconst;
import org.gdal.gdalconst.gdalconstConstants;
import java.io.File;
public static float[][] GetRasterValuesFromFilePath(String tiffFilePath)
{
System.out.println("Info: Get Raster Values From File Path: " + tiffFilePath + "...");
gdal.AllRegister();
Dataset ds = gdal.Open(tiffFilePath, gdalconstConstants.GA_ReadOnly);
if (ds == null)
{
System.err.println("ERR: GDALOpen failed - " + gdal.GetLastErrorNo());
System.err.println(gdal.GetLastErrorMsg());
System.exit(1);
}
Band band = ds.GetRasterBand(1);
Driver hDriver = ds.GetDriver();
//System.out.println("Driver: " + hDriver.getShortName() + "/" + hDriver.getLongName());
int width = ds.getRasterXSize();
int height = ds.getRasterYSize();
//System.out.println("Size is " + width + ", " + height);
float[][] resultArray = new float[height][width]; //注意这里高和宽别写反了
float[] buffer = new float[width * height];
band.ReadRaster(0, 0, width, height,gdalconst.GDT_Float32, buffer);
for (int i = 0; i < buffer.length; i++)
// resultArray.SetValue(buffer[i], i / width, i % width);
resultArray[i/width][i%width] = buffer[i];
ds.delete();
gdal.GDALDestroyDriverManager();
return resultArray;
}
JAVA 浮点二维数组float[][] -> geotiff文件:
- 输入: float[][]数组 ,待输出的geotiff文件路径,以及geotiff必要的参数,
- 输出 :geotiff文件到指定上述路径
写geotfff文件,需要一些必要的参数,先定义一个类管理它,然后,再依靠已有的tiff文件(姑且称之为模板tiff文件吧)中获取的这些属性,进行写tiff文件。
写geotiff文件必要的参数类:
public class GeotiffParameters
{
// ATTENTION: Assume all geotiff files share the same parameters as blow.
double[] transform= new double[6];
String projection;
String rasterFormat;
int xSize;
int ySize;
int bandCount;
int dataType; //from the members of class gdalconst
Double[] noValue = new Double[1]; // ATTENTION: Assume all geotiff files only have one band.
}
根据模板tiff获取上述参数:
public static GeotiffParameters GetGeotiffParameters(String tiffFilePath)
{
GeotiffParameters geotiffParameters = new GeotiffParameters();
gdal.AllRegister();
Dataset ds = gdal.Open(tiffFilePath, gdalconstConstants.GA_ReadOnly);
if (ds == null)
{
System.err.println("ERR: GDALOpen failed - " + gdal.GetLastErrorNo());
System.err.println(gdal.GetLastErrorMsg());
System.exit(1);
}
geotiffParameters.projection = ds.GetProjection();
ds.GetGeoTransform(geotiffParameters.transform);
Driver hDriver = ds.GetDriver();
//System.out.println("Driver: " + hDriver.getShortName() + "/" + hDriver.getLongName());
geotiffParameters.rasterFormat = hDriver.getShortName();
// columns(xSize) and rows(ySize) of rasters
geotiffParameters.xSize = ds.getRasterXSize();
geotiffParameters.ySize = ds.getRasterYSize();
geotiffParameters.bandCount = ds.getRasterCount();
geotiffParameters.dataType = ds.GetRasterBand(1).getDataType();
ds.GetRasterBand(1).GetNoDataValue(geotiffParameters.noValue);
//System.out.println(geotiffParameters.noValue[0]);//-3.4028234663852886E38
return geotiffParameters;
}
根据上述写geotiff必要的参数,开始写geotiff文件吧:
public static Boolean CreateRasterToFilePath(String geotiffOutputFilePath, float[][] resultArray)
{
System.out.println("Info: Create Raster Data(.tif file) To File Path: " + geotiffOutputFilePath + "...");
// if file exists, delete it.
File file = new File(geotiffOutputFilePath);
if(file.exists())
{
file.delete();
}
int countColumn = mGeotiffParameters.xSize; //322
int countRow = mGeotiffParameters.ySize; //562
int bandCount = mGeotiffParameters.bandCount;
int dataType = mGeotiffParameters.dataType;
String rasterFormat = mGeotiffParameters.rasterFormat;
gdal.AllRegister();
Driver driver = gdal.GetDriverByName(rasterFormat);
Dataset result = driver.Create(geotiffOutputFilePath, countColumn, countRow, bandCount, dataType);
//set attributes
result.SetGeoTransform(mGeotiffParameters.transform);
result.SetProjection(mGeotiffParameters.projection);
float[] tmpData = new float[countColumn * countRow];
float tmp;
for (int i = 0; i < countRow; ++i)
{
for (int j = 0; j < countColumn; ++j)
{
tmp = resultArray[i][j];
tmpData[i * countColumn + j] = tmp;
}
}
result.GetRasterBand(1).SetNoDataValue(mGeotiffParameters.noValue[0]);
// The value of xoff and yoff indicates the location that the upper left corner of the pixel block is to write.
int check = result.GetRasterBand(1).WriteRaster(0, 0, countColumn, countRow, dataType, tmpData);
if (check == gdalconst.CE_Failure)
assert false:"write tmp geotiff failed!";
else
return true;
return false;
}