use of org.apache.sis.referencing.operation.DefaultCoordinateOperationFactory in project sis by apache.
the class CRS method findOperations.
/**
* Finds mathematical operations that transform or convert coordinates from the given source to the
* given target coordinate reference system. If at least one operation exists, they are returned in
* preference order: the operation having the widest intersection between its
* {@linkplain AbstractCoordinateOperation#getDomainOfValidity() domain of validity}
* and the given area of interest are returned first.
*
* @param sourceCRS the CRS of source coordinates.
* @param targetCRS the CRS of target coordinates.
* @param areaOfInterest the area of interest, or {@code null} if none.
* @return mathematical operations from {@code sourceCRS} to {@code targetCRS}.
* @throws OperationNotFoundException if no operation was found between the given pair of CRS.
* @throws FactoryException if the operation can not be created for another reason.
*
* @see DefaultCoordinateOperationFactory#createOperations(CoordinateReferenceSystem, CoordinateReferenceSystem, CoordinateOperationContext)
*
* @since 1.0
*/
public static List<CoordinateOperation> findOperations(final CoordinateReferenceSystem sourceCRS, final CoordinateReferenceSystem targetCRS, final GeographicBoundingBox areaOfInterest) throws FactoryException {
ArgumentChecks.ensureNonNull("sourceCRS", sourceCRS);
ArgumentChecks.ensureNonNull("targetCRS", targetCRS);
final CoordinateOperationContext context = CoordinateOperationContext.fromBoundingBox(areaOfInterest);
final DefaultCoordinateOperationFactory factory = CoordinateOperations.factory();
try {
return factory.createOperations(sourceCRS, targetCRS, context);
} catch (UnavailableFactoryException e) {
if (AuthorityFactories.failure(e)) {
throw e;
} else
try {
return Collections.singletonList(factory.createOperation(sourceCRS, targetCRS, context));
} catch (FactoryException ex) {
ex.addSuppressed(e);
throw ex;
}
}
}
use of org.apache.sis.referencing.operation.DefaultCoordinateOperationFactory in project sis by apache.
the class CRS method findOperation.
/**
* Finds a mathematical operation that transforms or converts coordinates from the given source to the
* given target coordinate reference system. If an estimation of the geographic area containing the points
* to transform is known, it can be specified for helping this method to find a better suited operation.
* If no area of interest is specified, then the current default is the widest
* {@linkplain AbstractCoordinateOperation#getDomainOfValidity() domain of validity}.
* A future Apache SIS version may also take the country of current locale in account.
*
* <div class="note"><b>Note:</b>
* the area of interest is just one aspect that may affect the coordinate operation.
* Other aspects are the time of interest (because some coordinate operations take in account the
* plate tectonics movement) or the desired accuracy. For more control on the coordinate operation
* to create, see {@link CoordinateOperationContext}.</div>
*
* After the caller received a {@code CoordinateOperation} instance, the following methods can be invoked
* for checking if the operation suits the caller's needs:
*
* <ul>
* <li>{@link #getGeographicBoundingBox(CoordinateOperation)}
* for checking if the operation is valid in the caller's area of interest.</li>
* <li>{@link #getLinearAccuracy(CoordinateOperation)}
* for checking if the operation has sufficient accuracy for caller's purpose.</li>
* </ul>
*
* If the source and target CRS are equivalent, then this method returns an operation backed by an
* {@linkplain org.apache.sis.referencing.operation.transform.AbstractMathTransform#isIdentity() identity}
* transform. If there is no known operation between the given pair of CRS, then this method throws an
* {@link OperationNotFoundException}.
*
* @param sourceCRS the CRS of source coordinates.
* @param targetCRS the CRS of target coordinates.
* @param areaOfInterest the area of interest, or {@code null} if none.
* @return the mathematical operation from {@code sourceCRS} to {@code targetCRS}.
* @throws OperationNotFoundException if no operation was found between the given pair of CRS.
* @throws FactoryException if the operation can not be created for another reason.
*
* @see DefaultCoordinateOperationFactory#createOperation(CoordinateReferenceSystem, CoordinateReferenceSystem, CoordinateOperationContext)
*
* @since 0.7
*/
public static CoordinateOperation findOperation(final CoordinateReferenceSystem sourceCRS, final CoordinateReferenceSystem targetCRS, final GeographicBoundingBox areaOfInterest) throws FactoryException {
ArgumentChecks.ensureNonNull("sourceCRS", sourceCRS);
ArgumentChecks.ensureNonNull("targetCRS", targetCRS);
final CoordinateOperationContext context = CoordinateOperationContext.fromBoundingBox(areaOfInterest);
/*
* In principle we should just delegate to factory.createOperation(…). However this operation may fail
* if a connection to the EPSG database has been found, but the EPSG tables do not yet exist in that
* database and
*/
final DefaultCoordinateOperationFactory factory = CoordinateOperations.factory();
try {
return factory.createOperation(sourceCRS, targetCRS, context);
} catch (UnavailableFactoryException e) {
if (AuthorityFactories.failure(e)) {
throw e;
} else
try {
// Above method call replaced the EPSG factory by a fallback. Try again.
return factory.createOperation(sourceCRS, targetCRS, context);
} catch (FactoryException ex) {
ex.addSuppressed(e);
throw ex;
}
}
}
use of org.apache.sis.referencing.operation.DefaultCoordinateOperationFactory in project sis by apache.
the class EPSGDataAccess method createCoordinateOperation.
/**
* Creates an operation for transforming coordinates in the source CRS to coordinates in the target CRS.
* The returned object will either be a {@link Conversion} or a {@link Transformation}, depending on the code.
*
* <div class="note"><b>Example:</b>
* some EPSG codes for coordinate transformations are:
*
* <table class="sis" summary="EPSG codes examples">
* <tr><th>Code</th> <th>Description</th></tr>
* <tr><td>1133</td> <td>ED50 to WGS 84 (1)</td></tr>
* <tr><td>1241</td> <td>NAD27 to NAD83 (1)</td></tr>
* <tr><td>1173</td> <td>NAD27 to WGS 84 (4)</td></tr>
* <tr><td>6326</td> <td>NAD83(2011) to NAVD88 height (1)</td></tr>
* </table></div>
*
* @param code value allocated by EPSG.
* @return the operation for the given code.
* @throws NoSuchAuthorityCodeException if the specified {@code code} was not found.
* @throws FactoryException if the object creation failed for some other reason.
*/
@Override
@SuppressWarnings("null")
public synchronized CoordinateOperation createCoordinateOperation(final String code) throws NoSuchAuthorityCodeException, FactoryException {
ArgumentChecks.ensureNonNull("code", code);
CoordinateOperation returnValue = null;
try {
try (ResultSet result = executeQuery("Coordinate_Operation", "COORD_OP_CODE", "COORD_OP_NAME", "SELECT COORD_OP_CODE," + " COORD_OP_NAME," + " COORD_OP_TYPE," + " SOURCE_CRS_CODE," + " TARGET_CRS_CODE," + " COORD_OP_METHOD_CODE," + " COORD_TFM_VERSION," + " COORD_OP_ACCURACY," + " AREA_OF_USE_CODE," + " COORD_OP_SCOPE," + " REMARKS," + " DEPRECATED" + " FROM [Coordinate_Operation]" + " WHERE COORD_OP_CODE = ?", code)) {
while (result.next()) {
final Integer epsg = getInteger(code, result, 1);
final String name = getString(code, result, 2);
final String type = getString(code, result, 3).toLowerCase(Locale.US);
final boolean isTransformation = type.equals("transformation");
final boolean isConversion = type.equals("conversion");
final boolean isConcatenated = type.equals("concatenated operation");
final String sourceCode, targetCode;
final Integer methodCode;
if (isConversion) {
// Optional for conversions, mandatory for all others.
sourceCode = getOptionalString(result, 4);
targetCode = getOptionalString(result, 5);
} else {
sourceCode = getString(code, result, 4);
targetCode = getString(code, result, 5);
}
if (isConcatenated) {
// Not applicable to concatenated operation, mandatory for all others.
methodCode = getOptionalInteger(result, 6);
} else {
methodCode = getInteger(code, result, 6);
}
final String version = getOptionalString(result, 7);
final double accuracy = getOptionalDouble(result, 8);
final String area = getOptionalString(result, 9);
final String scope = getOptionalString(result, 10);
final String remarks = getOptionalString(result, 11);
final boolean deprecated = getOptionalBoolean(result, 12);
/*
* Create the source and target CRS for the codes fetched above. Those CRS are optional only for
* conversions (the above calls to getString(code, result, …) verified that those CRS are defined
* for other kinds of operation). Conversions in EPSG database are usually "defining conversions"
* without source and target CRS.
*
* In EPSG database 6.7, all defining conversions are projections and their dimensions are always 2.
* However, this default number of dimensions is not generalizable to other kind of operation methods.
* For example the "Geocentric translation" operation method has 3-dimensional source and target CRS.
*/
boolean isDimensionKnown = true;
final int sourceDimensions, targetDimensions;
final CoordinateReferenceSystem sourceCRS, targetCRS;
if (sourceCode != null) {
sourceCRS = owner.createCoordinateReferenceSystem(sourceCode);
sourceDimensions = sourceCRS.getCoordinateSystem().getDimension();
} else {
sourceCRS = null;
// Acceptable default for projections only.
sourceDimensions = 2;
isDimensionKnown = false;
}
if (targetCode != null) {
targetCRS = owner.createCoordinateReferenceSystem(targetCode);
targetDimensions = targetCRS.getCoordinateSystem().getDimension();
} else {
targetCRS = null;
// Acceptable default for projections only.
targetDimensions = 2;
isDimensionKnown = false;
}
/*
* Get the operation method. This is mandatory for conversions and transformations
* (it was checked by getInteger(code, result, …) above in this method) but optional
* for concatenated operations. Fetching parameter values is part of this block.
*/
final boolean isDeferred = Semaphores.query(Semaphores.METADATA_ONLY);
ParameterValueGroup parameters = null;
OperationMethod method = null;
if (methodCode != null && !isDeferred) {
method = owner.createOperationMethod(methodCode.toString());
if (isDimensionKnown) {
method = DefaultOperationMethod.redimension(method, sourceDimensions, targetDimensions);
}
parameters = method.getParameters().createValue();
fillParameterValues(methodCode, epsg, parameters);
}
/*
* Creates common properties. The 'version' and 'accuracy' are usually defined
* for transformations only. However, we check them for all kind of operations
* (including conversions) and copy the information unconditionally if present.
*
* NOTE: This block must be executed last before object creations below, because
* methods like createCoordinateReferenceSystem and createOperationMethod
* overwrite the properties map.
*/
Map<String, Object> opProperties = createProperties("Coordinate_Operation", name, epsg, area, scope, remarks, deprecated);
opProperties.put(CoordinateOperation.OPERATION_VERSION_KEY, version);
if (!Double.isNaN(accuracy)) {
opProperties.put(CoordinateOperation.COORDINATE_OPERATION_ACCURACY_KEY, TransformationAccuracy.create(accuracy));
}
/*
* Creates the operation. Conversions should be the only operations allowed to have
* null source and target CRS. In such case, the operation is a defining conversion
* (usually to be used later as part of a ProjectedCRS creation).
*/
final CoordinateOperation operation;
final CoordinateOperationFactory copFactory = owner.copFactory;
if (isDeferred) {
operation = new DeferredCoordinateOperation(opProperties, sourceCRS, targetCRS, owner);
} else if (isConversion && (sourceCRS == null || targetCRS == null)) {
operation = copFactory.createDefiningConversion(opProperties, method, parameters);
} else if (isConcatenated) {
/*
* Concatenated operation: we need to close the current result set, because
* we are going to invoke this method recursively in the following lines.
*/
result.close();
// Because this class uses a shared map.
opProperties = new HashMap<>(opProperties);
final List<String> codes = new ArrayList<>();
try (ResultSet cr = executeQuery("Coordinate_Operation Path", "SELECT SINGLE_OPERATION_CODE" + " FROM [Coordinate_Operation Path]" + " WHERE (CONCAT_OPERATION_CODE = ?)" + " ORDER BY OP_PATH_STEP", epsg)) {
while (cr.next()) {
codes.add(getString(code, cr, 1));
}
}
final CoordinateOperation[] operations = new CoordinateOperation[codes.size()];
ensureNoCycle(CoordinateOperation.class, epsg);
try {
for (int i = 0; i < operations.length; i++) {
operations[i] = owner.createCoordinateOperation(codes.get(i));
}
} finally {
endOfRecursivity(CoordinateOperation.class, epsg);
}
return copFactory.createConcatenatedOperation(opProperties, operations);
} else {
/*
* At this stage, the parameters are ready for use. Create the math transform and wrap it in the
* final operation (a Conversion or a Transformation). We need to give to MathTransformFactory
* some information about the context (source and target CRS) for allowing the factory to set
* the values of above-mentioned implicit parameters (semi-major and semi-minor axis lengths).
*
* The first special case may be removed in a future SIS version if the missing method is added
* to GeoAPI. Actually GeoAPI has a method doing part of the job, but incomplete (e.g. the pure
* GeoAPI method can not handle Molodensky transform because it does not give the target datum).
*/
final MathTransform mt;
final MathTransformFactory mtFactory = owner.mtFactory;
if (mtFactory instanceof DefaultMathTransformFactory) {
mt = ((DefaultMathTransformFactory) mtFactory).createParameterizedTransform(parameters, ReferencingUtilities.createTransformContext(sourceCRS, targetCRS, null));
} else {
// Fallback for non-SIS implementations. Work for map projections but not for Molodensky.
mt = mtFactory.createBaseToDerived(sourceCRS, parameters, targetCRS.getCoordinateSystem());
}
/*
* Give a hint to the factory about the type of the coordinate operation. ISO 19111 defines
* Conversion and Transformation, but SIS also have more specific sub-types. We begin with
* what we can infer from the EPSG database. Next, if the SIS MathTransform providers give
* more information, then we refine the type.
*/
Class<? extends SingleOperation> opType;
if (isTransformation) {
opType = Transformation.class;
} else if (isConversion) {
opType = Conversion.class;
} else {
opType = SingleOperation.class;
}
final OperationMethod provider = mtFactory.getLastMethodUsed();
if (provider instanceof DefaultOperationMethod) {
// SIS-specific
final Class<?> s = ((DefaultOperationMethod) provider).getOperationType();
if (s != null && opType.isAssignableFrom(s)) {
opType = s.asSubclass(SingleOperation.class);
}
}
opProperties.put(ReferencingServices.OPERATION_TYPE_KEY, opType);
opProperties.put(ReferencingServices.PARAMETERS_KEY, parameters);
/*
* Following restriction will be removed in a future SIS version if the method is added to GeoAPI.
*/
if (!(copFactory instanceof DefaultCoordinateOperationFactory)) {
throw new UnsupportedOperationException(error().getString(Errors.Keys.UnsupportedImplementation_1, copFactory.getClass()));
}
operation = ((DefaultCoordinateOperationFactory) copFactory).createSingleOperation(opProperties, sourceCRS, targetCRS, null, method, mt);
}
returnValue = ensureSingleton(operation, returnValue, code);
if (result.isClosed()) {
return returnValue;
}
}
}
} catch (SQLException exception) {
throw databaseFailure(CoordinateOperation.class, code, exception);
}
if (returnValue == null) {
throw noSuchAuthorityCode(CoordinateOperation.class, code);
}
return returnValue;
}
use of org.apache.sis.referencing.operation.DefaultCoordinateOperationFactory in project sis by apache.
the class Proj4Factory method opFactory.
/**
* Returns the factory for coordinate operation objects.
* The factory is backed by this {@code Proj4Factory} as the {@code MathTransformFactory} implementation.
*/
final DefaultCoordinateOperationFactory opFactory() {
DefaultCoordinateOperationFactory factory = opFactory;
if (factory == null) {
final Map<String, Object> properties = new HashMap<>(defaultProperties);
properties.put(ReferencingServices.CRS_FACTORY, crsFactory);
properties.put(ReferencingServices.CS_FACTORY, csFactory);
properties.put(ReferencingServices.DATUM_FACTORY, datumFactory);
factory = new DefaultCoordinateOperationFactory(properties, mtFactory);
opFactory = factory;
}
return factory;
}
Aggregations