Ignore:
Timestamp:
2017-09-08T00:42:43+02:00 (7 years ago)
Author:
bastiK
Message:

see #15229 - add registry for NTV2 grid file sources

removes dependency of NTV2GridShiftFileWrapper on Main

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/projection/datum/NTV2GridShiftFileWrapper.java

    r12776 r12777  
    88import java.util.Collections;
    99import java.util.List;
     10import java.util.Map;
     11import java.util.TreeMap;
    1012
    11 import org.openstreetmap.josm.Main;
    12 import org.openstreetmap.josm.io.CachedFile;
    13 import org.openstreetmap.josm.tools.Platform;
    1413import org.openstreetmap.josm.tools.PlatformVisitor;
    1514
     
    2423    private NTV2GridShiftFile instance;
    2524    private final String gridFileName;
     25
     26    public static float NTV2_SOURCE_PRIORITY_LOCAL = 10f;
     27    public static float NTV2_SOURCE_PRIORITY_DOWNLOAD = 5f;
     28
     29    private static Map<Float, NTV2GridShiftFileSource> sources =
     30            new TreeMap<Float, NTV2GridShiftFileSource>(Collections.reverseOrder());
     31
     32    /**
     33     * Register a source for NTV2 grid files.
     34     * @param priority the priority, sources with higher priority are checked first;
     35     * use {@link #NTV2_SOURCE_PRIORITY_LOCAL} for local files and
     36     * {@link #NTV2_SOURCE_PRIORITY_DOWNLOAD} for remote downloads
     37     * @param source the NTV2 grid file source
     38     * @since 12777
     39     */
     40    public static void registerNTV2GridShiftFileSource(float priority, NTV2GridShiftFileSource source) {
     41        sources.put(priority, source);
     42    }
    2643
    2744    /**
     
    6380    public synchronized NTV2GridShiftFile getShiftFile() throws IOException {
    6481        if (instance == null) {
    65             File grid = null;
    66             // Check is the grid is installed in default PROJ.4 directories
    67             for (File dir : Platform.determinePlatform().accept(DEFAULT_PROJ4_NTV2_SHIFT_DIRS)) {
    68                 File file = new File(dir, gridFileName);
    69                 if (file.exists() && file.isFile()) {
    70                     grid = file;
    71                     break;
    72                 }
    73             }
    74             // If not, search into PROJ_LIB directory
    75             if (grid == null) {
    76                 String projLib = System.getProperty("PROJ_LIB");
    77                 if (projLib != null && !projLib.isEmpty()) {
    78                     File dir = new File(projLib);
    79                     if (dir.exists() && dir.isDirectory()) {
    80                         File file = new File(dir, gridFileName);
    81                         if (file.exists() && file.isFile()) {
    82                             grid = file;
    83                         }
    84                     }
    85                 }
    86             }
    87             // If not, retrieve it from JOSM website
    88             String location = grid != null ? grid.getAbsolutePath() : (Main.getJOSMWebsite() + "/proj/" + gridFileName);
    89             // Try to load grid file
    90             try (CachedFile cf = new CachedFile(location); InputStream is = cf.getInputStream()) {
    91                 NTV2GridShiftFile ntv2 = new NTV2GridShiftFile();
    92                 ntv2.loadGridShiftFile(is, false);
    93                 instance = ntv2;
    94             }
     82            for (Map.Entry<Float, NTV2GridShiftFileSource> entry : sources.entrySet()) {
     83                NTV2GridShiftFileSource source = entry.getValue();
     84                try (InputStream is = source.getNTV2GridShiftFile(gridFileName)) {
     85                    if (is != null) {
     86                        NTV2GridShiftFile ntv2 = new NTV2GridShiftFile();
     87                        ntv2.loadGridShiftFile(is, false);
     88                        instance = ntv2;
     89                        break;
     90                     }
     91                 }
     92             }
    9593        }
    9694        return instance;
Note: See TracChangeset for help on using the changeset viewer.