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

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

see #15229 - fix warnings

  • Property svn:eol-style set to native
File size: 3.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.projection.datum;
3
4import java.io.File;
5import java.io.IOException;
6import java.io.InputStream;
7import java.util.Arrays;
8import java.util.Collections;
9import java.util.List;
10import java.util.Map;
11import java.util.TreeMap;
12
13import org.openstreetmap.josm.tools.PlatformVisitor;
14
15/**
16 * Wrapper for {@link NTV2GridShiftFile}.
17 *
18 * Loads the shift file from disk, when it is first accessed.
19 * @since 5226
20 */
21public class NTV2GridShiftFileWrapper {
22
23 private NTV2GridShiftFile instance;
24 private final String gridFileName;
25
26 public static final float NTV2_SOURCE_PRIORITY_LOCAL = 10f;
27 public static final float NTV2_SOURCE_PRIORITY_DOWNLOAD = 5f;
28
29 private static Map<Float, NTV2GridShiftFileSource> sources = new TreeMap<>(Collections.reverseOrder());
30
31 /**
32 * Register a source for NTV2 grid files.
33 * @param priority the priority, sources with higher priority are checked first;
34 * use {@link #NTV2_SOURCE_PRIORITY_LOCAL} for local files and
35 * {@link #NTV2_SOURCE_PRIORITY_DOWNLOAD} for remote downloads
36 * @param source the NTV2 grid file source
37 * @since 12777
38 */
39 public static void registerNTV2GridShiftFileSource(float priority, NTV2GridShiftFileSource source) {
40 sources.put(priority, source);
41 }
42
43 /**
44 * Constructs a new {@code NTV2GridShiftFileWrapper}.
45 * @param filename Path to the grid file (GSB format)
46 */
47 public NTV2GridShiftFileWrapper(String filename) {
48 this.gridFileName = filename;
49 }
50
51 /**
52 * Lists default directories where the ntv2 shift files (NAD) for the proj4
53 * library would be located on different platforms.
54 */
55 public static final PlatformVisitor<List<File>> DEFAULT_PROJ4_NTV2_SHIFT_DIRS =
56 new PlatformVisitor<List<File>>() {
57 @Override
58 public List<File> visitUnixoid() {
59 return Arrays.asList(new File("/usr/local/share/proj"), new File("/usr/share/proj"));
60 }
61
62 @Override
63 public List<File> visitWindows() {
64 return Arrays.asList(new File("C:\\PROJ\\NAD"));
65 }
66
67 @Override
68 public List<File> visitOsx() {
69 return Collections.emptyList();
70 }
71 };
72
73 /**
74 * Returns the actual {@link NTV2GridShiftFile} behind this wrapper.
75 * The grid file is only loaded once, when first accessed.
76 * @return The NTv2 grid file
77 * @throws IOException if the grid file cannot be found/loaded
78 */
79 public synchronized NTV2GridShiftFile getShiftFile() throws IOException {
80 if (instance == null) {
81 for (Map.Entry<Float, NTV2GridShiftFileSource> entry : sources.entrySet()) {
82 NTV2GridShiftFileSource source = entry.getValue();
83 try (InputStream is = source.getNTV2GridShiftFile(gridFileName)) {
84 if (is != null) {
85 NTV2GridShiftFile ntv2 = new NTV2GridShiftFile();
86 ntv2.loadGridShiftFile(is, false);
87 instance = ntv2;
88 break;
89 }
90 }
91 }
92 }
93 return instance;
94 }
95}
Note: See TracBrowser for help on using the repository browser.