001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.projection.datum;
003
004import java.io.InputStream;
005
006import org.openstreetmap.josm.io.CachedFile;
007
008/**
009 * Wrapper for {@link NTV2GridShiftFile}.
010 *
011 * Loads the shift file from disk, when it is first accessed.
012 * @since 5226
013 */
014public class NTV2GridShiftFileWrapper {
015
016    // CHECKSTYLE.OFF: LineLength
017
018    /**
019     * Used in Germany to convert coordinates between the DHDN (<i>Deutsches Hauptdreiecksnetz</i>)
020     * and ETRS89 (<i>European Terrestrial Reference System 1989</i>) datums.
021     * @see <a href="http://crs.bkg.bund.de/crseu/crs/descrtrans/eu-descrtrans.php?crs_id=REVfREhETiAvIEdLXzM=&op_id=REVfREhETiAoQmVUQSwgMjAwNykgdG8gRVRSUzg5">
022     * Description of Transformation - DE_DHDN (BeTA, 2007) to ETRS89</a>
023     */
024    public static final NTV2GridShiftFileWrapper BETA2007 = new NTV2GridShiftFileWrapper("resource://data/projection/BETA2007.gsb");
025
026    /**
027     * Used in France to convert coordinates between the NTF (<i>Nouvelle triangulation de la France</i>)
028     * and RGF93 (<i>Réseau géodésique français 1993</i>) datums.
029     * @see <a href="http://geodesie.ign.fr/contenu/fichiers/documentation/algorithmes/notice/NT111_V1_HARMEL_TransfoNTF-RGF93_FormatGrilleNTV2.pdf">
030     * [French] Transformation de coordonnées NTF – RGF93 / Format de grille NTv2</a>
031     */
032    public static final NTV2GridShiftFileWrapper ntf_rgf93 = new NTV2GridShiftFileWrapper("resource://data/projection/ntf_r93_b.gsb");
033
034    // CHECKSTYLE.ON: LineLength
035
036    private NTV2GridShiftFile instance;
037    private String gridFileName;
038
039    /**
040     * Constructs a new {@code NTV2GridShiftFileWrapper}.
041     * @param filename Path to the grid file (GSB format)
042     */
043    public NTV2GridShiftFileWrapper(String filename) {
044        this.gridFileName = filename;
045    }
046
047    /**
048     * Returns the actual {@link NTV2GridShiftFile} behind this wrapper.
049     * The grid file is only loaded once, when first accessed.
050     * @return The NTv2 grid file
051     */
052    public NTV2GridShiftFile getShiftFile() {
053        if (instance == null) {
054            try (InputStream is = new CachedFile(gridFileName).getInputStream()) {
055                instance = new NTV2GridShiftFile();
056                instance.loadGridShiftFile(is, false);
057            } catch (Exception e) {
058                throw new RuntimeException(e);
059            }
060        }
061        return instance;
062    }
063}