Search in sources :

Example 1 with ImmutableEnvelope

use of org.apache.sis.geometry.ImmutableEnvelope in project sis by apache.

the class TransformCommand method transform.

/**
 * Transforms the given coordinates.
 */
private void transform(final List<double[]> points) throws TransformException {
    final int dimension = operation.getSourceCRS().getCoordinateSystem().getDimension();
    final MathTransform mt = operation.getMathTransform();
    final double[] result = new double[mt.getTargetDimensions()];
    final double[] domainCoordinate;
    final DirectPositionView positionInDomain;
    final ImmutableEnvelope domainOfValidity;
    final GeographicBoundingBox bbox;
    if (toDomainOfValidity != null && (bbox = CRS.getGeographicBoundingBox(operation)) != null) {
        domainOfValidity = new ImmutableEnvelope(bbox);
        domainCoordinate = new double[toDomainOfValidity.getTargetDimensions()];
        positionInDomain = new DirectPositionView.Double(domainCoordinate, 0, domainCoordinate.length);
    } else {
        domainOfValidity = null;
        domainCoordinate = null;
        positionInDomain = null;
    }
    for (final double[] coordinates : points) {
        if (coordinates.length != dimension) {
            throw new MismatchedDimensionException(Errors.format(Errors.Keys.MismatchedDimensionForCRS_3, operation.getSourceCRS().getName().getCode(), dimension, coordinates.length));
        }
        /*
             * At this point we got the coordinates and they have the expected number of dimensions.
             * Now perform the coordinate operation and print each ordinate values.  We will switch
             * to scientific notation if the coordinate is much larger than expected.
             */
        mt.transform(coordinates, 0, result, 0, 1);
        for (int i = 0; i < result.length; i++) {
            if (i != 0) {
                out.print(',');
            }
            final double value = result[i];
            final String s;
            if (Math.abs(value) >= thresholdForScientificNotation[i]) {
                s = Double.toString(value);
            } else {
                coordinateFormat.setMinimumFractionDigits(numFractionDigits[i]);
                coordinateFormat.setMaximumFractionDigits(numFractionDigits[i]);
                s = coordinateFormat.format(value);
            }
            out.print(CharSequences.spaces(ordinateWidth - s.length()));
            out.print(s);
        }
        /*
             * Append a warning after the transformed coordinate values if the source coordinate was outside
             * the domain of validity. A failure to perform a coordinate transformation is also considered as
             * being out of the domain of valididty.
             */
        if (domainOfValidity != null) {
            boolean inside;
            try {
                toDomainOfValidity.transform(coordinates, 0, domainCoordinate, 0, 1);
                inside = domainOfValidity.contains(positionInDomain);
            } catch (TransformException e) {
                inside = false;
                warning(e);
            }
            if (!inside) {
                out.print(",    ");
                printQuotedText(Errors.getResources(locale).getString(Errors.Keys.OutsideDomainOfValidity), 0, X364.FOREGROUND_RED);
            }
        }
        out.println();
    }
}
Also used : MathTransform(org.opengis.referencing.operation.MathTransform) TransformException(org.opengis.referencing.operation.TransformException) ImmutableEnvelope(org.apache.sis.geometry.ImmutableEnvelope) GeographicBoundingBox(org.opengis.metadata.extent.GeographicBoundingBox) DefaultGeographicBoundingBox(org.apache.sis.metadata.iso.extent.DefaultGeographicBoundingBox) InternationalString(org.opengis.util.InternationalString) MismatchedDimensionException(org.opengis.geometry.MismatchedDimensionException) DirectPositionView(org.apache.sis.internal.referencing.DirectPositionView)

Aggregations

ImmutableEnvelope (org.apache.sis.geometry.ImmutableEnvelope)1 DirectPositionView (org.apache.sis.internal.referencing.DirectPositionView)1 DefaultGeographicBoundingBox (org.apache.sis.metadata.iso.extent.DefaultGeographicBoundingBox)1 MismatchedDimensionException (org.opengis.geometry.MismatchedDimensionException)1 GeographicBoundingBox (org.opengis.metadata.extent.GeographicBoundingBox)1 MathTransform (org.opengis.referencing.operation.MathTransform)1 TransformException (org.opengis.referencing.operation.TransformException)1 InternationalString (org.opengis.util.InternationalString)1