use of org.opengis.parameter.GeneralParameterValue in project sis by apache.
the class Formatter method append.
/**
* Appends the given {@code FormattableObject}.
* This method performs the following steps:
*
* <ul>
* <li>Invoke <code>object.{@linkplain FormattableObject#formatTo(Formatter) formatTo}(this)</code>.</li>
* <li>Prepend the keyword returned by the above method call (e.g. {@code "GEOCS"}).</li>
* <li>If the given object is an instance of {@link IdentifiedObject}, then append complementary information:</li>
* </ul>
*
* <blockquote><table class="sis">
* <caption>Complementary WKT elements</caption>
* <tr><th>WKT 2 element</th><th>WKT 1 element</th><th>For types</th></tr>
* <tr><td>{@code Anchor[…]}</td> <td></td> <td>{@link Datum}</td></tr>
* <tr><td>{@code Scope[…]}</td> <td></td> <td>{@link ReferenceSystem}, {@link Datum}, {@link CoordinateOperation}</td></tr>
* <tr><td>{@code Area[…]}</td> <td></td> <td>{@link ReferenceSystem}, {@link Datum}, {@link CoordinateOperation}</td></tr>
* <tr><td>{@code BBox[…]}</td> <td></td> <td>{@link ReferenceSystem}, {@link Datum}, {@link CoordinateOperation}</td></tr>
* <tr><td>{@code VerticalExtent[…]}</td><td></td> <td>{@link ReferenceSystem}, {@link Datum}, {@link CoordinateOperation}</td></tr>
* <tr><td>{@code TimeExtent[…]}</td> <td></td> <td>{@link ReferenceSystem}, {@link Datum}, {@link CoordinateOperation}</td></tr>
* <tr><td>{@code Id[…]}</td><td>{@code Authority[…]}</td><td>{@link IdentifiedObject}</td></tr>
* <tr><td>{@code Remarks[…]}</td> <td></td> <td>{@link ReferenceSystem}, {@link CoordinateOperation}</td></tr>
* </table></blockquote>
*
* @param object the formattable object to append to the WKT, or {@code null} if none.
*/
public void append(final FormattableObject object) {
if (object == null) {
return;
}
/*
* Safety check: ensure that we do not have circular dependencies (e.g. a ProjectedCRS contains
* a Conversion which may contain the ProjectedCRS as its target CRS). Without this protection,
* a circular dependency would cause an OutOfMemoryError.
*/
final int stackDepth = enclosingElements.size();
for (int i = stackDepth; --i >= 0; ) {
if (enclosingElements.get(i) == object) {
throw new IllegalStateException(Errors.getResources(locale).getString(Errors.Keys.CircularReference));
}
}
enclosingElements.add(object);
if (hasContextualUnit < 0) {
// Test if leftmost bit is set to 1.
throw new IllegalStateException(Errors.getResources(locale).getString(Errors.Keys.TreeDepthExceedsMaximum));
}
hasContextualUnit <<= 1;
/*
* Add a new line if it was requested, open the bracket and increase indentation in case the
* element to format contains other FormattableObject elements.
*/
appendSeparator();
int base = buffer.length();
elementStart = buffer.appendCodePoint(symbols.getOpeningBracket(0)).length();
indent(+1);
/*
* Formats the inner part, then prepend the WKT keyword.
* The result looks like the following:
*
* <previous text>,
* PROJCS["NAD27 / Idaho Central",
* GEOGCS[...etc...],
* ...etc...
*/
IdentifiedObject info = (object instanceof IdentifiedObject) ? (IdentifiedObject) object : null;
String keyword = object.formatTo(this);
if (keyword == null) {
if (info != null) {
setInvalidWKT(info, null);
} else {
setInvalidWKT(object.getClass(), null);
}
keyword = getName(object.getClass());
} else if (toUpperCase != 0) {
final Locale locale = symbols.getLocale();
keyword = (toUpperCase >= 0) ? keyword.toUpperCase(locale) : keyword.toLowerCase(locale);
}
if (highlightError && colors != null) {
final String color = colors.getAnsiSequence(ElementKind.ERROR);
if (color != null) {
buffer.insert(base, color + BACKGROUND_DEFAULT);
base += color.length();
}
}
highlightError = false;
buffer.insert(base, keyword);
/*
* When formatting geometry coordinates, we may need to shift all numbers by the width
* of the keyword inserted above in order to keep numbers properly aligned. Exemple:
*
* BOX[ 4.000 -10.000
* 50.000 2.000]
*/
if (keywordSpaceAt != null) {
final int length = keyword.length();
final CharSequence additionalMargin = CharSequences.spaces(keyword.codePointCount(0, length));
final int n = keywordSpaceAt.size();
for (int i = 0; i < n; ) {
int p = keywordSpaceAt.getInt(i);
// Take in account spaces added previously.
p += (++i * length);
buffer.insert(p, additionalMargin);
}
keywordSpaceAt.clear();
}
/*
* Format the SCOPE["…"], AREA["…"] and other elements. Some of those information
* are available only for Datum, CoordinateOperation and ReferenceSystem objects.
*/
if (info == null && convention.majorVersion() != 1 && object instanceof GeneralParameterValue) {
info = ((GeneralParameterValue) object).getDescriptor();
}
if (info != null) {
appendComplement(info, (stackDepth >= 1) ? enclosingElements.get(stackDepth - 1) : null, (stackDepth >= 2) ? enclosingElements.get(stackDepth - 2) : null);
}
/*
* Close the bracket, then update the queue of enclosed elements by removing this element.
*/
buffer.appendCodePoint(symbols.getClosingBracket(0));
indent(-1);
enclosingElements.remove(stackDepth);
hasContextualUnit >>>= 1;
}
use of org.opengis.parameter.GeneralParameterValue in project sis by apache.
the class TensorValuesTest method testValues.
/**
* Tests {@link TensorValues#values()}.
*/
@Test
@DependsOnMethod("testParameter")
public void testValues() {
final ParameterValueGroup group = createWKT1();
group.parameter(NUM_ROW).setValue(2);
group.parameter(NUM_COL).setValue(3);
List<GeneralParameterValue> values = group.values();
assertValueEquals(NUM_ROW, 2, values.get(0));
assertValueEquals(NUM_COL, 3, values.get(1));
assertEquals("size", 2, values.size());
/*
* Above list had no explicit parameters, since all of them had their default values.
* Now set some parameters to different values. Those parameters should now appear in
* the list.
*/
group.parameter("elt_0_1").setValue(8);
group.parameter("elt_1_1").setValue(7);
group.parameter("elt_1_2").setValue(6);
values = group.values();
assertValueEquals(NUM_ROW, 2, values.get(0));
assertValueEquals(NUM_COL, 3, values.get(1));
assertValueEquals("elt_0_1", 8.0, values.get(2));
assertValueEquals("elt_1_1", 7.0, values.get(3));
assertValueEquals("elt_1_2", 6.0, values.get(4));
assertEquals("size", 5, values.size());
}
use of org.opengis.parameter.GeneralParameterValue in project sis by apache.
the class DefaultParameterValueGroupTest method testValuesAddAllWithSubgroups.
/**
* Tests {@code DefaultParameterValueGroup.values().addAll(…)} with subgroups.
*
* @since 0.6
*/
@Test
@DependsOnMethod({ "testValuesAddAll", "testAddGroup", "testEqualsAndHashCode" })
public void testValuesAddAllWithSubgroups() {
final DefaultParameterDescriptorGroup group, subGroup;
final List<GeneralParameterDescriptor> descriptors = new ArrayList<>(descriptor.descriptors());
subGroup = new DefaultParameterDescriptorGroup(singletonMap(NAME_KEY, "theSubGroup"), 2, 4, descriptors.toArray(new GeneralParameterDescriptor[descriptors.size()]));
descriptors.add(subGroup);
group = new DefaultParameterDescriptorGroup(singletonMap(NAME_KEY, "theGroup"), descriptors.toArray(new GeneralParameterDescriptor[descriptors.size()]));
/*
* Prepare the GeneralParameterValue instances that we are going to add in the above group.
* We assign arbitrary integer values to each instance if order to be able to differentiate
* them, but the purpose of this test is not to verify those integer values.
*
* We intentionally:
* - Omit the creation of a mandatory parameter value
* - Create more sub-groups than the minimum required.
*/
final ParameterValue<?> v2 = (ParameterValue<?>) descriptor.descriptor("Mandatory 2").createValue();
final ParameterValue<?> v3 = (ParameterValue<?>) descriptor.descriptor("Optional 3").createValue();
final ParameterValueGroup g1 = subGroup.createValue();
final ParameterValueGroup g2 = subGroup.createValue();
final ParameterValueGroup g3 = subGroup.createValue();
v2.setValue(4);
v3.setValue(8);
g1.parameter("Mandatory 1").setValue(3);
g2.parameter("Optional 4").setValue(7);
g3.parameter("Mandatory 2").setValue(5);
final List<GeneralParameterValue> expected = new ArrayList<>(6);
assertTrue(expected.add(v2));
assertTrue(expected.add(v3));
assertTrue(expected.add(g1));
assertTrue(expected.add(g2));
assertTrue(expected.add(g3));
/*
* A newly created group should be initialized with 4 GeneralParameterValue instances because of
* the mandatory ones. After we added our own instances created above, the group should contains
* 6 instances (one more than what we added) because of the "Mandatory 1" parameter that we did
* not provided. Note that the element order in the 'values' collection does not need to be the
* order in which we provided our GeneralParameterValue instances.
*/
final List<GeneralParameterValue> values = group.createValue().values();
assertEquals("Initial size", 4, values.size());
assertTrue("List shall be modified", values.addAll(expected));
assertEquals("Size after addAll(…)", 6, values.size());
final ParameterValue<?> v1 = (ParameterValue<?>) values.get(0);
assertEquals("Default value", DefaultParameterDescriptorGroupTest.DEFAULT_VALUE, v1.getValue());
assertTrue(expected.add(v1));
assertSetEquals(expected, values);
}
use of org.opengis.parameter.GeneralParameterValue in project sis by apache.
the class ParameterValueList method set.
/**
* Sets the parameter at the given index. The descriptor of the given parameter must be one of those
* in the {@link DefaultParameterDescriptorGroup#descriptors()} list, and storing that parameter must
* be allowed by the cardinality constraints.
*/
@Override
public GeneralParameterValue set(final int index, final GeneralParameterValue parameter) {
ArgumentChecks.ensureValidIndex(size, index);
final GeneralParameterValue value = values[index];
ArgumentChecks.ensureNonNull("parameter", parameter);
final GeneralParameterDescriptor desc = parameter.getDescriptor();
if (!value.getDescriptor().equals(desc)) {
ensureDescriptorExists(desc);
ensureCanRemove(desc);
ensureCanAdd(desc);
}
values[index] = parameter;
return value;
}
use of org.opengis.parameter.GeneralParameterValue in project sis by apache.
the class ParameterValueList method add.
/**
* Adds a {@link ParameterValue} or an other {@link ParameterValueGroup} to this list.
* If an existing parameter is already included for the same name and adding the new
* parameter would increase the number past what is allowable by {@code maximumOccurs},
* then an {@link InvalidParameterCardinalityException} will be thrown.
*
* @param parameter new parameter to be added to this group.
* @return always {@code true} since this object changes as a result of this call.
* @throws IllegalArgumentException if the specified parameter is not allowable by the groups descriptor.
* @throws InvalidParameterCardinalityException if adding this parameter would result in more parameters
* than allowed by {@code maximumOccurs}.
*/
@Override
public boolean add(final GeneralParameterValue parameter) {
ArgumentChecks.ensureNonNull("parameter", parameter);
final GeneralParameterDescriptor desc = parameter.getDescriptor();
ensureDescriptorExists(desc);
/*
* If we had an uninitialized parameter (a parameter created by the DefaultParameterValueGroup constructor
* and never been queried or set by the user), then the given parameter will replace the uninitialized.
* The intent is to allow users to set its own parameters by a call to group.values().addAll(myParam).
* Otherwise the given parameter will be added, in which case we need to check the cardinality.
*/
final Identifier name = desc.getName();
int count = 0;
for (int i = 0; i < size; i++) {
final GeneralParameterValue value = values[i];
if (name.equals(value.getDescriptor().getName())) {
if (value instanceof UninitializedParameter) {
values[i] = parameter;
return true;
}
count++;
}
}
final int max = desc.getMaximumOccurs();
if (count >= max) {
throw new InvalidParameterCardinalityException(Errors.format(Errors.Keys.TooManyOccurrences_2, max, name), name.getCode());
}
addUnchecked(parameter);
modCount++;
return true;
}
Aggregations