Java Code Examples for org.opengis.geometry.DirectPosition#setOrdinate()

The following examples show how to use org.opengis.geometry.DirectPosition#setOrdinate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TransformSeparatorTest.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * Compares coordinate computed by a reference with coordinates computed by the transform to test.
 * We use this method when we can not easily analyze the {@link MathTransform} created by the test
 * case, for example because it may have been rearranged in arbitrary ways for optimization purpose
 * (e.g. {@link PassThroughTransform#tryConcatenate(boolean, MathTransform, MathTransformFactory)}).
 *
 * @param  tr1     first half of the transform to use as a reference.
 * @param  tr2     second half of the transform to use as a reference.
 * @param  test    the transform to test.
 * @param  random  random number generator for coordinate values.
 */
private static void compare(final MathTransform tr1, final MathTransform tr2, final MathTransform test, final Random random)
        throws TransformException
{
    DirectPosition source   = new GeneralDirectPosition(tr1.getSourceDimensions());
    DirectPosition step     = null;
    DirectPosition expected = null;
    DirectPosition actual   = null;
    for (int t=0; t<50; t++) {
        for (int i=source.getDimension(); --i>=0;) {
            source.setOrdinate(i, random.nextDouble());
        }
        step     = tr1 .transform(source,   step);
        expected = tr2 .transform(step, expected);
        actual   = test.transform(source, actual);
        assertEquals(expected, actual);
    }
}
 
Example 2
Source File: DirectPosition1DTest.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * Tests {@link DirectPosition2D#equals(Object)} method between different implementations.
 * The purpose of this test is also to run the assertion in the direct position implementations.
 */
@Test
public void testEquals() {
    assertTrue(DirectPosition1D     .class.desiredAssertionStatus());
    assertTrue(GeneralDirectPosition.class.desiredAssertionStatus());

    DirectPosition p1 = new DirectPosition1D     (48.543261561072285);
    DirectPosition p2 = new GeneralDirectPosition(48.543261561072285);
    assertTrue(p1.equals(p2));
    assertTrue(p2.equals(p1));
    assertEquals(p2.hashCode(), p1.hashCode());

    p1.setOrdinate(0, p1.getOrdinate(0) + 1);
    assertFalse(p1.equals(p2));
    assertFalse(p2.equals(p1));
    assertFalse(p2.hashCode() == p1.hashCode());
}
 
Example 3
Source File: DirectPosition2DTest.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * Tests {@link DirectPosition2D#equals(Object)} method between different implementations.
 * The purpose of this test is also to run the assertion in the direct position implementations.
 */
@Test
public void testEquals() {
    assertTrue(DirectPosition2D     .class.desiredAssertionStatus());
    assertTrue(GeneralDirectPosition.class.desiredAssertionStatus());

    DirectPosition p1 = new DirectPosition2D     (48.543261561072285, -123.47009555832284);
    DirectPosition p2 = new GeneralDirectPosition(48.543261561072285, -123.47009555832284);
    assertTrue(p1.equals(p2));
    assertTrue(p2.equals(p1));
    assertEquals(p2.hashCode(), p1.hashCode());

    p1.setOrdinate(0, p1.getOrdinate(0) + 1);
    assertFalse(p1.equals(p2));
    assertFalse(p2.equals(p1));
    assertFalse(p2.hashCode() == p1.hashCode());
}
 
Example 4
Source File: IdentityTransform.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Copies the values from {@code ptSrc} to {@code ptDst}.
 * Overrides the super-class method for performance reason.
 */
@Override
public DirectPosition transform(final DirectPosition ptSrc, final DirectPosition ptDst) {
    ArgumentChecks.ensureDimensionMatches("ptSrc", dimension, ptSrc);
    if (ptDst == null) {
        return new GeneralDirectPosition(ptSrc);
    }
    ArgumentChecks.ensureDimensionMatches("ptDst", dimension, ptDst);
    for (int i=0; i<dimension; i++) {
        ptDst.setOrdinate(i, ptSrc.getOrdinate(i));
    }
    return ptDst;
}
 
Example 5
Source File: WraparoundAdjustment.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Computes a position with coordinates equivalent to the given {@code pointOfInterest}, but
 * potentially shifted to interior of the domain of validity specified at construction time.
 * The dimensions that may be shifted are the ones having an axis with wraparound meaning.
 * In order to perform this operation, the position may be temporarily converted to a geographic CRS
 * and converted back to its original CRS.
 *
 * <p>The coordinate reference system should be specified in the {@code pointOfInterest},
 * or (as a fallback) in the {@code domainOfValidity} specified at construction time.</p>
 *
 * @param  pointOfInterest  the position to potentially shift to domain of validity interior.
 *         If a shift is needed, then the given position will be replaced by a new position;
 *         the given position will not be modified.
 * @return position potentially shifted to the domain of validity interior.
 * @throws TransformException if a coordinate conversion failed.
 */
public DirectPosition shift(DirectPosition pointOfInterest) throws TransformException {
    if (setIfNonNull(pointOfInterest.getCoordinateReferenceSystem())) {
        DirectPosition shifted;
        if (replaceCRS()) {
            shifted = geographicToAOI.inverse().transform(pointOfInterest, null);
        } else {
            shifted = pointOfInterest;              // To be replaced by a copy of 'pointOfInterest' when first needed.
        }
        final CoordinateSystem cs = crs.getCoordinateSystem();
        for (int i=cs.getDimension(); --i >= 0;) {
            final double period = range(cs, i);
            if (period > 0) {
                transformDomainToAOI();
                final double x = shifted.getOrdinate(i);
                double delta = domainOfValidity.getMinimum(i) - x;
                if (delta > 0) {                                        // Test for point before domain of validity.
                    delta = Math.ceil(delta / period);
                } else {
                    delta = domainOfValidity.getMaximum(i) - x;
                    if (delta < 0) {                                    // Test for point after domain of validity.
                        delta = Math.floor(delta / period);
                    } else {
                        continue;
                    }
                }
                if (delta != 0) {
                    isResultTransformed = true;
                    if (shifted == pointOfInterest) {
                        shifted = new GeneralDirectPosition(pointOfInterest);
                    }
                    pointOfInterest = shifted;                         // 'shifted' may have been set before the loop.
                    shifted.setOrdinate(i, x + delta * period);        // TODO: use Math.fma in JDK9.
                }
            }
        }
    }
    return toFinal().transform(pointOfInterest, null);
}
 
Example 6
Source File: GridExtent.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a slice of this given grid extent computed by a ratio between 0 and 1 inclusive.
 * This is a helper method for {@link GridDerivation#sliceByRatio(double, int...)} implementation.
 *
 * @param  slicePoint        a pre-allocated direct position to be overwritten by this method.
 * @param  sliceRatio        the ratio to apply on all grid dimensions except the ones to keep.
 * @param  dimensionsToKeep  the grid dimension to keep unchanged.
 */
final GridExtent sliceByRatio(final DirectPosition slicePoint, final double sliceRatio, final int[] dimensionsToKeep) {
    for (int i=slicePoint.getDimension(); --i >= 0;) {
        slicePoint.setOrdinate(i, sliceRatio * getSize(i, true) + getLow(i));       // TODO: use Math.fma
    }
    for (int i=0; i<dimensionsToKeep.length; i++) {
        slicePoint.setOrdinate(dimensionsToKeep[i], Double.NaN);
    }
    return slice(slicePoint, null);
}
 
Example 7
Source File: CategoryList.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Transforms the specified {@code ptSrc} and stores the result in {@code ptDst}.
 */
@Override
public final DirectPosition transform(final DirectPosition ptSrc, DirectPosition ptDst) throws TransformException {
    ArgumentChecks.ensureNonNull("ptSrc", ptSrc);
    ArgumentChecks.ensureDimensionMatches("ptSrc", 1, ptSrc);
    if (ptDst == null) {
        ptDst = new GeneralDirectPosition(1);
    } else {
        ArgumentChecks.ensureDimensionMatches("ptDst", 1, ptDst);
    }
    ptDst.setOrdinate(0, transform(ptSrc.getOrdinate(0)));
    return ptDst;
}
 
Example 8
Source File: AbstractMathTransform.java    From sis with Apache License 2.0 4 votes vote down vote up
/**
 * Transforms the specified {@code ptSrc} and stores the result in {@code ptDst}.
 * The default implementation performs the following steps:
 *
 * <ul>
 *   <li>Ensures that the dimension of the given points are consistent with the
 *       {@linkplain #getSourceDimensions() source} and {@linkplain #getTargetDimensions()
 *       target dimensions} of this math transform.</li>
 *   <li>Delegates to the {@link #transform(double[], int, double[], int, boolean)} method.</li>
 * </ul>
 *
 * This method does not update the associated {@link org.opengis.referencing.crs.CoordinateReferenceSystem} value.
 *
 * @param  ptSrc  the coordinate point to be transformed.
 * @param  ptDst  the coordinate point that stores the result of transforming {@code ptSrc}, or {@code null}.
 * @return the coordinate point after transforming {@code ptSrc} and storing the result in {@code ptDst},
 *         or a newly created point if {@code ptDst} was null.
 * @throws MismatchedDimensionException if {@code ptSrc} or {@code ptDst} doesn't have the expected dimension.
 * @throws TransformException if the point can not be transformed.
 */
@Override
public DirectPosition transform(final DirectPosition ptSrc, DirectPosition ptDst) throws TransformException {
    final int dimSource = getSourceDimensions();
    final int dimTarget = getTargetDimensions();
    ensureDimensionMatches("ptSrc", dimSource, ptSrc);
    if (ptDst != null) {
        ensureDimensionMatches("ptDst", dimTarget, ptDst);
        /*
         * Transforms the coordinates using a temporary 'double[]' buffer,
         * and copies the transformation result in the destination position.
         */
        final double[] array;
        if (dimSource >= dimTarget) {
            array = ptSrc.getCoordinate();
        } else {
            array = new double[dimTarget];
            for (int i=dimSource; --i>=0;) {
                array[i] = ptSrc.getOrdinate(i);
            }
        }
        transform(array, 0, array, 0, false);
        for (int i=0; i<dimTarget; i++) {
            ptDst.setOrdinate(i, array[i]);
        }
    } else {
        /*
         * Destination not set. We are going to create the destination here. Since we know that the
         * destination will be the SIS implementation, write directly into the `coordinates` array.
         */
        final GeneralDirectPosition destination = new GeneralDirectPosition(dimTarget);
        final double[] source;
        if (dimSource <= dimTarget) {
            source = destination.coordinates;
            for (int i=0; i<dimSource; i++) {
                source[i] = ptSrc.getOrdinate(i);
            }
        } else {
            source = ptSrc.getCoordinate();
        }
        transform(source, 0, destination.coordinates, 0, false);
        ptDst = destination;
    }
    return ptDst;
}