简要说明
在java,利用gdal,按照geojson(字符串转文件)裁剪遥感tif文件。
maven依赖
<!--需要安装完gdal后,本地install gdal包才能使用 -->
<!--gdal安装可参考 https://blog.csdn.net/qq_41613913/article/details/135743562 -->
<dependency>
<groupId>org.gdal</groupId>
<artifactId>gdal</artifactId>
<version>3.7.3</version>
</dependency>
样例代码
public static void main(String[] args) throws Exception {
//tif文件
String dataPath = "D:/data/test.tif";
//替换你的geojson字符串
String geojsonStr = "";
//将geoJson转成本地文件.geojson
String geoJsonFilePath = "D:/tmp/clipping.geojson";
File geoJsonFile = new File(geoJsonFilePath);
if (!geoJsonFile.getParentFile().exists()) {
geoJsonFile.getParentFile().mkdirs();
}
geoJsonFile.createNewFile();
Files.write(path, overlayRange.getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE);
//裁剪后的tif文件
String outputTiffPath = "D:/tmp/clipping.tif";
String command = "gdalwarp -cutline " + geoJsonFilePath + " -crop_to_cutline " + dataPath + " " + outputTiffPath;
// 使用Java执行GDAL命令
ProcessBuilder processBuilder = new ProcessBuilder(command.split(" "));
Process process = processBuilder.start();
int exitCode = process.waitFor();
if (exitCode == 0) {
System.out.println("执行完毕");
}
}