目地
用Java实现类似QGIS中【按掩膜图层裁剪栅格】的功能。
其实QGIS本身调用的就是gdalwarp.exe。
官方文档:https://gdal.org/programs/gdalwarp.html#gdalwarp
gdalwarp [--help-general] [--formats]
[-s_srs srs_def] [-t_srs srs_def] [-ct string] [-to "NAME=VALUE"]* [-novshiftgrid]
[-order n | -tps | -rpc | -geoloc] [-et err_threshold]
[-refine_gcps tolerance [minimum_gcps]]
[-te xmin ymin xmax ymax] [-te_srs srs_def]
[-tr xres yres] [-tap] [-ts width height]
[-ovr level|AUTO|AUTO-n|NONE] [-wo "NAME=VALUE"] [-ot Byte/Int16/...] [-wt Byte/Int16]
[-srcnodata "value [value...]"] [-dstnodata "value [value...]"]
[-srcalpha|-nosrcalpha] [-dstalpha]
[-r resampling_method] [-wm memory_in_mb] [-multi] [-q]
[-cutline datasource] [-cl layer] [-cwhere expression]
[-csql statement] [-cblend dist_in_pixels] [-crop_to_cutline]
[-if format]* [-of format] [-co "NAME=VALUE"]* [-overwrite]
[-nomd] [-cvmd meta_conflict_value] [-setci] [-oo NAME=VALUE]*
[-doo NAME=VALUE]*
srcfile* dstfile
参数 | 意义 | 意义 |
---|---|---|
-s_srs | 原坐标系统 | +proj=longlat +datum=WGS84 +no_defs |
-t_srs | 目标坐标系统 | +proj=tmerc +lat_0=0 +lon_0=111 +k=1 +x_0=500000 +y_0=0 +a=6378140 +b=6356755.288157528 +units=m +no_defs |
-to | 变换选项 | 暂时未知 |
-order n | 变换多项式顺序 1-3 | |
-tps | 强制基于已知控制点使用样条函数进行变换 | 只需输入设定即可 |
-rpc | 强制使用rpc模型 | 只需输入设定即可 |
-geoloc | 强制使用元数据中的地理位置数组 | 详细格式请参照:https://trac.osgeo.org/gdal/wiki/rfc4_geolocate |
-et | err_threshold 变换允许的偏差值 | 像素为单位 |
-refine_gcps tolerance minimum_gcps | 精化后控制点的最少数量 | |
-te xmin ymin xmax ymax | 输出文件地理范围 | 最大最小x、y值 |
-te_srs srs_def | 特殊指定SRS输出文件的地理参照 | GDAL/OGR forms, complete WKT, PROJ.4, EPSG:n or a file containing the WKT |
-tr | xres yres 输出图像的分辨率 | xres yres |
-tap | 输出图像范围的坐标系校正到图像分辨率一致 | 只需输入设定即可 |
-ts width height | 设置输出图像的宽和高 | 像素数 |
-ovr level | AUTO | AUTO-n |
-wo “NAME=VALUE” | 这里可以特别指定重投影选项 | 重投影选项都可以 |
-ot type | 输出数据类型 | type |
-wt type | 处理时使用的数据类型 | type |
-r resampling_method | 设定重采样算法 | resampling_method |
-srcnodata value [value…] | 源图像无数据处的值 | value |
-dstnodata value [value…] | 输出图像无数据处的值 | value |
-dstalpha | 创建输出数据集中的透明波段以辨识无数据值 | 只需输入设定即可 |
-wm memory_in_mb | 设定该应用程序内存 | memory_in_mb |
-multi | 是否使用多线程 | 只需输入设定即可 |
-q | 是否在控制台输出运行步骤提示 | 只需输入设定即可 |
-of | format 输出格式 | format |
-co | “NAME=VALUE” | 格式驱动所支持的选项 |
-cutline datasource | 激活OGR支持的矢量作为数据分割线 | datasource |
-cl layername | 指定特定名称的图层 | layername |
-cwhere expression | 基于属性查询分割线 | expression |
-csql query | 使用sql语句选择在-cl指定图层上的分割线要素 | |
-cblenddistance | 设置混合距离 | 按像素 |
-crop_to_cutline | 按照分割线裁剪数据集 | 只需输入设定即可 |
-overwrite | 是否覆写目标数据集 | 只需输入设定即可 |
-nomd | 不复制元数据 | 只需输入设定即可 |
-cvmd meta_conflict_value | 冲突元数据信息值 | “” |
-setci | 参照输入数据集的色彩索引表建立输出数据集波段的色彩索引表 | 只需输入设定即可 |
-oo NAME=VALUE | 打开选项 | 驱动打开选项 |
-doo NAME=VALUE | 输出选项 | 驱动输出选项 |
srcfile | 输入文件 | raw_spot.tif |
dstfile | 输出文件 | utm11.tif |
QGIS执行的命令类似如下:
gdalwarp -of GTiff -cutline D:/qgis/mask.shp -cl small -crop_to_cutline D:/qgis/src_dem.tif D:/qgis/clip.tif
Java实现
-
Tif文件的读取。
-
准备gdalwarp 参数WarpOptions 。
-
调用gdal的warp方法。
-
释放文件句柄资源。
import org.gdal.gdal.Dataset;
import org.gdal.gdal.ProgressCallback;
import org.gdal.gdal.WarpOptions;
import org.gdal.gdal.gdal;
import org.gdal.gdalconst.gdalconstConstants;
import java.util.Vector;
public class GDALClipBySharp {
public static void clip(String srcFile, String dstFile, String shpFile,String shpLayerName) {
// 读取要切的原图
Dataset srcDs = gdal.Open(srcFile, gdalconstConstants.GA_ReadOnly);
if (srcDs == null) {
System.err.println("GDALOpen failed - " + gdal.GetLastErrorNo());
System.err.println(gdal.GetLastErrorMsg());
return;
}
// 构建warp参数
Vector options = new Vector();
options.add("-of");
options.add("GTiff");
options.add("-cutline");//按照矢量图像线裁剪
options.add(shpFile);
options.add("-cl");
options.add(shpLayerName);
options.add("-crop_to_cutline"); //裁剪后图层范围使用矢量图层大小
options.add("-dstnodata");
options.add("-10001.0"); //nulldata的值
WarpOptions wo = new WarpOptions(options);
Dataset warp = gdal.Warp(dstFile, new Dataset[]{srcDs}, wo);
// 获取切图的原点坐标信息
double[] warpTransform = warp.GetGeoTransform();
System.out.println("warp原点坐标 = " + warpTransform[0] + "," + warpTransform[3]);
System.out.println("warp像素坐标差 = " + warpTransform[1] + "," + warpTransform[5]);
srcDs.delete();
}
public static void main(String[] args) {
gdal.AllRegister();
GDALClipBySharp.clip("D:\\qgis\\src_dem.tif",
"D:\\qgis\\clip.tif",
"D:\\qgis\\mask.shp",
"mask");
gdal.GDALDestroyDriverManager();
}
}