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.nio.file.Files;
|
---|
8 | import java.nio.file.InvalidPathException;
|
---|
9 | import java.util.Arrays;
|
---|
10 | import java.util.Collections;
|
---|
11 | import java.util.List;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.tools.Logging;
|
---|
14 | import org.openstreetmap.josm.tools.Platform;
|
---|
15 | import org.openstreetmap.josm.tools.PlatformVisitor;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * Shift file source that scans the common data directories of the proj4 library.
|
---|
19 | * @since 12777
|
---|
20 | */
|
---|
21 | public final class NTV2Proj4DirGridShiftFileSource implements NTV2GridShiftFileSource, PlatformVisitor<List<File>> {
|
---|
22 |
|
---|
23 | private NTV2Proj4DirGridShiftFileSource() {
|
---|
24 | // hide constructor
|
---|
25 | }
|
---|
26 |
|
---|
27 | // lazy initialization
|
---|
28 | private static class InstanceHolder {
|
---|
29 | static final NTV2Proj4DirGridShiftFileSource INSTANCE = new NTV2Proj4DirGridShiftFileSource();
|
---|
30 | }
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Get the singleton instance of this class.
|
---|
34 | * @return the singleton instance of this class
|
---|
35 | */
|
---|
36 | public static NTV2Proj4DirGridShiftFileSource getInstance() {
|
---|
37 | return InstanceHolder.INSTANCE;
|
---|
38 | }
|
---|
39 |
|
---|
40 | @Override
|
---|
41 | public InputStream getNTV2GridShiftFile(String gridFileName) {
|
---|
42 | File grid = null;
|
---|
43 | // Check is the grid is installed in default PROJ.4 directories
|
---|
44 | for (File dir : Platform.determinePlatform().accept(this)) {
|
---|
45 | File file = new File(dir, gridFileName);
|
---|
46 | if (file.exists() && file.isFile()) {
|
---|
47 | grid = file;
|
---|
48 | break;
|
---|
49 | }
|
---|
50 | }
|
---|
51 | // If not, search into PROJ_LIB directory
|
---|
52 | if (grid == null) {
|
---|
53 | String projLib = System.getProperty("PROJ_LIB");
|
---|
54 | if (projLib != null && !projLib.isEmpty()) {
|
---|
55 | File dir = new File(projLib);
|
---|
56 | if (dir.exists() && dir.isDirectory()) {
|
---|
57 | File file = new File(dir, gridFileName);
|
---|
58 | if (file.exists() && file.isFile()) {
|
---|
59 | grid = file;
|
---|
60 | }
|
---|
61 | }
|
---|
62 | }
|
---|
63 | }
|
---|
64 | if (grid != null) {
|
---|
65 | try {
|
---|
66 | return Files.newInputStream(grid.getAbsoluteFile().toPath());
|
---|
67 | } catch (IOException | InvalidPathException ex) {
|
---|
68 | Logging.warn("Unable to open NTV2 grid shift file: " + grid);
|
---|
69 | Logging.debug(ex);
|
---|
70 | }
|
---|
71 | }
|
---|
72 | return null;
|
---|
73 | }
|
---|
74 |
|
---|
75 | @Override
|
---|
76 | public List<File> visitUnixoid() {
|
---|
77 | return Arrays.asList(new File("/usr/local/share/proj"), new File("/usr/share/proj"));
|
---|
78 | }
|
---|
79 |
|
---|
80 | @Override
|
---|
81 | public List<File> visitWindows() {
|
---|
82 | return Arrays.asList(new File("C:\\PROJ\\NAD"));
|
---|
83 | }
|
---|
84 |
|
---|
85 | @Override
|
---|
86 | public List<File> visitOsx() {
|
---|
87 | return Collections.emptyList();
|
---|
88 | }
|
---|
89 | }
|
---|