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

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

see #16129 - projections rework for new ESRI file

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