Search in sources :

Example 11 with GenericName

use of org.opengis.util.GenericName in project sis by apache.

the class TypeBuilder method forName.

/**
 * Returns the element of the given name in the given list. The given name does not need to contains
 * all elements of a {@link ScopedName}; it can be only the tip (for example {@code "myName"} instead
 * of {@code "myScope:myName"}) provided that ignoring the name head does not create ambiguity.
 *
 * @param  types  the collection where to search for an element of the given name.
 * @param  name   name of the element to search.
 * @return element of the given name, or {@code null} if none were found.
 * @throws IllegalArgumentException if the given name is ambiguous.
 */
@SuppressWarnings("null")
final <E extends TypeBuilder> E forName(final List<E> types, final String name) {
    // Best type found so far.
    E best = null;
    // If two types are found at the same depth, the other type.
    E ambiguity = null;
    // Number of path elements that we had to ignore in the GenericName.
    int depth = Integer.MAX_VALUE;
    for (final E type : types) {
        GenericName candidate = type.getName();
        for (int d = 0; candidate != null; d++) {
            if (name.equals(candidate.toString())) {
                if (d < depth) {
                    best = type;
                    ambiguity = null;
                    depth = d;
                    break;
                }
                if (d == depth) {
                    ambiguity = type;
                    break;
                }
            }
            if (!(candidate instanceof ScopedName))
                break;
            candidate = ((ScopedName) candidate).tail();
        }
    }
    if (ambiguity != null) {
        throw new IllegalArgumentException(errors().getString(Errors.Keys.AmbiguousName_3, best.getName(), ambiguity.getName(), name));
    }
    return best;
}
Also used : GenericName(org.opengis.util.GenericName) ScopedName(org.opengis.util.ScopedName)

Example 12 with GenericName

use of org.opengis.util.GenericName in project sis by apache.

the class FeatureFormat method formatValue.

/**
 * Appends the given attribute value, in a truncated form if it exceed the maximal value length.
 *
 * @param  value   the value to append.
 * @param  table   where to append the value.
 * @param  length  number of characters appended before this method call in the current table cell.
 * @return number of characters appended after this method call in the current table cell, or -1 if
 *         the length exceed the maximal length (in which case the caller should break iteration).
 */
private int formatValue(final Object value, final TableAppender table, final int length) {
    String text;
    if (value instanceof InternationalString) {
        text = ((InternationalString) value).toString(displayLocale);
    } else if (value instanceof GenericName) {
        text = toString((GenericName) value);
    } else if (value instanceof AbstractIdentifiedType) {
        text = toString(((AbstractIdentifiedType) value).getName());
    } else if (value instanceof IdentifiedObject) {
        text = IdentifiedObjects.getIdentifierOrName((IdentifiedObject) value);
    } else if ((text = Geometries.toString(value)) == null) {
        text = value.toString();
    }
    final int remaining = MAXIMAL_VALUE_LENGTH - length;
    if (remaining >= text.length()) {
        table.append(text);
        return length + text.length();
    } else {
        table.append(text, 0, Math.max(0, remaining - 1)).append('…');
        return -1;
    }
}
Also used : GenericName(org.opengis.util.GenericName) InternationalString(org.opengis.util.InternationalString) InternationalString(org.opengis.util.InternationalString) IdentifiedObject(org.opengis.referencing.IdentifiedObject)

Example 13 with GenericName

use of org.opengis.util.GenericName in project sis by apache.

the class MapProjection method copyAliases.

/**
 * Copies all aliases except the ones for the given authority. If the given replacement is non-null,
 * then it will be used instead of the first occurrence of the omitted name.
 *
 * <p>This method does not copy the primary name. It is caller's responsibility to add it first.</p>
 *
 * @param  template     the parameter from which to copy the aliases.
 * @param  exclude      the authority of the alias to omit. Can not be EPSG.
 * @param  replacement  the alias to use instead of the omitted one, or {@code null} if none.
 * @param  newCode      the identifier to use instead of the omitted one, or {@code null} if none.
 * @param  builder      where to add the aliases.
 * @return the given {@code builder}, for method call chaining.
 */
private static ParameterBuilder copyAliases(final ParameterDescriptor<Double> template, final Citation exclude, GenericName replacement, ReferenceIdentifier newCode, final ParameterBuilder builder) {
    for (GenericName alias : template.getAlias()) {
        if (((Identifier) alias).getAuthority() == exclude) {
            if (replacement == null)
                continue;
            alias = replacement;
            replacement = null;
        }
        builder.addName(alias);
    }
    for (ReferenceIdentifier id : template.getIdentifiers()) {
        if (id.getAuthority() == exclude) {
            if (newCode == null)
                continue;
            id = newCode;
            newCode = null;
        }
        builder.addIdentifier(id);
    }
    return builder;
}
Also used : GenericName(org.opengis.util.GenericName) ReferenceIdentifier(org.opengis.referencing.ReferenceIdentifier)

Example 14 with GenericName

