001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.projection.proj;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import org.openstreetmap.josm.data.Bounds;
007import org.openstreetmap.josm.data.projection.ProjectionConfigurationException;
008import org.openstreetmap.josm.tools.Utils;
009
010/**
011 * Cassini-Soldner Projection (EPSG code 9806).
012 * The Cassini-Soldner Projection is the ellipsoidal version of the Cassini
013 * projection for the sphere. It is not conformal but as it is relatively simple
014 * to construct it was extensively used in the last century and is still useful
015 * for mapping areas with limited longitudinal extent. It has now largely
016 * been replaced by the conformal Transverse Mercator which it resembles. Like this,
017 * it has a straight central meridian along which the scale is true, all other
018 * meridians and parallels are curved, and the scale distortion increases
019 * rapidly with increasing distance from the central meridian.
020 * <p>
021 *
022 * This class has been derived from the implementation of the Geotools project;
023 * git 8cbf52d, org.geotools.referencing.operation.projection.CassiniSoldner
024 * at the time of migration.
025 */
026public class CassiniSoldner extends AbstractProj {
027
028    /**
029     * Meridian distance at the {@code latitudeOfOrigin}.
030     * Used for calculations for the ellipsoid.
031     */
032    private double ml0;
033
034    /**
035     * Contants used for the forward and inverse transform for the eliptical
036     * case of the Cassini-Soldner.
037     */
038    private static final double C1 = 0.16666666666666666666;
039    private static final double C2 = 0.008333333333333333333;
040    private static final double C3 = 0.041666666666666666666;
041    private static final double C4 = 0.33333333333333333333;
042    private static final double C5 = 0.066666666666666666666;
043
044    @Override
045    public String getName() {
046        return tr("Cassini-Soldner");
047    }
048
049    @Override
050    public String getProj4Id() {
051        return "cass";
052    }
053
054    @Override
055    public void initialize(ProjParameters params) throws ProjectionConfigurationException {
056        super.initialize(params);
057        if (params.lat0 == null)
058            throw new ProjectionConfigurationException(tr("Parameter ''{0}'' required.", "lat_0"));
059        double latitudeOfOrigin = Utils.toRadians(params.lat0);
060        ml0 = mlfn(latitudeOfOrigin, Math.sin(latitudeOfOrigin), Math.cos(latitudeOfOrigin));
061    }
062
063    @Override
064    public double[] project(double phi, double lam) {
065        double sinphi = Math.sin(phi);
066        double cosphi = Math.cos(phi);
067
068        double n = 1.0 / (Math.sqrt(1.0 - e2 * sinphi * sinphi));
069        double tn = Math.tan(phi);
070        double t = tn * tn;
071        double a1 = lam * cosphi;
072        double c = cosphi * cosphi * e2 / (1 - e2);
073        double a2 = a1 * a1;
074
075        double x = n * a1 * (1.0 - a2 * t * (C1 - (8.0 - t + 8.0 * c) * a2 * C2));
076        double y = mlfn(phi, sinphi, cosphi) - ml0 + n * tn * a2 * (0.5 + (5.0 - t + 6.0 * c) * a2 * C3);
077        return new double[] {x, y};
078    }
079
080    @Override
081    public double[] invproject(double x, double y) {
082        double ph1 = invMlfn(ml0 + y);
083        double tn = Math.tan(ph1);
084        double t = tn * tn;
085        double n = Math.sin(ph1);
086        double r = 1.0 / (1.0 - e2 * n * n);
087        n = Math.sqrt(r);
088        r *= (1.0 - e2) * n;
089        double dd = x / n;
090        double d2 = dd * dd;
091        double phi = ph1 - (n * tn / r) * d2 * (0.5 - (1.0 + 3.0 * t) * d2 * C3);
092        double lam = dd * (1.0 + t * d2 * (-C4 + (1.0 + 3.0 * t) * d2 * C5)) / Math.cos(ph1);
093        return new double[] {phi, lam};
094    }
095
096    @Override
097    public Bounds getAlgorithmBounds() {
098        return new Bounds(-89, -1.0, 89, 1.0, false);
099    }
100}