GeoTools读取Tiff文件

最近有个需求,要使用GeoServer自动发布InSAR处理的Tiff文件结果,基本思路:

(1)上传TIFF文件,通过GeoServer REST API发布成WMTS服务;

(2)使用GeoTools读取Tiff中的重要形变信息、像素坐标信息,同步发布成WMS服务;

网上查了很多资料,都没有实现GeoTools读取Tiff的功能,后面查询官网API,才实现此功能,特此记录。

    @Test
    public void readTiff() throws IOException, TransformException {
        String tiffPath = "E:\\20150503.tif";
        File tiffFile = new File(tiffPath);

        ParameterValue policy = AbstractGridFormat.OVERVIEW_POLICY.createValue();
        policy.setValue(OverviewPolicy.IGNORE);

        //this will basically read 4 tiles worth of data at once from the disk...
        ParameterValue gridsize = AbstractGridFormat.SUGGESTED_TILE_SIZE.createValue();

        //Setting read type: use JAI ImageRead (true) or ImageReaders read methods (false)
        ParameterValue useJaiRead = AbstractGridFormat.USE_JAI_IMAGEREAD.createValue();
        useJaiRead.setValue(true);

        GridCoverage2DReader reader = new GeoTiffReader(tiffFile);
        GridEnvelope dimensions = reader.getOriginalGridRange();
        GridCoordinates maxDimensions = dimensions.getHigh();
        int w = maxDimensions.getCoordinateValue(0) + 1;
        int h = maxDimensions.getCoordinateValue(1) + 1;
        int numBands = reader.getGridCoverageCount();

        GridCoverage2D coverage = reader.read(
                new GeneralParameterValue[]{policy, gridsize, useJaiRead}
        );
        GridGeometry2D geometry = coverage.getGridGeometry();
        
        for (int i = 0; i < w; i++) {
            for (int j = 0; j < h; j++) {

                org.geotools.geometry.Envelope2D pixelEnvelop =
                        geometry.gridToWorld(new GridEnvelope2D(i, j, 1, 1));

                double lat = pixelEnvelop.getCenterY();
                double lon = pixelEnvelop.getCenterX();

                double[] vals = new double[numBands];
                double[] values = coverage.evaluate(new GridCoordinates2D(i, j), vals);
                //Do something!
            }
        }

    }

GeoTools的Maven引用如下:

        
            org.geotools
            gt-image
            ${geotools.version}
        
        
            org.geotools
            gt-imagemosaic
            ${geotools.version}
            
                
                    org.geotools
                    gt-epsg-hsql
                
            
        
        
            org.geotools
            gt-render
            ${geotools.version}
        

        
            org.geotools
            gt-geotiff
            ${geotools.version}
        

GeoTools的版本:

    
        23.0
    

GeoTools的仓库:

    
        
            osgeo-release
            OSGeo Repository
            https://repo.osgeo.org/repository/release/
        
        
            osgeo-snapshot
            OSGeo Repository
            https://repo.osgeo.org/repository/snapshot/
        
    

需要注意的是:GeoTools 23的版本不再支持Java8,所以需要安装更高的版本,我本地安装了Java17,在IDEA中的配置如下:

GeoTools读取Tiff文件_第1张图片

你可能感兴趣的:(maven,GeoTools,TIFF数据)