use of org.opengis.util.GenericName in project sis by apache.

the class Types method create.

/**
 * Adds internationalized designation and definition information for all properties in the given type.
 * Then, returns the result of {@link FeatureTypeBuilder#build()}.
 *
 * @param  builder   the feature type builder for which to add designations and definitions.
 * @param  previous  previously created international strings as array of length 2.
 *                   The first element is the designation and the second element is the definition.
 */
private static DefaultFeatureType create(final FeatureTypeBuilder builder, final Map<String, InternationalString[]> previous) {
    for (final PropertyTypeBuilder p : builder.properties()) {
        final GenericName name = p.getName();
        if (!AttributeConvention.contains(name)) {
            final InternationalString[] resources = previous.computeIfAbsent(name.toString(), (key) -> new InternationalString[] { new ResourceInternationalString("org.apache.sis.internal.storage.gpx.Designations", key), new ResourceInternationalString("org.apache.sis.internal.storage.gpx.Definitions", key) });
            p.setDefinition(resources[1]);
            p.setDesignation(resources[0]);
        }
    }
    return builder.build();
}
Also used : PropertyTypeBuilder(org.apache.sis.feature.builder.PropertyTypeBuilder) GenericName(org.opengis.util.GenericName) ResourceInternationalString(org.apache.sis.util.iso.ResourceInternationalString) InternationalString(org.opengis.util.InternationalString) ResourceInternationalString(org.apache.sis.util.iso.ResourceInternationalString)

Example 15 with GenericName

use of org.opengis.util.GenericName in project sis by apache.

the class VerticalDatumTypes method guess.

/**
 * Guesses the type of a datum from its name, aliases or a given vertical axis. This is sometime needed
 * after XML unmarshalling or WKT parsing, since GML 3.2 and ISO 19162 do not contain any attribute for
 * the datum type.
 *
 * <p>This method uses heuristic rules and may be changed in any future SIS version.
 * If the type can not be determined, defaults to {@link VerticalDatumType#OTHER_SURFACE}.</p>
 *
 * @param  name     the name of the datum for which to guess a type, or {@code null} if unknown.
 * @param  aliases  the aliases of the datum for which to guess a type, or {@code null} if unknown.
 * @param  axis     the vertical axis for which to guess a type, or {@code null} if unknown.
 * @return a datum type, or {@link VerticalDatumType#OTHER_SURFACE} if none can be guessed.
 */
public static VerticalDatumType guess(final String name, final Collection<? extends GenericName> aliases, final CoordinateSystemAxis axis) {
    VerticalDatumType type = guess(name);
    if (type != null) {
        return type;
    }
    if (aliases != null) {
        for (final GenericName alias : aliases) {
            type = guess(alias.tip().toString());
            if (type != null) {
                return type;
            }
        }
    }
    if (axis != null) {
        final Unit<?> unit = axis.getUnit();
        if (Units.isLinear(unit)) {
            final String abbreviation = axis.getAbbreviation();
            if (abbreviation.length() == 1) {
                // Expected direction for accepting the type.
                AxisDirection dir = AxisDirection.UP;
                switch(abbreviation.charAt(0)) {
                    case 'h':
                        type = ELLIPSOIDAL;
                        break;
                    case 'H':
                        type = VerticalDatumType.GEOIDAL;
                        break;
                    case 'D':
                        type = VerticalDatumType.DEPTH;
                        dir = AxisDirection.DOWN;
                        break;
                    default:
                        return VerticalDatumType.OTHER_SURFACE;
                }
                if (dir.equals(axis.getDirection())) {
                    return type;
                }
            }
        } else if (Units.isPressure(unit)) {
            return VerticalDatumType.BAROMETRIC;
        }
    }
    return VerticalDatumType.OTHER_SURFACE;
}
Also used : GenericName(org.opengis.util.GenericName) VerticalDatumType(org.opengis.referencing.datum.VerticalDatumType) AxisDirection(org.opengis.referencing.cs.AxisDirection)

Aggregations

GenericName (org.opengis.util.GenericName)46 Test (org.junit.Test)11 ReferenceIdentifier (org.opengis.referencing.ReferenceIdentifier)9 InternationalString (org.opengis.util.InternationalString)9 ArrayList (java.util.ArrayList)5 IdentifiedObject (org.opengis.referencing.IdentifiedObject)5 NameFactory (org.opengis.util.NameFactory)5 Identifier (org.opengis.metadata.Identifier)4 ScopedName (org.opengis.util.ScopedName)4 HashMap (java.util.HashMap)3 DependsOnMethod (org.apache.sis.test.DependsOnMethod)3 IOException (java.io.IOException)2 UncheckedIOException (java.io.UncheckedIOException)2 Collection (java.util.Collection)2 IdentityHashMap (java.util.IdentityHashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 AbstractIdentifiedType (org.apache.sis.feature.AbstractIdentifiedType)2 NameToIdentifier (org.apache.sis.internal.metadata.NameToIdentifier)2 TableAppender (org.apache.sis.io.TableAppender)2