use of org.opengis.referencing.ReferenceSystem in project sis by apache.
the class Formatter method appendForSubtypes.
/**
* Appends the anchor, scope and domain of validity of the given object. Those information are available
* only for {@link ReferenceSystem}, {@link Datum} and {@link CoordinateOperation} objects.
*/
private void appendForSubtypes(final IdentifiedObject object) {
final InternationalString anchor, scope;
final Extent area;
if (object instanceof ReferenceSystem) {
anchor = null;
scope = ((ReferenceSystem) object).getScope();
area = ((ReferenceSystem) object).getDomainOfValidity();
} else if (object instanceof Datum) {
anchor = ((Datum) object).getAnchorPoint();
scope = ((Datum) object).getScope();
area = ((Datum) object).getDomainOfValidity();
} else if (object instanceof CoordinateOperation) {
anchor = null;
scope = ((CoordinateOperation) object).getScope();
area = ((CoordinateOperation) object).getDomainOfValidity();
} else {
return;
}
appendOnNewLine(WKTKeywords.Anchor, anchor, null);
appendOnNewLine(WKTKeywords.Scope, scope, ElementKind.SCOPE);
if (area != null) {
appendOnNewLine(WKTKeywords.Area, area.getDescription(), ElementKind.EXTENT);
append(Extents.getGeographicBoundingBox(area), BBOX_ACCURACY);
appendVerticalExtent(Extents.getVerticalRange(area));
appendTemporalExtent(Extents.getTimeRange(area));
}
}
use of org.opengis.referencing.ReferenceSystem in project sis by apache.
the class ReferencingFunctions method getIdentifiedObject.
/**
* Gets the CRS or other kind of object from the given code.
* If the code is a URN, then it can be any kind of object.
* Otherwise a Coordinate Reference System is assumed.
* This method caches the result.
*
* @param codeOrPath the code allocated by an authority, or the path to a file.
* @param type how to interpret {@code codeOrPath}, or {@code null} for guessing.
* @return the identified object for the given code.
* @throws FactoryException if an error occurred while creating the object.
* @throws DataStoreException if an error occurred while reading a data file.
*/
private IdentifiedObject getIdentifiedObject(final String codeOrPath, CodeType type) throws FactoryException, DataStoreException {
final CacheKey<IdentifiedObject> key = new CacheKey<>(IdentifiedObject.class, codeOrPath, null, null);
IdentifiedObject object = key.peek();
if (object == null) {
final Cache.Handler<IdentifiedObject> handler = key.lock();
try {
object = handler.peek();
if (object == null) {
if (type == null) {
type = CodeType.guess(codeOrPath);
}
if (type.equals(CodeType.URN)) {
object = CRS.getAuthorityFactory(null).createObject(codeOrPath);
} else if (type.isCRS) {
object = CRS.forCode(codeOrPath);
} else {
/*
* Apparently not an AUTHORITY:CODE string.
* Try to read a dataset from a file or URL, then get its CRS.
*/
final Metadata metadata;
try (DataStore store = DataStores.open(codeOrPath)) {
metadata = store.getMetadata();
}
if (metadata != null) {
for (final ReferenceSystem rs : metadata.getReferenceSystemInfo()) {
if (rs instanceof CoordinateReferenceSystem) {
return rs;
} else if (object == null) {
// Will be used as a fallback if we find no CRS.
object = rs;
}
}
}
if (object == null) {
throw new FactoryException(Errors.getResources(getJavaLocale()).getString(Errors.Keys.UnspecifiedCRS));
}
}
}
} finally {
handler.putAndUnlock(object);
}
}
return object;
}
use of org.opengis.referencing.ReferenceSystem in project sis by apache.
the class IdentifierCommand method create.
/**
* Creates an identifier row for the given CRS.
* This method gives precedence to {@code "urn:ogc:def:"} identifiers if possible.
*
* @return the row, or {@code null} if no identifier has been found.
*/
static Row create(ReferenceSystem rs) throws FactoryException {
String identifier = IdentifiedObjects.lookupURN(rs, null);
if (identifier == null) {
/*
* If we can not find an identifier matching the EPSG or WMS definitions,
* look at the identifiers declared in the CRS and verify their validity.
*/
for (final Identifier id : rs.getIdentifiers()) {
final String c = IdentifiedObjects.toURN(rs.getClass(), id);
if (c != null) {
identifier = c;
// Stop at the first "urn:ogc:def:…".
break;
}
if (identifier == null) {
// "AUTHORITY:CODE" as a fallback if no URN.
identifier = IdentifiedObjects.toString(id);
}
}
if (identifier == null) {
// No identifier found.
return null;
}
}
/*
* The CRS provided by the user contains identifier, but the 'lookupURN' operation above failed to
* find it. The most likely cause is that the user-provided CRS does not use the same axis order.
*/
State state;
try {
final ReferenceSystem def = CRS.forCode(identifier);
final ComparisonMode c = ComparisonMode.equalityLevel(def, rs);
if (c == null) {
state = State.MISMATCH;
} else
switch(c) {
case ALLOW_VARIANT:
{
state = State.AXIS_ORDER;
break;
}
case APPROXIMATIVE:
{
state = State.APPROXIMATIVE;
rs = def;
break;
}
default:
{
state = State.VALID;
rs = def;
break;
}
}
} catch (NoSuchAuthorityCodeException e) {
state = State.UNKNOWN;
}
return new Row(state, identifier, rs.getName().getCode());
}
Aggregations