All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.geotools.renderer.crs.GeographicHandlerFactory Maven / Gradle / Ivy

Go to download

The main module contains the GeoTools public interfaces that are used by other GeoTools modules (and GeoTools applications). Where possible we make use industry standard terms as provided by OGC and ISO standards. The formal GeoTools public api consists of gt-metadata, jts and the gt-main module. The main module contains the default implementations that are available provided to other GeoTools modules using our factory system. Factories are obtained from an appropriate FactoryFinder, giving applications a chance configure the factory used using the Factory Hints facilities. FilterFactory ff = CommonFactoryFinder.getFilterFactory(); Expression expr = ff.add( expression1, expression2 ); If you find yourself using implementation specific classes chances are you doing it wrong: Expression expr = new AddImpl( expression1, expressiom2 );

The newest version!
/*
 *    GeoTools - The Open Source Java GIS Toolkit
 *    http://geotools.org
 *
 *    (C) 2002-2008, Open Source Geospatial Foundation (OSGeo)
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 */
package org.geotools.renderer.crs;

import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.referencing.CRS;
import org.geotools.referencing.CRS.AxisOrder;
import org.geotools.referencing.operation.projection.Mercator;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.crs.GeographicCRS;
import org.opengis.referencing.operation.MathTransform;

/** Returns a {@link ProjectionHandler} for any {@link GeographicCRS} */
public class GeographicHandlerFactory implements ProjectionHandlerFactory {

    static final double MAX_LATITUDE = 89.99;
    static final double MIN_LATITUDE = -89.99;

    public ProjectionHandler getHandler(
            ReferencedEnvelope renderingEnvelope,
            CoordinateReferenceSystem sourceCrs,
            boolean wrap,
            int maxWraps)
            throws FactoryException {
        if (renderingEnvelope != null
                && renderingEnvelope.getCoordinateReferenceSystem() instanceof GeographicCRS) {
            CoordinateReferenceSystem crs = renderingEnvelope.getCoordinateReferenceSystem();
            GeographicCRS geogCrs = (GeographicCRS) CRS.getHorizontalCRS(crs);
            CoordinateReferenceSystem horizontalSourceCrs = CRS.getHorizontalCRS(sourceCrs);

            ReferencedEnvelope validArea = null;
            if (horizontalSourceCrs instanceof GeographicCRS
                    && !CRS.equalsIgnoreMetadata(horizontalSourceCrs, geogCrs)) {
                // datum shifts will create unpleasant effects if we have the poles in the mix,
                // cut them out
                if (!CRS.equalsIgnoreMetadata(horizontalSourceCrs, geogCrs)) {
                    if (CRS.getAxisOrder(sourceCrs) == AxisOrder.NORTH_EAST) {
                        validArea =
                                new ReferencedEnvelope(
                                        MIN_LATITUDE,
                                        MAX_LATITUDE,
                                        Float.MAX_VALUE,
                                        -Float.MAX_VALUE,
                                        horizontalSourceCrs);
                    } else {
                        validArea =
                                new ReferencedEnvelope(
                                        Integer.MAX_VALUE,
                                        -Integer.MAX_VALUE,
                                        MIN_LATITUDE,
                                        MAX_LATITUDE,
                                        horizontalSourceCrs);
                    }
                }
            }

            if (wrap && maxWraps > 0) {
                double centralMeridian =
                        geogCrs.getDatum().getPrimeMeridian().getGreenwichLongitude();
                WrappingProjectionHandler handler =
                        new WrappingProjectionHandler(
                                renderingEnvelope, validArea, sourceCrs, centralMeridian, maxWraps);
                handler.setDatelineWrappingCheckEnabled(
                        !isWrappingException(crs, horizontalSourceCrs));
                return handler;
            } else {
                return new ProjectionHandler(sourceCrs, validArea, renderingEnvelope);
            }
        }

        return null;
    }

    private boolean isWrappingException(
            CoordinateReferenceSystem sourceCrs, CoordinateReferenceSystem targetCRS)
            throws FactoryException {
        MathTransform mt = CRS.findMathTransform(sourceCrs, targetCRS, true);
        // this projection does not wrap coordinates, generates values larger than 180 instead
        if (mt instanceof Mercator) {
            return true;
        }

        return false;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy