Java GDAL开发环境搭建


  1. 首先从官网下载编译好的二进制包 GISInternals Support Site。 解压以后的文件如下:
    GDAL Unzip
    将该目录添加到自己的环境变量Path中,比如我的路径是:E:\C++\Library\gdal\release-1800-gdal-2-1-0-mapserver-7-0-1\bin

  2. 在Eclipse中新建一个Java工程。将下载好的GDAL中bin\gdal\java下面的文件拷贝到工程根目录下,如下图。(我新建了一个lib文件,将gdal.jar放到了lib文件夹中,其它4个jni相关的dll直接搁在更目录下了。当然,把这五个文件都放在根目录下完全OK)
    GDAL Java Project

  3. 把gdal.jar把添加到工程的build path中,如下图。
    GDAL Path

  4. OK!开始编码!

package cn.tzy.gdal;

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 HelloGDAL {
    public static void main(String[] args) {
        gdal.AllRegister();

        String fileName = "F:/2016/Data/World/wsiearth.tif";
        // 读取影像数据
        Dataset dataset = gdal.Open(fileName, gdalconstConstants.GA_ReadOnly);
        if (dataset == null) {
            System.err.println("GDALOpen failed - " + gdal.GetLastErrorNo());
            System.err.println(gdal.GetLastErrorMsg());

            System.exit(1);
        }

        Driver driver = dataset.GetDriver();
        System.out.println("Driver: " + driver.getShortName() + "/" + driver.getLongName());

        // 读取影像信息
        int xSize = dataset.getRasterXSize();
        int ySzie = dataset.getRasterYSize();
        int nBandCount = dataset.getRasterCount();
        System.out.println("Size is " + xSize + ", " + ySzie + ", " + nBandCount);

        Band band = dataset.GetRasterBand(1);
        int type = band.GetRasterDataType();
        // type为1,代表的是Eight bit unsigned integer
        System.out.println(type);

        dataset.delete();

        gdal.GDALDestroyDriverManager();

    }
}

运行结果如下:

Driver: GTiff/GeoTIFF
Size is 10020, 5010, 1
1

转载自:https://blog.csdn.net/theonegis/article/details/52585613

You may also like...