Search in sources :

Example 6 with Matrix

use of org.opengis.referencing.operation.Matrix in project sis by apache.

the class CoordinateSystemsTest method testScaleAndSwapAxesNonSquare.

/**
 * Tests {@link CoordinateSystems#swapAndScaleAxes(CoordinateSystem, CoordinateSystem)} with a non-square matrix.
 *
 * @throws IncommensurableException if a conversion between incompatible units was attempted.
 */
@Test
@DependsOnMethod("testSwapAndScaleAxes")
public void testScaleAndSwapAxesNonSquare() throws IncommensurableException {
    final DefaultCartesianCS cs = new DefaultCartesianCS(singletonMap(NAME_KEY, "Test"), new DefaultCoordinateSystemAxis(getProperties(HardCodedAxes.SOUTHING), "y", AxisDirection.SOUTH, Units.CENTIMETRE), new DefaultCoordinateSystemAxis(getProperties(HardCodedAxes.EASTING), "x", AxisDirection.EAST, Units.MILLIMETRE));
    Matrix matrix = swapAndScaleAxes(HardCodedCS.CARTESIAN_2D, cs);
    assertMatrixEquals("(x,y) → (y,x)", Matrices.create(3, 3, new double[] { 0, -100, 0, 1000, 0, 0, 0, 0, 1 }), matrix, STRICT);
    matrix = swapAndScaleAxes(HardCodedCS.CARTESIAN_3D, cs);
    assertMatrixEquals("(x,y,z) → (y,x)", Matrices.create(3, 4, new double[] { 0, -100, 0, 0, 1000, 0, 0, 0, 0, 0, 0, 1 }), matrix, STRICT);
}
Also used : Matrix(org.opengis.referencing.operation.Matrix) Test(org.junit.Test) DependsOnMethod(org.apache.sis.test.DependsOnMethod)

Example 7 with Matrix

use of org.opengis.referencing.operation.Matrix in project sis by apache.

the class DatumShiftGrid method derivativeInCell.

/**
 * Returns the derivative at the given grid indices.
 *
 * <div class="section">Default implementation</div>
 * The current implementation assumes that the derivative is constant everywhere in the cell
 * at the given indices. It does not yet take in account the fractional part of {@code gridX}
 * and {@code gridY}, because empirical tests suggest that the accuracy of such interpolation
 * is uncertain.
 *
 * @param  gridX  first grid ordinate of the point for which to get the translation.
 * @param  gridY  second grid ordinate of the point for which to get the translation.
 * @return the derivative at the given location.
 */
public Matrix derivativeInCell(final double gridX, final double gridY) {
    final int ix = Math.max(0, Math.min(gridSize[0] - 2, (int) gridX));
    final int iy = Math.max(0, Math.min(gridSize[1] - 2, (int) gridY));
    final Matrix derivative = Matrices.createDiagonal(getTranslationDimensions(), gridSize.length);
    for (int j = derivative.getNumRow(); --j >= 0; ) {
        final double orig = getCellValue(j, iy, ix);
        derivative.setElement(j, 0, derivative.getElement(j, 0) + (getCellValue(j, iy + 1, ix) - orig));
        derivative.setElement(j, 1, derivative.getElement(j, 1) + (getCellValue(j, iy, ix + 1) - orig));
    }
    return derivative;
}
Also used : Matrix(org.opengis.referencing.operation.Matrix)

Example 8 with Matrix

use of org.opengis.referencing.operation.Matrix in project sis by apache.

the class DatumShiftGrid method computeConversionFactors.

/**
 * Computes the conversion factors needed by {@link #interpolateInCell(double, double, double[])}.
 * This method takes only the 2 first dimensions. If a conversion factor can not be computed,
 * then it is set to NaN.
 */
@SuppressWarnings("fallthrough")
private void computeConversionFactors() {
    scaleX = Double.NaN;
    scaleY = Double.NaN;
    x0 = Double.NaN;
    y0 = Double.NaN;
    final double toStandardUnit = Units.toStandardUnit(coordinateUnit);
    if (!Double.isNaN(toStandardUnit)) {
        final Matrix m = coordinateToGrid.getMatrix();
        if (Matrices.isAffine(m)) {
            final int n = m.getNumCol() - 1;
            switch(m.getNumRow()) {
                // Fall through
                default:
                    y0 = m.getElement(1, n);
                    scaleY = diagonal(m, 1, n) / toStandardUnit;
                case 1:
                    x0 = m.getElement(0, n);
                    scaleX = diagonal(m, 0, n) / toStandardUnit;
                case 0:
                    break;
            }
        }
    }
}
Also used : Matrix(org.opengis.referencing.operation.Matrix)

Example 9 with Matrix

use of org.opengis.referencing.operation.Matrix in project sis by apache.

the class TensorParameters method toMatrix.

/**
 * Constructs a matrix from a group of parameters.
 * This operation is allowed only for tensors of {@linkplain #rank() rank} 2.
 *
 * @param  parameters  the group of parameters.
 * @return a matrix constructed from the specified group of parameters.
 * @throws InvalidParameterNameException if a parameter name was not recognized.
 *
 * @see #createValueGroup(Map, Matrix)
 */
