Search in sources :

Example 16 with OperationMethod

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

the class CC_OperationMethod method group.

/**
 * Wraps the given descriptors in a descriptor group of the given name. If the given name can be matched
 * to the name of one of the predefined operation method, then the predefined parameters will be used.
 *
 * <p>We try to use predefined parameters if possible because they contain information, especially the
 * {@link org.opengis.parameter.ParameterDescriptor#getValueClass()} property, which are not available
 * in the GML document.</p>
 *
 * <div class="note"><b>Note:</b>
 * this code is defined in this {@code CC_OperationMethod} class instead than in the
 * {@link DefaultOperationMethod} class in the hope to reduce the amount of code processed
 * by the JVM in the common case where JAXB (un)marshalling is not needed.</div>
 *
 * @param  name         the operation method name, to be also given to the descriptor group.
 * @param  descriptors  the parameter descriptors to wrap in a group. This array will be modified in-place.
 * @return a parameter group containing at least the given descriptors, or equivalent descriptors.
 */
public static ParameterDescriptorGroup group(final Identifier name, final GeneralParameterDescriptor[] descriptors) {
    OperationMethod method;
    try {
        method = CoordinateOperations.factory().getOperationMethod(name.getCode());
    } catch (FactoryException e) {
        // Use DefaultOperationMethod as the source class because it is the first public class in callers.
        Context.warningOccured(Context.current(), DefaultOperationMethod.class, "setDescriptors", e, true);
        method = null;
    }
    final Map<String, ?> properties = Collections.singletonMap(ParameterDescriptorGroup.NAME_KEY, name);
    if (method != null) {
        /*
             * Verify that the pre-defined operation method contains at least all the parameters specified by
             * the 'descriptors' array. If this is the case, then the pre-defined parameters will be used in
             * replacement of the given ones.
             */
        final ParameterDescriptorGroup parameters = method.getParameters();
        return CC_GeneralOperationParameter.merge(DefaultOperationMethod.class, properties, IdentifiedObjects.getProperties(parameters), 1, 1, descriptors, parameters, true);
    }
    return new DefaultParameterDescriptorGroup(properties, 1, 1, descriptors);
}
Also used : DefaultParameterDescriptorGroup(org.apache.sis.parameter.DefaultParameterDescriptorGroup) FactoryException(org.opengis.util.FactoryException) ParameterDescriptorGroup(org.opengis.parameter.ParameterDescriptorGroup) DefaultParameterDescriptorGroup(org.apache.sis.parameter.DefaultParameterDescriptorGroup) DefaultOperationMethod(org.apache.sis.referencing.operation.DefaultOperationMethod) DefaultOperationMethod(org.apache.sis.referencing.operation.DefaultOperationMethod) OperationMethod(org.opengis.referencing.operation.OperationMethod)

Example 17 with OperationMethod

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

the class ProvidersTest method ensureParameterUniqueness.

/**
 * Ensures that every parameter instance is unique. Actually this test is not strong requirement.
 * This is only for sharing existing resources by avoiding unnecessary objects duplication.
 *
 * @throws ReflectiveOperationException if the instantiation of a service provider failed.
 */
@Test
public void ensureParameterUniqueness() throws ReflectiveOperationException {
    final Map<GeneralParameterDescriptor, String> groupNames = new IdentityHashMap<>();
    final Map<GeneralParameterDescriptor, GeneralParameterDescriptor> parameters = new HashMap<>();
    final Map<Object, Object> namesAndIdentifiers = new HashMap<>();
    for (final Class<?> c : methods()) {
        final OperationMethod method = (OperationMethod) c.newInstance();
        final ParameterDescriptorGroup group = method.getParameters();
        final String operationName = group.getName().getCode();
        for (final GeneralParameterDescriptor param : group.descriptors()) {
            assertFalse("Parameter declared twice in the same group.", operationName.equals(groupNames.put(param, operationName)));
            /*
                 * Ensure uniqueness of the parameter descriptor as a whole.
                 */
            final Identifier name = param.getName();
            Object existing = parameters.put(param, param);
            if (existing != null && existing != param) {
                fail("Parameter “" + name.getCode() + "” defined in “" + operationName + '”' + " was already defined in “" + groupNames.get(existing) + "”." + " The same instance could be shared.");
            }
            /*
                 * Ensure uniqueness of each name and identifier.
                 */
            existing = namesAndIdentifiers.put(name, name);
            if (existing != null && existing != name) {
                fail("The name of parameter “" + name.getCode() + "” defined in “" + operationName + '”' + " was already defined elsewhere. The same instance could be shared.");
            }
            for (final GenericName alias : param.getAlias()) {
                existing = namesAndIdentifiers.put(alias, alias);
                if (existing != null && existing != alias) {
                    fail("Alias “" + alias + "” of parameter “" + name.getCode() + "” defined in “" + operationName + '”' + " was already defined elsewhere. The same instance could be shared.");
                }
            }
            for (final Identifier id : param.getIdentifiers()) {
                existing = namesAndIdentifiers.put(id, id);
                if (existing != null && existing != id) {
                    fail("Identifier “" + id + "” of parameter “" + name.getCode() + "” defined in “" + operationName + '”' + " was already defined elsewhere. The same instance could be shared.");
                }
            }
        }
    }
}
Also used : IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) IdentityHashMap(java.util.IdentityHashMap) ParameterDescriptorGroup(org.opengis.parameter.ParameterDescriptorGroup) GeneralParameterDescriptor(org.opengis.parameter.GeneralParameterDescriptor) DefaultOperationMethod(org.apache.sis.referencing.operation.DefaultOperationMethod) OperationMethod(org.opengis.referencing.operation.OperationMethod) GenericName(org.opengis.util.GenericName) Identifier(org.opengis.metadata.Identifier) Test(org.junit.Test)

