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

Last change on this file since 12620 was 12620, checked in by Don-vip, 7 years ago

see #15182 - deprecate all Main logging methods and introduce suitable replacements in Logging for most of them

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