2

I'm tackling a simple task: creating CoordinateReferenceSystem from EPSG code using

String code = "26910";
CoordinateReferenceSystem crs = ReferencingFactoryFinder.getCRSAuthorityFactory
("EPSG",null).createCoordinateReferenceSystem(code);

This snippet produces:

`Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class org.hsqldb.lib.FrameworkLogger
    at org.hsqldb.persist.Logger.getEventLogger(Unknown Source)
    at org.hsqldb.persist.Logger.logInfoEvent(Unknown Source)
    at org.hsqldb.persist.Logger.closePersistence(Unknown Source)
    at org.hsqldb.Database.reopen(Unknown Source)
    at org.hsqldb.Database.open(Unknown Source)
    at org.hsqldb.DatabaseManager.getDatabase(Unknown Source)
    at org.hsqldb.DatabaseManager.newSession(Unknown Source)
    at org.hsqldb.jdbc.JDBCConnection.<init>(Unknown Source)
    at org.hsqldb.jdbc.JDBCDriver.getConnection(Unknown Source)
    at org.hsqldb.jdbc.JDBCDataSource.getConnection(Unknown Source)
    at org.hsqldb.jdbc.JDBCDataSource.getConnection(Unknown Source)
    at org.geotools.referencing.factory.epsg.DirectEpsgFactory.getConnection(DirectEpsgFactory.java:3196)
    at org.geotools.referencing.factory.epsg.ThreadedEpsgFactory.createBackingStore(ThreadedEpsgFactory.java:436)
    at org.geotools.referencing.factory.DeferredAuthorityFactory.getBackingStore(DeferredAuthorityFactory.java:133)
    at org.geotools.referencing.factory.BufferedAuthorityFactory.isAvailable(BufferedAuthorityFactory.java:235)
    at org.geotools.referencing.factory.DeferredAuthorityFactory.isAvailable(DeferredAuthorityFactory.java:119)
    at org.geotools.factory.FactoryRegistry.isAvailable(FactoryRegistry.java:667)
    at org.geotools.factory.FactoryRegistry.isAcceptable(FactoryRegistry.java:501)
    at org.geotools.factory.FactoryRegistry.getServiceImplementation(FactoryRegistry.java:437)
    at org.geotools.factory.FactoryRegistry.getServiceProvider(FactoryRegistry.java:365)
    at org.geotools.factory.FactoryCreator.getServiceProvider(FactoryCreator.java:145)
    at org.geotools.referencing.ReferencingFactoryFinder.getAuthorityFactory(ReferencingFactoryFinder.java:220)
    at org.geotools.referencing.ReferencingFactoryFinder.getCRSAuthorityFactory(ReferencingFactoryFinder.java:440)`

I'm interested in using hsqldb. hsqldb-2.2.8.jar's in the CLASSPATH. How should I organise DB connection in java code?

UPDATE

My main goal is to get a handle on coordinate conversion using GeoTools, but doesn't seem to be easy. Please see the buggy code below:

import org.geotools.referencing.CRS;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;
import org.geotools.geojson.geom.GeometryJSON;
import com.vividsolutions.jts.geom.Geometry;
import org.geotools.geometry.jts.*;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

//------------------------------

    CoordinateReferenceSystem sourceCRS = null;
    CoordinateReferenceSystem targetCRS = null;
    try {
        sourceCRS = CRS.decode("epsg:4326");
        System.out.println("SOURCE \n" + sourceCRS);

        targetCRS = CRS.decode("epsg:23032");
        System.out.print("\nTARGET \n" + targetCRS);

        MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS, true);

        GeometryJSON jsonReader = new GeometryJSON();
        Geometry sourceGeometry = jsonReader.read(new FileInputStream("simple-geometry.json"));
        System.out.println("\n" + sourceGeometry.getCoordinate().toString());

        //the problem's here
        Geometry targetGeometry = JTS.transform(sourceGeometry, transform);

    } catch (FactoryException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

The point is that my IDE cannot resolve JTS.transform(/.../).

I have gt-epsg-hsql-13.0, gt-geojson-13.0, gt-jts-wrapper-13.0, gt-opengis-13.0, gt-referencing-13.0, jts-1.13 in CLASSPATH.

My pom.xml is:

<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0         http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>geotools_test_3</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-referencing</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-epsg-hsql</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>com.vividsolutions</groupId>
            <artifactId>jts</artifactId>
            <version>${geotools.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>

Does any one know what seem to be the problem?

Adam
  • 842
  • 2
  • 14
  • 22
  • I assume you're missing the `hsqldb-x.x.x.jar` in the classpath? What IDE you are using? In case of NetBeans: right-click the Libraries node in the project tab and add the jar file. – chris Jun 03 '15 at 12:05
  • No, I've added hsqldb in the classpath. I'm using Intellij IDEA Community Edition – Adam Jun 03 '15 at 12:08
  • can you list the geotools jars in your dependancies? I suspect you are missing an epsg one – Ian Turton Jun 04 '15 at 07:57
  • 1
    You'll need to add gt-api for JTS and possibly gt-main too – Ian Turton Aug 03 '15 at 12:11
  • Could you please give me a tip how to retrieve Datum with EPSG code using GeoTools? For example, I'd like to have Datum object containing datum parameters for EPSG:6277. Is it possible some how? – Adam Aug 05 '15 at 09:12

1 Answers1

1

The easiest way to generate a Coordinate System is to use the following code:

CoordinateReferenceSystem crs = null;
try {

    crs = CRS.decode("epsg:26910");
    System.out.println("got "+crs);

} catch (FactoryException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Making sure you have the following in your pom file

    <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-referencing</artifactId>
        <version>${geotools.version}</version>
    </dependency>
    <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-epsg-hsql</artifactId>
        <version>${geotools.version}</version>
    </dependency>
Ian Turton
  • 10,018
  • 1
  • 28
  • 47
  • sorry for the delay in answering and thank you very much for you help. what's wkt variable at your code sample? – Adam Jun 26 '15 at 12:47
  • It *was* the string where I stored the epsg code - that I replaced with "epsg:26910" to make it clearer – Ian Turton Jun 26 '15 at 12:55
  • Thanks for the answer. Basically, I need to solve a different task, I need to implement coordinate transformation and conversion. Let's say I need to transform WGS-84 datum coordinates to SK-42 datum, and then project those coordinates (Gauss-Kruger projection). Could you suggest sort of the best practice how to implement these operations using GeoTools? – Adam Jul 31 '15 at 13:09
  • exactly as above - geotools will go directly from epsg:4326 (WGS84) to which ever Gauss-Kruger projection you need (http://epsg.io/?q=Gauss-Kruger for codes) – Ian Turton Jul 31 '15 at 14:59
  • The point is I need to create Gauss-Kruger projection with custom parameters. Thus predefined projections won't work. Is there any tutorial upon my matter? I cannot find any relevant info. – Adam Aug 01 '15 at 16:08
  • Thank you for your help, I really appreciate it. – Adam Aug 03 '15 at 11:47
  • Could you please scope out the UPDATE section? – Adam Aug 03 '15 at 12:08