001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.data.projection.datum; 003 004import java.io.IOException; 005import java.io.InputStream; 006import java.util.Collections; 007import java.util.Map; 008import java.util.TreeMap; 009 010/** 011 * Wrapper for {@link NTV2GridShiftFile}. 012 * 013 * Loads the shift file from disk, when it is first accessed. 014 * @since 5226 015 */ 016public class NTV2GridShiftFileWrapper { 017 018 private NTV2GridShiftFile instance; 019 private final String gridFileName; 020 021 public static final float NTV2_SOURCE_PRIORITY_LOCAL = 10f; 022 public static final float NTV2_SOURCE_PRIORITY_DOWNLOAD = 5f; 023 024 private static Map<Float, NTV2GridShiftFileSource> sources = new TreeMap<>(Collections.reverseOrder()); 025 026 /** 027 * Register a source for NTV2 grid files. 028 * @param priority the priority, sources with higher priority are checked first; 029 * use {@link #NTV2_SOURCE_PRIORITY_LOCAL} for local files and 030 * {@link #NTV2_SOURCE_PRIORITY_DOWNLOAD} for remote downloads 031 * @param source the NTV2 grid file source 032 * @since 12777 033 */ 034 public static void registerNTV2GridShiftFileSource(float priority, NTV2GridShiftFileSource source) { 035 sources.put(priority, source); 036 } 037 038 /** 039 * Constructs a new {@code NTV2GridShiftFileWrapper}. 040 * @param filename Path to the grid file (GSB format) 041 */ 042 public NTV2GridShiftFileWrapper(String filename) { 043 this.gridFileName = filename; 044 } 045 046 /** 047 * Returns the actual {@link NTV2GridShiftFile} behind this wrapper. 048 * The grid file is only loaded once, when first accessed. 049 * @return The NTv2 grid file 050 * @throws IOException if the grid file cannot be found/loaded 051 */ 052 public synchronized NTV2GridShiftFile getShiftFile() throws IOException { 053 if (instance == null) { 054 for (Map.Entry<Float, NTV2GridShiftFileSource> entry : sources.entrySet()) { 055 NTV2GridShiftFileSource source = entry.getValue(); 056 try (InputStream is = source.getNTV2GridShiftFile(gridFileName)) { 057 if (is != null) { 058 NTV2GridShiftFile ntv2 = new NTV2GridShiftFile(); 059 ntv2.loadGridShiftFile(is, false); 060 instance = ntv2; 061 break; 062 } 063 } 064 } 065 } 066 return instance; 067 } 068}