use of org.apache.sis.referencing.factory.NoSuchAuthorityFactoryException in project sis by apache.
the class IdentifiedObjects method lookupURN.
/**
* Looks up a URN, such as {@code "urn:ogc:def:crs:EPSG:9.1:4326"}, of the specified object.
* This method searches in all {@linkplain org.apache.sis.referencing.factory.GeodeticAuthorityFactory geodetic
* authority factories} known to SIS for an object {@linkplain org.apache.sis.util.ComparisonMode#APPROXIMATIVE
* approximatively equals} to the specified object. Then there is a choice:
*
* <ul>
* <li>If a single matching object is found in the specified authority factory, then its URN is returned.</li>
* <li>Otherwise if the given object is a {@link CompoundCRS} or {@link ConcatenatedOperation}
* and all components have an URN, then this method returns a combined URN.</li>
* <li>Otherwise this method returns {@code null}.</li>
* </ul>
*
* <p><strong>Note that this method checks the identifier validity.</strong>
* If the given object declares explicitly an identifier, then this method will instantiate an object from the
* authority factory using that identifier and compare it with the given object. If the comparison fails, then
* this method returns {@code null}. Consequently this method may return {@code null} even if the given object
* declares explicitly its identifier. If the declared identifier is wanted unconditionally,
* one can use the following pattern instead:
*
* {@preformat java
* String urn = toURN(object.getClass(), getIdentifier(object, authority));
* }
*
* This method can be seen as a converse of {@link CRS#forCode(String)}.
*
* @param object the object (usually a {@linkplain org.apache.sis.referencing.crs.AbstractCRS
* coordinate reference system}) whose identifier is to be found, or {@code null}.
* @param authority the authority for the identifier to return, or {@code null} for
* the first identifier regardless its authority.
* @return the identifier, or {@code null} if none was found without ambiguity or if the given object was null.
* @throws FactoryException if an error occurred during the search.
*
* @see #newFinder(String)
* @see #toURN(Class, Identifier)
*
* @since 0.7
*/
public static String lookupURN(final IdentifiedObject object, final Citation authority) throws FactoryException {
if (object == null) {
return null;
}
IdentifiedObjectFinder finder;
try {
finder = newFinder(Citations.getCodeSpace(authority));
} catch (NoSuchAuthorityFactoryException e) {
warning("lookupURN", e);
finder = newFinder(null);
}
String urn = lookupURN(object, authority, finder);
if (urn != null) {
return urn;
}
/*
* If we didn't found a URN but the given object is made of smaller components, build a combined URN.
* Example: "urn:ogc:def:crs, crs:EPSG::27700, crs:EPSG::5701" (without spaces actually).
*/
final List<? extends IdentifiedObject> components;
if (object instanceof CompoundCRS) {
components = CRS.getSingleComponents((CompoundCRS) object);
} else if (object instanceof ConcatenatedOperation) {
components = ((ConcatenatedOperation) object).getOperations();
} else {
return null;
}
StringBuilder buffer = null;
for (final IdentifiedObject component : components) {
urn = lookupURN(component, authority, finder);
if (urn == null) {
return null;
}
assert urn.startsWith(DefinitionURI.PREFIX) : urn;
if (buffer == null) {
buffer = new StringBuilder(40).append(DefinitionURI.PREFIX).append(DefinitionURI.SEPARATOR).append(NameMeaning.toObjectType(object.getClass()));
}
buffer.append(DefinitionURI.COMPONENT_SEPARATOR).append(urn, DefinitionURI.PREFIX.length() + 1, urn.length());
}
return (buffer != null) ? buffer.toString() : null;
}
use of org.apache.sis.referencing.factory.NoSuchAuthorityFactoryException in project sis by apache.
the class AuthorityFactoriesTest method testCreateCRS.
/**
* Tests the {@code createCoordinateReferenceSystem(…)} method with various code.
*
* @throws FactoryException if a CRS creation failed.
*/
@Test
@DependsOnMethod("testCRS84")
public void testCreateCRS() throws FactoryException {
final CRSAuthorityFactory factory = AuthorityFactories.ALL;
final CRSAuthorityFactory wms = AuthorityFactories.ALL.getAuthorityFactory(CRSAuthorityFactory.class, Constants.OGC, null);
CoordinateReferenceSystem actual, expected;
actual = factory.createCoordinateReferenceSystem("CRS:84");
expected = wms.createCoordinateReferenceSystem("84");
assertSame(expected, actual);
assertSame(expected, factory.createObject("CRS:84"));
actual = factory.createCoordinateReferenceSystem("AUTO:42001,0,0");
expected = wms.createCoordinateReferenceSystem("42001,0,0");
assertSame(expected, actual);
assertSame(expected, factory.createObject("AUTO:42001,0,0"));
actual = factory.createCoordinateReferenceSystem("CRS:27");
expected = wms.createCoordinateReferenceSystem("27");
assertSame(expected, actual);
assertSame(expected, factory.createObject("CRS:27"));
try {
factory.createCoordinateReferenceSystem("84");
fail("Should not work without authority.");
} catch (NoSuchAuthorityCodeException exception) {
// This is the expected exception.
assertEquals("84", exception.getAuthorityCode());
}
try {
factory.createCoordinateReferenceSystem("FOO:84");
fail("Should not work with unknown authority.");
} catch (NoSuchAuthorityFactoryException exception) {
// This is the expected exception.
assertEquals("FOO", exception.getAuthority());
}
}
Aggregations