Ignore:
Timestamp:
2017-02-28T22:54:27+01:00 (9 years ago)
Author:
Don-vip
Message:

fix #14422 - Dynamic NTV2 grids

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/projection/datum/NTV2GridShiftFileWrapper.java

    r11374 r11642  
    22package org.openstreetmap.josm.data.projection.datum;
    33
     4import java.io.File;
    45import java.io.IOException;
    56import java.io.InputStream;
    67
     8import org.openstreetmap.josm.Main;
    79import org.openstreetmap.josm.io.CachedFile;
    8 import org.openstreetmap.josm.tools.JosmRuntimeException;
    910
    1011/**
     
    1516 */
    1617public class NTV2GridShiftFileWrapper {
    17 
    18     // CHECKSTYLE.OFF: LineLength
    19 
    20     /**
    21      * Used in Germany to convert coordinates between the DHDN (<i>Deutsches Hauptdreiecksnetz</i>)
    22      * and ETRS89 (<i>European Terrestrial Reference System 1989</i>) datums.
    23      * @see <a href="http://crs.bkg.bund.de/crseu/crs/descrtrans/eu-descrtrans.php?crs_id=REVfREhETiAvIEdLXzM=&op_id=REVfREhETiAoQmVUQSwgMjAwNykgdG8gRVRSUzg5">
    24      * Description of Transformation - DE_DHDN (BeTA, 2007) to ETRS89</a>
    25      */
    26     public static final NTV2GridShiftFileWrapper BETA2007 = new NTV2GridShiftFileWrapper("resource://data/projection/BETA2007.gsb");
    27 
    28     /**
    29      * Used in France to convert coordinates between the NTF (<i>Nouvelle triangulation de la France</i>)
    30      * and RGF93 (<i>Réseau géodésique français 1993</i>) datums.
    31      * @see <a href="http://geodesie.ign.fr/contenu/fichiers/documentation/algorithmes/notice/NT111_V1_HARMEL_TransfoNTF-RGF93_FormatGrilleNTV2.pdf">
    32      * [French] Transformation de coordonnées NTF – RGF93 / Format de grille NTv2</a>
    33      */
    34     public static final NTV2GridShiftFileWrapper ntf_rgf93 = new NTV2GridShiftFileWrapper("resource://data/projection/ntf_r93_b.gsb");
    35 
    36     // CHECKSTYLE.ON: LineLength
    3718
    3819    private NTV2GridShiftFile instance;
     
    5132     * The grid file is only loaded once, when first accessed.
    5233     * @return The NTv2 grid file
     34     * @throws IOException if the grid file cannot be found/loaded
    5335     */
    54     public NTV2GridShiftFile getShiftFile() {
     36    public synchronized NTV2GridShiftFile getShiftFile() throws IOException {
    5537        if (instance == null) {
    56             try (CachedFile cf = new CachedFile(gridFileName); InputStream is = cf.getInputStream()) {
    57                 instance = new NTV2GridShiftFile();
    58                 instance.loadGridShiftFile(is, false);
    59             } catch (IOException e) {
    60                 throw new JosmRuntimeException(e);
     38            File grid = null;
     39            // Check is the grid is installed in default PROJ.4 directories
     40            for (File dir : Main.platform.getDefaultProj4NadshiftDirectories()) {
     41                File file = new File(dir, gridFileName);
     42                if (file.exists() && file.isFile()) {
     43                    grid = file;
     44                    break;
     45                }
     46            }
     47            // If not, search into PROJ_LIB directory
     48            if (grid == null) {
     49                String projLib = System.getProperty("PROJ_LIB");
     50                if (projLib != null && !projLib.isEmpty()) {
     51                    File dir = new File(projLib);
     52                    if (dir.exists() && dir.isDirectory()) {
     53                        File file = new File(dir, gridFileName);
     54                        if (file.exists() && file.isFile()) {
     55                            grid = file;
     56                        }
     57                    }
     58                }
     59            }
     60            // If not, retrieve it from JOSM website
     61            String location = grid != null ? grid.getAbsolutePath() : (Main.getJOSMWebsite() + "/proj/" + gridFileName);
     62            // Try to load grid file
     63            try (CachedFile cf = new CachedFile(location); InputStream is = cf.getInputStream()) {
     64                NTV2GridShiftFile ntv2 = new NTV2GridShiftFile();
     65                ntv2.loadGridShiftFile(is, false);
     66                instance = ntv2;
    6167            }
    6268        }
Note: See TracChangeset for help on using the changeset viewer.