CoverageUtilities { public static double[] getBackgroundValues(GridCoverage2D coverage) { if(coverage==null){ throw new NullPointerException(Errors.format(ErrorKeys.NULL_PARAMETER_$2, "coverage","GridCoverage2D")); } final Object noData=coverage.getProperty("GC_NODATA"); if(noData!=null&& noData instanceof Number){ return new double[]{((Double)noData).doubleValue()}; } final GridSampleDimension[] sampleDimensions = coverage.getSampleDimensions(); final double[] background = new double[sampleDimensions.length]; boolean found=false; final int dataType = coverage.getRenderedImage().getSampleModel().getDataType(); for (int i=0; i<background.length; i++) { final List<Category> categories = sampleDimensions[i].getCategories(); if(categories!=null&&categories.size()>0){ for(Category category:categories){ if(category.getName().equals(NODATA)){ background[i]=category.geophysics(true).getRange().getMinimum(); found=true; break; } } } if(!found){ background[i]=suggestNoDataValue(dataType).doubleValue(); } } return background; } private  CoverageUtilities(); static CoordinateReferenceSystem getCRS2D(final Coverage coverage); static CoordinateReferenceSystem getHorizontalCRS(final Coverage coverage); static Envelope2D getEnvelope2D(final Coverage coverage); static double[] getBackgroundValues(GridCoverage2D coverage); static boolean hasRenderingCategories(final GridCoverage gridCoverage); static boolean hasTransform(final SampleDimension[] sampleDimensions); static boolean uses(final GridCoverage coverage, final RenderedImage image); static int getVisibleBand(final Object image); static ViewType preferredViewForOperation(final GridCoverage2D coverage,
            final Interpolation interpolation, final boolean hasFilter, final RenderingHints hints); static ViewType preferredViewAfterOperation(final GridCoverage2D coverage); static boolean isScaleTranslate(final MathTransform transform, final double EPS); static double[] getResolution(final AffineTransform gridToCRS); static boolean isSimpleGridToWorldTransform(final AffineTransform gridToCRS, double EPS); static boolean checkEmptySourceRegion(final ImageReadParam readParameters, 
            final Rectangle dimensions); static double getMosaicThreshold(int dataType); static Number suggestNoDataValue(int dataType); static final InternationalString NODATA; static final AffineTransform AXES_SWAP; static final AffineTransform IDENTITY_TRANSFORM; final static AffineTransform CENTER_TO_CORNER; final static AffineTransform CORNER_TO_CENTER; }
@Test public void testNodata() { final HashMap properties= new HashMap(); properties.put("GC_NODATA", Double.valueOf(-9999.0)); final GridGeometry2D gg2D = new GridGeometry2D( new Rectangle(0,0,800,600), new Rectangle(-180,90,360,180)); GridCoverage2D gc= CoverageFactoryFinder.getGridCoverageFactory(null). create( "test", ImageFunctionDescriptor.create(new MyImageFunction(), Integer.valueOf(800), Integer.valueOf(600), Float.valueOf(1.0f), Float.valueOf(1.0f), Float.valueOf(0.0f), Float.valueOf(0.0f), null), gg2D, null, null, properties); double[] bkg=CoverageUtilities.getBackgroundValues(gc); assertEquals(1, bkg.length); assertEquals(Double.valueOf(-9999.0), bkg[0]); final Category noDataCategory= new Category(CoverageUtilities.NODATA,new Color[]{Color.black},NumberRange.create(Double.valueOf(-9999.0),Double.valueOf(-9999.0)),NumberRange.create(Double.valueOf(-9999.0),Double.valueOf(-9999.0))); final GridSampleDimension gsd = new GridSampleDimension("test", new Category[]{noDataCategory},Unit.ONE); gc= CoverageFactoryFinder.getGridCoverageFactory(null). create( "test", ImageFunctionDescriptor.create(new MyImageFunction(), Integer.valueOf(800), Integer.valueOf(600), Float.valueOf(1.0f), Float.valueOf(1.0f), Float.valueOf(0.0f), Float.valueOf(0.0f), null), gg2D, new GridSampleDimension[]{gsd}, null, null); bkg=CoverageUtilities.getBackgroundValues(gc); assertEquals(1, bkg.length); assertEquals(Double.valueOf(-9999.0), bkg[0]); gc= CoverageFactoryFinder.getGridCoverageFactory(null). create( "test", ImageFunctionDescriptor.create(new MyImageFunction(), Integer.valueOf(800), Integer.valueOf(600), Float.valueOf(1.0f), Float.valueOf(1.0f), Float.valueOf(0.0f), Float.valueOf(0.0f), null), gg2D, null, null, null); bkg=CoverageUtilities.getBackgroundValues(gc); assertEquals(1, bkg.length); assertTrue(Double.isNaN(bkg[0])); }