Changeset 12777 in josm for trunk


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

Location:
trunk/src/org/openstreetmap/josm
Files:
2 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/Main.java

    r12776 r12777  
    1717import java.util.Iterator;
    1818import java.util.List;
    19 import java.util.Locale;
    2019import java.util.Map;
    2120import java.util.Objects;
     
    5756import org.openstreetmap.josm.tools.PlatformHook;
    5857import org.openstreetmap.josm.tools.PlatformHookOsx;
    59 import org.openstreetmap.josm.tools.PlatformHookUnixoid;
    6058import org.openstreetmap.josm.tools.PlatformHookWindows;
    6159import org.openstreetmap.josm.tools.Shortcut;
  • 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;
  • trunk/src/org/openstreetmap/josm/gui/MainApplication.java

    r12770 r12777  
    8585import org.openstreetmap.josm.data.osm.UserInfo;
    8686import org.openstreetmap.josm.data.osm.search.SearchMode;
     87import org.openstreetmap.josm.data.projection.datum.NTV2GridShiftFileSource;
     88import org.openstreetmap.josm.data.projection.datum.NTV2GridShiftFileWrapper;
     89import org.openstreetmap.josm.data.projection.datum.NTV2Proj4DirGridShiftFileSource;
    8790import org.openstreetmap.josm.data.validation.OsmValidator;
    8891import org.openstreetmap.josm.gui.ProgramArguments.Option;
     
    113116import org.openstreetmap.josm.gui.util.WindowGeometry;
    114117import org.openstreetmap.josm.gui.widgets.UrlLabel;
     118import org.openstreetmap.josm.io.CachedFile;
    115119import org.openstreetmap.josm.io.CertificateAmendment;
    116120import org.openstreetmap.josm.io.DefaultProxySelector;
     
    235239            menu.redo.setEnabled(redoSize > 0);
    236240        };
     241
     242    /**
     243     * Source of NTV2 shift files: Download from JOSM website.
     244     * @since 12777
     245     */
     246    public static final NTV2GridShiftFileSource JOSM_WEBSITE_NTV2_SOURCE = gridFileName -> {
     247        String location = Main.getJOSMWebsite() + "/proj/" + gridFileName;
     248        // Try to load grid file
     249        CachedFile cf = new CachedFile(location);
     250        try {
     251            return cf.getInputStream();
     252        } catch (IOException ex) {
     253            Logging.warn(ex);
     254            return null;
     255        }
     256    };
    237257
    238258    /**
     
    902922        Main.toolbar = toolbar;
    903923        ProjectionPreference.setProjection();
     924        NTV2GridShiftFileWrapper.registerNTV2GridShiftFileSource(
     925                NTV2GridShiftFileWrapper.NTV2_SOURCE_PRIORITY_LOCAL,
     926                NTV2Proj4DirGridShiftFileSource.getInstance());
     927        NTV2GridShiftFileWrapper.registerNTV2GridShiftFileSource(
     928                NTV2GridShiftFileWrapper.NTV2_SOURCE_PRIORITY_DOWNLOAD,
     929                JOSM_WEBSITE_NTV2_SOURCE);
    904930        GuiHelper.translateJavaInternalMessages();
    905931        preConstructorInit();
  • trunk/src/org/openstreetmap/josm/tools/PlatformHook.java

    r12776 r12777  
    2020
    2121import org.openstreetmap.josm.Main;
    22 import org.openstreetmap.josm.data.projection.datum.NTV2GridShiftFileWrapper;
     22import org.openstreetmap.josm.data.projection.datum.NTV2Proj4DirGridShiftFileSource;
    2323import org.openstreetmap.josm.io.CertificateAmendment.CertAmend;
    2424import org.openstreetmap.josm.tools.date.DateUtils;
     
    259259     */
    260260    default List<File> getDefaultProj4NadshiftDirectories() {
    261         return getPlatform().accept(NTV2GridShiftFileWrapper.DEFAULT_PROJ4_NTV2_SHIFT_DIRS);
     261        return getPlatform().accept(NTV2Proj4DirGridShiftFileSource.getInstance());
    262262    }
    263263
Note: See TracChangeset for help on using the changeset viewer.