Browse Source

根据TIFF读取像素点已经对应的经纬度

LAPTOP-BJJ3IV5R\SIWEI 9 tháng trước cách đây
mục cha
commit
989c2f23a1

+ 5 - 2
onemap-modules/onemap-spatial/pom.xml

@@ -119,14 +119,17 @@
             <artifactId>gt-tile-client</artifactId>
             <version>${org.geotools.version}</version>
         </dependency>
+        <dependency>
+            <groupId>org.geotools</groupId>
+            <artifactId>gt-epsg-hsql</artifactId>
+            <version>${org.geotools.version}</version>
+        </dependency>
         <dependency>
             <groupId>org.locationtech.jts</groupId>
             <artifactId>jts-core</artifactId>
             <version>1.19.0</version>
         </dependency>
 
-        <!--        l;jkjmj;jkl;-->
-
         <!-- SpringCloud Alibaba Nacos -->
         <dependency>
             <groupId>com.alibaba.cloud</groupId>

+ 69 - 0
onemap-modules/onemap-spatial/src/main/java/com/onemap/spatial/test/ReadPixel.java

@@ -0,0 +1,69 @@
+package com.onemap.spatial.test;
+
+import com.sun.xml.internal.bind.v2.TODO;
+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.opengis.referencing.operation.TransformException;
+
+import java.awt.image.Raster;
+import java.awt.image.RenderedImage;
+
+//TODO 根据栅格图层相关像素点信息
+public class ReadPixel {
+    /**
+     * 根据栅格图层相关像素点信息
+     *
+     * @param path 栅格文件路径
+     */
+    private static void getPixelValue(String path) throws Exception {
+        GeoTiffReader geoTiffReader = new GeoTiffReader(path);
+        // 读取影像数据,获取2D覆盖层,可以理解为平面坐标系对象
+        GridCoverage2D coverage2D = geoTiffReader.read(null);
+        // 获取影像的渲染图像
+        RenderedImage renderedImage = coverage2D.getRenderedImage();
+        int width = renderedImage.getWidth();
+        int height = renderedImage.getHeight();
+        System.err.println("影像列数: " + width + ",影像行数: " + height);
+
+        // 获取影像的栅格数据
+        Raster raster = renderedImage.getData();
+
+        double sampleDouble = raster.getSampleFloat(7047, 6037, 0);
+        System.out.println("像素值: " + sampleDouble);
+
+//
+
+//        for (int i = 0; i < width; i++) {
+//            for (int j = 0; j < height; j++) {
+//                // (x,y)点的值
+//                double sampleDouble = raster.getSampleFloat(i, j, 0);
+//                // 获取像素对应的经纬度信息
+//                double[] lonLatByXAndY = getLonLatByXAndY(i, j, coverage2D);
+//                System.out.println("列数: "+i +", 行数: "+j+ ", 经度: " + lonLatByXAndY[1] + ",纬度: " + lonLatByXAndY[0] + ",像素值: " + sampleDouble);
+////                System.out.println("像素值: " + sampleDouble);
+//
+//            }
+//        }
+    }
+
+    /**
+     * 根据横纵坐标获取经纬度,以及coverage2D获取取经纬度信息
+     * 注: 需要的话调用
+     */
+    private static double[] getLonLatByXAndY(int i, int j, GridCoverage2D coverage2D) throws TransformException {
+        GridCoordinates2D gridCoordinate = new GridCoordinates2D(i, j);
+        DirectPosition2D realWorldCoordinate = (DirectPosition2D) coverage2D.getGridGeometry().gridToWorld(gridCoordinate);
+        double longitude = realWorldCoordinate.getX();
+        double latitude = realWorldCoordinate.getY();
+        return new double[]{latitude, longitude};
+    }
+
+    // 测试
+    public static void main(String[] args) throws Exception {
+        String path = "C:\\Users\\SIWEI\\Desktop\\15区\\DEM大图\\ceshi.tif";
+        getPixelValue(path);
+    }
+
+}