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

Last change on this file since 15149 was 14624, checked in by Don-vip, 5 years ago

fix various SonarQube issues

File size: 3.2 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 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 File grid = null;
46 // Check is the grid is installed in default PROJ.4 directories
47 for (File dir : Platform.determinePlatform().accept(this)) {
48 File file = new File(dir, gridFileName);
49 if (file.exists() && file.isFile()) {
50 grid = file;
51 break;
52 }
53 }
54 // If not, search into PROJ_LIB directory
55 if (grid == null) {
56 String projLib = Utils.getSystemProperty("PROJ_LIB");
57 if (projLib != null && !projLib.isEmpty()) {
58 File dir = new File(projLib);
59 if (dir.exists() && dir.isDirectory()) {
60 File file = new File(dir, gridFileName);
61 if (file.exists() && file.isFile()) {
62 grid = file;
63 }
64 }
65 }
66 }
67 if (grid != null) {
68 try {
69 return Files.newInputStream(grid.getAbsoluteFile().toPath());
70 } catch (IOException | InvalidPathException ex) {
71 Logging.warn("Unable to open NTV2 grid shift file: " + grid);
72 Logging.debug(ex);
73 }
74 }
75 return null;
76 }
77
78 private static List<File> visit(String prefSuffix, String... defaults) {
79 return Config.getPref().getList("ntv2.proj4.grid.dir." + prefSuffix, Arrays.asList(defaults))
80 .stream().map(File::new).collect(Collectors.toList());
81 }
82
83 @Override
84 public List<File> visitUnixoid() {
85 return visit("unix", "/usr/local/share/proj", "/usr/share/proj");
86 }
87
88 @Override
89 public List<File> visitWindows() {
90 return visit("windows", "C:\\PROJ\\NAD");
91 }
92
93 @Override
94 public List<File> visitOsx() {
95 return Collections.emptyList();
96 }
97}
Note: See TracBrowser for help on using the repository browser.