Search in sources :

Example 16 with ReferenceIdentifier

use of org.opengis.referencing.ReferenceIdentifier in project sis by apache.

the class CitationsTest method testEPSG.

/**
 * Special tests dedicated to the {@link Citations#EPSG} constant. This is maybe the most important
 * citation declared in the {@link Citations} class, since it is declared as the authority of almost
 * all Coordinate Reference System (CRS) objects typically used by SIS.
 *
 * <p>Apache SIS identifies the EPSG authority with {@link Identifier} {@code "IOGP:EPSG"}.</p>
 */
@Test
public void testEPSG() {
    final Identifier identifier = getSingleton(EPSG.getIdentifiers());
    assertEquals("EPSG", getUnicodeIdentifier(EPSG));
    assertEquals("IOGP", ((ReferenceIdentifier) identifier).getCodeSpace());
    assertEquals("EPSG", identifier.getCode());
}
Also used : Identifier(org.opengis.metadata.Identifier) ReferenceIdentifier(org.opengis.referencing.ReferenceIdentifier) Test(org.junit.Test)

Example 17 with ReferenceIdentifier

use of org.opengis.referencing.ReferenceIdentifier in project sis by apache.

the class Citations method getIdentifier.

/**
 * Infers an identifier from the given citation, or returns {@code null} if no identifier has been found.
 * This method removes leading and trailing {@linkplain Character#isWhitespace(int) whitespaces}.
 * See {@link org.apache.sis.metadata.iso.citation.Citations#getIdentifier(Citation)}
 * for the public documentation of this method.
 *
 * <p><b>Which method to use:</b></p>
 * <ul>
 *   <li>For information purpose (e.g. some {@code toString()} methods), use {@code getIdentifier(…, false)}.</li>
 *   <li>For WKT formatting, use {@code getIdentifier(…, true)} in order to preserve formatting characters.</li>
 *   <li>For assigning a value to a {@code codeSpace} field, use
 *       {@link org.apache.sis.metadata.iso.citation.Citations#getUnicodeIdentifier(Citation)}.</li>
 * </ul>
 *
 * Use {@code getUnicodeIdentifier(…)} method when assigning values to be returned by methods like
 * {@link ReferenceIdentifier#getCodeSpace()}, since those values are likely to be compared without special
 * care about ignorable identifier characters. But if the intent is to format a more complex string
 * like WKT or {@code toString()}, then we suggest to use {@code getIdentifier(citation, true)} instead,
 * which will produce the same result but preserving the ignorable characters, which can be useful
 * for formatting purpose.
 *
 * @param  citation  the citation for which to get the identifier, or {@code null}.
 * @param  strict    {@code true} for returning a non-null value only if the identifier is a valid Unicode identifier.
 * @return a non-empty identifier for the given citation without leading or trailing whitespaces,
 *         or {@code null} if the given citation is null or does not declare any identifier or title.
 *
 * @see <a href="https://issues.apache.org/jira/browse/SIS-201">SIS-201</a>
 */
