在Java / GWT服务器端将坐标从EPSG:4326转换为EPSG:31467

时间:2018-03-10 10:42:43

标签: java eclipse coordinate-transformation geotools

我一直在我的GWT服务器端使用https://ariasprado.name/2012/08/13/quick-and-dirty-coordinate-transforming-using-geotools.html#comment-792中的以下代码,并按照说明完全附加了所有必需的jar。附带错误日志。非常感谢任何帮助。谢谢提前

    import org.geotools.geometry.DirectPosition2D;

    import org.geotools.referencing.CRS;

    import org.opengis.referencing.crs.CoordinateReferenceSystem;

    import org.opengis.referencing.operation.MathTransform;

    ...

    CoordinateReferenceSystem sourceCrs = CRS.decode("EPSG:25829");
    CoordinateReferenceSystem targetCrs = CRS.decode("EPSG:4326");

    double x = (double) 636497.59434;
    double y = (double) 4778964.017375;

    boolean lenient = true;
    MathTransform mathTransform 
        = CRS.findMathTransform(sourceCrs, targetCrs, lenient);

    DirectPosition2D srcDirectPosition2D 
        = new DirectPosition2D(sourceCrs, x, y);
    DirectPosition2D destDirectPosition2D 
        = new DirectPosition2D();
    mathTransform.transform(srcDirectPostion2D, destDirectPosition2D);

    double transX = destDirectPosition2D.x;
    double transY = destDirectPosition2D.y;


Mar 10, 2018 10:59:20 AM org.geotools.factory.FactoryRegistry scanForPlugins
WARNING: Can't load a service for category "MathTransformFactory". Cause is "ServiceConfigurationError: org.opengis.referencing.operation.MathTransformFactory: Provider org.geotools.referencing.operation.DefaultMathTransformFactory not a subtype".
java.util.ServiceConfigurationError: org.opengis.referencing.operation.MathTransformFactory: Provider org.geotools.referencing.operation.DefaultMathTransformFactory not a subtype
    at java.util.ServiceLoader.fail(ServiceLoader.java:239)
    at java.util.ServiceLoader.access$300(ServiceLoader.java:185)
    at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:376)
    at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:404)
    at java.util.ServiceLoader$1.next(ServiceLoader.java:480)
    at org.geotools.factory.FactoryRegistry.register(FactoryRegistry.java:823)
    at org.geotools.factory.FactoryRegistry.scanForPlugins(FactoryRegistry.java:769)
    at org.geotools.factory.FactoryRegistry.scanForPluginsIfNeeded(FactoryRegistry.java:802)
    at org.geotools.factory.FactoryRegistry.getUnfilteredProviders(FactoryRegistry.java:229)
    at org.geotools.factory.FactoryRegistry.getServiceImplementation(FactoryRegistry.java:429)
    at org.geotools.factory.FactoryRegistry.getServiceProvider(FactoryRegistry.java:364)
    at org.geotools.factory.FactoryCreator.getServiceProvider(FactoryCreator.java:144)
    at org.geotools.referencing.ReferencingFactoryFinder.getFactory(ReferencingFactoryFinder.java:194)
    at org.geotools.referencing.ReferencingFactoryFinder.getMathTransformFactory(ReferencingFactoryFinder.java:504)
    at org.geotools.referencing.factory.ReferencingFactoryContainer.getMathTransformFactory(ReferencingFactoryContainer.java:274)
    at org.geotools.referencing.factory.ReferencingFactoryContainer.initialize(ReferencingFactoryContainer.java:181)
    at org.geotools.referencing.factory.ReferencingFactoryContainer.getImplementationHints(ReferencingFactoryContainer.java:206)
    at org.geotools.factory.FactoryRegistry.usesAcceptableHints(FactoryRegistry.java:551)
    at org.geotools.factory.FactoryRegistry.isAcceptable(FactoryRegistry.java:505)
    at org.geotools.factory.FactoryRegistry.getServiceImplementation(FactoryRegistry.java:436)
    at org.geotools.factory.FactoryRegistry.getServiceProvider(FactoryRegistry.java:364)
    at org.geotools.factory.FactoryCreator.getServiceProvider(FactoryCreator.java:144)
    at org.geotools.referencing.factory.ReferencingFactoryContainer.instance(ReferencingFactoryContainer.java:171)
    at org.geotools.referencing.factory.epsg.ThreadedEpsgFactory.<init>(ThreadedEpsgFactory.java:194)
    at org.geotools.referencing.factory.epsg.ThreadedHsqlEpsgFactory.<init>(ThreadedHsqlEpsgFactory.java:132)
    at org.geotools.referencing.factory.epsg.ThreadedHsqlEpsgFactory.<init>(ThreadedHsqlEpsgFactory.java:118)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at java.lang.Class.newInstance(Class.java:442)
    at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:380)
    at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:404)
    at java.util.ServiceLoader$1.next(ServiceLoader.java:480)
    at org.geotools.factory.FactoryRegistry.register(FactoryRegistry.java:823)
    at org.geotools.factory.FactoryRegistry.scanForPlugins(FactoryRegistry.java:769)
    at org.geotools.factory.FactoryRegistry.scanForPluginsIfNeeded(FactoryRegistry.java:802)
    at org.geotools.factory.FactoryRegistry.getServiceProviders(FactoryRegistry.java:195)
    at org.geotools.referencing.ReferencingFactoryFinder.getFactories(ReferencingFactoryFinder.java:177)
    at org.geotools.referencing.ReferencingFactoryFinder.getCRSAuthorityFactories(ReferencingFactoryFinder.java:452)
    at org.geotools.referencing.DefaultAuthorityFactory.getBackingFactory(DefaultAuthorityFactory.java:88)
    at org.geotools.referencing.DefaultAuthorityFactory.<init>(DefaultAuthorityFactory.java:69)
    at org.geotools.referencing.CRS.getAuthorityFactory(CRS.java:250)
    at org.geotools.referencing.CRS.decode(CRS.java:488)
    at org.geotools.referencing.CRS.decode(CRS.java:416)
    at com.interfacema.gis.server.logic.BorderServlet.transformCRS(BorderServlet.java:342)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:587)
    at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:333)
    at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:303)
    at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:373)
    at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:812)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:587)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:577)
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:223)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1127)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515)
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
    at org.eclipse.jetty.server.handler.RequestLogHandler.handle(RequestLogHandler.java:95)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
    at org.eclipse.jetty.server.Server.handle(Server.java:499)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:311)
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257)
    at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:544)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555)
    at java.lang.Thread.run(Thread.java:748)

