source: josm/trunk/src/org/openstreetmap/josm/data/projection/datum/NTV2Proj4DirGridShiftFileSource.java

Last change on this file was 19050, checked in by taylor.smock, 2 days ago

Revert most var changes from r19048, fix most new compile warnings and checkstyle issues

Also, document why various ErrorProne checks were originally disabled and fix
generic SonarLint issues.

File size: 3.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.projection.datum;
3
4import java.io.File;
5import java.io.IOException;
6import java.io.InputStream;
7import java.nio.file.Files;
8import java.nio.file.InvalidPathException;
9import java.util.Arrays;
10import java.util.Collections;
11import java.util.List;
12import java.util.stream.Collectors;
13
14import org.openstreetmap.josm.spi.preferences.Config;
15import org.openstreetmap.josm.tools.Logging;
16import org.openstreetmap.josm.tools.Platform;
17import org.openstreetmap.josm.tools.PlatformVisitor;
18import 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 */
24public 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}
Note: See TracBrowser for help on using the repository browser.