Changeset 11642 in josm for trunk/src/org/openstreetmap


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

fix #14422 - Dynamic NTV2 grids

Location:
trunk/src/org/openstreetmap/josm
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/Main.java

    r11510 r11642  
    113113import org.openstreetmap.josm.tools.Territories;
    114114import org.openstreetmap.josm.tools.Utils;
     115import org.openstreetmap.josm.tools.bugreport.BugReport;
    115116
    116117/**
     
    523524
    524525        // This needs to be done before RightAndLefthandTraffic::initialize is called
    525         new InitializationTask(tr("Initializing internal boundaries data"), Territories::initialize).call();
     526        try {
     527            new InitializationTask(tr("Initializing internal boundaries data"), Territories::initialize).call();
     528        } catch (JosmRuntimeException e) {
     529            // Can happen if the current projection needs NTV2 grid which is not available
     530            // In this case we want the user be able to change his projection
     531            BugReport.intercept(e).warn();
     532        }
    526533
    527534        // contains several initialization tasks to be executed (in parallel) by a ExecutorService
  • trunk/src/org/openstreetmap/josm/data/projection/Projections.java

    r11374 r11642  
    5050 * Class to manage projections.
    5151 *
    52  * Use this class to query available projection or register new projections
    53  * from a plugin.
     52 * Use this class to query available projection or register new projections from a plugin.
    5453 */
    5554public final class Projections {
     
    5958     */
    6059    public static class ProjectionDefinition {
    61         public String code;
    62         public String name;
    63         public String definition;
    64 
     60        public final String code;
     61        public final String name;
     62        public final String definition;
     63
     64        /**
     65         * Constructs a new {@code ProjectionDefinition}.
     66         * @param code EPSG code
     67         * @param name projection name
     68         * @param definition projection definition (EPSG format)
     69         */
    6570        public ProjectionDefinition(String code, String name, String definition) {
    6671            this.code = code;
     
    141146                Ellipsoid.Bessel1841, 598.1, 73.7, 418.2, 0.202, 0.045, -2.455, 6.7));
    142147
    143         nadgrids.put("BETA2007.gsb", NTV2GridShiftFileWrapper.BETA2007);
    144         nadgrids.put("ntf_r93_b.gsb", NTV2GridShiftFileWrapper.ntf_rgf93);
    145 
    146         List<ProjectionDefinition> pds;
    147148        try {
    148             pds = loadProjectionDefinitions("resource://data/projection/custom-epsg");
     149            inits = new LinkedHashMap<>();
     150            for (ProjectionDefinition pd : loadProjectionDefinitions("resource://data/projection/custom-epsg")) {
     151                inits.put(pd.code, pd);
     152                loadNadgrids(pd.definition);
     153            }
    149154        } catch (IOException ex) {
    150155            throw new JosmRuntimeException(ex);
    151         }
    152         inits = new LinkedHashMap<>();
    153         for (ProjectionDefinition pd : pds) {
    154             inits.put(pd.code, pd);
    155156        }
    156157
     
    166167    private Projections() {
    167168        // Hide default constructor for utils classes
     169    }
     170
     171    private static void loadNadgrids(String definition) {
     172        final String key = CustomProjection.Param.nadgrids.key;
     173        if (definition.contains(key)) {
     174            try {
     175                String nadgridsId = CustomProjection.parseParameterList(definition, true).get(key);
     176                if (nadgridsId.startsWith("@")) {
     177                    nadgridsId = nadgridsId.substring(1);
     178                }
     179                if (!"null".equals(nadgridsId) && !nadgrids.containsKey(nadgridsId)) {
     180                    nadgrids.put(nadgridsId, new NTV2GridShiftFileWrapper(nadgridsId));
     181                }
     182            } catch (ProjectionConfigurationException e) {
     183                Main.trace(e);
     184            }
     185        }
    168186    }
    169187
  • trunk/src/org/openstreetmap/josm/data/projection/datum/NTV2Datum.java

    r5226 r11642  
    22package org.openstreetmap.josm.data.projection.datum;
    33
     4import java.io.IOException;
     5
    46import org.openstreetmap.josm.data.coor.LatLon;
    57import org.openstreetmap.josm.data.projection.Ellipsoid;
     8import org.openstreetmap.josm.tools.JosmRuntimeException;
    69
    710/**
     
    1013public class NTV2Datum extends AbstractDatum {
    1114
    12     protected NTV2GridShiftFileWrapper nadgrids;
     15    private final NTV2GridShiftFileWrapper nadgrids;
    1316
     17    /**
     18     * Constructs a new {@code NTV2Datum}.
     19     * @param name datum name
     20     * @param proj4Id PROJ.4 id
     21     * @param ellps ellipsoid
     22     * @param nadgrids NTV2 grid shift file wrapper
     23     */
    1424    public NTV2Datum(String name, String proj4Id, Ellipsoid ellps, NTV2GridShiftFileWrapper nadgrids) {
    1525        super(name, proj4Id, ellps);
     
    2030    public LatLon toWGS84(LatLon ll) {
    2131        NTV2GridShift gs = new NTV2GridShift(ll);
    22         nadgrids.getShiftFile().gridShiftForward(gs);
    23         return new LatLon(ll.lat() + gs.getLatShiftDegrees(), ll.lon() + gs.getLonShiftPositiveEastDegrees());
     32        try {
     33            nadgrids.getShiftFile().gridShiftForward(gs);
     34            return new LatLon(ll.lat() + gs.getLatShiftDegrees(), ll.lon() + gs.getLonShiftPositiveEastDegrees());
     35        } catch (IOException e) {
     36            throw new JosmRuntimeException(e);
     37        }
    2438    }
    2539
     
    2741    public LatLon fromWGS84(LatLon ll) {
    2842        NTV2GridShift gs = new NTV2GridShift(ll);
    29         nadgrids.getShiftFile().gridShiftReverse(gs);
    30         return new LatLon(ll.lat() + gs.getLatShiftDegrees(), ll.lon() + gs.getLonShiftPositiveEastDegrees());
     43        try {
     44            nadgrids.getShiftFile().gridShiftReverse(gs);
     45            return new LatLon(ll.lat() + gs.getLatShiftDegrees(), ll.lon() + gs.getLonShiftPositiveEastDegrees());
     46        } catch (IOException e) {
     47            throw new JosmRuntimeException(e);
     48        }
    3149    }
    3250}
  • 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        }
  • trunk/src/org/openstreetmap/josm/tools/PlatformHook.java

    r11156 r11642  
    99import java.security.NoSuchAlgorithmException;
    1010import java.security.cert.CertificateException;
     11import java.util.List;
    1112
    1213/**
     
    172173     */
    173174    File getDefaultUserDataDirectory();
     175
     176    /**
     177     * Returns the list of platform-dependent default datum shifting directories for the PROJ.4 library.
     178     * @return the list of platform-dependent default datum shifting directories for the PROJ.4 library
     179     * @since 11642
     180     */
     181    List<File> getDefaultProj4NadshiftDirectories();
    174182}
  • trunk/src/org/openstreetmap/josm/tools/PlatformHookOsx.java

    r11535 r11642  
    1515import java.lang.reflect.Proxy;
    1616import java.util.Arrays;
     17import java.util.Collections;
    1718import java.util.List;
    1819
     
    382383                Main.pref.getJOSMDirectoryBaseName());
    383384    }
     385
     386    @Override
     387    public List<File> getDefaultProj4NadshiftDirectories() {
     388        return Collections.emptyList();
     389    }
    384390}
  • trunk/src/org/openstreetmap/josm/tools/PlatformHookUnixoid.java

    r11613 r11642  
    1818import java.nio.file.Paths;
    1919import java.util.Arrays;
     20import java.util.List;
    2021import java.util.Locale;
    2122
     
    433434    }
    434435
     436    @Override
     437    public List<File> getDefaultProj4NadshiftDirectories() {
     438        return Arrays.asList(new File("/usr/local/share/proj"), new File("/usr/share/proj"));
     439    }
    435440}
  • trunk/src/org/openstreetmap/josm/tools/PlatformHookWindows.java

    r11218 r11642  
    5353import java.security.spec.X509EncodedKeySpec;
    5454import java.util.ArrayList;
     55import java.util.Arrays;
    5556import java.util.Collection;
    5657import java.util.Enumeration;
     
    572573        return def;
    573574    }
     575
     576    @Override
     577    public List<File> getDefaultProj4NadshiftDirectories() {
     578        return Arrays.asList(new File("C:\\PROJ\\NAD"));
     579    }
    574580}
Note: See TracChangeset for help on using the changeset viewer.