计算栅格中的波段数

时间:2019-04-25 16:26:34

标签: raster geotools

我一直在尝试计算光栅文件中的波段数。虽然下面的代码大部分有效,但是当我在投影栅格上尝试代码时,似乎抛出了异常。

我尝试了以下代码:

AbstractGridFormat format = GridFormatFinder.findFormat(rasterFile);
        // this is a bit hacky but does make more geotiffs work
        Hints hints = new Hints();
        if (format instanceof GeoTiffFormat) {
            hints = new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE);
        }
        reader = format.getReader(rasterFile, hints);
        GridCoverage2D result = reader.read(null);
        int bands = result.getNumSampleDimensions();
        System.out.println(bands)

我收到以下异常:

org.geotools.data.DataSourceException
    at org.geotools.gce.geotiff.GeoTiffReader.<init>(GeoTiffReader.java:266)
    at org.geotools.gce.geotiff.GeoTiffFormat.getReader(GeoTiffFormat.java:307)
    at org.geotools.gce.geotiff.GeoTiffFormat.getReader(GeoTiffFormat.java:76)
    at ImageLab.displayLayers(ImageLab.java:74)
    at ImageLab.getLayersAndDisplay(ImageLab.java:36)
    at ImageLab.main(ImageLab.java:25)
Caused by: org.geotools.data.DataSourceException
    at org.geotools.gce.geotiff.GeoTiffReader.getHRInfo(GeoTiffReader.java:500)
    at org.geotools.gce.geotiff.GeoTiffReader.<init>(GeoTiffReader.java:253)
    ... 5 more
Caused by: java.lang.NullPointerException
    at org.geotools.referencing.cs.AbstractCS.axisUsingUnit(AbstractCS.java:503)
    at org.geotools.referencing.cs.DefaultCartesianCS.usingUnit(DefaultCartesianCS.java:291)
    at org.geotools.coverage.grid.io.imageio.geotiff.GeoTiffMetadata2CRSAdapter.createUserDefinedPCS(GeoTiffMetadata2CRSAdapter.java:750)
    at org.geotools.coverage.grid.io.imageio.geotiff.GeoTiffMetadata2CRSAdapter.createProjectedCoordinateReferenceSystem(GeoTiffMetadata2CRSAdapter.java:245)
    at org.geotools.coverage.grid.io.imageio.geotiff.GeoTiffMetadata2CRSAdapter.createCoordinateSystem(GeoTiffMetadata2CRSAdapter.java:181)
    at org.geotools.gce.geotiff.GeoTiffReader.getHRInfo(GeoTiffReader.java:330)
    ... 6 more

Exception in thread "main" java.lang.NullPointerException
    at ImageLab.displayLayers(ImageLab.java:75)
    at ImageLab.getLayersAndDisplay(ImageLab.java:36)
    at ImageLab.main(ImageLab.java:25)

还为投影数据添加了一些代码。我的光栅文件位于WyonmingNAD27:32056投影系统中

1 个答案:

答案 0 :(得分:0)

我在代码中缺少以下依赖项。

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

添加以上依赖项后,CRS问题得到解决。