Package org.geoserver.catalog

Source Code of org.geoserver.catalog.CoverageDimensionCustomizerReaderTest

/* (c) 2014 Open Source Geospatial Foundation - all rights reserved
* (c) 2014 OpenPlans
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
*/
package org.geoserver.catalog;

import static org.junit.Assert.*;

import java.awt.Color;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.measure.quantity.Quantity;
import javax.measure.unit.BaseUnit;

import org.geoserver.catalog.CoverageDimensionCustomizerReader.WrappedSampleDimension;
import org.geoserver.catalog.impl.CoverageDimensionImpl;
import org.geoserver.test.GeoServerSystemTestSupport;
import org.geotools.coverage.Category;
import org.geotools.coverage.GridSampleDimension;
import org.geotools.referencing.operation.transform.IdentityTransform;
import org.geotools.referencing.operation.transform.LinearTransform1D;
import org.geotools.resources.i18n.Vocabulary;
import org.geotools.resources.i18n.VocabularyKeys;
import org.geotools.util.NumberRange;
import org.junit.Test;
import org.opengis.coverage.ColorInterpretation;
import org.opengis.coverage.SampleDimension;
import org.opengis.coverage.SampleDimensionType;

public class CoverageDimensionCustomizerReaderTest extends GeoServerSystemTestSupport {

    private static final double DELTA = 1E-4;

    /**
     * Test that the null values and range of a wrapped sampleDimension are the same
     * configured on the {@link CoverageDimensionInfo} object used to customize them 
     * @throws IOException
     */
    @Test
    public void testDimensionsWrapping() throws IOException {
        final GridSampleDimension sampleDim = new GridSampleDimension("original",
                SampleDimensionType.REAL_64BITS, ColorInterpretation.GRAY_INDEX,
                null, null, new double[]{-9999.0}, -1000d, 1000d, 1d, 0d, null);

        // Setting coverage dimension
        final CoverageDimensionImpl coverageDim = new CoverageDimensionImpl();
        final String wrappedName = "wrapped";
        coverageDim.setName(wrappedName);
        coverageDim.setDimensionType(SampleDimensionType.REAL_64BITS);

        final double newMinimum = -2000d;
        final double newMaximum = 2000d;
        final NumberRange<Double> range = new NumberRange<Double>(Double.class, newMinimum, newMaximum);
        coverageDim.setRange(range);

        final List<Double> nullValues = new ArrayList<Double>();
        final double noData1 = -32768d;
        final double noData2 = -32767d;
        nullValues.add(noData1);
        nullValues.add(noData2);
        coverageDim.setNullValues(nullValues);

        final SampleDimension wrappedDim = new WrappedSampleDimension(sampleDim, coverageDim);
        double[] noData = wrappedDim.getNoDataValues();

        assertEquals(2, noData.length);
        assertEquals(noData1, noData[0], DELTA);
        assertEquals(noData2, noData[1], DELTA);

        NumberRange wrappedRange = ((WrappedSampleDimension)wrappedDim).getRange();
        assertEquals(newMinimum, wrappedRange.getMinimum(), DELTA);
        assertEquals(newMaximum, wrappedRange.getMaximum(), DELTA);

        assertEquals(wrappedName, wrappedDim.getDescription().toString());
    }

