首页 > 其他分享 >读取影像文件坐标系

读取影像文件坐标系

时间:2022-11-11 10:13:13浏览次数:42  
标签:case 读取 dataType System break 影像 println 坐标系 gdal

package com.by;

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;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:zcx
 * @DATE:2022/11/11 9:38
 * @Description:
 * @Version 1.0
 */
@SpringBootTest
public class Readtiff {
    @Test
    public void m1(){
        String name="E:\\jinzita\\caiqie\\polygon2.tif";
        System.out.println("name:" + name);
        gdal.AllRegister();
        Dataset hDataset = gdal.OpenShared(name, gdalconstConstants.GA_ReadOnly);
        if (hDataset == null) {
            System.err.println("GDALOpen failed - " + gdal.GetLastErrorNo());
            System.err.println(gdal.GetLastErrorMsg());
            //System.exit(1);
        }
        Driver hDriver = hDataset.GetDriver();
        System.out.println("Driver: " + hDriver.getShortName() + "/" + hDriver.getLongName());
        //读取图像的宽度,x方向上的像素个数
        int iXSize = hDataset.getRasterXSize();
        //读取图像的高度,y方向上的像素个数
        int iYSize = hDataset.getRasterYSize();
        System.out.println("Size is " + iXSize + ", " + iYSize);


        String s = hDataset.GetProjectionRef();
        System.out.println("坐标系1:" + s);


        String pro = hDataset.GetProjection();
        System.out.println("坐标系2:" + pro);

        List<String> strings = new ArrayList<>();
        int count = hDataset.getRasterCount();
        System.out.println("波段数" + count);
        double dou[] = hDataset.GetGeoTransform();
        //x
        String west = String.valueOf(dou[0]);
        //y
        String east = String.valueOf(dou[3]);
        String south = String.valueOf(dou[1]);
        System.out.println(west);
        System.out.println(east);

        double x = add(mul(dou[1], iXSize), dou[0]);

        double y = add(mul(dou[5], iYSize), dou[3]);

        System.out.println(x);
        System.out.println(y);
        Band band = hDataset.GetRasterBand(1);
        int type = band.getDataType();
        System.out.println("类型" + type);
        int dataType = 0;
        switch (type) {
            case 0:
                dataType = 0;
                break;
            case 1:
                dataType = 8;
                break;
            case 2:
                dataType = 16;
                break;
            case 3:
                dataType = 16;
                break;
            case 4:
                dataType = 32;
                break;
            case 5:
                dataType = 32;
                break;
            case 6:
                dataType = 32;
                break;
            case 7:
                dataType = 64;
                break;
            case 8:
                dataType = 16;
                break;
            case 9:
                dataType = 32;
                break;
            case 10:
                dataType = 32;
                break;
            case 11:
                dataType = 64;
                break;
            default:
                dataType = 0;
                break;

        }
        hDataset.delete();
        // 可选
        gdal.GDALDestroyDriverManager();
    }
    public static double mul(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.multiply(b2).doubleValue();
    }
    public static double add(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.add(b2).doubleValue();
    }
}

标签:case,读取,dataType,System,break,影像,println,坐标系,gdal
From: https://www.cnblogs.com/zcxxcvbn/p/16879663.html

相关文章