使用GeoTools解析栅格格式TIF图数据

最近项目中需要解析发布的tif图的数据,我使用的是GeoTools进行解析,当然也可以使用 GDAL 等方式进行解析。直接贴代码吧就。

根据坐标解析对应点位的tif数据

   public static void main(String[] args) throws IOException, FactoryException {
        long time=System.currentTimeMillis();

        File file = new File("D:\\geoserver-2.19.0-bin\\data_dir\\data\\ImageMosaic\\nh3\\1_20230303.tif");

        /**
         * Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER:设置经度为第一轴顺序
         *
         */
        GeoTiffReader reader = new GeoTiffReader(file, new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE));
        GridCoverage2D coverage = reader.read(null);

        //设置坐标系
        CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:4326");
        // 将 GridCoverage 进行重采样转换为另一个 CRS
        coverage = (GridCoverage2D) Operations.DEFAULT.resample(coverage, targetCRS);

        CoordinateReferenceSystem crs = coverage.getCoordinateReferenceSystem2D();

//        Envelope env = coverage.getEnvelope();
//        RenderedImage image = coverage.getRenderedImage();
	// 设置经纬度及坐标系等信息
        DirectPosition position = new DirectPosition2D(crs, 29.67236, 113.54834);

        // assume double
        float[] f1 = (float[]) coverage.evaluate(position);
        double[] doubleArray = new double[f1.length];
        for (int i = 0; i < f1.length; i++) {
            doubleArray[i] = (double) f1[i];
        }
        double[] sample = doubleArray;

        // resample with the same array
        sample = coverage.evaluate(position, sample);
        System.out.println(sample);
    }

这里是通过经纬度来获取指定点位的数据,可以参考 GeoTools官网

根据tif图片获取所有像素的值和对应的坐标

public static void main(String[] args) throws Exception {

	File file = new File("D:\\geoserver-2.19.0-bin\\data_dir\\data\\ImageMosaic\\tifprediction\\2_TN_20221015.tif");
	GeoTiffReader reader = new GeoTiffReader(file, new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE));
	GridCoverage2D coverage = reader.read(null);

	//设置坐标系
	CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:4326");
	// 将 GridCoverage 进行重采样转换为另一个 CRS
	coverage = (GridCoverage2D) Operations.DEFAULT.resample(coverage, targetCRS);

	// 获取图像范围
	RenderedImage image = coverage.getRenderedImage();

	// 获取栅格数据集的几何属性
	GridGeometry2D geometry = coverage.getGridGeometry();


	Envelope2D envelope2D = coverage.getEnvelope2D();
	double width = image.getWidth();
	System.out.println("图片宽:" + width);
	double height = image.getHeight();
	System.out.println("图片高:" + height);
	//获取边界值
	double minX = envelope2D.getBounds().getMinX();
	double minY = envelope2D.getBounds().getMinY();
	double maxX = envelope2D.getBounds().getMaxX();
	double maxY = envelope2D.getBounds().getMaxY();
	double pixelWidth = (maxX - minX) / width;
	double pixelHeight = (maxY - minY) / height;
	DirectPosition upperCorner = envelope2D.getUpperCorner();
	DirectPosition lowerCorner = envelope2D.getLowerCorner();
	System.out.println("minX:" + minX);
	System.out.println("maxX:" + maxX);
	System.out.println("minY:" + minY);
	System.out.println("maxY:" + maxY);
	System.out.println("pixelWidth:" + pixelWidth);
	System.out.println("pixelHeight:" + pixelHeight);
	System.out.println("坐标最小值:" + lowerCorner);
	System.out.println("坐标最大值:" + upperCorner);

	//遍历所有像素
	for (int i = 0; i < width; i++) {
		for (int j = 0; j < height; j++) {
			double pixelValue = image.getData().getSampleDouble(i, j, 0);
			if (pixelValue > 0) {
				GridCoordinates2D gridPos = new GridCoordinates2D(i, j);
				// 将像素点转换为坐标
				DirectPosition worldPos = geometry.gridToWorld(gridPos);
				double longitude = worldPos.getCoordinate()[1];
				double latitude = worldPos.getCoordinate()[0];
				String coordinate = "point(" + longitude + "," + latitude + ")";
				System.out.println(coordinate);
				System.out.println("像素点 (" + i + ", " + j + ") 的值: " + pixelValue + "    坐标为:" + worldPos.getCoordinate()[0] + "," + worldPos.getCoordinate()[1]);
			}
		}
	}
}
posted @ 2023-05-23 11:50  香酥豆腐皮  阅读(2256)  评论(0)    收藏  举报
正在加载今日诗词....