|
@@ -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);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|