Pergunta

Estou tentando colocar um monte de pontos (x, y) em um mapa, que têm uma origem sola. Eu gostaria de configurar uma projeção transversal personalizada do Mercator, centralizada na minha origem e usá-la para projetar meus pontos no LON-LAT.

Parece o tipo de coisa que o Geotoolkit (ou Geotools) deve fazer, mas estou tendo uma grande dificuldade em determinar a melhor maneira de usar a biblioteca de referência.

Consegui escrever algum código que funcione às vezes, mas outras vezes recebo uma exceção de classe, enquanto o Geotoolkit tenta carregar o MathTransFormFactory. Qualquer ajuda seria muito apreciada. Aqui está o meu código:

 public class LonLatConverter {

    private static final double WGS84_AXIS_MINOR = 6356752.314;
    private static final double WGS84_AXIS_MAJOR = 6378137.000;
    private GeoLocation origin;

    private static MathTransformFactory factory;

    public LonLatConverter(GeoLocation origin) {
        this.origin = origin;
    }

    public GeoLocation toLonLat(Vector2D location) {
        double lon = 0;
        double lat = 0;
        try {
            MathTransformFactory factory = getMathTransformFactory();

            MathTransform tr = getMathTransform(factory);

            DirectPosition sourcePt = new GeneralDirectPosition(
                    location.getX(), location.getY());
            DirectPosition targetPt = tr.transform(sourcePt, null);

            lon = targetPt.getOrdinate(0);
            lat = targetPt.getOrdinate(1);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return new GeoLocation(lon, lat);
    }

    private MathTransform getMathTransform(MathTransformFactory factory)
            throws NoninvertibleTransformException, FactoryException {
        ParameterValueGroup p = factory
                .getDefaultParameters("Transverse_Mercator");
        p.parameter("semi_major").setValue(WGS84_AXIS_MAJOR);
        p.parameter("semi_minor").setValue(WGS84_AXIS_MINOR);
        p.parameter("central_meridian").setValue(origin.getLongitude());
        p.parameter("latitude_of_origin").setValue(origin.getLatitude());
        MathTransform tr = factory.createParameterizedTransform(p).inverse();
        return tr;
    }

    private MathTransformFactory getMathTransformFactory() {

        if (factory == null) {
            FactoryRegistry registry = new FactoryRegistry(
                    MathTransformFactory.class);

            factory = registry.getServiceProvider(MathTransformFactory.class,
                    null, null, Hints.MATH_TRANSFORM_FACTORY);
        }

        return factory;
    }
}

Obrigado,

Dan

Foi útil?

Solução

Acontece que a exceção é causada por uma interação estranha entre o Geotoolkit e o GWT (Google Web Toolkit) "DevMode" (a conversão de coordenadas está acontecendo no back-end de um aplicativo da Web GWT/Java). Para o registro, esta é a exceção:

Caused by: java.lang.RuntimeException: java.lang.ClassCastException: class org.geotoolkit.referencing.operation.DefaultMathTransformFactory
     at com.example.LonLatConverter.toLonLat(LonLatConverter.java:47)
...

Caused by: java.lang.ClassCastException: class org.geotoolkit.referencing.operation.DefaultMathTransformFactory
     at java.lang.Class.asSubclass(Class.java:3018)
     at org.geotoolkit.factory.FactoryRegistry.register(FactoryRegistry.java:921)
    at org.geotoolkit.factory.FactoryRegistry.scanForPlugins(FactoryRegistry.java:793)
    at org.geotoolkit.factory.FactoryRegistry.scanForPluginsIfNeeded(FactoryRegistry.java:843)
    at org.geotoolkit.factory.FactoryRegistry.getServiceProviders(FactoryRegistry.java:236)
    at com.example.LonLatConverter.getMathTransformFactory(LonLatConverter.java:71)
    at com.example.LonLatConverter.toLonLat(LonLatConverter.java:35)
...

Meu trabalho atual é usar o plug-in GWT Eclipse. Isso não lança a exceção, nem a construção da guerra e a implantação do aplicativo da Web para Tomcat ou Jetty. Tudo muito estranho, mas estou feliz em ignorá -lo por enquanto.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top