NoSuchAuthorityCodeExcpetion in geotools program (gradle build) converting between coordinate systems

ditkin :

I have a java geotools program to convert a coordinate between two coordinate systems. It attempts to convert between coordinate system ESPG:4326 and ESPG:5179.

I receive the following exception when trying to execute this line in my code:

CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:4326");

Exception in thread "main" org.opengis.referencing.NoSuchAuthorityCodeException: No code "EPSG:4326" from authority "EPSG" found for object of type "EngineeringCRS".
    at org.geotools.referencing.factory.epsg.CartesianAuthorityFactory.noSuchAuthorityException(CartesianAuthorityFactory.java:137)
    at org.geotools.referencing.factory.epsg.CartesianAuthorityFactory.createEngineeringCRS(CartesianAuthorityFactory.java:129)
    at org.geotools.referencing.factory.epsg.CartesianAuthorityFactory.createCoordinateReferenceSystem(CartesianAuthorityFactory.java:120)
    at org.geotools.referencing.factory.AuthorityFactoryAdapter.createCoordinateReferenceSystem(AuthorityFactoryAdapter.java:780)
    at org.geotools.referencing.factory.ThreadedAuthorityFactory.createCoordinateReferenceSystem(ThreadedAuthorityFactory.java:636)
    at org.geotools.referencing.DefaultAuthorityFactory.createCoordinateReferenceSystem(DefaultAuthorityFactory.java:177)
    at org.geotools.referencing.CRS.decode(CRS.java:517)
    at org.geotools.referencing.CRS.decode(CRS.java:440)
    at main.main(main.java:13)

My test program:

import org.geotools.geometry.jts.JTS;
import org.geotools.referencing.CRS;
import org.locationtech.jts.geom.Coordinate;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;

import javax.xml.crypto.dsig.TransformException;

public class main {

    public static void main(String[] args)  throws Exception {
        CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:4326");
        CoordinateReferenceSystem sourceCRS = CRS.decode("EPSG:5179");
        double coordinateX = 1307285;
        double coordinateY = 2229260;
        Coordinate in = new Coordinate(coordinateX, coordinateY);
        Coordinate out = in;

        MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS);
        Coordinate result = JTS.transform(in, out, transform);
        result.toString();
    }
}

My build.gradle:

plugins {
id 'java'
}
repositories {
    flatDir {
        dirs 'libs'
    }
    maven {
        url "http://download.osgeo.org/webdav/geotools/"
    }
    mavenCentral()
}
dependencies {
    implementation group: 'org.geotools', name: 'gt-main', version: '20.2'
    implementation('org.geotools:gt-referencing:20.2') {
        exclude group: 'javax.units', module: 'jsr108' 
    }
    files('libs/js4108-0.01.jar')
}

I thought it was sufficient to depend on gt-main and gt-referencing.

ditkin :

I needed to add the following dependencies

implementation group: 'org.geotools', name: 'gt-epsg-hsql', version: '20.2'
implementation group: 'org.geotools', name: 'gt-epsg-extension', version: '20.2'

This is documented in geotools referencing documentation

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=160960&siteId=1