public Matrix toMatrix(final ParameterValueGroup parameters) throws InvalidParameterNameException {
    if (rank() != 2) {
        throw new IllegalStateException();
    }
    ArgumentChecks.ensureNonNull("parameters", parameters);
    if (parameters instanceof TensorValues) {
        // More efficient implementation
        return ((TensorValues) parameters).toMatrix();
    }
    // Fallback on the general case (others implementations)
    final ParameterValue<?> numRow = parameters.parameter(dimensions[0].getName().getCode());
    final ParameterValue<?> numCol = parameters.parameter(dimensions[1].getName().getCode());
    final Matrix matrix = Matrices.createDiagonal(numRow.intValue(), numCol.intValue());
    final List<GeneralParameterValue> values = parameters.values();
    if (values != null) {
        for (final GeneralParameterValue param : values) {
            if (param == numRow || param == numCol) {
                continue;
            }
            final String name = param.getDescriptor().getName().getCode();
            IllegalArgumentException cause = null;
            int[] indices = null;
            try {
                indices = nameToIndices(name);
            } catch (IllegalArgumentException e) {
                cause = e;
            }
            if (indices == null) {
                throw (InvalidParameterNameException) new InvalidParameterNameException(Errors.format(Errors.Keys.UnexpectedParameter_1, name), name).initCause(cause);
            }
            matrix.setElement(indices[0], indices[1], ((ParameterValue<?>) param).doubleValue());
        }
    }
    return matrix;
}
Also used : GeneralParameterValue(org.opengis.parameter.GeneralParameterValue) Matrix(org.opengis.referencing.operation.Matrix) InvalidParameterNameException(org.opengis.parameter.InvalidParameterNameException)

Example 10 with Matrix

use of org.opengis.referencing.operation.Matrix in project sis by apache.

the class DefaultTransformationTest method verifyProperties.

/**
 * Asserts that at least some of the properties of the given {@code op} instance have the expected values
 * for an instance created by {@link #createGeocentricTranslation()}.
 */
@SuppressWarnings("SuspiciousToArrayCall")
private static void verifyProperties(final DefaultTransformation op) {
    assertEquals("name", "Tokyo to JGD2000 (GSI)", op.getName().getCode());
    assertEquals("sourceCRS", "Tokyo 1918", op.getSourceCRS().getName().getCode());
    assertEquals("targetCRS", "JGD2000", op.getTargetCRS().getName().getCode());
    assertEquals("method", "Geocentric translations", op.getMethod().getName().getCode());
    assertEquals("parameters", "Geocentric translations", op.getParameterDescriptors().getName().getCode());
    final ParameterValueGroup parameters = op.getParameterValues();
    final ParameterValue<?>[] values = parameters.values().toArray(new ParameterValue<?>[3]);
    assertEquals("parameters", "Geocentric translations", parameters.getDescriptor().getName().getCode());
    assertEquals("parameters[0]", "X-axis translation", values[0].getDescriptor().getName().getCode());
    assertEquals("parameters[1]", "Y-axis translation", values[1].getDescriptor().getName().getCode());
    assertEquals("parameters[2]", "Z-axis translation", values[2].getDescriptor().getName().getCode());
    assertEquals("parameters[0]", -146.414, values[0].doubleValue(), STRICT);
    assertEquals("parameters[1]", 507.337, values[1].doubleValue(), STRICT);
    assertEquals("parameters[2]", 680.507, values[2].doubleValue(), STRICT);
    assertEquals(3, values.length);
    final Matrix m = MathTransforms.getMatrix(op.getMathTransform());
    assertNotNull("transform", m);
    for (int j = m.getNumRow(); --j >= 0; ) {
        for (int i = m.getNumCol(); --i >= 0; ) {
            double expected = (i == j) ? 1 : 0;
            if (i == 2)
                switch(j) {
                    case 0:
                        expected = -146.414;
                        break;
                    case 1:
                        expected = 507.337;
                        break;
                    case 2:
                        expected = 680.507;
                        break;
                }
            assertEquals(expected, m.getElement(j, i), STRICT);
        }
    }
}
Also used : Matrix(org.opengis.referencing.operation.Matrix) ParameterValue(org.opengis.parameter.ParameterValue) ParameterValueGroup(org.opengis.parameter.ParameterValueGroup)

Aggregations

Matrix (org.opengis.referencing.operation.Matrix)63 Test (org.junit.Test)20 DependsOnMethod (org.apache.sis.test.DependsOnMethod)11 MathTransform (org.opengis.referencing.operation.MathTransform)8 HashMap (java.util.HashMap)6 ParameterValueGroup (org.opengis.parameter.ParameterValueGroup)6 NoninvertibleTransformException (org.opengis.referencing.operation.NoninvertibleTransformException)5 TransformException (org.opengis.referencing.operation.TransformException)5 FormattableObject (org.apache.sis.io.wkt.FormattableObject)4 DirectPosition1D (org.apache.sis.geometry.DirectPosition1D)3 ExtendedPrecisionMatrix (org.apache.sis.internal.referencing.ExtendedPrecisionMatrix)3 Matrix3 (org.apache.sis.referencing.operation.matrix.Matrix3)3 CoordinateSystem (org.opengis.referencing.cs.CoordinateSystem)3 FactoryException (org.opengis.util.FactoryException)3 DirectPosition2D (org.apache.sis.geometry.DirectPosition2D)2 DirectPositionView (org.apache.sis.internal.referencing.DirectPositionView)2 Parameterized (org.apache.sis.parameter.Parameterized)2 MatrixSIS (org.apache.sis.referencing.operation.matrix.MatrixSIS)2 GeneralParameterValue (org.opengis.parameter.GeneralParameterValue)2 ParameterValue (org.opengis.parameter.ParameterValue)2