Example 18 with OperationMethod

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

the class ProvidersTest method testRedimension.

/**
 * Tests {@link AbstractProvider#redimension(int, int)} on all providers managed by {@link Providers}.
 */
@Test
public void testRedimension() {
    final Map<Class<?>, Boolean> redimensionables = new HashMap<>(100);
    for (final Class<?> type : methods()) {
        assertNull(type.getName(), redimensionables.put(type, Boolean.FALSE));
    }
    for (final Class<?> type : redimensionables()) {
        assertEquals(type.getName(), Boolean.FALSE, redimensionables.put(type, Boolean.TRUE));
    }
    final Providers providers = new Providers();
    for (final OperationMethod method : providers) {
        if (method instanceof ProviderMock) {
            // Skip the methods that were defined only for test purpose.
            continue;
        }
        final int sourceDimensions = method.getSourceDimensions();
        final int targetDimensions = method.getTargetDimensions();
        final Boolean isRedimensionable = redimensionables.get(method.getClass());
        assertNotNull(method.getClass().getName(), isRedimensionable);
        if (isRedimensionable) {
            for (int newSource = 2; newSource <= 3; newSource++) {
                for (int newTarget = 2; newTarget <= 3; newTarget++) {
                    final OperationMethod redim = ((DefaultOperationMethod) method).redimension(newSource, newTarget);
                    assertEquals("sourceDimensions", newSource, redim.getSourceDimensions().intValue());
                    assertEquals("targetDimensions", newTarget, redim.getTargetDimensions().intValue());
                    if (!(method instanceof Affine)) {
                        if (newSource == sourceDimensions && newTarget == targetDimensions) {
                            assertSame("When asking the original number of dimensions, expected the original instance.", method, redim);
                        } else {
                            assertNotSame("When asking a different number of dimensions, expected a different instance.", method, redim);
                        }
                        assertSame("When asking the original number of dimensions, expected the original instance.", method, ((DefaultOperationMethod) redim).redimension(sourceDimensions, targetDimensions));
                    }
                }
            }
        } else if (method instanceof MapProjection) {
            final OperationMethod proj3D = ((MapProjection) method).redimension(sourceDimensions ^ 1, targetDimensions ^ 1);
            assertNotSame("redimension(3,3) should return a new method.", method, proj3D);
            assertSame("redimension(2,2) should give back the original method.", method, ((DefaultOperationMethod) proj3D).redimension(sourceDimensions, targetDimensions));
            assertSame("Value of redimension(3,3) should have been cached.", proj3D, ((MapProjection) method).redimension(sourceDimensions ^ 1, targetDimensions ^ 1));
        } else
            try {
                ((DefaultOperationMethod) method).redimension(sourceDimensions ^ 1, targetDimensions ^ 1);
                fail("Type " + method.getClass().getName() + " is not in our list of redimensionable methods.");
            } catch (IllegalArgumentException e) {
                final String message = e.getMessage();
                assertTrue(message, message.contains(method.getName().getCode()));
            }
    }
}
Also used : IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) DefaultOperationMethod(org.apache.sis.referencing.operation.DefaultOperationMethod) OperationMethod(org.opengis.referencing.operation.OperationMethod) DefaultOperationMethod(org.apache.sis.referencing.operation.DefaultOperationMethod) Test(org.junit.Test)

Example 19 with OperationMethod

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

the class MolodenskyTest method testRedimension.

/**
 * Implementation of {@link #testRedimension()} to be shared with other provider having similar capability.
 */
static void testRedimension(final AbstractProvider provider) {
    for (int sourceDimensions = 2; sourceDimensions <= 3; sourceDimensions++) {
        for (int targetDimensions = 2; targetDimensions <= 3; targetDimensions++) {
            final OperationMethod redim = provider.redimension(sourceDimensions, targetDimensions);
            assertEquals("sourceDimensions", sourceDimensions, redim.getSourceDimensions().intValue());
            assertEquals("targetDimensions", targetDimensions, redim.getTargetDimensions().intValue());
        }
    }
}
Also used : OperationMethod(org.opengis.referencing.operation.OperationMethod)

Example 20 with OperationMethod

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

the class EPSGFactoryTest method testConversion.