Mar 10, 2018 10:59:20 AM org.geotools.factory.FactoryRegistry scanForPlugins
WARNING: Can't load a service for category "DatumFactory". Cause is "ServiceConfigurationError: org.opengis.referencing.datum.DatumFactory: Provider org.geotools.referencing.factory.ReferencingObjectFactory not a subtype".
java.util.ServiceConfigurationError: org.opengis.referencing.datum.DatumFactory: Provider org.geotools.referencing.factory.ReferencingObjectFactory not a subtype
    at java.util.ServiceLoader.fail(ServiceLoader.java:239)
    at java.util.ServiceLoader.access$300(ServiceLoader.java:185)
    at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:376)
    at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:404)
    at java.util.ServiceLoader$1.next(ServiceLoader.java:480)
    at org.geotools.factory.FactoryRegistry.register(FactoryRegistry.java:823)
    at org.geotools.factory.FactoryRegistry.scanForPlugins(FactoryRegistry.java:769)
    at org.geotools.factory.FactoryRegistry.scanForPluginsIfNeeded(FactoryRegistry.java:802)
    at org.geotools.factory.FactoryRegistry.getUnfilteredProviders(FactoryRegistry.java:229)
    at org.geotools.factory.FactoryRegistry.getServiceImplementation(FactoryRegistry.java:429)
    at org.geotools.factory.FactoryRegistry.getServiceProvider(FactoryRegistry.java:364)
    at org.geotools.factory.FactoryCreator.getServiceProvider(FactoryCreator.java:144)
    at org.geotools.referencing.ReferencingFactoryFinder.getFactory(ReferencingFactoryFinder.java:194)
    at org.geotools.referencing.ReferencingFactoryFinder.getDatumFactory(ReferencingFactoryFinder.java:233)
    at org.geotools.referencing.factory.ReferencingFactoryContainer.getDatumFactory(ReferencingFactoryContainer.java:232)
    at org.geotools.referencing.factory.ReferencingFactoryContainer.initialize(ReferencingFactoryContainer.java:182)
    at org.geotools.referencing.factory.ReferencingFactoryContainer.getImplementationHints(ReferencingFactoryContainer.java:206)
    at org.geotools.factory.FactoryRegistry.usesAcceptableHints(FactoryRegistry.java:551)
    at org.geotools.factory.FactoryRegistry.isAcceptable(FactoryRegistry.java:505)
    at org.geotools.factory.FactoryRegistry.getServiceImplementation(FactoryRegistry.java:436)
    at org.geotools.factory.FactoryRegistry.getServiceProvider(FactoryRegistry.java:364)
    at org.geotools.factory.FactoryCreator.getServiceProvider(FactoryCreator.java:144)
    at org.geotools.referencing.factory.ReferencingFactoryContainer.instance(ReferencingFactoryContainer.java:171)
    at org.geotools.referencing.factory.epsg.ThreadedEpsgFactory.<init>(ThreadedEpsgFactory.java:194)
    at org.geotools.referencing.factory.epsg.ThreadedHsqlEpsgFactory.<init>(ThreadedHsqlEpsgFactory.java:132)
    at org.geotools.referencing.factory.epsg.ThreadedHsqlEpsgFactory.<init>(ThreadedHsqlEpsgFactory.java:118)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at java.lang.Class.newInstance(Class.java:442)
    at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:380)
    at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:404)
    at java.util.ServiceLoader$1.next(ServiceLoader.java:480)
    at org.geotools.factory.FactoryRegistry.register(FactoryRegistry.java:823)
    at org.geotools.factory.FactoryRegistry.scanForPlugins(FactoryRegistry.java:769)
    at org.geotools.factory.FactoryRegistry.scanForPluginsIfNeeded(FactoryRegistry.java:802)
    at org.geotools.factory.FactoryRegistry.getServiceProviders(FactoryRegistry.java:195)
    at org.geotools.referencing.ReferencingFactoryFinder.getFactories(ReferencingFactoryFinder.java:177)
    at org.geotools.referencing.ReferencingFactoryFinder.getCRSAuthorityFactories(ReferencingFactoryFinder.java:452)
    at org.geotools.referencing.DefaultAuthorityFactory.getBackingFactory(DefaultAuthorityFactory.java:88)
    at org.geotools.referencing.DefaultAuthorityFactory.<init>(DefaultAuthorityFactory.java:69)
    at org.geotools.referencing.CRS.getAuthorityFactory(CRS.java:250)
    at org.geotools.referencing.CRS.decode(CRS.java:488)
    at org.geotools.referencing.CRS.decode(CRS.java:416)
    at com.interfacema.gis.server.logic.BorderServlet.transformCRS(BorderServlet.java:342)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:587)
    at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:333)
    at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:303)
    at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:373)
    at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:812)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:587)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:577)
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:223)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1127)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515)
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
    at org.eclipse.jetty.server.handler.RequestLogHandler.handle(RequestLogHandler.java:95)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
    at org.eclipse.jetty.server.Server.handle(Server.java:499)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:311)
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257)
    at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:544)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555)
    at java.lang.Thread.run(Thread.java:748)

