use of org.apache.sis.referencing.factory.MissingFactoryResourceException in project sis by apache.
the class CoordinateOperationRegistry method search.
/**
* Returns operations for conversions or transformations between two coordinate reference systems.
* This method extracts the authority code from the supplied {@code sourceCRS} and {@code targetCRS},
* and submit them to the {@link #registry}. If no operation is found for those codes, then this method
* returns {@code null}.
*
* @param sourceCRS source coordinate reference system.
* @param targetCRS target coordinate reference system.
* @return a coordinate operation from {@code sourceCRS} to {@code targetCRS}, or {@code null}
* if no such operation is explicitly defined in the underlying database.
* @throws IllegalArgumentException if the coordinate systems are not of the same type or axes do not match.
* @throws IncommensurableException if the units are not compatible or a unit conversion is non-linear.
* @throws FactoryException if an error occurred while creating the operation.
*/
private List<CoordinateOperation> search(final CoordinateReferenceSystem sourceCRS, final CoordinateReferenceSystem targetCRS) throws IllegalArgumentException, IncommensurableException, FactoryException {
final List<String> sources = findCode(sourceCRS);
if (sources.isEmpty())
return null;
final List<String> targets = findCode(targetCRS);
if (targets.isEmpty())
return null;
final List<CoordinateOperation> operations = new ArrayList<>();
boolean foundDirectOperations = false;
boolean useDeprecatedOperations = false;
for (final String sourceID : sources) {
for (final String targetID : targets) {
if (sourceID.equals(targetID)) {
/*
* Above check is necessary because this method may be invoked in some situations where the code
* are equal while the CRS are not. Such situation should be illegal, but unfortunately it still
* happen because many software products are not compliant with EPSG definition of axis order.
* In such cases we will need to compute a transform from sourceCRS to targetCRS ignoring the
* source and target codes. The CoordinateOperationFinder class can do that, providing that we
* prevent this CoordinateOperationRegistry to (legitimately) claims that the operation from
* sourceCode to targetCode is the identity transform.
*/
return null;
}
/*
* Some pairs of CRS have a lot of coordinate operations backed by datum shift grids.
* We do not want to load all of them until we found the right coordinate operation.
* The non-public Semaphores.METADATA_ONLY mechanism instructs EPSGDataAccess to
* instantiate DeferredCoordinateOperation instead of full coordinate operations.
*/
final boolean mdOnly = Semaphores.queryAndSet(Semaphores.METADATA_ONLY);
try {
Collection<CoordinateOperation> authoritatives;
try {
authoritatives = registry.createFromCoordinateReferenceSystemCodes(sourceID, targetID);
final boolean inverse = Containers.isNullOrEmpty(authoritatives);
if (inverse) {
/*
* No operation from 'source' to 'target' available. But maybe there is an inverse operation.
* This is typically the case when the user wants to convert from a projected to a geographic CRS.
* The EPSG database usually contains transformation paths for geographic to projected CRS only.
*/
if (foundDirectOperations) {
// Ignore inverse operations if we already have direct ones.
continue;
}
authoritatives = registry.createFromCoordinateReferenceSystemCodes(targetID, sourceID);
if (Containers.isNullOrEmpty(authoritatives)) {
continue;
}
} else if (!foundDirectOperations) {
foundDirectOperations = true;
// Keep only direct operations.
operations.clear();
}
} catch (NoSuchAuthorityCodeException | MissingFactoryResourceException e) {
/*
* sourceCode or targetCode is unknown to the underlying authority factory.
* Ignores the exception and fallback on the generic algorithm provided by
* CoordinateOperationFinder.
*/
log(null, e);
continue;
}
/*
* If we found at least one non-deprecated operation, we will stop the search at
* the first deprecated one (assuming that deprecated operations are sorted last).
* Deprecated operations are kept only if there is no non-deprecated operations.
*/
try {
for (final CoordinateOperation candidate : authoritatives) {
if (candidate != null) {
// Paranoiac check.
if ((candidate instanceof Deprecable) && ((Deprecable) candidate).isDeprecated()) {
if (!useDeprecatedOperations && !operations.isEmpty())
break;
useDeprecatedOperations = true;
} else if (useDeprecatedOperations) {
useDeprecatedOperations = false;
// Replace deprecated operations by non-deprecated ones.
operations.clear();
}
operations.add(candidate);
}
}
} catch (BackingStoreException exception) {
throw exception.unwrapOrRethrow(FactoryException.class);
}
} finally {
if (!mdOnly) {
Semaphores.clear(Semaphores.METADATA_ONLY);
}
}
}
}
/*
* At this point we got the list of coordinate operations. Now, sort them in preference order.
* We will loop over all coordinate operations and select the one having the largest intersection
* with the area of interest. Note that if the user did not specified an area of interest himself,
* then we need to get one from the CRS. This is necessary for preventing the transformation from
* NAD27 to NAD83 in Idaho to select the transform for Alaska (since the later has a larger area).
*/
CoordinateOperationSorter.sort(operations, Extents.getGeographicBoundingBox(areaOfInterest));
final ListIterator<CoordinateOperation> it = operations.listIterator();
while (it.hasNext()) {
/*
* At this point we filtered a CoordinateOperation by looking only at its metadata.
* Code following this point will need the full coordinate operation, including its
* MathTransform. So if we got a deferred operation, we need to resolve it now.
* Conversely, we should not use metadata below this point because the call to
* inverse(CoordinateOperation) is not guaranteed to preserve all metadata.
*/
CoordinateOperation operation = it.next();
try {
if (operation instanceof DeferredCoordinateOperation) {
operation = ((DeferredCoordinateOperation) operation).create();
}
if (operation instanceof SingleOperation && operation.getMathTransform() == null) {
operation = fromDefiningConversion((SingleOperation) operation, foundDirectOperations ? sourceCRS : targetCRS, foundDirectOperations ? targetCRS : sourceCRS);
if (operation == null) {
it.remove();
continue;
}
}
if (!foundDirectOperations) {
operation = inverse(operation);
}
} catch (NoninvertibleTransformException | MissingFactoryResourceException e) {
/*
* If we failed to get the real CoordinateOperation instance, remove it from
* the collection and try again in order to get the next best choices.
*/
log(null, e);
it.remove();
// Try again with the next best case.
continue;
}
/*
* It is possible that the CRS given to this method were not quite right. For example the user
* may have created his CRS from a WKT using a different axis order than the order specified by
* the authority and still (wrongly) call those CRS "EPSG:xxxx". So we check if the source and
* target CRS for the operation we just created are equivalent to the CRS specified by the user.
*
* NOTE: FactoryException may be thrown if we fail to create a transform from the user-provided
* CRS to the authority-provided CRS. That transform should have been only an identity transform,
* or a simple affine transform if the user specified wrong CRS as explained in above paragraph.
* If we fail here, we are likely to fail for all other transforms. So we are better to let the
* FactoryException propagate.
*/
operation = complete(operation, sourceCRS, targetCRS);
if (filter(operation)) {
if (stopAtFirst) {
operations.clear();
operations.add(operation);
break;
}
it.set(operation);
} else {
it.remove();
}
}
return operations;
}
Aggregations