Search in sources :

Example 21 with ParameterDescriptorGroup

use of org.opengis.parameter.ParameterDescriptorGroup in project sis by apache.

the class DefaultOperationMethod method updateDescriptors.

/**
 * Invoked by {@link AbstractSingleOperation} for completing the parameter descriptor.
 */
final void updateDescriptors(final GeneralParameterDescriptor[] descriptors) {
    final ParameterDescriptorGroup previous = parameters;
    parameters = new DefaultParameterDescriptorGroup(IdentifiedObjects.getProperties(previous), previous.getMinimumOccurs(), previous.getMaximumOccurs(), descriptors);
}
Also used : DefaultParameterDescriptorGroup(org.apache.sis.parameter.DefaultParameterDescriptorGroup) ParameterDescriptorGroup(org.opengis.parameter.ParameterDescriptorGroup) DefaultParameterDescriptorGroup(org.apache.sis.parameter.DefaultParameterDescriptorGroup)

Example 22 with ParameterDescriptorGroup

use of org.opengis.parameter.ParameterDescriptorGroup 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 23 with ParameterDescriptorGroup

use of org.opengis.parameter.ParameterDescriptorGroup in project sis by apache.

the class CC_OperationMethod method store.

/**
 * Stores the given {@code parameters} into the given {@code addTo} collection.
 * This method copies only the <em>references</em> if possible. However is some
 * cases the values may need to be copied in new parameter instances.
 *
 * <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  parameters    the parameters to add to the {@code addTo} collection.
 * @param  addTo         where to store the {@code parameters}.
 * @param  replacements  the replacements to apply in the {@code GeneralParameterValue} instances.
 */
public static void store(final GeneralParameterValue[] parameters, final Collection<GeneralParameterValue> addTo, final Map<GeneralParameterDescriptor, GeneralParameterDescriptor> replacements) {
    for (GeneralParameterValue p : parameters) {
        final GeneralParameterDescriptor replacement = replacements.get(p.getDescriptor());
        if (replacement != null) {
            if (p instanceof ParameterValue<?>) {
                final ParameterValue<?> source = (ParameterValue<?>) p;
                final ParameterValue<?> target = new DefaultParameterValue<>((ParameterDescriptor<?>) replacement);
                final Object value = source.getValue();
                final Unit<?> unit = source.getUnit();
                if (unit == null) {
                    target.setValue(value);
                } else if (value instanceof double[]) {
                    target.setValue((double[]) value, unit);
                } else {
                    target.setValue(((Number) value).doubleValue(), unit);
                }
                p = target;
            } else if (p instanceof ParameterValueGroup) {
                final ParameterValueGroup source = (ParameterValueGroup) p;
                final ParameterValueGroup target = new DefaultParameterValueGroup((ParameterDescriptorGroup) replacement);
                final Collection<GeneralParameterValue> values = source.values();
                store(values.toArray(new GeneralParameterValue[values.size()]), target.values(), replacements);
                p = target;
            }
        }
        addTo.add(p);
    }
}
Also used : DefaultParameterValue(org.apache.sis.parameter.DefaultParameterValue) GeneralParameterValue(org.opengis.parameter.GeneralParameterValue) ParameterValue(org.opengis.parameter.ParameterValue) DefaultParameterValue(org.apache.sis.parameter.DefaultParameterValue) GeneralParameterValue(org.opengis.parameter.GeneralParameterValue) DefaultParameterValueGroup(org.apache.sis.parameter.DefaultParameterValueGroup) ParameterValueGroup(org.opengis.parameter.ParameterValueGroup) ParameterDescriptorGroup(org.opengis.parameter.ParameterDescriptorGroup) DefaultParameterDescriptorGroup(org.apache.sis.parameter.DefaultParameterDescriptorGroup) GeneralParameterDescriptor(org.opengis.parameter.GeneralParameterDescriptor) DefaultParameterValueGroup(org.apache.sis.parameter.DefaultParameterValueGroup) Collection(java.util.Collection)

Example 24 with ParameterDescriptorGroup

use of org.opengis.parameter.ParameterDescriptorGroup 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 25 with ParameterDescriptorGroup

use of org.opengis.parameter.ParameterDescriptorGroup in project sis by apache.

the class SingleOperationMarshallingTest method createMercatorMethod.

/**
 * Creates the test operation method.
 */
private static DefaultOperationMethod createMercatorMethod() {
    final ParameterBuilder builder = new ParameterBuilder();
    builder.setCodeSpace(EPSG, "EPSG").setRequired(true);
    ParameterDescriptor<?>[] parameters = { builder.addIdentifier("8801").addName("Latitude of natural origin").create(0, Units.DEGREE), builder.addIdentifier("8802").addName("Longitude of natural origin").create(0, Units.DEGREE) // There is more parameters for a Mercator projection, but 2 is enough for this test.
    };
    builder.addName(null, "Mercator (1SP)");
    final ParameterDescriptorGroup descriptor = builder.createGroup(parameters);
    final Map<String, Object> properties = new HashMap<>(4);
    properties.put(DefaultOperationMethod.NAME_KEY, descriptor.getName());
    properties.put(DefaultOperationMethod.FORMULA_KEY, new DefaultFormula("See EPSG guide."));
    return new DefaultOperationMethod(properties, 2, 2, descriptor);
}
Also used : HashMap(java.util.HashMap) GeneralParameterDescriptor(org.opengis.parameter.GeneralParameterDescriptor) ParameterDescriptor(org.opengis.parameter.ParameterDescriptor) ParameterDescriptorGroup(org.opengis.parameter.ParameterDescriptorGroup) ParameterBuilder(org.apache.sis.parameter.ParameterBuilder)

Aggregations

ParameterDescriptorGroup (org.opengis.parameter.ParameterDescriptorGroup)27 DefaultParameterDescriptorGroup (org.apache.sis.parameter.DefaultParameterDescriptorGroup)13 GeneralParameterDescriptor (org.opengis.parameter.GeneralParameterDescriptor)13 Test (org.junit.Test)8 HashMap (java.util.HashMap)7 DependsOnMethod (org.apache.sis.test.DependsOnMethod)6 ParameterDescriptor (org.opengis.parameter.ParameterDescriptor)5 ParameterValueGroup (org.opengis.parameter.ParameterValueGroup)5 DefaultOperationMethod (org.apache.sis.referencing.operation.DefaultOperationMethod)4 ParameterNotFoundException (org.opengis.parameter.ParameterNotFoundException)4 OperationMethod (org.opengis.referencing.operation.OperationMethod)3 ArrayList (java.util.ArrayList)2 IdentityHashMap (java.util.IdentityHashMap)2 FormattableObject (org.apache.sis.io.wkt.FormattableObject)2 ParameterBuilder (org.apache.sis.parameter.ParameterBuilder)2 GeneralParameterValue (org.opengis.parameter.GeneralParameterValue)2 FactoryException (org.opengis.util.FactoryException)2 Collection (java.util.Collection)1 LinkedHashMap (java.util.LinkedHashMap)1 UnmodifiableArrayList (org.apache.sis.internal.util.UnmodifiableArrayList)1