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    /**
017     * Used in Germany to convert coordinates between the DHDN (<i>Deutsches Hauptdreiecksnetz</i>)
018     * and ETRS89 (<i>European Terrestrial Reference System 1989</i>) datums.
019     * @see <a href="http://crs.bkg.bund.de/crseu/crs/descrtrans/eu-descrtrans.php?crs_id=REVfREhETiAvIEdLXzM=&op_id=REVfREhETiAoQmVUQSwgMjAwNykgdG8gRVRSUzg5">
020     * Description of Transformation - DE_DHDN (BeTA, 2007) to ETRS89</a>
021     */
022    public static final NTV2GridShiftFileWrapper BETA2007 = new NTV2GridShiftFileWrapper("resource://data/projection/BETA2007.gsb");
023
024    /**
025     * Used in France to convert coordinates between the NTF (<i>Nouvelle triangulation de la France</i>)
026     * and RGF93 (<i>R?seau g?od?sique fran?ais 1993</i>) datums.
027     * @see <a href="http://geodesie.ign.fr/contenu/fichiers/documentation/algorithmes/notice/NT111_V1_HARMEL_TransfoNTF-RGF93_FormatGrilleNTV2.pdf">
028     * [French] Transformation de coordonn?es NTF ? RGF93 / Format de grille NTv2</a>
029     */
030    public static final NTV2GridShiftFileWrapper ntf_rgf93 = new NTV2GridShiftFileWrapper("resource://data/projection/ntf_r93_b.gsb");
031
032    private NTV2GridShiftFile instance = null;
033    private String gridFileName;
034
035    /**
036     * Constructs a new {@code NTV2GridShiftFileWrapper}.
037     * @param filename Path to the grid file (GSB format)
038     */
039    public NTV2GridShiftFileWrapper(String filename) {
040        this.gridFileName = filename;
041    }
042
043    /**
044     * Returns the actual {@link NTV2GridShiftFile} behind this wrapper.
045     * The grid file is only loaded once, when first accessed.
046     * @return The NTv2 grid file
047     */
048    public NTV2GridShiftFile getShiftFile() {
049        if (instance == null) {
050            try (InputStream is = new CachedFile(gridFileName).getInputStream()) {
051                instance = new NTV2GridShiftFile();
052                instance.loadGridShiftFile(is, false);
053            } catch (Exception e) {
054                throw new RuntimeException(e);
055            }
056        }
057        return instance;
058    }
059}