public static String getIdentifier(final Citation citation, final boolean strict) {
    if (citation != null) {
        // Whether 'identifier' is a Unicode identifier.
        boolean isUnicode = false;
        // The best identifier found so far.
        String identifier = null;
        // Code space of the identifier, or null if none.
        String codeSpace = null;
        final Iterator<? extends Identifier> it = iterator(citation.getIdentifiers());
        if (it != null)
            while (it.hasNext()) {
                final Identifier id = it.next();
                if (id != null && !isDeprecated(id)) {
                    final String candidate = CharSequences.trimWhitespaces(id.getCode());
                    if (candidate != null && !candidate.isEmpty()) {
                        /*
                         * For a non-empty identifier, verify if both the code and its codespace are valid
                         * Unicode identifiers. If a codespace exists, then the code does not need to begin
                         * with a "Unicode identifier start" (it may be a "Unicode identifier part").
                         */
                        String cs = (id instanceof ReferenceIdentifier) ? CharSequences.trimWhitespaces(((ReferenceIdentifier) id).getCodeSpace()) : null;
                        if (cs == null || cs.isEmpty()) {
                            cs = null;
                            isUnicode = CharSequences.isUnicodeIdentifier(candidate);
                        } else {
                            isUnicode = CharSequences.isUnicodeIdentifier(cs);
                            if (isUnicode)
                                for (int i = 0; i < candidate.length(); ) {
                                    final int c = candidate.codePointAt(i);
                                    if (!Character.isUnicodeIdentifierPart(c) && (strict || (c != '.' && c != '-'))) {
                                        /*
                                     * Above special case for '.' and '-' characters is documented
                                     * in the public Citations.getIdentifier(Citation) method.
                                     */
                                        isUnicode = false;
                                        break;
                                    }
                                    i += Character.charCount(c);
                                }
                        }
                        /*
                         * If we found a Unicode identifier, we are done and we can exit the loop.
                         * Otherwise retain the first identifier and continue the search for Unicode identifier.
                         */
                        if (identifier == null || isUnicode) {
                            identifier = candidate;
                            codeSpace = cs;
                            if (isUnicode)
                                break;
                        }
                    }
                }
            }
        /*
             * If no identifier has been found, fallback on the first title or alternate title.
             * We search for alternate titles because ISO specification said that those titles
             * are often used for abbreviations. Again we give preference to Unicode identifiers,
             * which are typically alternate titles.
             */
        if (identifier == null) {
            // Whitepaces removed by toString(…).
            identifier = toString(citation.getTitle());
            if (identifier != null) {
                if (identifier.isEmpty()) {
                    identifier = null;
                } else {
                    isUnicode = CharSequences.isUnicodeIdentifier(identifier);
                }
            }
            if (!isUnicode) {
                final Iterator<? extends InternationalString> iterator = iterator(citation.getAlternateTitles());
                if (iterator != null)
                    while (iterator.hasNext()) {
                        final String candidate = toString(iterator.next());
                        if (candidate != null && !candidate.isEmpty()) {
                            isUnicode = CharSequences.isUnicodeIdentifier(candidate);
                            if (identifier == null || isUnicode) {
                                identifier = candidate;
                                if (isUnicode)
                                    break;
                            }
                        }
                    }
            }
        }
        /*
             * Finished searching in the identifiers, title and alternate titles. If the identifier that
             * we found is not a valid Unicode identifier, we will return it only if the caller did not
             * asked for strictly valid Unicode identifier.
             */
        if (isUnicode || !strict) {
            if (codeSpace != null && !isEPSG(codeSpace, identifier)) {
                return codeSpace + (strict ? '_' : DEFAULT_SEPARATOR) + identifier;
            } else {
                return identifier;
            }
        }
    }
    return null;
}
Also used : Identifier(org.opengis.metadata.Identifier) ReferenceIdentifier(org.opengis.referencing.ReferenceIdentifier) ReferenceIdentifier(org.opengis.referencing.ReferenceIdentifier) InternationalString(org.opengis.util.InternationalString)

Example 18 with ReferenceIdentifier

use of org.opengis.referencing.ReferenceIdentifier in project sis by apache.

the class CharSequenceSubstitutionTest method testLegacy.

/**
 * Tests unmarshalling of {@code "RS_Identifier"} element. This element was defined in legacy ISO 19139:2007
 * but has been removed in ISO 19115-3. That element is extensively used for Coordinate Reference Systems in
 * GML 3.2.
 *
 * @throws JAXBException if the unmarshalling failed.
 */
@Test
@DependsOnMethod("testAnchorForString")
public void testLegacy() throws JAXBException {
    final String expected = "<gmd:RS_Identifier xmlns:gmd=\"" + LegacyNamespaces.GMD + '"' + " xmlns:gmx=\"" + LegacyNamespaces.GMX + '"' + " xmlns:gco=\"" + LegacyNamespaces.GCO + '"' + " xmlns:xlink=\"" + Namespaces.XLINK + "\">\n" + "  <gmd:code>\n" + "    <gmx:Anchor xlink:href=\"SDN:L101:2:4326\">EPSG:4326</gmx:Anchor>\n" + "  </gmd:code>\n" + "  <gmd:codeSpace>\n" + "    <gco:CharacterString>L101</gco:CharacterString>\n" + "  </gmd:codeSpace>\n" + "</gmd:RS_Identifier>";
    final ReferenceIdentifier id = unmarshal(ReferenceIdentifier.class, expected);
    assertEquals("codespace", "L101", id.getCodeSpace());
    assertEquals("code", "EPSG:4326", id.getCode());
}
Also used : ReferenceIdentifier(org.opengis.referencing.ReferenceIdentifier) Test(org.junit.Test) DependsOnMethod(org.apache.sis.test.DependsOnMethod)

