001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.projection.datum;
003
004import java.io.IOException;
005
006import org.openstreetmap.josm.data.coor.LatLon;
007import org.openstreetmap.josm.data.projection.Ellipsoid;
008import org.openstreetmap.josm.tools.JosmRuntimeException;
009
010/**
011 * Datum based of NTV2 grid shift file.
012 * @since 5073
013 */
014public class NTV2Datum extends AbstractDatum {
015
016    private final NTV2GridShiftFileWrapper nadgrids;
017
018    /**
019     * Constructs a new {@code NTV2Datum}.
020     * @param name datum name
021     * @param proj4Id PROJ.4 id
022     * @param ellps ellipsoid
023     * @param nadgrids NTV2 grid shift file wrapper
024     */
025    public NTV2Datum(String name, String proj4Id, Ellipsoid ellps, NTV2GridShiftFileWrapper nadgrids) {
026        super(name, proj4Id, ellps);
027        this.nadgrids = nadgrids;
028    }
029
030    @Override
031    public LatLon toWGS84(LatLon ll) {
032        NTV2GridShift gs = new NTV2GridShift(ll);
033        try {
034            nadgrids.getShiftFile().gridShiftForward(gs);
035            return new LatLon(ll.lat() + gs.getLatShiftDegrees(), ll.lon() + gs.getLonShiftPositiveEastDegrees());
036        } catch (IOException e) {
037            throw new JosmRuntimeException(e);
038        }
039    }
040
041    @Override
042    public LatLon fromWGS84(LatLon ll) {
043        NTV2GridShift gs = new NTV2GridShift(ll);
044        try {
045            nadgrids.getShiftFile().gridShiftReverse(gs);
046            return new LatLon(ll.lat() + gs.getLatShiftDegrees(), ll.lon() + gs.getLonShiftPositiveEastDegrees());
047        } catch (IOException e) {
048            throw new JosmRuntimeException(e);
049        }
050    }
051}