use of org.opengis.util.FactoryException in project sis by apache.
the class TransformSeparator method separate.
/**
* Separates the math transform specified at construction time for given dimension indices.
* This method creates a math transform that use only the {@linkplain #addSourceDimensions(int...) specified
* source dimensions} and return only the {@linkplain #addTargetDimensions(int...) specified target dimensions}.
* If the source or target dimensions were not specified, then they will be inferred as below:
*
* <ul class="verbose">
* <li>If source dimensions were unspecified, then the returned transform will keep at least all source
* dimensions needed for computing the specified target dimensions. In many cases the returned transform
* unconditionally keep all source dimensions, but not necessarily. If all source dimensions need to be
* kept, it is better to {@linkplain #addSourceDimensionRange(int, int) specify that explicitely}.</li>
*
* <li>If target dimensions were unspecified, then the returned transform will expect only the specified
* source dimensions as inputs, and the target dimensions will be inferred automatically.</li>
* </ul>
*
* The source and target dimensions actually used can be queried by calls to {@link #getSourceDimensions()}
* or {@link #getTargetDimensions()} after this {@code separate()} method.
*
* @return the separated math transform.
* @throws FactoryException if the transform can not be separated.
*/
public MathTransform separate() throws FactoryException {
MathTransform tr = transform;
if (sourceDimensions == null || containsAll(sourceDimensions, 0, tr.getSourceDimensions())) {
if (targetDimensions != null && !containsAll(targetDimensions, 0, tr.getTargetDimensions())) {
tr = filterTargetDimensions(tr, targetDimensions);
}
if (sourceDimensions == null) {
sourceDimensions = series(0, transform.getSourceDimensions());
}
if (targetDimensions == null) {
targetDimensions = series(0, transform.getTargetDimensions());
}
} else {
/*
* At this point there is at least one source dimensions to take in account.
* Source dimensions are more difficult to process than target dimensions.
*/
final int[] requested = targetDimensions;
// May update targetDimensions.
tr = filterSourceDimensions(tr, sourceDimensions);
assert ArraysExt.isSorted(targetDimensions, true) : "targetDimensions";
if (requested != null) {
final int[] inferred = targetDimensions;
targetDimensions = requested;
final int[] subDimensions = new int[requested.length];
for (int i = 0; i < requested.length; i++) {
final int r = requested[i];
final int j = Arrays.binarySearch(inferred, r);
if (j < 0) {
/*
* The user asked for some target dimensions that we can not keep, probably
* because at least one of the requested target dimensions has a dependency to a
* source dimension that does not appear in the list of source dimensions to keep.
*/
throw new FactoryException(Resources.format(Resources.Keys.CanNotSeparateTargetDimension_1, r));
}
subDimensions[i] = j;
}
tr = filterTargetDimensions(tr, subDimensions);
}
}
/*
* We are done. But do a final verification on the number of dimensions.
*/
int type = 0;
int expected = sourceDimensions.length;
int actual = tr.getSourceDimensions();
if (actual == expected) {
type = 1;
expected = targetDimensions.length;
actual = tr.getTargetDimensions();
if (actual == expected) {
return tr;
}
}
throw new FactoryException(Resources.format(Resources.Keys.MismatchedTransformDimension_3, type, expected, actual));
}
use of org.opengis.util.FactoryException in project sis by apache.
the class InterpolatedTransform method createGeodeticTransformation.
/**
* Creates a transformation between two geodetic CRS. This factory method combines the
* {@code InterpolatedTransform} instance with the steps needed for converting values between
* geodetic and grid coordinates.
*
* <div class="section">Unit of measurement</div>
* The unit of measurement is determined by {@link DatumShiftGrid#getCoordinateUnit()}:
* <ul>
* <li>If the datum shift unit {@linkplain Units#isAngular(Unit) is angular}, then the transform
* will work with input and output coordinates in degrees of angle.</li>
* <li>If the datum shift unit {@linkplain Units#isLinear(Unit) is linear}, then the transform
* will work with input and output coordinates in metres.</li>
* <li>If the datum shift unit {@linkplain Units#isTemporal(Unit) is temporal}, then the transform
* will work with input and output coordinates in seconds.</li>
* <li>Generally for all units other than angular, the transform will work with input and output
* coordinates in the unit given by {@link Unit#getSystemUnit()}.</li>
* </ul>
*
* @param <T> dimension of the coordinate and the translation unit.
* @param factory the factory to use for creating the transform.
* @param grid the grid of datum shifts from source to target datum.
* The {@link DatumShiftGrid#interpolateInCell DatumShiftGrid.interpolateInCell(…)}
* method shall compute translations from <em>source</em> to <em>target</em> as
* {@linkplain DatumShiftGrid#isCellValueRatio() ratio of offsets divided by cell sizes}.
* @return the transformation between geodetic coordinates.
* @throws FactoryException if an error occurred while creating a transform.
*/
public static <T extends Quantity<T>> MathTransform createGeodeticTransformation(final MathTransformFactory factory, final DatumShiftGrid<T, T> grid) throws FactoryException {
ArgumentChecks.ensureNonNull("grid", grid);
final InterpolatedTransform tr;
try {
if (grid.getTranslationDimensions() == 2) {
tr = new InterpolatedTransform2D(grid);
} else {
tr = new InterpolatedTransform(grid);
}
} catch (NoninvertibleMatrixException e) {
throw new FactoryException(e.getLocalizedMessage(), e);
}
return tr.context.completeTransform(factory, tr);
}
use of org.opengis.util.FactoryException in project sis by apache.
the class MathTransforms method concatenate.
/**
* Concatenates the two given transforms. The returned transform will implement
* {@link MathTransform1D} or {@link MathTransform2D} if the dimensions of the
* concatenated transform are equal to 1 or 2 respectively.
*
* @param tr1 the first math transform.
* @param tr2 the second math transform.
* @return the concatenated transform.
* @throws MismatchedDimensionException if the output dimension of the first transform
* does not match the input dimension of the second transform.
*
* @see DefaultMathTransformFactory#createConcatenatedTransform(MathTransform, MathTransform)
*/
public static MathTransform concatenate(final MathTransform tr1, final MathTransform tr2) throws MismatchedDimensionException {
ArgumentChecks.ensureNonNull("tr1", tr1);
ArgumentChecks.ensureNonNull("tr2", tr2);
final MathTransform tr;
try {
tr = ConcatenatedTransform.create(tr1, tr2, null);
} catch (FactoryException e) {
// Should never happen actually.
throw new IllegalArgumentException(e);
}
assert isValid(getSteps(tr)) : tr;
return tr;
}
use of org.opengis.util.FactoryException in project sis by apache.
the class GeodeticObjectParser method parseObject.
/**
* Parses a <cite>Well Know Text</cite> (WKT).
*
* @param text the text to be parsed.
* @param position the position to start parsing from.
* @return the parsed object.
* @throws ParseException if the string can not be parsed.
*/
@Override
public final Object parseObject(final String text, final ParsePosition position) throws ParseException {
final Object object;
try {
object = super.parseObject(text, position);
/*
* After parsing the object, we may have been unable to set the VerticalCRS of VerticalExtent instances.
* First, try to set a default VerticalCRS for Mean Sea Level Height in metres. In the majority of cases
* that should be enough. If not (typically because the vertical extent uses other unit than metre), try
* to create a new CRS using the unit declared in the WKT.
*/
if (verticalElements != null) {
Exception ex = null;
try {
// Optional operation.
verticalElements = verticalElements.resolve(referencing.getMSLH());
} catch (UnsupportedOperationException e) {
ex = e;
}
if (verticalElements != null)
try {
verticalElements = verticalElements.complete(crsFactory, csFactory);
} catch (FactoryException e) {
if (ex == null)
ex = e;
else
ex.addSuppressed(e);
}
if (verticalElements != null) {
warning(null, (String) null, Errors.formatInternational(Errors.Keys.CanNotAssignUnitToDimension_2, WKTKeywords.VerticalExtent, verticalElements.unit), ex);
}
}
} finally {
verticalElements = null;
verticalCRS = null;
axisOrder.clear();
// for letting the garbage collector do its work.
properties.clear();
}
return object;
}
use of org.opengis.util.FactoryException in project sis by apache.
the class GeodeticObjectParser method parseMethod.
/**
* Parses a {@code "Method"} (WKT 2) element, without the parameters.
*
* @param parent the parent element.
* @param keywords the element keywords.
* @return the operation method.
* @throws ParseException if the {@code "Method"} element can not be parsed.
*/
private OperationMethod parseMethod(final Element parent, final String... keywords) throws ParseException {
final Element element = parent.pullElement(MANDATORY, keywords);
final String name = element.pullString("method");
Map<String, ?> properties = parseMetadataAndClose(element, name, null);
// See NOTE 2 in parseDerivingConversion.
final Identifier id = toIdentifier(properties.remove(IdentifiedObject.IDENTIFIERS_KEY));
/*
* The map projection method may be specified by an EPSG identifier (or any other authority),
* which is preferred to the method name since the later is potentially ambiguous. However not
* all CoordinateOperationFactory may accept identifier as an argument to 'getOperationMethod'.
* So if an identifier is present, we will try to use it but fallback on the name if we can
* not use the identifier.
*/
FactoryException suppressed = null;
if (id instanceof ReferenceIdentifier)
try {
// CodeSpace is a mandatory attribute in ID[…] elements, so we do not test for null values.
return referencing.getOperationMethod(opFactory, mtFactory, ((ReferenceIdentifier) id).getCodeSpace() + DefaultNameSpace.DEFAULT_SEPARATOR + id.getCode());
} catch (FactoryException e) {
suppressed = e;
}
try {
return referencing.getOperationMethod(opFactory, mtFactory, name);
} catch (FactoryException e) {
if (suppressed != null) {
e.addSuppressed(suppressed);
}
throw element.parseFailed(e);
}
}
Aggregations