Java Code Examples for org.opengis.geometry.DirectPosition#getDimension()

The following examples show how to use org.opengis.geometry.DirectPosition#getDimension() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LinearTransformBuilder.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the index where to fetch a target position for the given source position in the flattened array.
 * This is the same work as {@link LinearTransformBuilder#flatIndex(DirectPosition)}, but without throwing
 * exception if the position is invalid. Instead, -1 is returned as a sentinel value for invalid source
 * (including mismatched number of dimensions).
 *
 * <p>The default implementation assumes a grid. This method must be overridden by {@link Ungridded}.</p>
 *
 * @see LinearTransformBuilder#flatIndex(DirectPosition)
 */
int flatIndex(final DirectPosition source) {
    final double[][] targets = LinearTransformBuilder.this.targets;
    if (targets != null) {
        final int[] gridSize = LinearTransformBuilder.this.gridSize;
        int i = gridSize.length;
        if (i == source.getDimension()) {
            int offset = 0;
            while (i != 0) {
                final int size = gridSize[--i];
                final double coordinate = source.getOrdinate(i);
                final int index = (int) coordinate;
                if (index < 0 || index >= size || index != coordinate) {
                    return -1;
                }
                offset = offset * size + index;
            }
            if (!Double.isNaN(targets[0][offset])) return offset;
        }
    }
    return -1;
}
 
Example 2
Source File: AbstractDirectPosition.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * Implementation of the public {@link #toString()} and {@link DirectPosition2D#toString()} methods
 * for formatting a {@code POINT} element from a direct position in <cite>Well Known Text</cite>
 * (WKT) format.
 *
 * @param  position           the position to format.
 * @param  isSinglePrecision  {@code true} if every coordinate values can be casted to {@code float}.
 * @return the point as a {@code POINT} in WKT format.
 *
 * @see ArraysExt#isSinglePrecision(double[])
 */
static String toString(final DirectPosition position, final boolean isSinglePrecision) {
    final StringBuilder buffer = new StringBuilder(32).append("POINT");
    final int dimension = position.getDimension();
    if (dimension == 0) {
        buffer.append("()");
    } else {
        char separator = '(';
        for (int i=0; i<dimension; i++) {
            buffer.append(separator);
            final double coordinate = position.getOrdinate(i);
            if (isSinglePrecision) {
                buffer.append((float) coordinate);
            } else {
                buffer.append(coordinate);
            }
            trimFractionalPart(buffer);
            separator = ' ';
        }
        buffer.append(')');
    }
    return buffer.toString();
}
 
Example 3
Source File: TransformSeparatorTest.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * Compares coordinate computed by a reference with coordinates computed by the transform to test.
 * We use this method when we can not easily analyze the {@link MathTransform} created by the test
 * case, for example because it may have been rearranged in arbitrary ways for optimization purpose
 * (e.g. {@link PassThroughTransform#tryConcatenate(boolean, MathTransform, MathTransformFactory)}).
 *
 * @param  tr1     first half of the transform to use as a reference.
 * @param  tr2     second half of the transform to use as a reference.
 * @param  test    the transform to test.
 * @param  random  random number generator for coordinate values.
 */
private static void compare(final MathTransform tr1, final MathTransform tr2, final MathTransform test, final Random random)
        throws TransformException
{
    DirectPosition source   = new GeneralDirectPosition(tr1.getSourceDimensions());
    DirectPosition step     = null;
    DirectPosition expected = null;
    DirectPosition actual   = null;
    for (int t=0; t<50; t++) {
        for (int i=source.getDimension(); --i>=0;) {
            source.setOrdinate(i, random.nextDouble());
        }
        step     = tr1 .transform(source,   step);
        expected = tr2 .transform(step, expected);
        actual   = test.transform(source, actual);
        assertEquals(expected, actual);
    }
}
 
Example 4
Source File: ArrayEnvelope.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs an envelope defined by two corners given as direct positions.
 * If at least one corner is associated to a CRS, then the new envelope will also
 * be associated to that CRS.
 *
 * @param  lowerCorner  the limits in the direction of decreasing coordinate values for each dimension.
 * @param  upperCorner  the limits in the direction of increasing coordinate values for each dimension.
 * @throws MismatchedDimensionException if the two positions do not have the same dimension.
 * @throws MismatchedReferenceSystemException if the CRS of the two position are not equal.
 */
public ArrayEnvelope(final DirectPosition lowerCorner, final DirectPosition upperCorner)
        throws MismatchedDimensionException, MismatchedReferenceSystemException
{
    crs = getCommonCRS(lowerCorner, upperCorner);           // This performs also an argument check.
    final int dimension = lowerCorner.getDimension();
    ensureDimensionMatches("crs", dimension, crs);
    ensureSameDimension(dimension, upperCorner.getDimension());
    coordinates = new double[dimension * 2];
    for (int i=0; i<dimension; i++) {
        coordinates[i            ] = lowerCorner.getOrdinate(i);
        coordinates[i + dimension] = upperCorner.getOrdinate(i);
    }
    verifyRanges(crs, coordinates);
}
 
Example 5
Source File: AbstractEnvelope.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Returns {@code true} if at least one coordinate in the given position
 * is {@link Double#NaN}. This is used for assertions only.
 */
static boolean hasNaN(final DirectPosition position) {
    for (int i=position.getDimension(); --i>=0;) {
        if (Double.isNaN(position.getOrdinate(i))) {
            return true;
        }
    }
    return false;
}
 
Example 6
Source File: GridExtent.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a slice of this given grid extent computed by a ratio between 0 and 1 inclusive.
 * This is a helper method for {@link GridDerivation#sliceByRatio(double, int...)} implementation.
 *
 * @param  slicePoint        a pre-allocated direct position to be overwritten by this method.
 * @param  sliceRatio        the ratio to apply on all grid dimensions except the ones to keep.
 * @param  dimensionsToKeep  the grid dimension to keep unchanged.
 */
final GridExtent sliceByRatio(final DirectPosition slicePoint, final double sliceRatio, final int[] dimensionsToKeep) {
    for (int i=slicePoint.getDimension(); --i >= 0;) {
        slicePoint.setOrdinate(i, sliceRatio * getSize(i, true) + getLow(i));       // TODO: use Math.fma
    }
    for (int i=0; i<dimensionsToKeep.length; i++) {
        slicePoint.setOrdinate(dimensionsToKeep[i], Double.NaN);
    }
    return slice(slicePoint, null);
}
 
Example 7
Source File: Plane.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Computes the values of all {@code sum_*} fields from randomly distributed points.
 * Value of all other fields are undetermined..
 */
Fit(final Iterable<? extends DirectPosition> points) {
    int i = 0, n = 0;
    for (final DirectPosition p : points) {
        final int dimension = p.getDimension();
        if (dimension != DIMENSION) {
            throw new MismatchedDimensionException(Errors.format(Errors.Keys.MismatchedDimension_3,
                        Strings.toIndexed("points", i), DIMENSION, dimension));
        }
        i++;
        final double x = p.getOrdinate(0); if (Double.isNaN(x)) continue;
        final double y = p.getOrdinate(1); if (Double.isNaN(y)) continue;
        final double z = p.getOrdinate(2); if (Double.isNaN(z)) continue;
        xx.setToProduct(x, x);
        yy.setToProduct(y, y);
        xy.setToProduct(x, y);
        zx.setToProduct(z, x);
        zy.setToProduct(z, y);
        sum_x .add(x );
        sum_y .add(y );
        sum_z .add(z );
        sum_xx.add(xx);
        sum_yy.add(yy);
        sum_xy.add(xy);
        sum_zx.add(zx);
        sum_zy.add(zy);
        n++;
    }
    this.n = n;
}
 
Example 8
Source File: ArgumentChecks.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Ensures that the given direct position, if non-null, has the expected number of dimensions.
 * This method does nothing if the given direct position is null.
 *
 * @param  name      the name of the argument to be checked. Used only if an exception is thrown.
 * @param  expected  the expected number of dimensions.
 * @param  position  the direct position to check for its dimension, or {@code null}.
 * @throws MismatchedDimensionException if the given direct position is non-null and does
 *         not have the expected number of dimensions.
 */
public static void ensureDimensionMatches(final String name, final int expected, final DirectPosition position)
        throws MismatchedDimensionException
{
    if (position != null) {
        final int dimension = position.getDimension();
        if (dimension != expected) {
            throw new MismatchedDimensionException(Errors.format(
                    Errors.Keys.MismatchedDimension_3, name, expected, dimension));
        }
    }
}
 
Example 9
Source File: LinearTransformBuilder.java    From sis with Apache License 2.0 4 votes vote down vote up
/**
 * Sets all control point (source, target) pairs, overwriting any previous setting.
 * The source positions are the keys in given map, and the target positions are the associated values.
 * The map should not contain two entries with the same source position.
 * Coordinate reference systems are ignored.
 * Null positions are silently ignored.
 * Positions with NaN or infinite coordinates cause an exception to be thrown.
 *
 * <p>All source positions shall have the same number of dimensions (the <cite>source dimension</cite>),
 * and all target positions shall have the same number of dimensions (the <cite>target dimension</cite>).
 * However the source dimension does not need to be the same than the target dimension.
 * Apache SIS currently supports only one- or two-dimensional source positions,
 * together with arbitrary target dimension.</p>
 *
 * <p>If this builder has been created with the {@link #LinearTransformBuilder(int...)} constructor,
 * then the coordinate values of all source positions shall be integers in the [0 … {@code gridSize[0]}-1]
 * range for the first dimension (typically column indices), in the [0 … {@code gridSize[1]}-1] range for
 * the second dimension (typically row indices), <i>etc</i>. This constraint does not apply for builders
 * created with the {@link #LinearTransformBuilder()} constructor.</p>
 *
 * @param  sourceToTarget  a map of source positions to target positions.
 *         Source positions are assumed precise and target positions are assumed uncertain.
 * @throws IllegalStateException if {@link #create(MathTransformFactory) create(…)} has already been invoked.
 * @throws IllegalArgumentException if the given positions contain NaN or infinite coordinate values.
 * @throws IllegalArgumentException if this builder has been {@linkplain #LinearTransformBuilder(int...)
 *         created for a grid} but some source coordinates are not indices in that grid.
 * @throws MismatchedDimensionException if some positions do not have the expected number of dimensions.
 *
 * @since 0.8
 */
public void setControlPoints(final Map<? extends Position, ? extends Position> sourceToTarget)
        throws MismatchedDimensionException
{
    ensureModifiable();
    ArgumentChecks.ensureNonNull("sourceToTarget", sourceToTarget);
    sources    = null;
    targets    = null;
    numPoints  = 0;
    int srcDim = 0;
    int tgtDim = 0;
    for (final Map.Entry<? extends Position, ? extends Position> entry : sourceToTarget.entrySet()) {
        final DirectPosition src = position(entry.getKey());   if (src == null) continue;
        final DirectPosition tgt = position(entry.getValue()); if (tgt == null) continue;
        /*
         * The first time that we get a non-null source and target coordinate, allocate the arrays.
         * The sources arrays are allocated only if the source coordinates are randomly distributed.
         */
        if (targets == null) {
            tgtDim = tgt.getDimension();
            if (tgtDim <= 0) {
                throw mismatchedDimension("target", 2, tgtDim);
            }
            if (gridSize == null) {
                srcDim = src.getDimension();
                if (srcDim <= 0) {
                    throw mismatchedDimension("source", 2, srcDim);
                }
                final int capacity = sourceToTarget.size();
                sources = new double[srcDim][capacity];
                targets = new double[tgtDim][capacity];
            } else {
                srcDim = gridSize.length;
                allocate(tgtDim);
            }
        }
        /*
         * Verify that the source and target coordinates have the expected number of dimensions before to store
         * the coordinates. If the grid size is known, we do not need to store the source coordinates. Instead,
         * we compute its index in the fixed-size target arrays.
         */
        int d;
        if ((d = src.getDimension()) != srcDim) throw mismatchedDimension("source", srcDim, d);
        if ((d = tgt.getDimension()) != tgtDim) throw mismatchedDimension("target", tgtDim, d);
        boolean isValid = true;
        int index;
        if (gridSize != null) {
            index = flatIndex(src);
        } else {
            index = numPoints;
            for (int i=0; i<srcDim; i++) {
                isValid &= Double.isFinite(sources[i][index] = src.getOrdinate(i));
            }
        }
        for (int i=0; i<tgtDim; i++) {
            isValid &= Double.isFinite(targets[i][index] = tgt.getOrdinate(i));
        }
        /*
         * If the point contains some NaN or infinite coordinate values, it is okay to leave it as-is
         * (without incrementing 'numPoints') provided that we ensure that at least one value is NaN.
         * For convenience, we set only the first coordinate to NaN. The ControlPoints map will check
         * for the first coordinate too, so we need to keep this policy consistent.
         */
        if (isValid) {
            numPoints++;
        } else {
            targets[0][index] = Double.NaN;
            throw new IllegalArgumentException(Errors.format(Errors.Keys.IllegalMapping_2, src, tgt));
        }
    }
}
 
Example 10
Source File: EllipsoidToCentricTransform.java    From sis with Apache License 2.0 3 votes vote down vote up
/**
 * Computes the derivative at the given location.
 * This method relaxes a little bit the {@code MathTransform} contract by accepting two- or three-dimensional
 * points even if the number of dimensions does not match the {@link #getSourceDimensions()} value.
 *
 * <div class="note"><b>Rational:</b>
 * that flexibility on the number of dimensions is required for calculation of {@linkplain #inverse() inverse}
 * transform derivative, because that calculation needs to inverse a square matrix with all terms in it before
 * to drop the last row in the two-dimensional case.</div>
 *
 * @param  point  the coordinate point where to evaluate the derivative.
 * @return the derivative at the specified point (never {@code null}).
 * @throws TransformException if the derivative can not be evaluated at the specified point.
 */
@Override
public Matrix derivative(final DirectPosition point) throws TransformException {
    final int dim = point.getDimension();
    final boolean wh;
    final double h;
    switch (dim) {
        default: throw mismatchedDimension("point", getSourceDimensions(), dim);
        case 3:  wh = true;  h = point.getOrdinate(2); break;
        case 2:  wh = false; h = 0; break;
    }
    return transform(point.getOrdinate(0), point.getOrdinate(1), h, null, 0, true, wh);
}