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

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

reduce visibility of Projections maps

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