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