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