【问题标题】:Coordinate projection with GeoTools使用 GeoTools 进行坐标投影
【发布时间】:2015-08-03 20:07:06
【问题描述】:

我正在处理一个简单的任务:使用

从 EPSG 代码创建 CoordinateReferenceSystem
String code = "26910";
CoordinateReferenceSystem crs = ReferencingFactoryFinder.getCRSAuthorityFactory
("EPSG",null).createCoordinateReferenceSystem(code);

这个 sn-p 产生:

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

我对使用 hsqldb 很感兴趣。 hsqldb-2.2.8.jar 在 CLASSPATH 中。 java代码中如何组织数据库连接?

更新

我的主要目标是使用 GeoTools 处理坐标转换,但似乎并不容易。请参阅下面的错误代码:

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

重点是我的 IDE 无法解析 JTS.transform(/.../)。

我在 CLASSPATH 中有 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。

我的 pom.xml 是:

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

有人知道问题出在哪里吗?

【问题讨论】:

  • 我假设您在类路径中缺少hsqldb-x.x.x.jar?你用的是什么IDE?对于 NetBeans:右键单击项目选项卡中的库节点并添加 jar 文件。
  • 不,我在类路径中添加了 hsqldb。我正在使用 Intellij IDEA 社区版
  • 你能列出你的依赖项中的 geotools jars 吗?我怀疑你错过了一个 epsg 的
  • 您需要为 JTS 添加 gt-api,可能还需要添加 gt-main
  • 您能否给我一个提示,如何使用 GeoTools 使用 EPSG 代码检索基准?例如,我想让 Datum 对象包含 EPSG:6277 的基准参数。有可能吗?

标签: java geotools


【解决方案1】:

生成坐标系最简单的方法是使用以下代码:

CoordinateReferenceSystem crs = null;
try {

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

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

确保您的 pom 文件中有以下内容

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

【讨论】:

  • 很抱歉延迟回复,非常感谢您的帮助。您的代码示例中的 wkt 变量是什么?
  • 我存储 epsg 代码的字符串 - 我用“epsg:26910”替换以使其更清晰
  • 感谢您的回答。基本上,我需要解决一个不同的任务,我需要实现坐标变换和转换。假设我需要将 WGS-84 基准坐标转换为 SK-42 基准,然后投影这些坐标(高斯-克鲁格投影)。您能否建议一些如何使用 GeoTools 实现这些操作的最佳实践?
  • 和上面一样——geotools 将直接从 epsg:4326 (WGS84) 转到您需要的任何 Gauss-Kruger 投影(代码为epsg.io/?q=Gauss-Kruger
  • 重点是我需要使用自定义参数创建高斯-克鲁格投影。因此,预定义的投影将不起作用。有没有关于我的问题的教程?我找不到任何相关信息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-14
  • 2021-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多