Search in sources :

Example 1 with PointOutsideCoverageException

use of org.apache.sis.coverage.PointOutsideCoverageException in project sis by apache.

the class GridExtentTest method testSlice.

/**
 * Tests {@link GridExtent#slice(DirectPosition, int[])}.
 */
@Test
public void testSlice() {
    final GeneralDirectPosition slicePoint = new GeneralDirectPosition(226.7, 47.2);
    final GridExtent extent = create3D();
    final GridExtent slice = extent.slice(slicePoint, new int[] { 1, 2 });
    assertEquals("dimension", 3, slice.getDimension());
    assertExtentEquals(slice, 0, 100, 499);
    assertExtentEquals(slice, 1, 227, 227);
    assertExtentEquals(slice, 2, 47, 47);
    /*
         * Verify that point outside the GridExtent causes an exception to be thrown.
         * The message is localized but the grid coordinates "(900, 47)" are currently
         * unlocalized, so the check below should work in any locale (note that it may
         * change in future SIS version).
         */
    slicePoint.setOrdinate(0, 900);
    try {
        extent.slice(slicePoint, new int[] { 1, 2 });
        fail("Expected PointOutsideCoverageException");
    } catch (PointOutsideCoverageException e) {
        final String message = e.getLocalizedMessage();
        // See above comment.
        assertTrue(message, message.contains("(900, 47)"));
    }
}
Also used : GeneralDirectPosition(org.apache.sis.geometry.GeneralDirectPosition) PointOutsideCoverageException(org.apache.sis.coverage.PointOutsideCoverageException) Test(org.junit.Test)

Example 2 with PointOutsideCoverageException

use of org.apache.sis.coverage.PointOutsideCoverageException in project sis by apache.

the class GridEvaluator method apply.

/**
 * Returns a sequence of double values for a given point in the coverage.
 * The CRS of the given point may be any coordinate reference system;
 * coordinate conversions will be applied as needed.
 * If the CRS of the point is undefined, then it is assumed to be the {@linkplain #getCoverage() coverage} CRS.
 * The returned sequence includes a value for each {@linkplain SampleDimension sample dimension}.
 *
 * <p>The default interpolation type used when accessing grid values for points which fall between
 * grid cells is nearest neighbor. This default interpolation method may change in future version.</p>
 *
 * <p>The default implementation invokes {@link GridCoverage#render(GridExtent)} for a small region
 * around the point. Subclasses should override with more efficient implementation.</p>
 *
 * @param  point   the coordinate point where to evaluate.
 * @return the sample values at the specified point, or {@code null} if the point is outside the coverage.
 *         For performance reason, this method may return the same array
 *         on every method call by overwriting previous values.
 *         Callers should not assume that the array content stay valid for a long time.
 * @throws PointOutsideCoverageException if the evaluation failed because the input point
 *         has invalid coordinates and the {@link #isNullIfOutside()} flag is {@code false}.
 * @throws CannotEvaluateException if the values can not be computed at the specified coordinates
 *         for another reason. This exception may be thrown if the coverage data type can not be
 *         converted to {@code double} by an identity or widening conversion.
 *         Subclasses may relax this constraint if appropriate.
 */
@Override
public double[] apply(final DirectPosition point) throws CannotEvaluateException {
    /*
         * TODO: instead of restricting to a single point, keep the automatic size (1 or 2),
         * invoke render for each slice, then interpolate. We would keep a value of 1 in the
         * size array if we want to disable interpolation in some particular axis (e.g. time).
         */
    final GridGeometry gridGeometry = coverage.gridGeometry;
    final long[] size = new long[gridGeometry.getDimension()];
    java.util.Arrays.fill(size, 1);
    try {
        final FractionalGridCoordinates gc = toGridPosition(point);
        try {
            final GridExtent subExtent = gc.toExtent(gridGeometry.extent, size, nullIfOutside);
            if (subExtent != null) {
                return evaluate(coverage.render(subExtent), 0, 0);
            }
        } catch (ArithmeticException | IndexOutOfBoundsException | DisjointExtentException ex) {
            if (!nullIfOutside) {
                throw (PointOutsideCoverageException) new PointOutsideCoverageException(gc.pointOutsideCoverage(gridGeometry.extent)).initCause(ex);
            }
        }
    } catch (PointOutsideCoverageException ex) {
        throw ex;
    } catch (RuntimeException | TransformException ex) {
        throw new CannotEvaluateException(ex.getMessage(), ex);
    }
    // May reach this point only if `nullIfOutside` is true.
    return null;
}
Also used : PointOutsideCoverageException(org.apache.sis.coverage.PointOutsideCoverageException) TransformException(org.opengis.referencing.operation.TransformException) CannotEvaluateException(org.apache.sis.coverage.CannotEvaluateException)

Example 3 with PointOutsideCoverageException

use of org.apache.sis.coverage.PointOutsideCoverageException in project sis by apache.

the class GridCoverage2DTest method testEvaluator.

/**
 * Tests {@link GridEvaluator#apply(DirectPosition)}.
 */
@Test
public void testEvaluator() {
    final GridEvaluator evaluator = createTestCoverage().evaluator();
    /*
         * Test evaluation at indeger indices. No interpolation should be applied.
         */
    assertArrayEquals(new double[] { 2 }, evaluator.apply(new DirectPosition2D(0, 0)), STRICT);
    assertArrayEquals(new double[] { 5 }, evaluator.apply(new DirectPosition2D(1, 0)), STRICT);
    assertArrayEquals(new double[] { -5 }, evaluator.apply(new DirectPosition2D(0, 1)), STRICT);
    assertArrayEquals(new double[] { -10 }, evaluator.apply(new DirectPosition2D(1, 1)), STRICT);
    /*
         * Test evaluation at fractional indices. Current interpolation is nearest neighor rounding,
         * but future version may do a bilinear interpolation.
         */
    assertArrayEquals(new double[] { 2 }, evaluator.apply(new DirectPosition2D(-0.499, -0.499)), STRICT);
    assertArrayEquals(new double[] { 2 }, evaluator.apply(new DirectPosition2D(0.499, 0.499)), STRICT);
    /*
         * Test some points that are outside the coverage extent.
         */
    try {
        evaluator.apply(new DirectPosition2D(-0.51, 0));
        fail("Expected PointOutsideCoverageException.");
    } catch (PointOutsideCoverageException ex) {
        assertNotNull(ex.getMessage());
    }
    try {
        evaluator.apply(new DirectPosition2D(1.51, 0));
        fail("Expected PointOutsideCoverageException.");
    } catch (PointOutsideCoverageException ex) {
        assertNotNull(ex.getMessage());
    }
}
Also used : PointOutsideCoverageException(org.apache.sis.coverage.PointOutsideCoverageException) DirectPosition2D(org.apache.sis.geometry.DirectPosition2D) Test(org.junit.Test)

Aggregations

PointOutsideCoverageException (org.apache.sis.coverage.PointOutsideCoverageException)3 Test (org.junit.Test)2 CannotEvaluateException (org.apache.sis.coverage.CannotEvaluateException)1 DirectPosition2D (org.apache.sis.geometry.DirectPosition2D)1 GeneralDirectPosition (org.apache.sis.geometry.GeneralDirectPosition)1 TransformException (org.opengis.referencing.operation.TransformException)1