Changeset 9126 in josm


Ignore:
Timestamp:
2015-12-15T10:30:38+01:00 (8 years ago)
Author:
bastiK
Message:

see #12186 - rework loading of projection definitions
to make it accessible from outside

File:
1 edited

Legend:

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

    r9108 r9126  
    1212import java.util.HashMap;
    1313import java.util.HashSet;
     14import java.util.LinkedHashMap;
    1415import java.util.List;
    1516import java.util.Locale;
     
    1819import java.util.regex.Matcher;
    1920import java.util.regex.Pattern;
    20 
    2121import org.openstreetmap.josm.Main;
    2222import org.openstreetmap.josm.data.coor.EastNorth;
     
    4040import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
    4141import org.openstreetmap.josm.io.CachedFile;
    42 import org.openstreetmap.josm.tools.Pair;
    4342import org.openstreetmap.josm.tools.Utils;
    4443
     
    4948public final class Projections {
    5049
     50    /**
     51     * Class to hold information about one projection.
     52     */
     53    public static class ProjectionDefinition {
     54        public String code;
     55        public String name;
     56        public String definition;
     57
     58        public ProjectionDefinition(String code, String name, String definition) {
     59            this.code = code;
     60            this.name = name;
     61            this.definition = definition;
     62        }
     63    }
     64
    5165    private Projections() {
    5266        // Hide default constructor for utils classes
     
    7286    static final Map<String, Datum> datums = new HashMap<>();
    7387    static final Map<String, NTV2GridShiftFileWrapper> nadgrids = new HashMap<>();
    74     static final Map<String, Pair<String, String>> inits = new HashMap<>();
     88    static final Map<String, ProjectionDefinition> inits;
    7589
    7690    static {
     
    128142        nadgrids.put("ntf_r93_b.gsb", NTV2GridShiftFileWrapper.ntf_rgf93);
    129143
    130         loadInits();
     144        try {
     145            inits = loadProjectionDefinitions("resource://data/projection/epsg");
     146        } catch (IOException ex) {
     147            throw new RuntimeException(ex);
     148        }
    131149    }
    132150
     
    173191     */
    174192    public static String getInit(String id) {
    175         Pair<String, String> r = inits.get(id.toUpperCase(Locale.ENGLISH));
    176         if (r == null) return null;
    177         return r.b;
    178     }
    179 
    180     /**
    181      * Load +init "presets" from file
    182      */
    183     private static void loadInits() {
    184         Pattern epsgPattern = Pattern.compile("<(\\d+)>(.*)<>");
     193        ProjectionDefinition pd = inits.get(id.toUpperCase(Locale.ENGLISH));
     194        if (pd == null) return null;
     195        return pd.definition;
     196    }
     197
     198    /**
     199     * Load projection definitions from file.
     200     *
     201     * @param path the path
     202     * @return projection definitions
     203     * @throws java.io.IOException
     204     */
     205    public static Map<String, ProjectionDefinition> loadProjectionDefinitions(String path) throws IOException {
    185206        try (
    186             InputStream in = new CachedFile("resource://data/projection/epsg").getInputStream();
     207            InputStream in = new CachedFile(path).getInputStream();
    187208            BufferedReader r = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
    188209        ) {
    189             String line, lastline = "";
    190             while ((line = r.readLine()) != null) {
    191                 line = line.trim();
    192                 if (!line.startsWith("#") && !line.isEmpty()) {
    193                     if (!lastline.startsWith("#")) throw new AssertionError("EPSG file seems corrupted");
    194                     String name = lastline.substring(1).trim();
    195                     Matcher m = epsgPattern.matcher(line);
    196                     if (m.matches()) {
    197                         inits.put("EPSG:" + m.group(1), Pair.create(name, m.group(2).trim()));
    198                     } else {
    199                         Main.warn("Failed to parse line from the EPSG projection definition: "+line);
    200                     }
    201                 }
    202                 lastline = line;
    203             }
     210            return loadProjectionDefinitions(r);
    204211        } catch (IOException ex) {
    205212            throw new RuntimeException(ex);
    206213        }
     214    }
     215
     216    /**
     217     * Load projection definitions from file.
     218     *
     219     * @param r the reader
     220     * @return projection definitions
     221     * @throws java.io.IOException
     222     */
     223    public static Map<String, ProjectionDefinition> loadProjectionDefinitions(BufferedReader r) throws IOException {
     224        Map<String, ProjectionDefinition> result = new LinkedHashMap<>();
     225        Pattern epsgPattern = Pattern.compile("<(\\d+)>(.*)<>");
     226        String line, lastline = "";
     227        while ((line = r.readLine()) != null) {
     228            line = line.trim();
     229            if (!line.startsWith("#") && !line.isEmpty()) {
     230                if (!lastline.startsWith("#")) throw new AssertionError("EPSG file seems corrupted");
     231                String name = lastline.substring(1).trim();
     232                Matcher m = epsgPattern.matcher(line);
     233                if (m.matches()) {
     234                    String code = "EPSG:" + m.group(1);
     235                    String definition = m.group(2).trim();
     236                    result.put(code, new ProjectionDefinition(code, name, definition));
     237                } else {
     238                    Main.warn("Failed to parse line from the EPSG projection definition: "+line);
     239                }
     240            }
     241            lastline = line;
     242        }
     243        return result;
    207244    }
    208245
     
    236273        }
    237274        if (proj == null) {
    238             Pair<String, String> pair = inits.get(code);
    239             if (pair == null) return null;
    240             String name = pair.a;
    241             String init = pair.b;
    242             proj = new CustomProjection(name, code, init, null);
     275            ProjectionDefinition pd = inits.get(code);
     276            if (pd == null) return null;
     277            proj = new CustomProjection(pd.name, code, pd.definition, null);
    243278        }
    244279        projectionsByCode_cache.put(code, proj);
Note: See TracChangeset for help on using the changeset viewer.