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