Mar 10, 2018 10:59:20 AM org.geotools.factory.FactoryRegistry scanForPlugins
WARNING: Can't load a service for category "DatumFactory". Cause is "ServiceConfigurationError: org.opengis.referencing.datum.DatumFactory: Provider org.geotools.referencing.factory.DatumAliases not a subtype"...


WARNING: Can't load a service for category "CSFactory". Cause is "ServiceConfigurationError: org.opengis.referencing.cs.CSFactory: Provider org.geotools.referencing.factory.ReferencingObjectFactory not a subtype"...


WARNING: Can't load a service for category "CRSAuthorityFactory". Cause is "ServiceConfigurationError: org.opengis.referencing.crs.CRSAuthorityFactory: Provider org.geotools.referencing.factory.epsg.DefaultFactory not a subtype"...


WARNING: Can't load a service for category "CRSAuthorityFactory". Cause is "ServiceConfigurationError: org.opengis.referencing.crs.CRSAuthorityFactory: Provider org.geotools.referencing.factory.epsg.FactoryUsingWKT not a subtype"...


WARNING: Can't load a service for category "CRSAuthorityFactory". Cause is "ServiceConfigurationError: org.opengis.referencing.crs.CRSAuthorityFactory: Provider org.geotools.referencing.factory.epsg.LongitudeFirstFactory not a subtype"...

Mar 10, 2018 10:59:20 AM org.geotools.factory.FactoryRegistry scanForPlugins
WARNING: Can't load a service for category "CRSAuthorityFactory". Cause is "ServiceConfigurationError: org.opengis.referencing.crs.CRSAuthorityFactory: Provider org.geotools.referencing.factory.wms.AutoCRSFactory not a subtype"...

Mar 10, 2018 10:59:20 AM org.geotools.factory.FactoryRegistry scanForPlugins
WARNING: Can't load a service for category "CRSAuthorityFactory". Cause is "ServiceConfigurationError: org.opengis.referencing.crs.CRSAuthorityFactory: Provider org.geotools.referencing.factory.URN_AuthorityFactory not a subtype"...

