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

Last change on this file since 10250 was 10212, checked in by Don-vip, 8 years ago

sonar - squid:S2221 - "Exception" should not be caught when not required by called methods

  • Property svn:eol-style set to native
File size: 15.4 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.ArrayList;
10import java.util.Collection;
11import java.util.Collections;
12import java.util.HashMap;
13import java.util.HashSet;
14import java.util.LinkedHashMap;
15import java.util.List;
16import java.util.Locale;
17import java.util.Map;
18import java.util.Set;
19import java.util.regex.Matcher;
20import java.util.regex.Pattern;
21
22import org.openstreetmap.josm.Main;
23import org.openstreetmap.josm.data.coor.EastNorth;
24import org.openstreetmap.josm.data.coor.LatLon;
25import org.openstreetmap.josm.data.projection.datum.Datum;
26import org.openstreetmap.josm.data.projection.datum.GRS80Datum;
27import org.openstreetmap.josm.data.projection.datum.NTV2GridShiftFileWrapper;
28import org.openstreetmap.josm.data.projection.datum.SevenParameterDatum;
29import org.openstreetmap.josm.data.projection.datum.ThreeParameterDatum;
30import org.openstreetmap.josm.data.projection.datum.WGS84Datum;
31import org.openstreetmap.josm.data.projection.proj.AlbersEqualArea;
32import org.openstreetmap.josm.data.projection.proj.CassiniSoldner;
33import org.openstreetmap.josm.data.projection.proj.ClassProjFactory;
34import org.openstreetmap.josm.data.projection.proj.DoubleStereographic;
35import org.openstreetmap.josm.data.projection.proj.LambertAzimuthalEqualArea;
36import org.openstreetmap.josm.data.projection.proj.LambertConformalConic;
37import org.openstreetmap.josm.data.projection.proj.LonLat;
38import org.openstreetmap.josm.data.projection.proj.Mercator;
39import org.openstreetmap.josm.data.projection.proj.ObliqueMercator;
40import org.openstreetmap.josm.data.projection.proj.PolarStereographic;
41import org.openstreetmap.josm.data.projection.proj.Proj;
42import org.openstreetmap.josm.data.projection.proj.ProjFactory;
43import org.openstreetmap.josm.data.projection.proj.Sinusoidal;
44import org.openstreetmap.josm.data.projection.proj.SwissObliqueMercator;
45import org.openstreetmap.josm.data.projection.proj.TransverseMercator;
46import org.openstreetmap.josm.gui.preferences.projection.ProjectionChoice;
47import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
48import org.openstreetmap.josm.io.CachedFile;
49import org.openstreetmap.josm.tools.Utils;
50
51/**
52 * Class to manage projections.
53 *
54 * Use this class to query available projection or register new projections
55 * from a plugin.
56 */
57public final class Projections {
58
59 /**
60 * Class to hold information about one projection.
61 */
62 public static class ProjectionDefinition {
63 public String code;
64 public String name;
65 public String definition;
66
67 public ProjectionDefinition(String code, String name, String definition) {
68 this.code = code;
69 this.name = name;
70 this.definition = definition;
71 }
72 }
73
74 private static final Set<String> allCodes = new HashSet<>();
75 private static final Map<String, ProjectionChoice> allProjectionChoicesByCode = new HashMap<>();
76 private static final Map<String, Projection> projectionsByCode_cache = new HashMap<>();
77
78 /*********************************
79 * Registry for custom projection
80 *
81 * should be compatible to PROJ.4
82 */
83 static final Map<String, ProjFactory> projs = new HashMap<>();
84 static final Map<String, Ellipsoid> ellipsoids = new HashMap<>();
85 static final Map<String, Datum> datums = new HashMap<>();
86 static final Map<String, NTV2GridShiftFileWrapper> nadgrids = new HashMap<>();
87 static final Map<String, ProjectionDefinition> inits;
88
89 static {
90 registerBaseProjection("aea", AlbersEqualArea.class, "core");
91 registerBaseProjection("cass", CassiniSoldner.class, "core");
92 registerBaseProjection("laea", LambertAzimuthalEqualArea.class, "core");
93 registerBaseProjection("lcc", LambertConformalConic.class, "core");
94 registerBaseProjection("lonlat", LonLat.class, "core");
95 registerBaseProjection("merc", Mercator.class, "core");
96 registerBaseProjection("omerc", ObliqueMercator.class, "core");
97 registerBaseProjection("somerc", SwissObliqueMercator.class, "core");
98 registerBaseProjection("sinu", Sinusoidal.class, "core");
99 registerBaseProjection("stere", PolarStereographic.class, "core");
100 registerBaseProjection("sterea", DoubleStereographic.class, "core");
101 registerBaseProjection("tmerc", TransverseMercator.class, "core");
102
103 ellipsoids.put("airy", Ellipsoid.Airy);
104 ellipsoids.put("mod_airy", Ellipsoid.AiryMod);
105 ellipsoids.put("aust_SA", Ellipsoid.AustSA);
106 ellipsoids.put("bessel", Ellipsoid.Bessel1841);
107 ellipsoids.put("bess_nam", Ellipsoid.BesselNamibia);
108 ellipsoids.put("clrk66", Ellipsoid.Clarke1866);
109 ellipsoids.put("clrk80", Ellipsoid.Clarke1880);
110 ellipsoids.put("clrk80ign", Ellipsoid.ClarkeIGN);
111 ellipsoids.put("evrstSS", Ellipsoid.EverestSabahSarawak);
112 ellipsoids.put("intl", Ellipsoid.Hayford);
113 ellipsoids.put("helmert", Ellipsoid.Helmert);
114 ellipsoids.put("krass", Ellipsoid.Krassowsky);
115 ellipsoids.put("GRS67", Ellipsoid.GRS67);
116 ellipsoids.put("GRS80", Ellipsoid.GRS80);
117 ellipsoids.put("WGS66", Ellipsoid.WGS66);
118 ellipsoids.put("WGS72", Ellipsoid.WGS72);
119 ellipsoids.put("WGS84", Ellipsoid.WGS84);
120
121 datums.put("WGS84", WGS84Datum.INSTANCE);
122 datums.put("NAD83", GRS80Datum.INSTANCE);
123 datums.put("carthage", new ThreeParameterDatum(
124 "Carthage 1934 Tunisia", "carthage",
125 Ellipsoid.ClarkeIGN, -263.0, 6.0, 431.0));
126 datums.put("GGRS87", new ThreeParameterDatum(
127 "Greek Geodetic Reference System 1987", "GGRS87",
128 Ellipsoid.GRS80, -199.87, 74.79, 246.62));
129 datums.put("hermannskogel", new SevenParameterDatum(
130 "Hermannskogel", "hermannskogel",
131 Ellipsoid.Bessel1841, 577.326, 90.129, 463.919, 5.137, 1.474, 5.297, 2.4232));
132 datums.put("ire65", new SevenParameterDatum(
133 "Ireland 1965", "ire65",
134 Ellipsoid.AiryMod, 482.530, -130.596, 564.557, -1.042, -0.214, -0.631, 8.15));
135 datums.put("nzgd49", new SevenParameterDatum(
136 "New Zealand Geodetic Datum 1949", "nzgd49",
137 Ellipsoid.Hayford, 59.47, -5.04, 187.44, 0.47, -0.1, 1.024, -4.5993));
138 datums.put("OSGB36", new SevenParameterDatum(
139 "Airy 1830", "OSGB36",
140 Ellipsoid.Airy, 446.448, -125.157, 542.060, 0.1502, 0.2470, 0.8421, -20.4894));
141 datums.put("potsdam", new SevenParameterDatum(
142 "Potsdam Rauenberg 1950 DHDN", "potsdam",
143 Ellipsoid.Bessel1841, 598.1, 73.7, 418.2, 0.202, 0.045, -2.455, 6.7));
144
145 nadgrids.put("BETA2007.gsb", NTV2GridShiftFileWrapper.BETA2007);
146 nadgrids.put("ntf_r93_b.gsb", NTV2GridShiftFileWrapper.ntf_rgf93);
147
148 List<ProjectionDefinition> pds;
149 try {
150 pds = loadProjectionDefinitions("resource://data/projection/custom-epsg");
151 } catch (IOException ex) {
152 throw new RuntimeException(ex);
153 }
154 inits = new LinkedHashMap<>();
155 for (ProjectionDefinition pd : pds) {
156 inits.put(pd.code, pd);
157 }
158
159 for (ProjectionChoice pc : ProjectionPreference.getProjectionChoices()) {
160 for (String code : pc.allCodes()) {
161 allProjectionChoicesByCode.put(code, pc);
162 }
163 }
164 allCodes.addAll(inits.keySet());
165 allCodes.addAll(allProjectionChoicesByCode.keySet());
166 }
167
168 private Projections() {
169 // Hide default constructor for utils classes
170 }
171
172 /**
173 * Convert from lat/lon to easting/northing using the current projection.
174 *
175 * @param ll the geographical point to convert (in WGS84 lat/lon)
176 * @return the corresponding east/north coordinates
177 */
178 public static EastNorth project(LatLon ll) {
179 if (ll == null) return null;
180 return Main.getProjection().latlon2eastNorth(ll);
181 }
182
183 /**
184 * Convert from easting/norting to lat/lon using the current projection.
185 *
186 * @param en the geographical point to convert (in projected coordinates)
187 * @return the corresponding lat/lon (WGS84)
188 */
189 public static LatLon inverseProject(EastNorth en) {
190 if (en == null) return null;
191 return Main.getProjection().eastNorth2latlon(en);
192 }
193
194 /**
195 * Plugins can register additional base projections.
196 *
197 * @param id The "official" PROJ.4 id. In case the projection is not supported
198 * by PROJ.4, use some prefix, e.g. josm:myproj or gdal:otherproj.
199 * @param fac The base projection factory.
200 * @param origin Multiple plugins may implement the same base projection.
201 * Provide plugin name or similar string, so it be differentiated.
202 */
203 public static void registerBaseProjection(String id, ProjFactory fac, String origin) {
204 projs.put(id, fac);
205 }
206
207 public static void registerBaseProjection(String id, Class<? extends Proj> projClass, String origin) {
208 registerBaseProjection(id, new ClassProjFactory(projClass), origin);
209 }
210
211 /**
212 * Get a base projection by id.
213 *
214 * @param id the id, for example "lonlat" or "tmerc"
215 * @return the corresponding base projection if the id is known, null otherwise
216 */
217 public static Proj getBaseProjection(String id) {
218 ProjFactory fac = projs.get(id);
219 if (fac == null) return null;
220 return fac.createInstance();
221 }
222
223 /**
224 * Get an ellipsoid by id.
225 *
226 * @param id the id, for example "bessel" or "WGS84"
227 * @return the corresponding ellipsoid if the id is known, null otherwise
228 */
229 public static Ellipsoid getEllipsoid(String id) {
230 return ellipsoids.get(id);
231 }
232
233 /**
234 * Get a geodetic datum by id.
235 *
236 * @param id the id, for example "potsdam" or "WGS84"
237 * @return the corresponding datum if the id is known, null otherwise
238 */
239 public static Datum getDatum(String id) {
240 return datums.get(id);
241 }
242
243 /**
244 * Get a NTV2 grid database by id.
245 * @param id the id
246 * @return the corresponding NTV2 grid if the id is known, null otherwise
247 */
248 public static NTV2GridShiftFileWrapper getNTV2Grid(String id) {
249 return nadgrids.get(id);
250 }
251
252 /**
253 * Get the projection definition string for the given code.
254 * @param code the code
255 * @return the string that can be processed by #{link CustomProjection}.
256 * Null, if the code isn't supported.
257 */
258 public static String getInit(String code) {
259 ProjectionDefinition pd = inits.get(code.toUpperCase(Locale.ENGLISH));
260 if (pd == null) return null;
261 return pd.definition;
262 }
263
264 /**
265 * Load projection definitions from file.
266 *
267 * @param path the path
268 * @return projection definitions
269 * @throws IOException in case of I/O error
270 */
271 public static List<ProjectionDefinition> loadProjectionDefinitions(String path) throws IOException {
272 try (
273 CachedFile cf = new CachedFile(path);
274 InputStream in = cf.getInputStream();
275 BufferedReader r = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
276 ) {
277 return loadProjectionDefinitions(r);
278 }
279 }
280
281 /**
282 * Load projection definitions from file.
283 *
284 * @param r the reader
285 * @return projection definitions
286 * @throws IOException in case of I/O error
287 */
288 public static List<ProjectionDefinition> loadProjectionDefinitions(BufferedReader r) throws IOException {
289 List<ProjectionDefinition> result = new ArrayList<>();
290 Pattern epsgPattern = Pattern.compile("<(\\d+)>(.*)<>");
291 String line, lastline = "";
292 while ((line = r.readLine()) != null) {
293 line = line.trim();
294 if (!line.startsWith("#") && !line.isEmpty()) {
295 if (!lastline.startsWith("#")) throw new AssertionError("EPSG file seems corrupted");
296 String name = lastline.substring(1).trim();
297 Matcher m = epsgPattern.matcher(line);
298 if (m.matches()) {
299 String code = "EPSG:" + m.group(1);
300 String definition = m.group(2).trim();
301 result.add(new ProjectionDefinition(code, name, definition));
302 } else {
303 Main.warn("Failed to parse line from the EPSG projection definition: "+line);
304 }
305 }
306 lastline = line;
307 }
308 return result;
309 }
310
311 /**
312 * Get a projection by code.
313 * @param code the code, e.g. "EPSG:2026"
314 * @return the corresponding projection, if the code is known, null otherwise
315 */
316 public static Projection getProjectionByCode(String code) {
317 Projection proj = projectionsByCode_cache.get(code);
318 if (proj != null) return proj;
319 ProjectionChoice pc = allProjectionChoicesByCode.get(code);
320 if (pc != null) {
321 Collection<String> pref = pc.getPreferencesFromCode(code);
322 pc.setPreferences(pref);
323 try {
324 proj = pc.getProjection();
325 } catch (RuntimeException e) {
326 String cause = e.getMessage();
327 Main.warn("Unable to get projection "+code+" with "+pc + (cause != null ? ". "+cause : ""));
328 }
329 }
330 if (proj == null) {
331 ProjectionDefinition pd = inits.get(code);
332 if (pd == null) return null;
333 proj = new CustomProjection(pd.name, code, pd.definition, null);
334 }
335 projectionsByCode_cache.put(code, proj);
336 return proj;
337 }
338
339 /**
340 * Get a list of all supported projection codes.
341 *
342 * @return all supported projection codes
343 * @see #getProjectionByCode(java.lang.String)
344 */
345 public static Collection<String> getAllProjectionCodes() {
346 return Collections.unmodifiableCollection(allCodes);
347 }
348
349 /**
350 * Get a list of ids of all registered base projections.
351 *
352 * @return all registered base projection ids
353 * @see #getBaseProjection(java.lang.String)
354 */
355 public static Collection<String> getAllBaseProjectionIds() {
356 return projs.keySet();
357 }
358
359 private static String listKeys(Map<String, ?> map) {
360 List<String> keys = new ArrayList<>(map.keySet());
361 Collections.sort(keys);
362 return Utils.join(", ", keys);
363 }
364
365 /**
366 * Replies the list of projections as string (comma separated).
367 * @return the list of projections as string (comma separated)
368 * @since 8533
369 */
370 public static String listProjs() {
371 return listKeys(projs);
372 }
373
374 /**
375 * Replies the list of ellipsoids as string (comma separated).
376 * @return the list of ellipsoids as string (comma separated)
377 * @since 8533
378 */
379 public static String listEllipsoids() {
380 return listKeys(ellipsoids);
381 }
382
383 /**
384 * Replies the list of datums as string (comma separated).
385 * @return the list of datums as string (comma separated)
386 * @since 8533
387 */
388 public static String listDatums() {
389 return listKeys(datums);
390 }
391
392 /**
393 * Replies the list of nadgrids as string (comma separated).
394 * @return the list of nadgrids as string (comma separated)
395 * @since 8533
396 */
397 public static String listNadgrids() {
398 return listKeys(nadgrids);
399 }
400}
Note: See TracBrowser for help on using the repository browser.