Package org.geotools.geometry

Examples of org.geotools.geometry.GeneralDirectPosition


    public static void assertTransformEquals3_3(final MathTransform transform,
                                                final double  x, final double  y, final double  z,
                                                final double ex, final double ey, final double ez)
            throws TransformException
    {
        final GeneralDirectPosition source = new GeneralDirectPosition(x,y,z);
        final GeneralDirectPosition target = new GeneralDirectPosition(3);
        assertSame(target, transform.transform(source, target));
        final String message = "Expected ("+ex+", "+ey+", "+ez+"), "+
              "transformed=("+target.ordinates[0]+", "+target.ordinates[1]+", "+target.ordinates[2]+")";
        assertEquals(message, ex, target.ordinates[0], EPS);
        assertEquals(message, ey, target.ordinates[1], EPS);
View Full Code Here


    public static void assertTransformEquals2_3(final MathTransform transform,
                                                final double  x, final double  y,
                                                final double ex, final double ey, final double ez)
            throws TransformException
    {
        final GeneralDirectPosition source = new GeneralDirectPosition(x,y);
        final GeneralDirectPosition target = new GeneralDirectPosition(3);
        assertSame(target, transform.transform(source, target));
        final String message = "Expected ("+ex+", "+ey+", "+ez+"), "+
              "transformed=("+target.ordinates[0]+", "+target.ordinates[1]+", "+target.ordinates[2]+")";
        assertEquals(message, ex, target.ordinates[0], EPS);
        assertEquals(message, ey, target.ordinates[1], EPS);
View Full Code Here

    public static void assertTransformEquals3_2(final MathTransform transform,
                                                final double  x, final double  y, final double  z,
                                                final double ex, final double ey)
            throws TransformException
    {
        final GeneralDirectPosition source = new GeneralDirectPosition(x,y,z);
        final GeneralDirectPosition target = new GeneralDirectPosition(2);
        assertSame(target, transform.transform(source, target));
        final String message = "Expected ("+ex+", "+ey+"), "+
              "transformed=("+target.ordinates[0]+", "+target.ordinates[1]+")";
        assertEquals(message, ex, target.ordinates[0], EPS);
        assertEquals(message, ey, target.ordinates[1], EPS);
View Full Code Here

    public static void assertTransformEquals3_1(final MathTransform transform,
                                                final double  x, final double  y, final double  z,
                                                                                  final double ez)
            throws TransformException
    {
        final GeneralDirectPosition source = new GeneralDirectPosition(x,y,z);
        final GeneralDirectPosition target = new GeneralDirectPosition(1);
        assertSame(target, transform.transform(source, target));
        final String message = "Expected ("+ez+"), "+
              "transformed=("+target.ordinates[0]+")";
        assertEquals(message, ez, target.ordinates[0], 1E-2); // Greater tolerance level for Z.
    }
View Full Code Here

            CoordinateReferenceSystem crs = DefaultEngineeringCRS.CARTESIAN_2D;

            DirectPosition minDp = new DirectPosition2D(crs, 10.0, 10.0);
            DirectPosition maxDp = new DirectPosition2D(crs, 1000.0, 1000.0);

            Envelope env = new GeneralEnvelope(new GeneralDirectPosition(minDp),
                    new GeneralDirectPosition(maxDp));

            // Lets Generate some known points that will define interpolation
            HashMap /*<DirectPosition2D, Float>*/ pointsAndValues = new HashMap();

            pointsAndValues.put(new DirectPosition2D(crs, 130, 805), 6.5);
View Full Code Here

      float[][] g = new float[10][10];
      g[0][0]=2;
      CoordinateReferenceSystem crs = DefaultEngineeringCRS.CARTESIAN_2D;

      // Define the Envelope for our work; this will be the bounds of the final interpolation
      GeneralDirectPosition min = new GeneralDirectPosition( 0.0,   0.0 );
      GeneralDirectPosition max = new GeneralDirectPosition( 100.0, 100.0 );
      GeneralEnvelope env = new GeneralEnvelope(min, max);   
      env.setCoordinateReferenceSystem(crs);
     
      Hints k;
      GridCoverageFactory fac = new GridCoverageFactory();
View Full Code Here

            /*
             * Destination not set.  We are going to create the destination here.  Since we know
             * that the destination will be the Geotools implementation, write directly into the
             * 'ordinates' array.
             */
            final GeneralDirectPosition destination;
            ptDst = destination = new GeneralDirectPosition(dimTarget);
            final double[] source;
            if (dimSource <= dimTarget) {
                source = destination.ordinates;
                for (int i=dimSource; --i>=0;) {
                    source[i] = ptSrc.getOrdinate(i);
View Full Code Here

        final int pointDim = point.getDimension();
        if (pointDim != transDim+nSkipped) {
            throw new MismatchedDimensionException(Errors.format(
                        ErrorKeys.MISMATCHED_DIMENSION_$3, "point", pointDim, transDim + nSkipped));
        }
        final GeneralDirectPosition subPoint = new GeneralDirectPosition(transDim);
        for (int i=0; i<transDim; i++) {
            subPoint.ordinates[i] = point.getOrdinate(i + firstAffectedOrdinate);
        }
        return expand(toGMatrix(subTransform.derivative(subPoint)),
                      firstAffectedOrdinate, numTrailingOrdinates, 0);
View Full Code Here

                      sourceDim, envelope.getDimension()));
        }
        int coordinateNumber = 0;
        GeneralEnvelope transformed = null;
        if (targetPt == null) {
            targetPt = new GeneralDirectPosition(transform.getTargetDimensions());
        }
        /*
         * Before to run the loops, we must initialize the coordinates to the minimal values.
         * This coordinates will be updated in the 'switch' statement inside the 'while' loop.
         */
        final GeneralDirectPosition sourcePt = new GeneralDirectPosition(sourceDim);
        for (int i=sourceDim; --i>=0;) {
            sourcePt.setOrdinate(i, envelope.getMinimum(i));
        }
  loop: while (true) {
            /*
             * Transform a point and add the transformed point to the destination envelope.
             * Note that the very last point to be projected must be the envelope center.
             */
            if (targetPt != transform.transform(sourcePt, targetPt)) {
                throw new UnsupportedImplementationException(transform.getClass());
            }
            if (transformed != null) {
                transformed.add(targetPt);
            } else {
                transformed = new GeneralEnvelope(targetPt, targetPt);
            }
            /*
             * Get the next point's coordinates.  The 'coordinateNumber' variable should
             * be seen as a number in base 5 where the number of digits is equals to the
             * number of dimensions. For example, a 4-D space would have numbers ranging
             * from "0000" to "4444" (numbers in base 4). The digits are then translated
             * into minimal, central or maximal ordinates. The outer loop stops when the
             * counter roll back to "0000".  Note that 'targetPt' must keep the value of
             * the last projected point, which must be the envelope center identified by
             * "4444" in the 4-D case.
             */
            int n = ++coordinateNumber;
            for (int i=sourceDim; --i>=0;) {
                switch (n % 5) {
                    case 0:  sourcePt.setOrdinate(i, envelope.getMinimum(i)); n /= 5; break;
                    case 1:  sourcePt.setOrdinate(i, envelope.getMaximum(i)); continue loop;
                    case 2:  sourcePt.setOrdinate(i, (envelope.getMinimum(i) + envelope.getMedian (i)) / 2); continue loop;
                    case 3:  sourcePt.setOrdinate(i, (envelope.getMedian (i) + envelope.getMaximum(i)) / 2); continue loop;
                    case 4:  sourcePt.setOrdinate(i, envelope.getMedian (i)); continue loop;
                    default: throw new AssertionError(n); // Should never happen
                }
            }
            break;
        }
View Full Code Here

                throw new MismatchedReferenceSystemException(
                        Errors.format(ErrorKeys.MISMATCHED_COORDINATE_REFERENCE_SYSTEM));
            }
        }
        MathTransform mt = operation.getMathTransform();
        final GeneralDirectPosition centerPt = new GeneralDirectPosition(mt.getTargetDimensions());
        final GeneralEnvelope transformed = transform(mt, envelope, centerPt);
        /*
         * If the source envelope crosses the expected range of valid coordinates, also projects
         * the range bounds as a safety. Example: if the source envelope goes from 150 to 200°E,
         * some map projections will interpret 200° as if it was -160°, and consequently produce
         * an envelope which do not include the 180°W extremum. We will add those extremum points
         * explicitly as a safety. It may leads to bigger than necessary target envelope, but the
         * contract is to include at least the source envelope, not to returns the smallest one.
         */
        if (sourceCRS != null) {
            final CoordinateSystem cs = sourceCRS.getCoordinateSystem();
            if (cs != null) { // Should never be null, but check as a paranoiac safety.
                DirectPosition sourcePt = null;
                DirectPosition targetPt = null;
                final int dimension = cs.getDimension();
                for (int i=0; i<dimension; i++) {
                    final CoordinateSystemAxis axis = cs.getAxis(i);
                    if (axis == null) { // Should never be null, but check as a paranoiac safety.
                        continue;
                    }
                    final double min = envelope.getMinimum(i);
                    final double max = envelope.getMaximum(i);
                    final double  v1 = axis.getMinimumValue();
                    final double  v2 = axis.getMaximumValue();
                    final boolean b1 = (v1 > min && v1 < max);
                    final boolean b2 = (v2 > min && v2 < max);
                    if (!b1 && !b2) {
                        continue;
                    }
                    if (sourcePt == null) {
                        sourcePt = new GeneralDirectPosition(dimension);
                        for (int j=0; j<dimension; j++) {
                            sourcePt.setOrdinate(j, envelope.getMedian(j));
                        }
                    }
                    if (b1) {
                        sourcePt.setOrdinate(i, v1);
                        transformed.add(targetPt = mt.transform(sourcePt, targetPt));
                    }
                    if (b2) {
                        sourcePt.setOrdinate(i, v2);
                        transformed.add(targetPt = mt.transform(sourcePt, targetPt));
                    }
                    sourcePt.setOrdinate(i, envelope.getMedian(i));
                }
            }
        }

        // check the target CRSS
        /*
         * Special case for polar stereographic, if the envelope contains the origin, then
         * the whole set of longitudes should be included
         */
        final CoordinateReferenceSystem targetCRS = operation.getTargetCRS();
        if (targetCRS == null) {
            return transformed;
        }
        GeneralEnvelope generalEnvelope = toGeneralEnvelope(envelope);
        MapProjection sourceProjection = CRS.getMapProjection(sourceCRS);
        if (sourceProjection instanceof PolarStereographic) {
            PolarStereographic ps = (PolarStereographic) sourceProjection;
            ParameterValue<?> fe = ps.getParameterValues().parameter(
                    MapProjection.AbstractProvider.FALSE_EASTING.getName().getCode());
            double originX = fe.doubleValue();
            ParameterValue<?> fn = ps.getParameterValues().parameter(
                    MapProjection.AbstractProvider.FALSE_NORTHING.getName().getCode());
            double originY = fn.doubleValue();
            DirectPosition2D origin = new DirectPosition2D(originX, originY);
            if (generalEnvelope.contains(origin)) {
                if (targetCRS instanceof GeographicCRS) {
                    DirectPosition lowerCorner = transformed.getLowerCorner();
                    if (getAxisOrder(targetCRS) == AxisOrder.NORTH_EAST) {
                        lowerCorner.setOrdinate(1, -180);
                        transformed.add(lowerCorner);
                        lowerCorner.setOrdinate(1, 180);
                        transformed.add(lowerCorner);
                    } else {
                        lowerCorner.setOrdinate(0, -180);
                        transformed.add(lowerCorner);
                        lowerCorner.setOrdinate(0, 180);
                        transformed.add(lowerCorner);
                    }
                } else {
                    // there is no guarantee that the whole range of longitudes will make
                    // sense for the target projection. We do a 1deg sampling as a compromise
                    // between
                    // speed and accuracy
                    DirectPosition lc = transformed.getLowerCorner();
                    DirectPosition uc = transformed.getUpperCorner();
                    for (int j = -180; j < 180; j++) {
                        expandEnvelopeByLongitude(j, lc, transformed, targetCRS);
                        expandEnvelopeByLongitude(j, uc, transformed, targetCRS);
                    }

                }
            } else {
                // check where the point closes to the origin is, make sure it's included
                // in the tranformation points
                if (generalEnvelope.getMinimum(0) < originX
                        && generalEnvelope.getMaximum(0) > originX) {
                    DirectPosition lc = generalEnvelope.getLowerCorner();
                    lc.setOrdinate(0, originX);
                    mt.transform(lc, lc);
                    transformed.add(lc);
                    DirectPosition uc = generalEnvelope.getUpperCorner();
                    uc.setOrdinate(0, originX);
                    mt.transform(uc, uc);
                    transformed.add(uc);
                }
                if (generalEnvelope.getMinimum(1) < originY
                        && generalEnvelope.getMaximum(1) > originY) {
                    DirectPosition lc = generalEnvelope.getLowerCorner();
                    lc.setOrdinate(1, originY);
                    mt.transform(lc, lc);
                    transformed.add(lc);
                    DirectPosition uc = generalEnvelope.getUpperCorner();
                    uc.setOrdinate(1, originY);
                    mt.transform(uc, uc);
                    transformed.add(uc);
                }

            }
        }
       
        /*
         * Now takes the target CRS in account...
         */
        transformed.setCoordinateReferenceSystem(targetCRS);
        final CoordinateSystem targetCS = targetCRS.getCoordinateSystem();
        if (targetCS == null) {
            // It should be an error, but we keep this method tolerant.
            return transformed;
        }
        /*
         * Checks for singularity points. For example the south pole is a singularity point in
         * geographic CRS because we reach the maximal value allowed on one particular geographic
         * axis, namely latitude. This point is not a singularity in the stereographic projection,
         * where axis extends toward infinity in all directions (mathematically) and south pole
         * has nothing special apart being the origin (0,0).
         *
         * Algorithm:
         *
         * 1) Inspect the target axis, looking if there is any bounds. If bounds are found, get
         *    the coordinates of singularity points and project them from target to source CRS.
         *
         *    Example: if the transformed envelope above is (80°S to 85°S, 10°W to 50°W), and if
         *             target axis inspection reveal us that the latitude in target CRS is bounded
         *             at 90°S, then project (90°S,30°W) to source CRS. Note that the longitude is
         *             set to the the center of the envelope longitude range (more on this later).
         *
         * 2) If the singularity point computed above is inside the source envelope, add that
         *    point to the target (transformed) envelope.
         *
         * Note: We could choose to project the (-180, -90), (180, -90), (-180, 90), (180, 90)
         * points, or the (-180, centerY), (180, centerY), (centerX, -90), (centerX, 90) points
         * where (centerX, centerY) are transformed from the source envelope center. It make
         * no difference for polar projections because the longitude is irrelevant at pole, but
         * may make a difference for the 180° longitude bounds.  Consider a Mercator projection
         * where the transformed envelope is between 20°N and 40°N. If we try to project (-180,90),
         * we will get a TransformException because the Mercator projection is not supported at
         * pole. If we try to project (-180, 30) instead, we will get a valid point. If this point
         * is inside the source envelope because the later overlaps the 180° longitude, then the
         * transformed envelope will be expanded to the full (-180 to 180) range. This is quite
         * large, but at least it is correct (while the envelope without expansion is not).
         */
        DirectPosition sourcePt = null;
        DirectPosition targetPt = null;
        final int dimension = targetCS.getDimension();
        for (int i=0; i<dimension; i++) {
            final CoordinateSystemAxis axis = targetCS.getAxis(i);
            if (axis == null) { // Should never be null, but check as a paranoiac safety.
                continue;
            }
            boolean testMax = false; // Tells if we are testing the minimal or maximal value.
            do {
                final double extremum = testMax ? axis.getMaximumValue() : axis.getMinimumValue();
                if (Double.isInfinite(extremum) || Double.isNaN(extremum)) {
                    /*
                     * The axis is unbounded. It should always be the case when the target CRS is
                     * a map projection, in which case this loop will finish soon and this method
                     * will do nothing more (no object instantiated, no MathTransform inversed...)
                     */
                    continue;
                }
                if (targetPt == null) {
                    try {
                        mt = mt.inverse();
                    } catch (NoninvertibleTransformException exception) {
                        /*
                         * If the transform is non invertible, this method can't do anything. This
                         * is not a fatal error because the envelope has already be transformed by
                         * the caller. We lost the check for singularity points performed by this
                         * method, but it make no difference in the common case where the source
                         * envelope didn't contains any of those points.
                         *
                         * Note that this exception is normal if target dimension is smaller than
                         * source dimension, since the math transform can not reconstituate the
                         * lost dimensions. So we don't log any warning in this case.
                         */
                        if (dimension >= mt.getSourceDimensions()) {
                            unexpectedException("transform", exception);
                        }
                        return transformed;
                    }
                    targetPt = new GeneralDirectPosition(mt.getSourceDimensions());
                    for (int j=0; j<dimension; j++) {
                        targetPt.setOrdinate(j, centerPt.getOrdinate(j));
                    }
                }
                targetPt.setOrdinate(i, extremum);
                try {
                    sourcePt = mt.transform(targetPt, sourcePt);
                } catch (TransformException e) {
                    /*
                     * This exception may be normal. For example we are sure to get this exception
                     * when trying to project the latitude extremums with a cylindrical Mercator
                     * projection. Do not log any message and try the other points.
                     */
                    continue;
                }
                if (generalEnvelope.contains(sourcePt)) {
                    transformed.add(targetPt);
                }
            } while ((testMax = !testMax) == true);
            if (targetPt != null) {
                targetPt.setOrdinate(i, centerPt.getOrdinate(i));
            }
        }
        return transformed;
    }
View Full Code Here

TOP

Related Classes of org.geotools.geometry.GeneralDirectPosition

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.