Install Proj4J with Gradle
masterTo include the core Proj4J library in a Gradle project, add the following implementation dependency:
dependencies {
implementation 'org.locationtech.proj4j:proj4j:{latest version}'
}repository·master·Indexed 42 days ago
https://github.com/locationtech/proj4jA Java library for converting coordinates between different geospatial coordinate reference systems, designed for compatibility with proj.4 parameters. It includes modules for EPSG dataset distribution (proj4j-epsg) and GeoAPI interface wrappers (proj4j-geoapi) for 2D and 3D coordinate transformations.
To include the core Proj4J library in a Gradle project, add the following implementation dependency:
dependencies {
implementation 'org.locationtech.proj4j:proj4j:{latest version}'
}To include the core Proj4J library in a Maven project, add the following dependency. Note that as of version 1.2.2, the core library does not include EPSG licensed files, so you may need to include the proj4j-epsg dependency as well for full functionality.
<properties>
<proj4j.version>{latest version}</proj4j.version>
</properties>
<dependency>
<groupId>org.locationtech.proj4j</groupId>
<artifactId>proj4j</artifactId>
<version>${proj4j.version}</version>
</dependency>To include the EPSG dataset module in a Gradle project, add the following implementation dependency:
implementation 'org.locationtech.proj4j:proj4j-epsg:{latest version}'The proj4j-epsg module distributes a portion of the EPSG dataset. This artifact is released under the EPSG database distribution license. Use this dependency to ensure Proj4J can operate with standard EPSG definitions.
<properties>
<proj4j.version>{latest version}</proj4j.version>
</properties>
<dependency>
<groupId>org.locationtech.proj4j</groupId>
<artifactId>proj4j-epsg</artifactId>
<version>${proj4j.version}</version>
</dependency>To include the GeoAPI wrapper module in a Gradle project, add the following implementation dependency:
implementation 'org.locationtech.proj4j:proj4j-geoapi:{latest version}'The proj4j-geoapi module provides wrappers to use Proj4J behind GeoAPI interfaces, which are Java interfaces derived from OGC/ISO standards.
<properties>
<proj4j.version>{latest version}</proj4j.version>
</properties>
<dependency>
<groupId>org.locationtech.proj4j</groupId>
<artifactId>proj4j-geoapi</artifactId>
<version>${proj4j.version}</version>
</dependency>To transform coordinates between different Coordinate Reference Systems (CRSs), follow these steps:
CRSFactory to instantiate your source and target systems.CoordinateTransform: Use a CoordinateTransformFactory to create a transform object between your two CRSs..transform() method. Note that the result is written into a provided ProjCoordinate object (an output parameter).// 1. Obtaining CRSs by name
CRSFactory crsFactory = new CRSFactory();
CoordinateReferenceSystem WGS84 = crsFactory.createFromName("epsg:4326");
CoordinateReferenceSystem UTM = crsFactory.createFromName("epsg:25833");
// OR 2. Obtaining CRSs using parameters
CRSFactory crsFactoryParams = new CRSFactory();
CoordinateReferenceSystem WGS84Params = crsFactoryParams.createFromParameters("WGS84",
"+proj=longlat +datum=WGS84 +no_defs");
CoordinateReferenceSystem UTMParams = crsFactoryParams.createFromParameters("UTM",
"+proj=utm +zone=33 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs");
// 3. Transforming coordinates
CoordinateTransformFactory ctFactory = new CoordinateTransformFactory();
CoordinateTransform wgsToUtm = ctFactory.createTransform(WGS84, UTM);
// `result` is an output parameter to `transform()`
ProjCoordinate result = new ProjCoordinate();
wgsToUtm.transform(new ProjCoordinate(lon, lat), result);If your application uses the GeoAPI (OpenGIS) standard, TransformWrapper2D provides a bridge between PROJ4J's CoordinateTransform and the MathTransform2D interface. It allows you to perform 2D coordinate transformations using standard GeoAPI methods.
Key characteristics:
double[], float[], or Point2D objects.java.awt.Shape objects by transforming their control points..inverse().The TransformWrapper3D class is a GeoAPI-compatible wrapper for PROJ4J coordinate transformations specifically designed for three-dimensional (3D) coordinates. It implements the MathTransform interface, allowing PROJ4J transformations to be used in environments expecting GeoAPI standards.
Key characteristics:
TRIDIMENSIONAL).transform methods to handle transformations between double[] and float[] arrays.srcPts == dstPts) and the destination offset is less than the source offset, the class automatically creates a temporary copy to prevent data corruption during the transformation process.inverse() method, which is lazily computed and cached.// Example of transforming 3D points using TransformWrapper3D
// Note: TransformWrapper3D is typically obtained via GeoAPI-compatible factory methods
// Double precision transformation
transform.transform(srcPts, 0, dstPts, 0, numPts);
// Single precision transformation
transform.transform(srcPtsFloat, 0, dstPtsFloat, 0, numPts);
// Mixed precision: float source to double destination
transform.transform(srcPtsFloat, 0, dstPtsDouble, 0, numPts);
// Mixed precision: double source to float destination
transform.transform(srcPtsDouble, 0, dstPtsFloat, 0, numPts);To perform a reverse transformation (from target CRS back to source CRS), call the .inverse() method. The result is cached after the first call to improve performance.
MathTransform2D inverseTransform = mathTransform.inverse();The TransformWrapper2D class implements several transform methods to handle different data formats. Note that when using the same array for both source and destination (srcPts == dstPts), the implementation handles overlapping memory by creating a temporary copy if necessary to prevent data corruption.
// Transforming a single Point2D
Point2D srcPt = new Point2D.Double(x, y);
Point2D dstPt = new Point2D.Double();
mathTransform.transform(srcPt, dstPt);
// Transforming arrays of coordinates (double precision)
// srcOff and dstOff are the starting indices in the arrays
// numPts is the number of 2D coordinate pairs to transform
mathTransform.transform(srcPts, srcOff, dstPts, dstOff, numPts);
// Transforming arrays of coordinates (float precision)
mathTransform.transform(srcPts, srcOff, dstPts, dstOff, numPts);You can transform a java.awt.Shape (like a Path2D) into a new shape in the target coordinate system. The implementation iterates through the shape's path segments (MOVETO, LINETO, QUADTO, CUBICTO) and transforms the control points.
Warning: This is a simple implementation that transforms control points; it does not check if straight lines should be converted to curves due to the transformation.
// Returns a new Shape representing the transformed geometry
Shape transformedShape = mathTransform.createTransformedShape(originalShape);