This is an automated email from the ASF dual-hosted git repository.

desruisseaux pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git

commit 9fd226e1106392adde7f658600b0c72be2a02ee3
Author: Martin Desruisseaux <martin.desruisse...@geomatys.com>
AuthorDate: Mon Aug 8 13:44:51 2022 +0200

    Allow to request parameter value in a unit of measurement different than 
the one specified by the parameter descriptor.
    Add a clarification in javadoc about implicit map projection parameters.
---
 .../org/apache/sis/parameter/ParameterBuilder.java | 12 ++++---
 .../java/org/apache/sis/parameter/Parameters.java  | 37 +++++++++++++++++++++-
 2 files changed, 44 insertions(+), 5 deletions(-)

diff --git 
a/core/sis-referencing/src/main/java/org/apache/sis/parameter/ParameterBuilder.java
 
b/core/sis-referencing/src/main/java/org/apache/sis/parameter/ParameterBuilder.java
index fc3881964f..6e96894c2d 100644
--- 
a/core/sis-referencing/src/main/java/org/apache/sis/parameter/ParameterBuilder.java
+++ 
b/core/sis-referencing/src/main/java/org/apache/sis/parameter/ParameterBuilder.java
@@ -420,10 +420,14 @@ public class ParameterBuilder extends 
Builder<ParameterBuilder> {
      *     <td>Array of 1 or 2 elements mapped to {@code 
"standard_parallel_1"} and {@code "standard_parallel_2"}.</td></tr>
      * </table>
      *
-     * <div class="note"><b>Note:</b>
-     * When the {@code "earth_radius"} parameter is read, its value is the
-     * {@linkplain 
org.apache.sis.referencing.datum.DefaultEllipsoid#getAuthalicRadius() authalic 
radius}
-     * computed from the semi-major and semi-minor axis lengths.</div>
+     * <b>Notes:</b>
+     * <ul>
+     *   <li>The {@code "standard_parallel"} parameter descriptor is added 
only if the {@code parameters} argument
+     *       contains {@code "standard_parallel_1"} and {@code 
"standard_parallel_2"} descriptors.</li>
+     *   <li>When the {@code "earth_radius"} parameter is read, its value is 
the
+     *       {@linkplain 
org.apache.sis.referencing.datum.DefaultEllipsoid#getAuthalicRadius() authalic 
radius}
+     *       computed from the semi-major and semi-minor axis lengths.</li>
+     * </ul>
      *
      * Map projection parameter groups always have a {@linkplain 
DefaultParameterDescriptorGroup#getMinimumOccurs()
      * minimum} and {@linkplain 
DefaultParameterDescriptorGroup#getMaximumOccurs() maximum occurrence} of 1,
diff --git 
a/core/sis-referencing/src/main/java/org/apache/sis/parameter/Parameters.java 
b/core/sis-referencing/src/main/java/org/apache/sis/parameter/Parameters.java
index 27a5a4e33b..fc7ef71141 100644
--- 
a/core/sis-referencing/src/main/java/org/apache/sis/parameter/Parameters.java
+++ 
b/core/sis-referencing/src/main/java/org/apache/sis/parameter/Parameters.java
@@ -22,6 +22,7 @@ import java.util.List;
 import java.io.Serializable;
 import javax.xml.bind.annotation.XmlTransient;
 import javax.measure.Unit;
+import javax.measure.IncommensurableException;
 import org.opengis.util.MemberName;
 import org.opengis.metadata.Identifier;
 import org.opengis.metadata.citation.Citation;
@@ -101,7 +102,7 @@ import org.apache.sis.util.Debug;
  * overriding one method has no impact on other methods.
  *
  * @author  Martin Desruisseaux (Geomatys)
- * @version 1.1
+ * @version 1.3
  * @since   0.4
  * @module
  */
@@ -669,6 +670,40 @@ public abstract class Parameters implements 
ParameterValueGroup, Cloneable {
         }
     }
 
+    /**
+     * Returns the floating point value of the parameter identified by the 
given descriptor,
+     * converted to the given unit of measurement. See {@link 
#getValue(ParameterDescriptor)}
+     * for more information about how this method uses the given {@code 
parameter} argument.
+     *
+     * @param  parameter  the name or alias of the parameter to look for.
+     * @param  unit       the desired unit of measurement.
+     * @return the requested parameter value if it exists, or the 
<strong>non-null</strong>
+     *         {@linkplain DefaultParameterDescriptor#getDefaultValue() 
default value} otherwise.
+     * @throws ParameterNotFoundException if the given {@code parameter} name 
or alias is not legal for this group.
+     * @throws IllegalStateException if the value is not defined and there is 
no default value.
+     * @throws IllegalArgumentException if the specified unit is invalid for 
the parameter.
+     *
+     * @see DefaultParameterValue#doubleValue(Unit)
+     *
+     * @since 1.3
+     */
+    public double doubleValue(final ParameterDescriptor<? extends Number> 
parameter, final Unit<?> unit) throws ParameterNotFoundException {
+        ArgumentChecks.ensureNonNull("unit", unit);
+        final ParameterValue<?> value = getParameter(parameter);
+        if (value != null) {
+            return value.doubleValue(unit);
+        } else {
+            double d = defaultValue(parameter).doubleValue();
+            final Unit<?> source = parameter.getUnit();
+            if (source != null) try {
+                d = source.getConverterToAny(unit).convert(d);
+            } catch (IncommensurableException e) {
+                throw new 
IllegalArgumentException(Errors.format(Errors.Keys.IncompatibleUnits_2, source, 
unit), e);
+            }
+            return d;
+        }
+    }
+
     /**
      * Returns the floating point values of the parameter identified by the 
given descriptor.
      * See {@link #getValue(ParameterDescriptor)} for more information about 
how this method

Reply via email to