Hello list,

I'm having trouble using a CRSAuthorityFactory inside the Jetty servlet
container.

I'm using Geotools 2.5-SNAPSHOT and the epsg-hsql-2.5-SNAPSHOT.jar library
jar, but also tried it with the gt-epsg-wtk-2.5-SNAPSHOT.jar library, but
produced the same error. The code runs fine running in a standalone
environment (J2SE), but fails inside Jetty.

It fails on calling:

CRSAuthorityFactory factory = CRS.getAuthorityFactory(true);

but I also tried

CRSAuthorityFactory factory =
ReferencingFactoryFinder.getCRSAuthorityFactory("EPSG:4326", hints);

and it produces the same result

The error follows below and the example code right after it.

Has anyone come across this and knows how to solve it? I've searched the
wiki, but I guess it wasn't enough.

Maven ended up adding gt-referencing gt-referencing-2.5-SNAPSHOT.jar and
referencing-2.5-SNAPSHOT.jar to the project class path, but I'm not sure if
this is right. If there is anything I could add that would be of any help,
let me know.

Thank you.
Alex.

Error:

WARNING: Can't load a service for category "CRSAuthorityFactory". Cause is
"ServiceConfigurationError: org.opengis.referencing.crs.CRSAuthorityFactory:
Provider org (...) lang.ClassCastException: class
org.geotools.referencing.operation.DefaultMathTransformFactory".
sun.misc.ServiceConfigurationError:
org.opengis.referencing.crs.CRSAuthorityFactory: Provider
org.geotools.referencing.factory.epsg.DefaultFactory could not be
instantiated: java.lang.ClassCastException: class
org.geotools.referencing.operation.DefaultMathTransformFactory
    at sun.misc.Service.fail(Unknown Source)
    at sun.misc.Service.access$200(Unknown Source)
    at sun.misc.Service$LazyIterator.next(Unknown Source)
    at
org.geotools.factory.FactoryRegistry.register(FactoryRegistry.java:829)
    at
org.geotools.factory.FactoryRegistry.scanForPlugins(FactoryRegistry.java:773)
    at
org.geotools.factory.FactoryRegistry.scanForPluginsIfNeeded(FactoryRegistry.java:808)
    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:143)
    at
org.geotools.referencing.ReferencingFactoryFinder.getAuthorityFactory(ReferencingFactoryFinder.java:216)
    at
org.geotools.referencing.ReferencingFactoryFinder.getCRSAuthorityFactory(ReferencingFactoryFinder.java:436)


Example class:

public class TransformExample
{
    static Integer WGS84_SRID = 4326;

    PrecisionModel precisionModel = new PrecisionModel();

    private Map<Integer, GeometryFactory> idGeometryFactoryMap = new
HashMap<Integer, GeometryFactory>();


    CoordinateReferenceSystem wgs84CRS;
    CRSAuthorityFactory factory;
    Hints hints ;

    public TransformExample()
    {
        hints = new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER,
Boolean.TRUE);

        factory =
ReferencingFactoryFinder.getCRSAuthorityFactory("EPSG:4326", hints);
//        factory = CRS.getAuthorityFactory(true);

//        CoordinateReferenceSystem crs =
factory.createCoordinateReferenceSystem("EPSG:4326");

