In this post we will how to transform 2D coordinates from one coordinate reference system (CRS) to another one using the open source, Java library GeoTools.

In the first section a code snippet is shown, and in the second one the list of needed jars is given.

## 1. The code.

The following Java code snippet transforms a set of coordinates (x, y) expressed in the ETRS 89, UTM zone 29N (EPSG code 25829) coordinate system to WGS84 (EPSG code 4326). The source coordinates are stored in the variables *x* and *y*, and the result is left in the variables *transX* and *transY*.

import org.geotools.geometry.DirectPosition2D;
import org.geotools.referencing.CRS;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;
...
CoordinateReferenceSystem sourceCrs = CRS.decode("EPSG:25829");
CoordinateReferenceSystem targetCrs = CRS.decode("EPSG:4326");
double x = (double) 636497.59434;
double y = (double) 4778964.017375;
boolean lenient = true;
MathTransform mathTransform
= CRS.findMathTransform(sourceCrs, targetCrs, lenient);
DirectPosition2D srcDirectPosition2D
= new DirectPosition2D(sourceCrs, x, y);
DirectPosition2D destDirectPosition2D
= new DirectPosition2D();
mathTransform.transform(srcDirectPostion2D, destDirectPosition2D);
double transX = destDirectPosition2D.x;
double transY = destDirectPosition2D.y;

More…