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

Last change on this file since 13561 was 13434, checked in by Don-vip, 6 years ago

see #8039, see #10456 - support read-only data layers

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