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