zhongrj
2024-12-02 1b571842429899c253c8bce5c6e01aedb056416a
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package org.springblade.modules.dem;
 
import lombok.SneakyThrows;
import org.geotools.coverage.grid.GridCoordinates2D;
import org.geotools.coverage.grid.GridCoverage2D;
import org.geotools.gce.geotiff.GeoTiffReader;
import org.geotools.geometry.DirectPosition2D;
import org.geotools.referencing.CRS;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.locationtech.jts.geom.Coordinate;
import org.opengis.geometry.DirectPosition;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform2D;
 
import java.io.File;
 
public class DemUtils {
 
    /**
     * 根据点获取海拔高
     * @param file
     * @param point
     * @return
     */
    @SneakyThrows
    public static int calculateMaxByPoint(File file, Coordinate point) {
        //获取高程文件
        GeoTiffReader reader = new GeoTiffReader(file);
        GridCoverage2D coverage = reader.read(null);
 
        CoordinateReferenceSystem crs = coverage.getCoordinateReferenceSystem2D();
        DirectPosition position = new DirectPosition2D(crs, point.x, point.y);
 
        int[] pixelValue = (int[]) coverage.evaluate(position);
 
        return pixelValue[0];
    }
 
    /**
     * 计算两点之间最高海拔
     * @param file
     * @param start
     * @param end
     * @return
     */
    @SneakyThrows
    public static Coordinate calculateMaxElevation(File file, Coordinate start, Coordinate end) {
        //获取高程文件
        GeoTiffReader reader = new GeoTiffReader(file);
        GridCoverage2D coverage = reader.read(null);
 
        CoordinateReferenceSystem crs = coverage.getCoordinateReferenceSystem2D();
        MathTransform2D toGrid = (MathTransform2D) CRS.findMathTransform(DefaultGeographicCRS.WGS84, crs, true);
        MathTransform2D fromGrid = (MathTransform2D) CRS.findMathTransform(crs, DefaultGeographicCRS.WGS84, true);
 
        DirectPosition2D startPos = new DirectPosition2D(start.x, start.y);
        DirectPosition2D endPos = new DirectPosition2D(end.x, end.y);
 
        GridCoordinates2D startGrid = new GridCoordinates2D();
        GridCoordinates2D endGrid = new GridCoordinates2D();
        toGrid.transform(startPos, startGrid);
        toGrid.transform(endPos, endGrid);
 
        int maxPixelValue = Integer.MIN_VALUE;
 
        Coordinate retCoordinate = new Coordinate();
 
        // 线性插值,计算中间的点
        for (int i = 0; i <= 10000; i++) {
 
            double t = i / 10000.0;
 
            double lon = start.x + t * (end.x - start.x);
            double lat = start.y + t * (end.y - start.y);
 
            DirectPosition position = new DirectPosition2D(crs, lon, lat);
            int[] pixelValue = (int[]) coverage.evaluate(position);
 
            if (pixelValue[0] > maxPixelValue) {
                maxPixelValue = pixelValue[0];
                retCoordinate.setX(lon);
                retCoordinate.setY(lat);
                retCoordinate.setZ(maxPixelValue);
            }
        }
 
        return retCoordinate;
 
    }
 
    public static void main(String[] args) {
        Coordinate start = new Coordinate(115.856725497, 28.624514734);
////        Coordinate end = new Coordinate(115.84832847430599, 28.629107705136526);
//        Coordinate end = new Coordinate(115.85650343813958, 28.62419061027172);
//
        String demPath = "C:\\Users\\vip_x\\Desktop\\无人机\\高程\\江西省12.5米DEM_CGCS2000\\江西省12.5米DEM_CGCS2000.tif";
        File file = new File(demPath);
//
//        Coordinate height = calculateMaxElevation(file,start,end);
//        System.out.println("height:" + height);
//
////        double maxElevation = calculateMaxElevation(file, start, end);
////        System.out.println("Maximum elevation between points: " + maxElevation);
//
        int height2 = calculateMaxByPoint(file,start);
        System.out.println("height2 = " + height2);
    }
 
}