Search in sources :

Example 26 with ReferenceIdentifier

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

the class Code method forIdentifiedObject.

/**
 * Returns a {@code <gml:identifier>} for the given identified object, or {@code null} if none.
 * This method searches for the following identifiers, in preference order:
 * <ul>
 *   <li>The first identifier having a code that begin with {@code "urn:"}.</li>
 *   <li>The first identifier having a code that begin with {@code "http:"}.</li>
 *   <li>The first identifier in the {@code "EPSG"} codespace, converted to the {@code "urn:} syntax.</li>
 *   <li>The first identifier in other codespace, converted to the {@code "urn:} syntax if possible.</li>
 * </ul>
 *
 * @param  type         the type of the identified object.
 * @param  identifiers  the object identifiers, or {@code null} if none.
 * @return the {@code <gml:identifier>} as a {@code Code} instance, or {@code null} if none.
 */
public static Code forIdentifiedObject(final Class<?> type, final Iterable<? extends ReferenceIdentifier> identifiers) {
    if (identifiers != null) {
        boolean isHTTP = false;
        boolean isEPSG = false;
        ReferenceIdentifier fallback = null;
        for (final ReferenceIdentifier identifier : identifiers) {
            final String code = identifier.getCode();
            // Paranoiac check.
            if (code == null)
                continue;
            if (code.regionMatches(true, 0, "urn:", 0, 4)) {
                return new Code(identifier);
            }
            if (!isHTTP) {
                isHTTP = code.regionMatches(true, 0, "http:", 0, 5);
                if (isHTTP) {
                    fallback = identifier;
                } else if (!isEPSG) {
                    isEPSG = Constants.EPSG.equalsIgnoreCase(identifier.getCodeSpace());
                    if (isEPSG || fallback == null) {
                        fallback = identifier;
                    }
                }
            }
        }
        /*
             * If no "urn:" or "http:" form has been found, try to create a "urn:" form from the first identifier.
             * For example "EPSG:4326" may be converted to "urn:ogc:def:crs:EPSG:8.2:4326". If the first identifier
             * can not be converted to a "urn:" form, then it will be returned as-is.
             */
        if (fallback != null) {
            if (!isHTTP) {
                final String urn = NameMeaning.toURN(type, fallback.getCodeSpace(), fallback.getVersion(), fallback.getCode());
                if (urn != null) {
                    final Code code = new Code();
                    /*
                         * Rational for EPSG special case below:
                         * -------------------------------------
                         * Apache SIS already formats the Identifier.getCodeSpace() value in the URN.
                         * This value is "EPSG" for IdentifiedObject instances from the EPSG database.
                         * But GML additionally have a "codeSpace" attribute, and common usage seems to
                         * give the "OGP" or "IOGP" value to that attribute as in the following example:
                         *
                         *     <gml:identifier codeSpace="IOGP">urn:ogc:def:crs:EPSG::4326</gml:identifier>
                         *
                         * A discussion can be found in the comments of https://issues.apache.org/jira/browse/SIS-196
                         *
                         * Where to take this "IOGP" value from? It is not the Identifier.getCodeSpace() String value
                         * since ISO 19115-1 clearly uses the "EPSG" value in their example.  We could consider using
                         * the Identifier.getAuthority() value, which is a Citation. But the "EPSG" part in above URN
                         * is named "the authority" in URN specification, which suggest that Identifier.getAuthority()
                         * should return a citation for the "EPSG Geodetic Parameter Dataset" rather than for the IOGP
                         * organisation.
                         *
                         * Apache SIS declares IOGP as the codespace of the EPSG codespace, i.e. the identifier of the
                         * EPSG authority is "IOGP:EPSG". So the code below searches for the "IOGP" part of the above.
                         * However there is no indication at this time that objects from other sources than SIS would
                         * follow such convention, so we also keep a hard-coded "IOGP" default value for now.
                         *
                         * A symmetrical special handling for EPSG is done in the 'getIdentifier()' method of this class.
                         *
                         * See https://issues.apache.org/jira/browse/SIS-199
                         */
                    final Citation authority = fallback.getAuthority();
                    if (isEPSG) {
                        // Default value if we do not find a codespace below.
                        code.codeSpace = Constants.IOGP;
                        if (authority != null) {
                            for (final Identifier id : authority.getIdentifiers()) {
                                if (Constants.EPSG.equalsIgnoreCase(id.getCode())) {
                                    if (id instanceof ReferenceIdentifier) {
                                        final String cs = ((ReferenceIdentifier) id).getCodeSpace();
                                        if (cs != null) {
                                            code.codeSpace = cs;
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    } else {
                        code.codeSpace = getCodeSpace(authority);
                    }
                    code.code = urn;
                    return code;
                }
            }
            return new Code(fallback);
        }
    }
    return null;
}
Also used : ReferenceIdentifier(org.opengis.referencing.ReferenceIdentifier) NamedIdentifier(org.apache.sis.referencing.NamedIdentifier) Identifier(org.opengis.metadata.Identifier) ReferenceIdentifier(org.opengis.referencing.ReferenceIdentifier) Citation(org.opengis.metadata.citation.Citation)

Example 27 with ReferenceIdentifier

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

the class AbstractIdentifiedObjectTest method testWithManyIdentifiers.

/**
 * Tests the {@link AbstractIdentifiedObject#AbstractIdentifiedObject(Map)} constructor
 * with more than one identifier. This method also tries a different identifier implementation
 * than the one used by {@link #testWithSingleIdentifier()}.
 */
@Test
@DependsOnMethod("testWithSingleIdentifier")
public void testWithManyIdentifiers() {
    final Set<ReferenceIdentifier> identifiers = new LinkedHashSet<>(4);
    assertTrue(identifiers.add(new NamedIdentifier(EPSG, "7019")));
    assertTrue(identifiers.add(new NamedIdentifier(EPSG, "IgnoreMe")));
    final AbstractIdentifiedObject object = new AbstractIdentifiedObject(properties(identifiers));
    final ReferenceIdentifier gmlId = validate(object, identifiers, "epsg-7019");
    assertNotNull("gmlId", gmlId);
    assertEquals("gmlId.codespace", "EPSG", gmlId.getCodeSpace());
    assertEquals("gmlId.code", "7019", gmlId.getCode());
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ReferenceIdentifier(org.opengis.referencing.ReferenceIdentifier) Test(org.junit.Test) DependsOnMethod(org.apache.sis.test.DependsOnMethod)

Example 28 with ReferenceIdentifier

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

the class AbstractIdentifiedObjectTest method validate.

/**
 * Validates the given object created by the test methods.
 *
 * @param  object       the object to validate.
 * @param  identifiers  the expected value of {@link AbstractIdentifiedObject#getIdentifiers()}.
 * @param  gmlID        the expected value of {@link AbstractIdentifiedObject#getID()}.
 * @return the value of {@link AbstractIdentifiedObject#getIdentifier()}.
 */
private static ReferenceIdentifier validate(final AbstractIdentifiedObject object, final Set<ReferenceIdentifier> identifiers, final String gmlID) {
    Validators.validate(object);
    final ReferenceIdentifier name = object.getName();
    assertEquals("name", "GRS 1980", name.getCode());
    assertEquals("codespace", "EPSG", name.getCodeSpace());
    assertEquals("version", "8.3", name.getVersion());
    assertEquals("aliases", "International 1979", getSingleton(object.getAlias()).toString());
    assertEquals("names", name, getSingleton(object.getNames()));
    assertEquals("identifiers", identifiers, object.getIdentifiers());
    assertEquals("ID", gmlID, object.getID());
    assertEquals("remarks", "Adopted by IUGG 1979 Canberra", object.getRemarks().toString(Locale.ENGLISH));
    assertEquals("remarks_fr", "Adopté par IUGG 1979 Canberra", object.getRemarks().toString(Locale.FRENCH));
    final Code code = object.getIdentifier();
    return (code != null) ? code.getIdentifier() : null;
}
Also used : ReferenceIdentifier(org.opengis.referencing.ReferenceIdentifier) Code(org.apache.sis.internal.jaxb.referencing.Code)

Example 29 with ReferenceIdentifier

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

the class AbstractIdentifiedObjectTest method testWithoutIdentifier.

/**
 * Tests the {@link AbstractIdentifiedObject#AbstractIdentifiedObject(Map)} constructor without identifier.
 * This method compares the property values against the expected values.
 */
@Test
public void testWithoutIdentifier() {
    final Set<ReferenceIdentifier> identifiers = Collections.<ReferenceIdentifier>emptySet();
    final AbstractIdentifiedObject object = new AbstractIdentifiedObject(properties(identifiers));
    final ReferenceIdentifier gmlId = validate(object, identifiers, "GRS1980");
    assertNull("gmlId", gmlId);
}
Also used : ReferenceIdentifier(org.opengis.referencing.ReferenceIdentifier) Test(org.junit.Test)

Example 30 with ReferenceIdentifier

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

the class AbstractIdentifiedObjectTest method testAsSubtype.

/**
 * Tests {@link AbstractIdentifiedObject#getIdentifier()} with a sub-type of {@code AbstractIdentifiedObject}.
 * The use of a subtype will allow {@code getIdentifier()} to build a URN and {@code getId()} to know what to
 * insert between {@code "epsg-"} and the code.
 */
@Test
@DependsOnMethod("testWithManyIdentifiers")
public void testAsSubtype() {
    final ReferenceIdentifier identifier = new NamedIdentifier(EPSG, "7019");
    final Set<ReferenceIdentifier> identifiers = Collections.singleton(identifier);
    final AbstractIdentifiedObject object = new AbstractDatum(properties(identifiers));
    final ReferenceIdentifier gmlId = validate(object, identifiers, "epsg-datum-7019");
    assertNotNull("gmlId", gmlId);
    assertEquals("gmlId.codespace", "EPSG", gmlId.getCodeSpace());
    assertEquals("gmlId.code", "7019", gmlId.getCode());
}
Also used : AbstractDatum(org.apache.sis.referencing.datum.AbstractDatum) ReferenceIdentifier(org.opengis.referencing.ReferenceIdentifier) Test(org.junit.Test) DependsOnMethod(org.apache.sis.test.DependsOnMethod)

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