[geotk] [Newbie] Coordinate heading + transformation

Martin Desruisseaux martin.desruisseaux at geomatys.fr
Tue Aug 18 05:40:30 EDT 2009


Hello Sam

You will find attached to this email a demo program with comments which will 
hopefully help to make it run. I will try to post this demo somewhere on the web 
site later.

	Regards,

		Martin

-------------- next part --------------
package demo;

import org.opengis.geometry.DirectPosition;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.operation.MathTransform;
import org.opengis.referencing.operation.TransformException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;

import org.geotoolkit.referencing.CRS;
import org.geotoolkit.referencing.crs.DefaultGeocentricCRS;
import org.geotoolkit.referencing.crs.DefaultGeographicCRS;
import org.geotoolkit.geometry.GeneralDirectPosition;


/**
 * Demonstration of a few coordinate conversions.
 *
 * Requirements: geotk-utility, geotk-metadata, geotk-referencing, geotk-epsg, JavaDB or Derby
 * (geotk-epsg and JavaDB/Derby are required for geocentricToProjected only).
 */
public class CoordinateConversion {
    /**
     * Converts a coordinate from Geocentric CRS to Geographic CRS.
     * This demo uses predefined CRS constants for simplicity. The
     * coordinate is Everest Mount (27°59′17″N 86°55′31″E).
     *
     * @throws FactoryException If an error occured while searching for a conversion.
     * @throws TransformException If an error occured while performing the conversion.
     */
    public static void geocentricToGeographic() throws FactoryException, TransformException {
        CoordinateReferenceSystem sourceCRS = DefaultGeocentricCRS.CARTESIAN;
        CoordinateReferenceSystem targetCRS = DefaultGeographicCRS.WGS84_3D;
        MathTransform tr = CRS.findMathTransform(sourceCRS, targetCRS);
        /*
         * At this point we can convert an arbitrary amount of coordinates using the
         * same MathTransform object. It could be in concurrent threads if we wish.
         */
        DirectPosition sourcePt = new GeneralDirectPosition(302742.5, 5636029.0, 2979489.2);
        DirectPosition targetPt = tr.transform(sourcePt, null);
        System.out.println("Source point: " + sourcePt);
        System.out.println("Target point: " + targetPt);
    }

    /**
     * This is the same demo than above, except that the target CRS is now a
     * projected one except than a geographic one. Because there is so many
     * projected CRS available, Geotoolkit.org does not define any constant
     * for them. We have to pick one from a database.
     *
     * For this demo, we use EPSG:3395 which stands for "WGS 84 / World Mercator".
     * For browsing projections codes on-line, see http://www.epsg-registry.org/
     *
     * NOTE: For running this test, the JavaDB or Derby database must be available
     * on the classpath. JavaDB is bundled in Sun JDK distribution and doesn't need
     * a separated download, but still need to be declared explicitly on the classpath.
     * It is usually located in a "db" folder in the Java installation directory.
     *
     * @throws FactoryException If an error occured while searching for a conversion.
     * @throws TransformException If an error occured while performing the conversion.
     */
    public static void geocentricToProjected() throws FactoryException, TransformException {
        CoordinateReferenceSystem sourceCRS = DefaultGeocentricCRS.CARTESIAN;
        CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:3395");
        MathTransform tr = CRS.findMathTransform(sourceCRS, targetCRS);
        /*
         * At this point we can convert an arbitrary amount of coordinates using the
         * same MathTransform object. It could be in concurrent threads if we wish.
         */
        DirectPosition sourcePt = new GeneralDirectPosition(302742.5, 5636029.0, 2979489.2);
        DirectPosition targetPt = tr.transform(sourcePt, null);
        System.out.println("Source point: " + sourcePt);
        System.out.println("Target point: " + targetPt);
    }

    /**
     * Runs the demo from the command line.
     *
     * @throws FactoryException If an error occured while searching for a conversion.
     * @throws TransformException If an error occured while performing the conversion.
     */
    public static void main(String[] args) throws FactoryException, TransformException {
        System.out.println("Geographic to Geocentric CRS");
        geocentricToGeographic();

        System.out.println();
        System.out.println("Geographic to Geocentric CRS");
        geocentricToProjected();
    }
}


More information about the Geotoolkit mailing list