1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.projection.datum;
|
---|
3 |
|
---|
4 | import java.io.File;
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.io.InputStream;
|
---|
7 | import java.util.Arrays;
|
---|
8 | import java.util.Collections;
|
---|
9 | import java.util.List;
|
---|
10 | import java.util.Map;
|
---|
11 | import java.util.TreeMap;
|
---|
12 |
|
---|
13 | import 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 | */
|
---|
21 | public 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 | }
|
---|