source: josm/trunk/src/org/openstreetmap/josm/data/projection/datum/NTV2GridShiftFileWrapper.java@ 13627

Last change on this file since 13627 was 13627, checked in by Don-vip, 6 years ago

see #16129 - datum javadoc

  • Property svn:eol-style set to native
File size: 2.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.projection.datum;
3
4import java.io.IOException;
5import java.io.InputStream;
6import java.util.Collections;
7import java.util.Map;
8import java.util.TreeMap;
9
10/**
11 * Wrapper for {@link NTV2GridShiftFile}.
12 *
13 * Loads the shift file from disk, when it is first accessed.
14 * @since 5226
15 */
16public class NTV2GridShiftFileWrapper {
17
18 private NTV2GridShiftFile instance;
19 private final String gridFileName;
20
21 /** Priority for local NTV2 grid files */
22 public static final float NTV2_SOURCE_PRIORITY_LOCAL = 10f;
23 /** Priority for downloaded NTV2 grid files */
24 public static final float NTV2_SOURCE_PRIORITY_DOWNLOAD = 5f;
25
26 private static Map<Float, NTV2GridShiftFileSource> sources = new TreeMap<>(Collections.reverseOrder());
27
28 /**
29 * Register a source for NTV2 grid files.
30 * @param priority the priority, sources with higher priority are checked first;
31 * use {@link #NTV2_SOURCE_PRIORITY_LOCAL} for local files and
32 * {@link #NTV2_SOURCE_PRIORITY_DOWNLOAD} for remote downloads
33 * @param source the NTV2 grid file source
34 * @since 12777
35 */
36 public static void registerNTV2GridShiftFileSource(float priority, NTV2GridShiftFileSource source) {
37 sources.put(priority, source);
38 }
39
40 /**
41 * Constructs a new {@code NTV2GridShiftFileWrapper}.
42 * @param filename Path to the grid file (GSB format)
43 */
44 public NTV2GridShiftFileWrapper(String filename) {
45 this.gridFileName = filename;
46 }
47
48 /**
49 * Returns the actual {@link NTV2GridShiftFile} behind this wrapper.
50 * The grid file is only loaded once, when first accessed.
51 * @return The NTv2 grid file
52 * @throws IOException if the grid file cannot be found/loaded
53 */
54 public synchronized NTV2GridShiftFile getShiftFile() throws IOException {
55 if (instance == null) {
56 for (Map.Entry<Float, NTV2GridShiftFileSource> entry : sources.entrySet()) {
57 NTV2GridShiftFileSource source = entry.getValue();
58 try (InputStream is = source.getNTV2GridShiftFile(gridFileName)) {
59 if (is != null) {
60 NTV2GridShiftFile ntv2 = new NTV2GridShiftFile();
61 ntv2.loadGridShiftFile(is, false);
62 instance = ntv2;
63 break;
64 }
65 }
66 }
67 }
68 return instance;
69 }
70}
Note: See TracBrowser for help on using the repository browser.