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;
}
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());
}
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;
}
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);
}
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());
}
Aggregations