    /**
     * Test that the wrapped nodata categories contains the defined nodata
     *
     * @throws IOException
     */
    @Test
    public void testNoDataCategoryWrapping() throws IOException {

        // Setting coverage dimension
        final CoverageDimensionImpl coverageDim = new CoverageDimensionImpl();
        final String wrappedName = "wrapped";
        coverageDim.setName(wrappedName);
        coverageDim.setDimensionType(SampleDimensionType.REAL_64BITS);

        // Definition of the nodata
        final List<Double> nullValues = new ArrayList<Double>();
        final double noData1 = -32768d;
        final double noData2 = -32767d;
        nullValues.add(noData1);
        nullValues.add(noData2);
        coverageDim.setNullValues(nullValues);

        // Qualitative nodata category
        GridSampleDimension sampleDim = new GridSampleDimension("original",
                new Category[] { new Category(
                        Vocabulary.formatInternational(VocabularyKeys.NODATA),
                        new Color(0, 0, 0, 0), 0) }, null);

        // Wrap the dimension
        GridSampleDimension wrappedDim = new WrappedSampleDimension(sampleDim, coverageDim);

        // Ensure nodata are correct
        double[] noData = wrappedDim.getNoDataValues();
        assertEquals(2, noData.length);
        assertEquals(noData1, noData[0], DELTA);
        assertEquals(noData2, noData[1], DELTA);

        // Extract categories
        List<Category> categories = wrappedDim.getCategories();

        // Ensure NoData Category is present
        Category category = categories.get(0);
        assertTrue(category.getName().equals(Category.NODATA.getName()));

        // Check that it does not contain sampleToGeophisics and that the Range contains only NaN
        assertNull(category.getSampleToGeophysics());
        assertEquals(category.geophysics(true).getRange().getMinimum(), Double.NaN, DELTA);
        assertEquals(category.geophysics(true).getRange().getMaximum(), Double.NaN, DELTA);

        // Quantitative nodata category
        sampleDim = new GridSampleDimension("original", new Category[] { new Category(
                Vocabulary.formatInternational(VocabularyKeys.NODATA), new Color[] { new Color(0,
                        0, 0, 0) }, NumberRange.create(-9999, -9999), NumberRange.create(-9999,
                        -9999)) }, null);

        // Wrap the dimension
        wrappedDim = new WrappedSampleDimension(sampleDim, coverageDim);

        // Ensure nodata are correct
        noData = wrappedDim.getNoDataValues();
        assertEquals(2, noData.length);
        assertEquals(noData1, noData[0], DELTA);
        assertEquals(noData2, noData[1], DELTA);

        // Extract categories
        categories = wrappedDim.getCategories();

        // Ensure NoData Category is present
        category = categories.get(0);
        assertTrue(category.getName().equals(Category.NODATA.getName()));

        // Check if it contains sampleToGeophisics and the Range contains the first nodata defined
        assertNotNull(category.getSampleToGeophysics());
        assertEquals(category.geophysics(true).getRange().getMinimum(), noData1, DELTA);
        assertEquals(category.geophysics(true).getRange().getMaximum(), noData1, DELTA);
    }

    /**
     * Test that if no range is defined, Category values or Default values are used
     *
     * @throws IOException
     */
    @Test
    public void testNoRange() throws IOException {
        GridSampleDimension sampleDim = new GridSampleDimension("original",
                SampleDimensionType.REAL_64BITS, ColorInterpretation.GRAY_INDEX, null, null,
                new double[] { -9999.0 }, -1000d, 1000d, 1d, 0d, null);

        // Setting coverage dimension
        final CoverageDimensionImpl coverageDim = new CoverageDimensionImpl();
        final String wrappedName = "wrapped";
        coverageDim.setName(wrappedName);
        coverageDim.setDimensionType(SampleDimensionType.REAL_64BITS);
        // Creation of the WrappedSampleDimension
        SampleDimension wrappedDim = new WrappedSampleDimension(sampleDim, coverageDim);
        // Get the range
        NumberRange<? extends Number> wrappedRange = ((WrappedSampleDimension) wrappedDim)
                .getRange();
        // Ensure the range is not present
        assertNull(wrappedRange);
        // Check if min and max are taken from the categories
        assertEquals(-9999, wrappedDim.getMinimumValue(), DELTA);
        assertEquals(1000, wrappedDim.getMaximumValue(), DELTA);
        // Check that the description is equal to the sample dimension name
        assertEquals(wrappedName, wrappedDim.getDescription().toString());

        // Configure a new GridSampleDimension without categories
        sampleDim = new GridSampleDimension("original", null, new BaseUnit<Quantity>("test"));
        // New wrapped sample dimension
        wrappedDim = new WrappedSampleDimension(sampleDim, coverageDim);
        // Get the range
        wrappedRange = ((WrappedSampleDimension) wrappedDim).getRange();
        // Ensure the range is not present
        assertNull(wrappedRange);
        // Check if min and max are taken from the categories
        assertEquals(Double.NEGATIVE_INFINITY, wrappedDim.getMinimumValue(), DELTA);
        assertEquals(Double.POSITIVE_INFINITY, wrappedDim.getMaximumValue(), DELTA);
    }
}
TOP

Related Classes of org.geoserver.catalog.CoverageDimensionCustomizerReaderTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.