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