Proj4J

repository·master·Indexed 42 days ago

https://github.com/locationtech/proj4j

A 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.

Tokens
3K
Snippets
11
Records
13
Agent score
52%

What's inside Proj4J

  1. Install Proj4J with Maven

    master

    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>
  2. Install Proj4J-EPSG with Maven

    master

    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>
  3. Install Proj4J-GeoAPI with Maven

    master

    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>
  4. Transform coordinates using Proj4J

    master

    To transform coordinates between different Coordinate Reference Systems (CRSs), follow these steps:

    1. Create a CRSFactory to instantiate your source and target systems.
    2. Obtain CRSs: You can create them by name (e.g., using EPSG codes) or by passing Proj4-style parameter strings.
    3. Create a CoordinateTransform: Use a CoordinateTransformFactory to create a transform object between your two CRSs.
    4. Perform the transformation: Use the .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);
  5. Use TransformWrapper2D for GeoAPI MathTransform2D compatibility

    master

    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:

    • Dimensions: Always operates on 2D coordinates (Source: 2, Target: 2).
    • Precision: Supports transformations using double[], float[], or Point2D objects.
    • Shape Support: Can transform java.awt.Shape objects by transforming their control points.
    • Inverse: Supports retrieving the inverse transformation via .inverse().
  6. Use TransformWrapper3D for 3D coordinate transformations

    master

    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:

    • Dimensions: Both source and target dimensions are fixed at 3 (TRIDIMENSIONAL).
    • Precision Support: It provides overloaded transform methods to handle transformations between double[] and float[] arrays.
    • In-place Safety: If the source and destination arrays overlap (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 Operations: You can obtain the inverse transformation using the 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);
  7. Transform points and arrays with TransformWrapper2D

    master

    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);
  8. Transform java.awt.Shape using TransformWrapper2D

    master

    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);