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<OverviewPolicy> policy = AbstractGridFormat.OVERVIEW_POLICY.createValue();
        policy.setValue(OverviewPolicy.IGNORE);

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

        //Setting read type: use JAI ImageRead (true) or ImageReaders read methods (false)
        ParameterValue<Boolean> 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引用如下:

        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-image</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-imagemosaic</artifactId>
            <version>${geotools.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.geotools</groupId>
                    <artifactId>gt-epsg-hsql</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-render</artifactId>
            <version>${geotools.version}</version>
        </dependency>

        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-geotiff</artifactId>
            <version>${geotools.version}</version>
        </dependency>

GeoTools的版本:

    <properties>
        <geotools.version>23.0</geotools.version>
    </properties>

GeoTools的仓库:

    <repositories>
        <repository>
            <id>osgeo-release</id>
            <name>OSGeo Repository</name>
            <url>https://repo.osgeo.org/repository/release/</url>
        </repository>
        <repository>
            <id>osgeo-snapshot</id>
            <name>OSGeo Repository</name>
            <url>https://repo.osgeo.org/repository/snapshot/</url>
        </repository>
    </repositories>

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

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值