- Timestamp:
- 2015-12-15T10:30:38+01:00 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/projection/Projections.java
r9108 r9126 12 12 import java.util.HashMap; 13 13 import java.util.HashSet; 14 import java.util.LinkedHashMap; 14 15 import java.util.List; 15 16 import java.util.Locale; … … 18 19 import java.util.regex.Matcher; 19 20 import java.util.regex.Pattern; 20 21 21 import org.openstreetmap.josm.Main; 22 22 import org.openstreetmap.josm.data.coor.EastNorth; … … 40 40 import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference; 41 41 import org.openstreetmap.josm.io.CachedFile; 42 import org.openstreetmap.josm.tools.Pair;43 42 import org.openstreetmap.josm.tools.Utils; 44 43 … … 49 48 public final class Projections { 50 49 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 51 65 private Projections() { 52 66 // Hide default constructor for utils classes … … 72 86 static final Map<String, Datum> datums = new HashMap<>(); 73 87 static final Map<String, NTV2GridShiftFileWrapper> nadgrids = new HashMap<>(); 74 static final Map<String, P air<String, String>> inits = new HashMap<>();88 static final Map<String, ProjectionDefinition> inits; 75 89 76 90 static { … … 128 142 nadgrids.put("ntf_r93_b.gsb", NTV2GridShiftFileWrapper.ntf_rgf93); 129 143 130 loadInits(); 144 try { 145 inits = loadProjectionDefinitions("resource://data/projection/epsg"); 146 } catch (IOException ex) { 147 throw new RuntimeException(ex); 148 } 131 149 } 132 150 … … 173 191 */ 174 192 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 { 185 206 try ( 186 InputStream in = new CachedFile( "resource://data/projection/epsg").getInputStream();207 InputStream in = new CachedFile(path).getInputStream(); 187 208 BufferedReader r = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8)); 188 209 ) { 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); 204 211 } catch (IOException ex) { 205 212 throw new RuntimeException(ex); 206 213 } 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; 207 244 } 208 245 … … 236 273 } 237 274 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); 243 278 } 244 279 projectionsByCode_cache.put(code, proj);
Note:
See TracChangeset
for help on using the changeset viewer.