先粘上参考大佬的链接:https://www.jianshu.com/p/c25f9360459f
下面开始,因为业务需要需要用栅格数据生成tiff图,所以一直在找。但是java版一直没有找到,参考大神的加自己翻写C++版本实现的功能。
gdal 官网api连接:https://gdal.org/java/overview-summary.html
废话不多说,先上代码。
public static void main(String[] args) {
String strVectorFile = "D:\\test118.tiff";
String txtArrayFile = "C:/Users/Administrator/Desktop/WPI_F_WH_D_Z_Z_195017_195117_AL_2d_avg.txt";
gdal.AllRegister();
gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8","YES");
//gdal.SetConfigOption("SHAPE_ENCODING","CP936");
SpatialReference ref = new SpatialReference();
//只有这个投影支持输出geotiff OGC WKT 错误参数:WGS84
ref.SetWellKnownGeogCS("WGS84");
String[] defWkt = null;
//ref.ExportToWkt();// 可配置String数组 defWkt
//驱动名称
String strDriverName = "GTiff";
org.gdal.gdal.Driver oDriver = gdal.GetDriverByName(strDriverName);
if (oDriver == null) {
System.out.println(strDriverName+ " 驱动不可用!\n");
return;
}
// @param数 (name, xsize, ysize, bands, java.lang.String[] options)
String[] options = null;// 6个参数
Dataset dataset = oDriver.Create(strVectorFile, 45, 45, 1, gdalconst.GDT_Float32);
// @param double[] argin 地理坐标 米转经纬度单位
// 0:左上角x坐标 1:东西方向空间分辨率 2:x方向旋转角0° 3:左上角y坐标 4:y方向旋转角0° 5:南北方向空间分辨率
double [] argin = {95, 1, 0, 45, 0, -1};
dataset.SetGeoTransform(argin);
// @param int xoff(偏移量), int yoff, int xsize, int ysize, int buf_type, byte[] array
Band band = dataset.GetRasterBand(1);// 波段 (色彩)
double[][] txtArray = utils.Utils.readTxtToArray(txtArrayFile);
float[] arr = new float[45 *45];
int c = 0;
for (int j = 0; j < 45 /*txtArray.length*/; j++) {
for (int j2 = 0; j2 < 45/*txtArray[j].length*/; j2++) {
arr[c] = (float) txtArray[j][j2];
c++;
}
}
// ReadRaster(int xoff, int yoff, int xsize, int ysize, int buf_type, float[] array)
// 参数说明: 起始x 起始y x数量(行) y数量(列) 数据类型 数组
band.WriteRaster(0, 0, 45, 45, gdalconst.GDT_Float32, arr);
dataset.delete();
oDriver.delete();
}
double[][] txtArray = utils.Utils.readTxtToArray(txtArrayFile); 这行是读取txt栅格文件转换成二维数组。
public static double[][] readTxtToArray(String path) {
List<List<Double>> conList = new ArrayList<List<Double>>();
double[][] douline = null;
try {
File file = new File(path);
String encoding = "UTF-8";
if (file.isFile() && file.exists()) {
InputStreamReader read = new InputStreamReader(
new FileInputStream(file), encoding);
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = "";
int len = 0;
while ((lineTxt = bufferedReader.readLine()) != null) {
String[] myArray=lineTxt.trim().replaceAll("\\s+",",").split(",");
List<Double> lineList = new ArrayList<Double>();
for (String str : myArray) {
lineList.add(Double.parseDouble(str));
}
conList.add(lineList);
len++;
}
read.close();
douline = new double[conList.size()][conList.get(0).size()];
for (int i = 0; i <conList.size(); i++) {
for (int j = 0; j < conList.get(i).size(); j++) {
if(!conList.get(i).isEmpty()&&conList.get(i).size()!=0){
douline[i][j]=conList.get(i).get(j);
}
}
}
} else {
System.out.println("文件不存在!");
}
} catch (Exception e) {
e.printStackTrace();
}
return douline;
}
好啦,到这里就结束了。学的不是技术,是梦想。大家加油哦。