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 */
013public class NTV2Datum extends AbstractDatum {
014
015    private final NTV2GridShiftFileWrapper nadgrids;
016
017    /**
018     * Constructs a new {@code NTV2Datum}.
019     * @param name datum name
020     * @param proj4Id PROJ.4 id
021     * @param ellps ellipsoid
022     * @param nadgrids NTV2 grid shift file wrapper
023     */
024    public NTV2Datum(String name, String proj4Id, Ellipsoid ellps, NTV2GridShiftFileWrapper nadgrids) {
025        super(name, proj4Id, ellps);
026        this.nadgrids = nadgrids;
027    }
028
029    @Override
030    public LatLon toWGS84(LatLon ll) {
031        NTV2GridShift gs = new NTV2GridShift(ll);
032        try {
033            nadgrids.getShiftFile().gridShiftForward(gs);
034            return new LatLon(ll.lat() + gs.getLatShiftDegrees(), ll.lon() + gs.getLonShiftPositiveEastDegrees());
035        } catch (IOException e) {
036            throw new JosmRuntimeException(e);
037        }
038    }
039
040    @Override
041    public LatLon fromWGS84(LatLon ll) {
042        NTV2GridShift gs = new NTV2GridShift(ll);
043        try {
044            nadgrids.getShiftFile().gridShiftReverse(gs);
045            return new LatLon(ll.lat() + gs.getLatShiftDegrees(), ll.lon() + gs.getLonShiftPositiveEastDegrees());
046        } catch (IOException e) {
047            throw new JosmRuntimeException(e);
048        }
049    }
050}