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

Last change on this file since 12795 was 12786, checked in by bastiK, 7 years ago

see #15182 - remove GUI references from Projections class

  • Property svn:eol-style set to native
File size: 16.5 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.function.Supplier;
17import java.util.regex.Matcher;
18import java.util.regex.Pattern;
19
20import org.openstreetmap.josm.Main;
21import org.openstreetmap.josm.data.coor.EastNorth;
22import org.openstreetmap.josm.data.coor.ILatLon;
23import org.openstreetmap.josm.data.coor.LatLon;
24import org.openstreetmap.josm.data.projection.datum.Datum;
25import org.openstreetmap.josm.data.projection.datum.GRS80Datum;
26import org.openstreetmap.josm.data.projection.datum.NTV2GridShiftFileWrapper;
27import org.openstreetmap.josm.data.projection.datum.SevenParameterDatum;
28import org.openstreetmap.josm.data.projection.datum.ThreeParameterDatum;
29import org.openstreetmap.josm.data.projection.datum.WGS84Datum;
30import org.openstreetmap.josm.data.projection.proj.AlbersEqualArea;
31import org.openstreetmap.josm.data.projection.proj.CassiniSoldner;
32import org.openstreetmap.josm.data.projection.proj.ClassProjFactory;
33import org.openstreetmap.josm.data.projection.proj.DoubleStereographic;
34import org.openstreetmap.josm.data.projection.proj.LambertAzimuthalEqualArea;
35import org.openstreetmap.josm.data.projection.proj.LambertConformalConic;
36import org.openstreetmap.josm.data.projection.proj.LonLat;
37import org.openstreetmap.josm.data.projection.proj.Mercator;
38import org.openstreetmap.josm.data.projection.proj.ObliqueMercator;
39import org.openstreetmap.josm.data.projection.proj.PolarStereographic;
40import org.openstreetmap.josm.data.projection.proj.Proj;
41import org.openstreetmap.josm.data.projection.proj.ProjFactory;
42import org.openstreetmap.josm.data.projection.proj.Sinusoidal;
43import org.openstreetmap.josm.data.projection.proj.SwissObliqueMercator;
44import org.openstreetmap.josm.data.projection.proj.TransverseMercator;
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, Supplier<Projection>> projectionSuppliersByCode = 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 allCodes.addAll(inits.keySet());
159 }
160
161 private Projections() {
162 // Hide default constructor for utils classes
163 }
164
165 private static void loadNadgrids(String definition) {
166 final String key = CustomProjection.Param.nadgrids.key;
167 if (definition.contains(key)) {
168 try {
169 String nadgridsId = CustomProjection.parseParameterList(definition, true).get(key);
170 if (nadgridsId.startsWith("@")) {
171 nadgridsId = nadgridsId.substring(1);
172 }
173 if (!"null".equals(nadgridsId) && !nadgrids.containsKey(nadgridsId)) {
174 nadgrids.put(nadgridsId, new NTV2GridShiftFileWrapper(nadgridsId));
175 }
176 } catch (ProjectionConfigurationException e) {
177 Logging.trace(e);
178 }
179 }
180 }
181
182 /**
183 * Convert from lat/lon to easting/northing using the current projection.
184 *
185 * @param ll the geographical point to convert (in WGS84 lat/lon)
186 * @return the corresponding east/north coordinates
187 * @since 12725
188 * @deprecated use <code>Main.getProjection().latlon2eastNorth(ll)</code>
189 */
190 @Deprecated
191 public static EastNorth project(ILatLon ll) {
192 if (ll == null) return null;
193 return Main.getProjection().latlon2eastNorth(ll);
194 }
195
196 /**
197 * Convert from lat/lon to easting/northing using the current projection.
198 *
199 * @param ll the geographical point to convert (in WGS84 lat/lon)
200 * @return the corresponding east/north coordinates
201 * @deprecated use <code>Main.getProjection().latlon2eastNorth(ll)</code>
202 */
203 @Deprecated
204 public static EastNorth project(LatLon ll) {
205 return project((ILatLon) ll);
206 }
207
208 /**
209 * Convert from easting/norting to lat/lon using the current projection.
210 *
211 * @param en the geographical point to convert (in projected coordinates)
212 * @return the corresponding lat/lon (WGS84)
213 * @deprecated use <code>Main.getProjection().eastNorth2latlon(en)</code>
214 */
215 @Deprecated
216 public static LatLon inverseProject(EastNorth en) {
217 if (en == null) return null;
218 return Main.getProjection().eastNorth2latlon(en);
219 }
220
221 /**
222 * Plugins can register additional base projections.
223 *
224 * @param id The "official" PROJ.4 id. In case the projection is not supported
225 * by PROJ.4, use some prefix, e.g. josm:myproj or gdal:otherproj.
226 * @param fac The base projection factory.
227 * @param origin Multiple plugins may implement the same base projection.
228 * Provide plugin name or similar string, so it be differentiated.
229 */
230 public static void registerBaseProjection(String id, ProjFactory fac, String origin) {
231 projs.put(id, fac);
232 }
233
234 public static void registerBaseProjection(String id, Class<? extends Proj> projClass, String origin) {
235 registerBaseProjection(id, new ClassProjFactory(projClass), origin);
236 }
237
238 /**
239 * Register a projection supplier, that is, a factory class for projections.
240 * @param code the code of the projection that will be returned
241 * @param supplier a supplier to return a projection with given code
242 * @since 12786
243 */
244 public static void registerProjectionSupplier(String code, Supplier<Projection> supplier) {
245 projectionSuppliersByCode.put(code, supplier);
246 allCodes.add(code);
247 }
248
249 /**
250 * Get a base projection by id.
251 *
252 * @param id the id, for example "lonlat" or "tmerc"
253 * @return the corresponding base projection if the id is known, null otherwise
254 */
255 public static Proj getBaseProjection(String id) {
256 ProjFactory fac = projs.get(id);
257 if (fac == null) return null;
258 return fac.createInstance();
259 }
260
261 /**
262 * Get an ellipsoid by id.
263 *
264 * @param id the id, for example "bessel" or "WGS84"
265 * @return the corresponding ellipsoid if the id is known, null otherwise
266 */
267 public static Ellipsoid getEllipsoid(String id) {
268 return ellipsoids.get(id);
269 }
270
271 /**
272 * Get a geodetic datum by id.
273 *
274 * @param id the id, for example "potsdam" or "WGS84"
275 * @return the corresponding datum if the id is known, null otherwise
276 */
277 public static Datum getDatum(String id) {
278 return datums.get(id);
279 }
280
281 /**
282 * Get a NTV2 grid database by id.
283 * @param id the id
284 * @return the corresponding NTV2 grid if the id is known, null otherwise
285 */
286 public static NTV2GridShiftFileWrapper getNTV2Grid(String id) {
287 return nadgrids.get(id);
288 }
289
290 /**
291 * Get the projection definition string for the given code.
292 * @param code the code
293 * @return the string that can be processed by #{link CustomProjection}.
294 * Null, if the code isn't supported.
295 */
296 public static String getInit(String code) {
297 ProjectionDefinition pd = inits.get(code.toUpperCase(Locale.ENGLISH));
298 if (pd == null) return null;
299 return pd.definition;
300 }
301
302 /**
303 * Load projection definitions from file.
304 *
305 * @param path the path
306 * @return projection definitions
307 * @throws IOException in case of I/O error
308 */
309 public static List<ProjectionDefinition> loadProjectionDefinitions(String path) throws IOException {
310 try (
311 CachedFile cf = new CachedFile(path);
312 BufferedReader r = cf.getContentReader()
313 ) {
314 return loadProjectionDefinitions(r);
315 }
316 }
317
318 /**
319 * Load projection definitions from file.
320 *
321 * @param r the reader
322 * @return projection definitions
323 * @throws IOException in case of I/O error
324 */
325 public static List<ProjectionDefinition> loadProjectionDefinitions(BufferedReader r) throws IOException {
326 List<ProjectionDefinition> result = new ArrayList<>();
327 Pattern epsgPattern = Pattern.compile("<(\\d+)>(.*)<>");
328 String line, lastline = "";
329 while ((line = r.readLine()) != null) {
330 line = line.trim();
331 if (!line.startsWith("#") && !line.isEmpty()) {
332 if (!lastline.startsWith("#")) throw new AssertionError("EPSG file seems corrupted");
333 String name = lastline.substring(1).trim();
334 Matcher m = epsgPattern.matcher(line);
335 if (m.matches()) {
336 String code = "EPSG:" + m.group(1);
337 String definition = m.group(2).trim();
338 result.add(new ProjectionDefinition(code, name, definition));
339 } else {
340 Logging.warn("Failed to parse line from the EPSG projection definition: "+line);
341 }
342 }
343 lastline = line;
344 }
345 return result;
346 }
347
348 /**
349 * Get a projection by code.
350 * @param code the code, e.g. "EPSG:2026"
351 * @return the corresponding projection, if the code is known, null otherwise
352 */
353 public static Projection getProjectionByCode(String code) {
354 Projection proj = projectionsByCode_cache.get(code);
355 if (proj != null) return proj;
356
357 ProjectionDefinition pd = inits.get(code);
358 if (pd != null) {
359 proj = new CustomProjection(pd.name, code, pd.definition);
360 }
361 if (proj == null) {
362 Supplier<Projection> ps = projectionSuppliersByCode.get(code);
363 if (ps != null) {
364 proj = ps.get();
365 }
366 }
367 if (proj != null) {
368 projectionsByCode_cache.put(code, proj);
369 }
370 return proj;
371 }
372
373 /**
374 * Get a list of all supported projection codes.
375 *
376 * @return all supported projection codes
377 * @see #getProjectionByCode(java.lang.String)
378 */
379 public static Collection<String> getAllProjectionCodes() {
380 return Collections.unmodifiableCollection(allCodes);
381 }
382
383 /**
384 * Get a list of ids of all registered base projections.
385 *
386 * @return all registered base projection ids
387 * @see #getBaseProjection(java.lang.String)
388 */
389 public static Collection<String> getAllBaseProjectionIds() {
390 return projs.keySet();
391 }
392
393 private static String listKeys(Map<String, ?> map) {
394 List<String> keys = new ArrayList<>(map.keySet());
395 Collections.sort(keys);
396 return Utils.join(", ", keys);
397 }
398
399 /**
400 * Replies the list of projections as string (comma separated).
401 * @return the list of projections as string (comma separated)
402 * @since 8533
403 */
404 public static String listProjs() {
405 return listKeys(projs);
406 }
407
408 /**
409 * Replies the list of ellipsoids as string (comma separated).
410 * @return the list of ellipsoids as string (comma separated)
411 * @since 8533
412 */
413 public static String listEllipsoids() {
414 return listKeys(ellipsoids);
415 }
416
417 /**
418 * Replies the list of datums as string (comma separated).
419 * @return the list of datums as string (comma separated)
420 * @since 8533
421 */
422 public static String listDatums() {
423 return listKeys(datums);
424 }
425
426 /**
427 * Replies the list of nadgrids as string (comma separated).
428 * @return the list of nadgrids as string (comma separated)
429 * @since 8533
430 */
431 public static String listNadgrids() {
432 return listKeys(nadgrids);
433 }
434}
Note: See TracBrowser for help on using the repository browser.