Example 19 with ReferenceIdentifier

use of org.opengis.referencing.ReferenceIdentifier in project sis by apache.

the class SimpleIdentifiedObject method toString.

/**
 * Returns a pseudo-WKT representation for debugging purpose.
 */
@Override
public String toString() {
    final String code, codespace;
    final Citation authority;
    final ReferenceIdentifier name = this.name;
    if (name != null) {
        code = name.getCode();
        codespace = name.getCodeSpace();
        authority = name.getAuthority();
    } else {
        code = null;
        codespace = null;
        authority = null;
    }
    final StringBuilder buffer = new StringBuilder("IdentifiedObject[\"");
    if (codespace != null) {
        buffer.append(codespace).append(DefaultNameSpace.DEFAULT_SEPARATOR);
    }
    buffer.append(code).append('"');
    final String identifier = Citations.getIdentifier(authority, true);
    if (identifier != null) {
        // "Id" should be consistent with WKTKeywords.Id.
        buffer.append(", Id[\"").append(identifier).append("\"]");
    }
    return buffer.append(']').toString();
}
Also used : ReferenceIdentifier(org.opengis.referencing.ReferenceIdentifier) InternationalString(org.opengis.util.InternationalString) Citation(org.opengis.metadata.citation.Citation)

Example 20 with ReferenceIdentifier

use of org.opengis.referencing.ReferenceIdentifier in project sis by apache.

the class Builder method addNamesAndIdentifiers.

/**
 * Adds all non-deprecated names and identifiers from the given object.
 * Other properties like description and remarks are ignored.
 *
 * <p>This is a convenience method for using an existing object as a template, before to modify
 * some names by calls to {@link #rename(Citation, CharSequence[])}.</p>
 *
 * @param  object  the object from which to copy the references to names and identifiers.
 * @return {@code this}, for method call chaining.
 *
 * @since 0.6
 */
public B addNamesAndIdentifiers(final IdentifiedObject object) {
    ensureNonNull("object", object);
    for (final ReferenceIdentifier id : object.getIdentifiers()) {
        if (!isDeprecated(id)) {
            addIdentifier(id);
        }
    }
    ReferenceIdentifier id = object.getName();
    if (!isDeprecated(id)) {
        addName(id);
    }
    for (final GenericName alias : object.getAlias()) {
        if (!isDeprecated(alias)) {
            addName(alias);
        }
    }
    return self();
}
Also used : GenericName(org.opengis.util.GenericName) ReferenceIdentifier(org.opengis.referencing.ReferenceIdentifier)

Aggregations

ReferenceIdentifier (org.opengis.referencing.ReferenceIdentifier)35 Test (org.junit.Test)12 DependsOnMethod (org.apache.sis.test.DependsOnMethod)8 Identifier (org.opengis.metadata.Identifier)8 GenericName (org.opengis.util.GenericName)8 ImmutableIdentifier (org.apache.sis.metadata.iso.ImmutableIdentifier)6 InternationalString (org.opengis.util.InternationalString)5 Citation (org.opengis.metadata.citation.Citation)3 IdentifiedObject (org.opengis.referencing.IdentifiedObject)3 SimpleCitation (org.apache.sis.internal.simple.SimpleCitation)2 FactoryException (org.opengis.referencing.FactoryException)2 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)2 ValueComboBoxData (com.sldeditor.ui.widgets.ValueComboBoxData)1 CodeDefinition (eu.esdihumboldt.hale.common.instance.geometry.impl.CodeDefinition)1 WKTDefinition (eu.esdihumboldt.hale.common.instance.geometry.impl.WKTDefinition)1 CRSDefinition (eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Code (org.apache.sis.internal.jaxb.referencing.Code)1