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