        try {
            wgs84CRS = factory.createCoordinateReferenceSystem("EPSG:4326");
        } catch (NoSuchAuthorityCodeException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FactoryException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public Point pointFrom(double lat, double lon, GeometryFactory
geometryFactory)
    {
        final Coordinate coord = new Coordinate(lon, lat);
        final Coordinate coordSeqAr[] = { coord };
        final CoordinateArraySequence coordSeq = new
CoordinateArraySequence(coordSeqAr);

        return new Point(coordSeq, geometryFactory);
    }


    public Point wgs84PointFrom(double lat, double lon)
    {
        GeometryFactory geomFact = getOrCreateGeometryFactory(WGS84_SRID);
        return pointFrom(lat, lon, geomFact);
    }


    private GeometryFactory getOrCreateGeometryFactory(Integer srid)
    {
        GeometryFactory geometryFactory = idGeometryFactoryMap.get(srid);
        if (geometryFactory == null)
        {
            geometryFactory = new GeometryFactory(precisionModel,
srid.intValue());
            idGeometryFactoryMap.put(srid, geometryFactory);
        }

        return geometryFactory;
    }

    /**
     * Returns the EPSG id for the  UTM zone of the given coordinate
     * @param coord
     * @return
     */
    public int utmzone(Coordinate coord)
    {
        int pref;
        if (coord.y > 0)
        {
            pref = 32600;
        }
        else
        {
            pref = 32700;
        }
        final int zone = (int) Math.floor((coord.x+180)/6)+1;
        return zone + pref;
    }

    private Map<Integer,CoordinateReferenceSystem>
coordinateReferenceSystemMap = new HashMap<Integer,
CoordinateReferenceSystem>();


    private CoordinateReferenceSystem
getCoordinateReferenceSystemForUTMZone(
            Integer utmzone) throws NoSuchAuthorityCodeException,
FactoryException {
        synchronized (coordinateReferenceSystemMap) {
            CoordinateReferenceSystem targetCRS =
coordinateReferenceSystemMap.get(utmzone);

            if (targetCRS == null)
            {
                targetCRS = CRS.decode("EPSG:" + utmzone);
                coordinateReferenceSystemMap.put(utmzone, targetCRS);
            }
            return targetCRS;
        }
    }

    private Map<Integer, MathTransform> transformWgs84ToUtmMap = new
HashMap<Integer, MathTransform>();
    private Map<Integer, MathTransform> transformUtmToWgs84Map = new
HashMap<Integer, MathTransform>();

    @SuppressWarnings("unchecked")
    public <T extends Geometry> T convertToUTM(T geom)
    {
        int utmzone = utmzone(geom.getCoordinate());
        final Integer utmZoneInteger = new Integer(utmzone);

        try
        {
            synchronized (transformWgs84ToUtmMap)
            {
                MathTransform transform =
transformWgs84ToUtmMap.get(utmZoneInteger);
                if (transform == null)
                {
                        CoordinateReferenceSystem targetCRS =
getCoordinateReferenceSystemForUTMZone(utmZoneInteger);
                        transform = CRS.findMathTransform(wgs84CRS,
targetCRS);
                        transformWgs84ToUtmMap.put(utmZoneInteger,
transform);
                }
                final T transformedGeom = (T) JTS.transform(geom,
transform);
                transformedGeom.setSRID(utmzone);
                return transformedGeom;
            }
        }
        catch (NoSuchAuthorityCodeException e)
        {
            throw new RuntimeException(e);
        }
        catch (FactoryException e)
        {
            throw new RuntimeException(e);
        }
        catch (MismatchedDimensionException e)
        {
            throw new RuntimeException(e);
        }
        catch (TransformException e)
        {
            throw new RuntimeException(e);
        }
    }

    @SuppressWarnings("unchecked")
    public <T extends Geometry> T  convertUtmToWgs84(T geom) {
        int utmzone = geom.getSRID();
        final Integer utmZoneInteger = new Integer(utmzone);

        try
        {
            synchronized (transformUtmToWgs84Map)
            {
                MathTransform transform =
transformUtmToWgs84Map.get(utmZoneInteger);
                if (transform == null)
                {
                        CoordinateReferenceSystem targetCRS =
getCoordinateReferenceSystemForUTMZone(utmZoneInteger);
                        transform = CRS.findMathTransform(targetCRS,
wgs84CRS);
                        transformUtmToWgs84Map.put(utmZoneInteger,
transform);
                }
                final T transformedGeom = (T) JTS.transform(geom,
transform);
                transformedGeom.setSRID(utmzone);
                return transformedGeom;
            }
        }
        catch (NoSuchAuthorityCodeException e)
        {
            throw new RuntimeException(e);
        }
        catch (FactoryException e)
        {
            throw new RuntimeException(e);
        }
        catch (MismatchedDimensionException e)
        {
            throw new RuntimeException(e);
        }
        catch (TransformException e)
        {
            throw new RuntimeException(e);
        }
    }
}
------------------------------------------------------------------------------
Throughout its 18-year history, RSA Conference consistently attracts the
world's best and brightest in the field, creating opportunities for Conference
attendees to learn about information security's most important issues through
interactions with peers, luminaries and emerging and established companies.
http://p.sf.net/sfu/rsaconf-dev2dev
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to