CRS changes for revisions 13689:14063

Command line:

svn diff --extensions "--unified --ignore-space-change --ignore-all-space --ignore-eol-style" -r13689:14063 https://svn.osgeo.org/geotools/trunk/modules/library/referencing/src/main/java/org/geotools/referencing/CRS.java
Revision 13689Revision 14063
import java.util.TreeSet;

// OpenGIS dependencies
import org.opengis.metadata.citation.Citation;
import org.opengis.referencing.Factory;
import org.opengis.referencing.FactoryException;
import java.util.TreeSet;

// OpenGIS dependencies
import org.geotools.factory.Hints;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.opengis.metadata.citation.Citation;
import org.opengis.referencing.Factory;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.operation.MathTransform;
import org.opengis.referencing.operation.CoordinateOperation;


/**
 * Simple utility class for making use of the {@link CoordinateReferenceSystem}
 * and associated {@link Factory} implementations.
import org.opengis.referencing.operation.MathTransform;
import org.opengis.referencing.operation.CoordinateOperation;

import com.vividsolutions.jts.geom.Coordinate;


/**
 * Simple utility class for making use of the {@link CoordinateReferenceSystem}
 * and associated {@link Factory} implementations.
    public Object  factory( CoordinateOperationFactory factory ) throws FactoryException;
}

/**
 * Grab transform between two CoordianteReference Systems.
 * <p>
    public Object  factory( CoordinateOperationFactory factory ) throws FactoryException;
}


/**
 * Grab transform between two CoordianteReference Systems.
 * <p>
 *
 * @param from
 * @param to
 * @return MathTransform, or null if unavailable
 * @throws FactoryException only if MathTransform is unavailable due to error
 */
public static MathTransform transform( final CoordinateReferenceSystem from, final CoordinateReferenceSystem to ) throws FactoryException {
    List list = visit( new OperationVisitor() {
        public Object factory( CoordinateOperationFactory factory ) throws FactoryException {
            CoordinateOperation opperation = factory.createOperation( from, to );
            return opperation.getMathTransform();
        }
    });
    return list.isEmpty() ? null : (MathTransform) list.get(0);
 *
 * @param from
 * @param to
 * @param lenientTransforms if true then the transforms created will not throw bursa wolf required exception during datum
 * shifts if the bursa wolf paramaters are not specified. Instead it will assume a no datum shift.
 * @return MathTransform, or null if unavailable
 * @throws FactoryException only if MathTransform is unavailable due to error
 */
public static MathTransform transform( final CoordinateReferenceSystem from, final CoordinateReferenceSystem to, boolean lenientTransforms ) throws FactoryException {
    if( lenientTransforms )
        return FactoryFinder.getCoordinateOperationFactory(new Hints(Hints.LENIENT_DATUM_SHIFT, Boolean.TRUE)).createOperation(from, to).getMathTransform();
    else
        return transform(from, to);
}
/**
 * Grab transform between two CoordianteReference Systems.
 * <p>
 * Sample use:<pre><code>
 * MathTransform transform = CRS.transform( CRS.decode("EPSG:42102"), CRS.decode("EPSG:4326") );
 * </code></pre>
 * </p>
 *
 * @param from
 * @param to
 * @return MathTransform, or null if unavailable
 * @throws FactoryException only if MathTransform is unavailable due to error
 */
public static MathTransform transform( final CoordinateReferenceSystem from, final CoordinateReferenceSystem to ) throws FactoryException {
    List list = visit( new OperationVisitor() {
        public Object factory( CoordinateOperationFactory factory ) throws FactoryException {
            CoordinateOperation operation = factory.createOperation( from, to );
            return operation.getMathTransform();
        }
    });
    return list.isEmpty() ? null : (MathTransform) list.get(0);
        if( trouble != null ) notFound.initCause( trouble );
        throw notFound;
    }
}
        if( trouble != null ) notFound.initCause( trouble );
        throw notFound;
    }

    /**
     * ESTIMATE the distance between the two points.
     *    1. transforms both points to lat/lon
     *    2. find the distance between the two points
     *
     *  NOTE: we're using ellipsoid calculations.
     *
     * @param p1   first point
     * @param p2   second point
     * @param crs  reference system the two points are in
     * @return approximate distance between the two points, in meters
     */
    public static double distance(Coordinate p1, Coordinate p2, CoordinateReferenceSystem crs) throws Exception
    {
        GeodeticCalculator gc = new GeodeticCalculator() ;  // WGS84

        double[] cs        = new double[4];
        double[] csLatLong = new double[4];
        cs[0] = p1.x;
        cs[1] = p1.y;
        cs[2] = p2.x;
        cs[3] = p2.y;

        MathTransform transform = distanceOperationFactory.createOperation(crs,DefaultGeographicCRS.WGS84).getMathTransform();
        transform.transform(cs, 0, csLatLong, 0, 2);
           //these could be backwards depending on what WSG84 you use
        gc.setAnchorPoint(csLatLong[0],csLatLong[1]);
        gc.setDestinationPoint(csLatLong[2],csLatLong[3]);

        return gc.getOrthodromicDistance();
}

    private final static CoordinateOperationFactory distanceOperationFactory;
    static {
        Hints hints=new Hints(Hints.LENIENT_DATUM_SHIFT, Boolean.TRUE);
        distanceOperationFactory=FactoryFinder.getCoordinateOperationFactory(hints);
    }


}