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 |
|
---|
8 | import org.openstreetmap.josm.Main;
|
---|
9 | import org.openstreetmap.josm.io.CachedFile;
|
---|
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 | /**
|
---|
23 | * Constructs a new {@code NTV2GridShiftFileWrapper}.
|
---|
24 | * @param filename Path to the grid file (GSB format)
|
---|
25 | */
|
---|
26 | public NTV2GridShiftFileWrapper(String filename) {
|
---|
27 | this.gridFileName = filename;
|
---|
28 | }
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Returns the actual {@link NTV2GridShiftFile} behind this wrapper.
|
---|
32 | * The grid file is only loaded once, when first accessed.
|
---|
33 | * @return The NTv2 grid file
|
---|
34 | * @throws IOException if the grid file cannot be found/loaded
|
---|
35 | */
|
---|
36 | public synchronized NTV2GridShiftFile getShiftFile() throws IOException {
|
---|
37 | if (instance == null) {
|
---|
38 | File grid = null;
|
---|
39 | // Check is the grid is installed in default PROJ.4 directories
|
---|
40 | for (File dir : Main.platform.getDefaultProj4NadshiftDirectories()) {
|
---|
41 | File file = new File(dir, gridFileName);
|
---|
42 | if (file.exists() && file.isFile()) {
|
---|
43 | grid = file;
|
---|
44 | break;
|
---|
45 | }
|
---|
46 | }
|
---|
47 | // If not, search into PROJ_LIB directory
|
---|
48 | if (grid == null) {
|
---|
49 | String projLib = System.getProperty("PROJ_LIB");
|
---|
50 | if (projLib != null && !projLib.isEmpty()) {
|
---|
51 | File dir = new File(projLib);
|
---|
52 | if (dir.exists() && dir.isDirectory()) {
|
---|
53 | File file = new File(dir, gridFileName);
|
---|
54 | if (file.exists() && file.isFile()) {
|
---|
55 | grid = file;
|
---|
56 | }
|
---|
57 | }
|
---|
58 | }
|
---|
59 | }
|
---|
60 | // If not, retrieve it from JOSM website
|
---|
61 | String location = grid != null ? grid.getAbsolutePath() : (Main.getJOSMWebsite() + "/proj/" + gridFileName);
|
---|
62 | // Try to load grid file
|
---|
63 | try (CachedFile cf = new CachedFile(location); InputStream is = cf.getInputStream()) {
|
---|
64 | NTV2GridShiftFile ntv2 = new NTV2GridShiftFile();
|
---|
65 | ntv2.loadGridShiftFile(is, false);
|
---|
66 | instance = ntv2;
|
---|
67 | }
|
---|
68 | }
|
---|
69 | return instance;
|
---|
70 | }
|
---|
71 | }
|
---|