张洋洋
2025-01-16 94d808f30053f8d03185cca31df3c8673d815699
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.se.simu.utils;
 
import com.alibaba.fastjson.JSONArray;
import org.gdal.gdal.Dataset;
import org.gdal.gdal.gdal;
 
public class TiffCoordinateExtractorUtil {
    public static void main(String[] args) {
        getCoordinate("D:\\城市内涝\\sem\\DEM.tif");
    }
    public static JSONArray getCoordinate(String tifPath){
        gdal.AllRegister();
        JSONArray array=new JSONArray();
        Dataset dataset = gdal.Open(tifPath);
        if (dataset!= null) {
            double[] geotransform = dataset.GetGeoTransform();
            if (geotransform!= null) {
                double xmin = geotransform[0];
                double ymax = geotransform[3];
                double xmax = geotransform[0] + geotransform[1] * dataset.getRasterXSize();
                double ymin = geotransform[3] + geotransform[5] * dataset.getRasterYSize();
                System.out.println("左上角经纬度: (" + xmin + ", " + ymax + ")");
                array.add(ProjectionToGeographicUtil.getPoint(xmin,ymax));
                System.out.println("右上角经纬度: (" + xmax + ", " + ymax + ")");
                array.add(ProjectionToGeographicUtil.getPoint(xmax,ymax));
                System.out.println("左下角经纬度: (" + xmin + ", " + ymin + ")");
                array.add(ProjectionToGeographicUtil.getPoint(xmin,ymin));
                System.out.println("右下角经纬度: (" + xmax + ", " + ymin + ")");
                array.add(ProjectionToGeographicUtil.getPoint(xmax,ymin));
            }
        }
        System.out.println(array.toJSONString());
        return array;
    }
}