window的下载地址:http://www.gisinternals.com/,根据自己的需要下载。
1、下载已编译好的gdal,如我下载的 x64 位的 release-1911-x64-gdal-3-0-0-mapserver-7-4-0, 解压之后,会得到如下图所示文件:
image.png2、jdk 配置gdal
将 gdal/bin目录下所有的dll扩展名文件 和 gdal/bin/gdal/java/gdalalljni.dll 拷贝到 jdk/bin目录下
3、配置环境变量
在环境变量中增加 PROJ_LIB 变量名,变量值为 gdal/bin/proj6/share。配置完后记得重启或注销计算机使配置生效。
image.png
4、在java项目中引入并使用gdal
1、新建maven项目,并创建lib文件夹,将 gdal/bin/gdal/java/gdal.jar 拷贝进来,并重命名为 gdal-3.0.0.jar
image.png2、pom.xml中引用gdal依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.geovis</groupId>
<artifactId>gdal</artifactId>
<version>1.0-SNAPSHOT</version>
<name>gdal</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.gdal</groupId>
<artifactId>gdal</artifactId>
<version>3.0.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/gdal-3.0.0.jar</systemPath>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
5、测试
package com.geovis.tiff;
import org.gdal.gdal.Band;
import org.gdal.gdal.Dataset;
import org.gdal.gdal.Driver;
import org.gdal.gdal.gdal;
import org.gdal.gdalconst.gdalconstConstants;
public class ReadTiff {
public static void main(String[] args) {
gdal.AllRegister();
String rasterFilePath = "D:/study/GF1_PMS2_E103.5_N31.9_20130527_L1A0000101973-MSS2.tif";
Dataset dataset = gdal.Open(rasterFilePath, gdalconstConstants.GA_ReadOnly);
if (dataset == null) {
System.out.println("GDAL read error: " + gdal.GetLastErrorMsg());
}
Driver driver = dataset.GetDriver();
System.out.println("driver short name: " + driver.getShortName());
System.out.println("driver long name: " + driver.getLongName());
System.out.println("metadata list: " + driver.GetMetadata_List());
int xsize = dataset.getRasterXSize();
int ysize = dataset.getRasterYSize();
int count = dataset.getRasterCount();
String proj = dataset.GetProjection();
Band band = dataset.GetRasterBand(1);
// 左上角点坐标 lon lat: transform[0]、transform[3]
// 像素分辨率 x、y方向 : transform[1]、transform[5]
// 旋转角度: transform[2]、transform[4])
double[] transform = dataset.GetGeoTransform();
for(int i=0; i<transform.length; i++){
System.out.println("transform: " + transform[i]);
}
}
}
网友评论