Mar 10, 2018 10:59:20 AM org.geotools.factory.FactoryRegistry scanForPlugins
WARNING: Can't load a service for category "CRSAuthorityFactory". Cause is "ServiceConfigurationError: org.opengis.referencing.crs.CRSAuthorityFactory: Provider org.geotools.referencing.factory.HTTP_AuthorityFactory not a subtype"...

Mar 10, 2018 10:59:20 AM org.geotools.factory.FactoryRegistry scanForPlugins
WARNING: Can't load a service for category "CRSAuthorityFactory". Cause is "ServiceConfigurationError: org.opengis.referencing.crs.CRSAuthorityFactory: Provider org.geotools.referencing.factory.epsg.LongitudeFirstEpsgDecorator not a subtype"...

Mar 10, 2018 10:59:20 AM org.geotools.factory.FactoryRegistry scanForPlugins
WARNING: Can't load a service for category "CRSAuthorityFactory". Cause is "ServiceConfigurationError: org.opengis.referencing.crs.CRSAuthorityFactory: Provider org.geotools.referencing.factory.epsg.ThreadedHsqlEpsgFactory not a subtype"...

1 个答案:

答案 0 :(得分:1)

判断代码,它实际上看起来很好。 (我将在下面添加一个CRSCoordinateTransformer课程,其中肯定&#34;适用于我&#34;。)

所以我想一个类路径问题。如果您拥有所有必需的库?我的猜测是你有API库但不是实际的实现。 GeoTools提供坐标变换器的不同实现。例如gt2-epsg-hsql从嵌入式HSQLDB数据库加载转换。你是否包括其中一个JAR,或者你只有gt2-referencing

以下是来自已知转换有效的项目的mvn dependency:tree的摘录:

[INFO] +- org.geotools:gt2-epsg-hsql:jar:2.4.5:compile
[INFO] |  +- org.geotools:gt2-referencing:jar:2.4.5:compile
[INFO] |  |  +- java3d:vecmath:jar:1.3.1:compile
[INFO] |  |  \- org.geotools:gt2-metadata:jar:2.4.5:compile
[INFO] |  |     +- org.opengis:geoapi-nogenerics:jar:2.1.0:compile
[INFO] |  |     \- javax.units:jsr108:jar:0.01:compile
[INFO] |  \- hsqldb:hsqldb:jar:1.8.0.7:compile

该版本可能已经过时了,但这应该可以了解需要哪些库。如果您使用Maven,请运行mvn dependency:tree并检查(和/或发布)输出。

现在承诺的CRSCoordinateTransformer课程:

package org.hisrc.stopdirect.service.impl;

import org.geotools.geometry.DirectPosition2D;
import org.geotools.referencing.CRS;
import org.hisrc.stopdirect.service.CoordinateTransformer;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;
import org.opengis.referencing.operation.TransformException;

public class CRSCoordinateTransformer implements CoordinateTransformer {
    private final MathTransform forwardMathTransform;
    private final MathTransform reverseMathTransform;
    private final CoordinateReferenceSystem sourceCoordinateReferenceSystem;
    private final CoordinateReferenceSystem targetCoordinateReferenceSystem;

    public CRSCoordinateTransformer() {
        try {
            sourceCoordinateReferenceSystem = CRS.decode("EPSG:31468");
            targetCoordinateReferenceSystem = CRS.decode("EPSG:4326");
            this.forwardMathTransform = CRS.findMathTransform(sourceCoordinateReferenceSystem,
                    targetCoordinateReferenceSystem, true);
            this.reverseMathTransform = CRS.findMathTransform(targetCoordinateReferenceSystem,
                    sourceCoordinateReferenceSystem, true);
        } catch (FactoryException fex) {
            throw new ExceptionInInitializerError(fex);
        }

    }

    @Override
    public double[] lonLatToXY(double lon, double lat) throws TransformException {
        DirectPosition2D srcDirectPosition2D = new DirectPosition2D(sourceCoordinateReferenceSystem, lat, lon);
        DirectPosition2D destDirectPosition2D = new DirectPosition2D();
        try {
            reverseMathTransform.transform(srcDirectPosition2D, destDirectPosition2D);
            return new double[] { destDirectPosition2D.y, destDirectPosition2D.x };
        } catch (Error error) {
            throw error;
        }
    }

    @Override
    public double[] xyToLonLat(double x, double y) throws TransformException {

        DirectPosition2D srcDirectPosition2D = new DirectPosition2D(sourceCoordinateReferenceSystem, y, x);
        DirectPosition2D destDirectPosition2D = new DirectPosition2D();
        forwardMathTransform.transform(srcDirectPosition2D, destDirectPosition2D);

        return new double[] { destDirectPosition2D.y, destDirectPosition2D.x };
    }
}