/**
 * Tests the "UTM zone 10N" conversion (EPSG:16010).
 *
 * @throws FactoryException if an error occurred while querying the factory.
 */
@Test
@DependsOnMethod("testProjectedWithSharedConversion")
public void testConversion() throws FactoryException {
    final EPSGFactory factory = TestFactorySource.factory;
    assumeNotNull(factory);
    /*
         * Fetch directly the "UTM zone 10N" operation. Because this operation was not obtained in
         * the context of a projected CRS, the source and target CRS shall be unspecified (i.e. null).
         */
    final CoordinateOperation operation = factory.createCoordinateOperation("16010");
    assertEpsgNameAndIdentifierEqual("UTM zone 10N", 16010, operation);
    assertInstanceOf("EPSG::16010", Conversion.class, operation);
    assertNull("sourceCRS", operation.getSourceCRS());
    assertNull("targetCRS", operation.getTargetCRS());
    assertNull("transform", operation.getMathTransform());
    /*
         * Fetch the "WGS 72 / UTM zone 10N" projected CRS.
         * The operation associated to this CRS should now define the source and target CRS.
         */
    final ProjectedCRS crs = factory.createProjectedCRS("32210");
    final CoordinateOperation projection = crs.getConversionFromBase();
    assertEpsgNameAndIdentifierEqual("WGS 72 / UTM zone 10N", 32210, crs);
    assertEpsgNameAndIdentifierEqual("UTM zone 10N", 16010, projection);
    assertNotSame("The defining conversion and the actual conversion should differ since the " + "actual conversion should have semi-axis length values.", projection, operation);
    assertInstanceOf("EPSG::16010", CylindricalProjection.class, projection);
    assertNotNull("sourceCRS", projection.getSourceCRS());
    assertNotNull("targetCRS", projection.getTargetCRS());
    assertNotNull("transform", projection.getMathTransform());
    /*
         * Compare the conversion obtained directly with the conversion obtained
         * indirectly through a projected CRS. Both should use the same method.
         */
    final OperationMethod copMethod = ((SingleOperation) operation).getMethod();
    final OperationMethod crsMethod = ((SingleOperation) projection).getMethod();
    assertEpsgNameAndIdentifierEqual("Transverse Mercator", 9807, copMethod);
    assertEpsgNameAndIdentifierEqual("Transverse Mercator", 9807, crsMethod);
    try {
        assertSame("Conversion method", copMethod, crsMethod);
        assertSame("Conversion method", copMethod, factory.createOperationMethod("9807"));
    } catch (AssertionError error) {
        out.println("The following contains more information about a JUnit test failure.");
        out.println("See the JUnit report for the stack trace. Below is a cache dump.");
        out.println("See the operation method EPSG:9807 and compare with:");
        out.print("  - Method obtained directly:   ");
        out.println(System.identityHashCode(copMethod));
        out.print("  - Method obtained indirectly: ");
        out.println(System.identityHashCode(crsMethod));
        out.println("Content of EPSGFactory cache:");
        factory.printCacheContent(out);
        throw error;
    }
}
Also used : AbstractCoordinateOperation(org.apache.sis.referencing.operation.AbstractCoordinateOperation) CoordinateOperation(org.opengis.referencing.operation.CoordinateOperation) SingleOperation(org.opengis.referencing.operation.SingleOperation) OperationMethod(org.opengis.referencing.operation.OperationMethod) Test(org.junit.Test) DependsOnMethod(org.apache.sis.test.DependsOnMethod)

Aggregations

OperationMethod (org.opengis.referencing.operation.OperationMethod)29 Test (org.junit.Test)17 ParameterValueGroup (org.opengis.parameter.ParameterValueGroup)11 DependsOnMethod (org.apache.sis.test.DependsOnMethod)8 DefaultOperationMethod (org.apache.sis.referencing.operation.DefaultOperationMethod)6 Identifier (org.opengis.metadata.Identifier)5 HashMap (java.util.HashMap)4 GeneralParameterDescriptor (org.opengis.parameter.GeneralParameterDescriptor)4 CC_OperationParameterGroupTest (org.apache.sis.internal.jaxb.referencing.CC_OperationParameterGroupTest)3 GeneralParameterValue (org.opengis.parameter.GeneralParameterValue)3 ParameterDescriptorGroup (org.opengis.parameter.ParameterDescriptorGroup)3 IdentifiedObject (org.opengis.referencing.IdentifiedObject)3 MathTransform (org.opengis.referencing.operation.MathTransform)3 MathTransformFactory (org.opengis.referencing.operation.MathTransformFactory)3 NoSuchIdentifierException (org.opengis.util.NoSuchIdentifierException)3 IdentityHashMap (java.util.IdentityHashMap)2 DefaultProjectedCRS (org.apache.sis.referencing.crs.DefaultProjectedCRS)2 InvalidGeodeticParameterException (org.apache.sis.referencing.factory.InvalidGeodeticParameterException)2 DefaultConversion (org.apache.sis.referencing.operation.DefaultConversion)2 ParameterDescriptor (org.opengis.parameter.ParameterDescriptor)2