source: josm/trunk/src/org/openstreetmap/josm/data/projection/Projections.java@ 7402

Last change on this file since 7402 was 7248, checked in by bastiK, 10 years ago

reworked MirroredInputStream (renamed to CachedFile):

  • no more awkwardly open and close InputStream if you just want the underlying file (e.g. to get file inside zip file)
  • make it easier to add configuration parameters, without having endless list of parameters for the constructor (Factory style, similar to ImageProvider)

breaks plugins; see #10139

File size: 7.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.projection;
3
4import java.io.BufferedReader;
5import java.io.IOException;
6import java.io.InputStream;
7import java.io.InputStreamReader;
8import java.nio.charset.StandardCharsets;
9import java.util.Collection;
10import java.util.Collections;
11import java.util.HashMap;
12import java.util.HashSet;
13import java.util.Map;
14import java.util.Set;
15import java.util.regex.Matcher;
16import java.util.regex.Pattern;
17
18import org.openstreetmap.josm.Main;
19import org.openstreetmap.josm.data.coor.EastNorth;
20import org.openstreetmap.josm.data.coor.LatLon;
21import org.openstreetmap.josm.data.projection.datum.Datum;
22import org.openstreetmap.josm.data.projection.datum.NTV2GridShiftFileWrapper;
23import org.openstreetmap.josm.data.projection.datum.WGS84Datum;
24import org.openstreetmap.josm.data.projection.proj.ClassProjFactory;
25import org.openstreetmap.josm.data.projection.proj.LambertConformalConic;
26import org.openstreetmap.josm.data.projection.proj.LonLat;
27import org.openstreetmap.josm.data.projection.proj.Mercator;
28import org.openstreetmap.josm.data.projection.proj.Proj;
29import org.openstreetmap.josm.data.projection.proj.ProjFactory;
30import org.openstreetmap.josm.data.projection.proj.SwissObliqueMercator;
31import org.openstreetmap.josm.data.projection.proj.TransverseMercator;
32import org.openstreetmap.josm.gui.preferences.projection.ProjectionChoice;
33import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
34import org.openstreetmap.josm.io.CachedFile;
35import org.openstreetmap.josm.tools.Pair;
36
37/**
38 * Class to handle projections
39 *
40 */
41public final class Projections {
42
43 private Projections() {
44 // Hide default constructor for utils classes
45 }
46
47 public static EastNorth project(LatLon ll) {
48 if (ll == null) return null;
49 return Main.getProjection().latlon2eastNorth(ll);
50 }
51
52 public static LatLon inverseProject(EastNorth en) {
53 if (en == null) return null;
54 return Main.getProjection().eastNorth2latlon(en);
55 }
56
57 /*********************************
58 * Registry for custom projection
59 *
60 * should be compatible to PROJ.4
61 */
62 public static final Map<String, ProjFactory> projs = new HashMap<>();
63 public static final Map<String, Ellipsoid> ellipsoids = new HashMap<>();
64 public static final Map<String, Datum> datums = new HashMap<>();
65 public static final Map<String, NTV2GridShiftFileWrapper> nadgrids = new HashMap<>();
66 public static final Map<String, Pair<String, String>> inits = new HashMap<>();
67
68 static {
69 registerBaseProjection("lonlat", LonLat.class, "core");
70 registerBaseProjection("josm:smerc", Mercator.class, "core");
71 registerBaseProjection("lcc", LambertConformalConic.class, "core");
72 registerBaseProjection("somerc", SwissObliqueMercator.class, "core");
73 registerBaseProjection("tmerc", TransverseMercator.class, "core");
74
75 ellipsoids.put("clarkeIGN", Ellipsoid.clarkeIGN);
76 ellipsoids.put("intl", Ellipsoid.hayford);
77 ellipsoids.put("GRS67", Ellipsoid.GRS67);
78 ellipsoids.put("GRS80", Ellipsoid.GRS80);
79 ellipsoids.put("WGS84", Ellipsoid.WGS84);
80 ellipsoids.put("bessel", Ellipsoid.Bessel1841);
81
82 datums.put("WGS84", WGS84Datum.INSTANCE);
83
84 nadgrids.put("BETA2007.gsb", NTV2GridShiftFileWrapper.BETA2007);
85 nadgrids.put("ntf_r93_b.gsb", NTV2GridShiftFileWrapper.ntf_rgf93);
86
87 loadInits();
88 }
89
90 /**
91 * Plugins can register additional base projections.
92 *
93 * @param id The "official" PROJ.4 id. In case the projection is not supported
94 * by PROJ.4, use some prefix, e.g. josm:myproj or gdal:otherproj.
95 * @param fac The base projection factory.
96 * @param origin Multiple plugins may implement the same base projection.
97 * Provide plugin name or similar string, so it be differentiated.
98 */
99 public static void registerBaseProjection(String id, ProjFactory fac, String origin) {
100 projs.put(id, fac);
101 }
102
103 public static void registerBaseProjection(String id, Class<? extends Proj> projClass, String origin) {
104 registerBaseProjection(id, new ClassProjFactory(projClass), origin);
105 }
106
107 public static Proj getBaseProjection(String id) {
108 ProjFactory fac = projs.get(id);
109 if (fac == null) return null;
110 return fac.createInstance();
111 }
112
113 public static Ellipsoid getEllipsoid(String id) {
114 return ellipsoids.get(id);
115 }
116
117 public static Datum getDatum(String id) {
118 return datums.get(id);
119 }
120
121 public static NTV2GridShiftFileWrapper getNTV2Grid(String id) {
122 return nadgrids.get(id);
123 }
124
125 public static String getInit(String id) {
126 return inits.get(id.toUpperCase()).b;
127 }
128
129 /**
130 * Load +init "presets" from file
131 */
132 private static void loadInits() {
133 Pattern epsgPattern = Pattern.compile("<(\\d+)>(.*)<>");
134 try (
135 InputStream in = new CachedFile("resource://data/projection/epsg").getInputStream();
136 BufferedReader r = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
137 ) {
138 String line, lastline = "";
139 while ((line = r.readLine()) != null) {
140 line = line.trim();
141 if (!line.startsWith("#") && !line.isEmpty()) {
142 if (!lastline.startsWith("#")) throw new AssertionError("EPSG file seems corrupted");
143 String name = lastline.substring(1).trim();
144 Matcher m = epsgPattern.matcher(line);
145 if (m.matches()) {
146 inits.put("EPSG:" + m.group(1), Pair.create(name, m.group(2).trim()));
147 } else {
148 Main.warn("Failed to parse line from the EPSG projection definition: "+line);
149 }
150 }
151 lastline = line;
152 }
153 } catch (IOException ex) {
154 throw new RuntimeException(ex);
155 }
156 }
157
158 private static final Set<String> allCodes = new HashSet<>();
159 private static final Map<String, ProjectionChoice> allProjectionChoicesByCode = new HashMap<>();
160 private static final Map<String, Projection> projectionsByCode_cache = new HashMap<>();
161
162 static {
163 for (ProjectionChoice pc : ProjectionPreference.getProjectionChoices()) {
164 for (String code : pc.allCodes()) {
165 allProjectionChoicesByCode.put(code, pc);
166 }
167 }
168 allCodes.addAll(inits.keySet());
169 allCodes.addAll(allProjectionChoicesByCode.keySet());
170 }
171
172 public static Projection getProjectionByCode(String code) {
173 Projection proj = projectionsByCode_cache.get(code);
174 if (proj != null) return proj;
175 ProjectionChoice pc = allProjectionChoicesByCode.get(code);
176 if (pc != null) {
177 Collection<String> pref = pc.getPreferencesFromCode(code);
178 pc.setPreferences(pref);
179 try {
180 proj = pc.getProjection();
181 } catch (Exception e) {
182 String cause = e.getMessage();
183 Main.warn("Unable to get projection "+code+" with "+pc + (cause != null ? ". "+cause : ""));
184 }
185 }
186 if (proj == null) {
187 Pair<String, String> pair = inits.get(code);
188 if (pair == null) return null;
189 String name = pair.a;
190 String init = pair.b;
191 proj = new CustomProjection(name, code, init, null);
192 }
193 projectionsByCode_cache.put(code, proj);
194 return proj;
195 }
196
197 public static Collection<String> getAllProjectionCodes() {
198 return Collections.unmodifiableCollection(allCodes);
199 }
200}
Note: See TracBrowser for help on using the repository browser.