Ignore:
Timestamp:
2012-12-26T15:47:43+01:00 (11 years ago)
Author:
bastiK
Message:

add option to select projection by code

this means it is no longer necessary to write a GUI for each new projection, because they are accessible in this list

File:
1 edited

Legend:

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

    r5554 r5634  
    77import java.io.InputStreamReader;
    88import java.util.Collection;
     9import java.util.Collections;
    910import java.util.HashMap;
     11import java.util.HashSet;
    1012import java.util.Map;
     13import java.util.Set;
    1114import java.util.regex.Matcher;
    1215import java.util.regex.Pattern;
     
    113116
    114117    public static String getInit(String id) {
    115         return inits.get(id.toLowerCase()).b;
     118        return inits.get(id.toUpperCase()).b;
    116119    }
    117120
     
    132135                    Matcher m = epsgPattern.matcher(line);
    133136                    if (m.matches()) {
    134                         inits.put("epsg:" + m.group(1), Pair.create(name, m.group(2).trim()));
     137                        inits.put("EPSG:" + m.group(1), Pair.create(name, m.group(2).trim()));
    135138                    } else {
    136139                        System.err.println("Warning: failed to parse line from the epsg projection definition: "+line);
     
    144147    }
    145148
    146     private final static Map<String, ProjectionChoice> allCodesPC = new HashMap<String, ProjectionChoice>();
    147     private final static Map<String, Projection> allCodes = new HashMap<String, Projection>();
     149    private final static Set<String> allCodes = new HashSet<String>();
     150    private final static Map<String, ProjectionChoice> allProjectionChoicesByCode = new HashMap<String, ProjectionChoice>();
     151    private final static Map<String, Projection> projectionsByCode_cache = new HashMap<String, Projection>();
    148152
    149153    static {
    150         // FIXME: use {@link #inits}, because it may contain more codes in future
    151         // than exposed by the ProjectionChoices
    152154        for (ProjectionChoice pc : ProjectionPreference.getProjectionChoices()) {
    153155            for (String code : pc.allCodes()) {
    154                 allCodesPC.put(code, pc);
     156                allProjectionChoicesByCode.put(code, pc);
    155157            }
    156158        }
     159        allCodes.addAll(inits.keySet());
     160        allCodes.addAll(allProjectionChoicesByCode.keySet());
    157161    }
    158162
    159163    public static Projection getProjectionByCode(String code) {
    160         Projection p = allCodes.get(code);
    161         if (p != null) return p;
    162         ProjectionChoice pc = allCodesPC.get(code);
    163         if (pc == null) return null;
    164         Collection<String> pref = pc.getPreferencesFromCode(code);
    165         pc.setPreferences(pref);
    166         p = pc.getProjection();
    167         allCodes.put(code, p);
    168         return p;
     164        Projection proj = projectionsByCode_cache.get(code);
     165        if (proj != null) return proj;
     166        ProjectionChoice pc = allProjectionChoicesByCode.get(code);
     167        if (pc != null) {
     168            Pair<String, String> pair = inits.get(code);
     169            if (pair == null) return null;
     170            String name = pair.a;
     171            String init = pair.b;
     172            proj = new CustomProjection(name, code, init, null);
     173        } else {
     174            Collection<String> pref = pc.getPreferencesFromCode(code);
     175            pc.setPreferences(pref);
     176            proj = pc.getProjection();
     177        }
     178        projectionsByCode_cache.put(code, proj);
     179        return proj;
     180    }
     181
     182    public static Collection<String> getAllProjectionCodes() {
     183        return Collections.unmodifiableCollection(allCodes);
    169184    }
    170185
Note: See TracChangeset for help on using the changeset viewer.