GeoTools : Transformation through EPSG

The best way to make a transformation (when it is possible), is to use the EPSG database for performing it.
The EPSG database (www.epsg.org) contains many information like :

  • the existing CRS in the world
  • the main conversion between two CRS (for instance from geocentric to projected,...)
  • the main transformations between two CRS (for instance from WSG84 to NTF , ...)
  • and so on...

You have first to download an access database file from the epsg website..
On windows, go to the ODBC data source management window to reference this new access database...

You also have to use the epsg-access-2.1.x.jar library in order to be able to access to the database through jdbc (this file is attached but should be found in the delivery too).

Put this library in your classpath and write a piece of code like the following :

package cordinateTransformation;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.NoSuchElementException;
import java.util.Vector;

import org.geotools.geometry.GeneralDirectPosition;
import org.geotools.referencing.CRS;
import org.geotools.referencing.crs.GeocentricCRS;
import org.geotools.referencing.operation.MathTransformFactory;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.crs.GeographicCRS;
import org.opengis.referencing.operation.MathTransform;
import org.opengis.referencing.operation.TransformException;
import org.opengis.spatialschema.geometry.DirectPosition;
import org.opengis.spatialschema.geometry.MismatchedDimensionException;

  

public class CoordinatesTransformer {
	CoordinateReferenceSystem sourceCRS;
	CoordinateReferenceSystem targetCRS;
	
	public static void main(String[] args) {
		BufferedReader buffer = new BufferedReader(new InputStreamReader(
				System.in));
		CoordinatesTransformer aTransformer = new  CoordinatesTransformer();
		try {
			String coordinateX = buffer.readLine();
			String coordinateY = buffer.readLine();
			String coordinateZ = buffer.readLine();
			DirectPosition aPosition = aTransformer.transform(coordinateX,coordinateY,coordinateZ);
			System.out.println(aPosition.toString());
		} catch (IOException e) {
			e.printStackTrace();
		} catch (MismatchedDimensionException e) { 
			e.printStackTrace();
		} catch (NoSuchElementException e) { 
			e.printStackTrace();
		} catch (FactoryException e) { 
			e.printStackTrace();
		} catch (TransformException e) { 
			e.printStackTrace();
		}
	}
	
	public DirectPosition transform(String x, String y, String z) throws NoSuchElementException, FactoryException, MismatchedDimensionException, TransformException {
		 
			sourceCRS = CRS.decode("EPSG:4978"); //WGS84 geocentrique
			targetCRS = CRS.decode("EPSG:4979"); //WGS84 geographique 3D
			     
			 MathTransform math =CRS.transform( sourceCRS, targetCRS );
			
		     DirectPosition pt1 = new GeneralDirectPosition(new Double(x),new Double(y) ,new Double(z) );        
		     return math.transform( pt1, null );
	}
	 
}

Attachments:

epsg-access-2.1.x.jar (application/java-archive)