1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.projection;
|
---|
3 |
|
---|
4 | import java.io.BufferedReader;
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.util.ArrayList;
|
---|
7 | import java.util.Collection;
|
---|
8 | import java.util.Collections;
|
---|
9 | import java.util.HashMap;
|
---|
10 | import java.util.HashSet;
|
---|
11 | import java.util.LinkedHashMap;
|
---|
12 | import java.util.List;
|
---|
13 | import java.util.Locale;
|
---|
14 | import java.util.Map;
|
---|
15 | import java.util.Set;
|
---|
16 | import java.util.function.Supplier;
|
---|
17 | import java.util.regex.Matcher;
|
---|
18 | import java.util.regex.Pattern;
|
---|
19 | import java.util.stream.Collectors;
|
---|
20 |
|
---|
21 | import org.openstreetmap.josm.data.projection.datum.Datum;
|
---|
22 | import org.openstreetmap.josm.data.projection.datum.GRS80Datum;
|
---|
23 | import org.openstreetmap.josm.data.projection.datum.NTV2GridShiftFileWrapper;
|
---|
24 | import org.openstreetmap.josm.data.projection.datum.SevenParameterDatum;
|
---|
25 | import org.openstreetmap.josm.data.projection.datum.ThreeParameterDatum;
|
---|
26 | import org.openstreetmap.josm.data.projection.datum.WGS84Datum;
|
---|
27 | import org.openstreetmap.josm.data.projection.proj.AlbersEqualArea;
|
---|
28 | import org.openstreetmap.josm.data.projection.proj.AzimuthalEquidistant;
|
---|
29 | import org.openstreetmap.josm.data.projection.proj.CassiniSoldner;
|
---|
30 | import org.openstreetmap.josm.data.projection.proj.ClassProjFactory;
|
---|
31 | import org.openstreetmap.josm.data.projection.proj.DoubleStereographic;
|
---|
32 | import org.openstreetmap.josm.data.projection.proj.EquidistantCylindrical;
|
---|
33 | import org.openstreetmap.josm.data.projection.proj.LambertAzimuthalEqualArea;
|
---|
34 | import org.openstreetmap.josm.data.projection.proj.LambertConformalConic;
|
---|
35 | import org.openstreetmap.josm.data.projection.proj.LonLat;
|
---|
36 | import org.openstreetmap.josm.data.projection.proj.Mercator;
|
---|
37 | import org.openstreetmap.josm.data.projection.proj.ObliqueMercator;
|
---|
38 | import org.openstreetmap.josm.data.projection.proj.PolarStereographic;
|
---|
39 | import org.openstreetmap.josm.data.projection.proj.Proj;
|
---|
40 | import org.openstreetmap.josm.data.projection.proj.ProjFactory;
|
---|
41 | import org.openstreetmap.josm.data.projection.proj.Sinusoidal;
|
---|
42 | import org.openstreetmap.josm.data.projection.proj.SwissObliqueMercator;
|
---|
43 | import org.openstreetmap.josm.data.projection.proj.TransverseMercator;
|
---|
44 | import org.openstreetmap.josm.io.CachedFile;
|
---|
45 | import org.openstreetmap.josm.tools.JosmRuntimeException;
|
---|
46 | import org.openstreetmap.josm.tools.Logging;
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Class to manage projections.
|
---|
50 | *
|
---|
51 | * Use this class to query available projection or register new projections from a plugin.
|
---|
52 | */
|
---|
53 | public 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.intern();
|
---|
80 | this.name = name.intern();
|
---|
81 | this.definition = definition.intern();
|
---|
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> projectionsByCodeCache = 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 | return projectionsByCodeCache.computeIfAbsent(code, Projections::computeProjectionByCode);
|
---|
364 | }
|
---|
365 |
|
---|
366 | private static Projection computeProjectionByCode(String code) {
|
---|
367 | Projection proj = null;
|
---|
368 | ProjectionDefinition pd = inits.get(code);
|
---|
369 | if (pd != null) {
|
---|
370 | CustomProjection cproj = new CustomProjection(pd.name, code, null);
|
---|
371 | try {
|
---|
372 | cproj.update(pd.definition);
|
---|
373 | } catch (ProjectionConfigurationException ex) {
|
---|
374 | throw new JosmRuntimeException("Error loading " + code, ex);
|
---|
375 | }
|
---|
376 | proj = cproj;
|
---|
377 | }
|
---|
378 | if (proj == null) {
|
---|
379 | Supplier<Projection> ps = projectionSuppliersByCode.get(code);
|
---|
380 | if (ps != null) {
|
---|
381 | proj = ps.get();
|
---|
382 | }
|
---|
383 | }
|
---|
384 | return proj;
|
---|
385 | }
|
---|
386 |
|
---|
387 | /**
|
---|
388 | * Get a list of all supported projection codes.
|
---|
389 | *
|
---|
390 | * @return all supported projection codes
|
---|
391 | * @see #getProjectionByCode(java.lang.String)
|
---|
392 | */
|
---|
393 | public static Collection<String> getAllProjectionCodes() {
|
---|
394 | return Collections.unmodifiableCollection(allCodes);
|
---|
395 | }
|
---|
396 |
|
---|
397 | /**
|
---|
398 | * Get a list of ids of all registered base projections.
|
---|
399 | *
|
---|
400 | * @return all registered base projection ids
|
---|
401 | * @see #getBaseProjection(java.lang.String)
|
---|
402 | */
|
---|
403 | public static Collection<String> getAllBaseProjectionIds() {
|
---|
404 | return projs.keySet();
|
---|
405 | }
|
---|
406 |
|
---|
407 | private static String listKeys(Map<String, ?> map) {
|
---|
408 | return map.keySet().stream()
|
---|
409 | .sorted()
|
---|
410 | .collect(Collectors.joining(", "));
|
---|
411 | }
|
---|
412 |
|
---|
413 | /**
|
---|
414 | * Replies the list of projections as string (comma separated).
|
---|
415 | * @return the list of projections as string (comma separated)
|
---|
416 | * @since 8533
|
---|
417 | */
|
---|
418 | public static String listProjs() {
|
---|
419 | return listKeys(projs);
|
---|
420 | }
|
---|
421 |
|
---|
422 | /**
|
---|
423 | * Replies the list of ellipsoids as string (comma separated).
|
---|
424 | * @return the list of ellipsoids as string (comma separated)
|
---|
425 | * @since 8533
|
---|
426 | */
|
---|
427 | public static String listEllipsoids() {
|
---|
428 | return listKeys(ellipsoids);
|
---|
429 | }
|
---|
430 |
|
---|
431 | /**
|
---|
432 | * Replies the list of datums as string (comma separated).
|
---|
433 | * @return the list of datums as string (comma separated)
|
---|
434 | * @since 8533
|
---|
435 | */
|
---|
436 | public static String listDatums() {
|
---|
437 | return listKeys(datums);
|
---|
438 | }
|
---|
439 |
|
---|
440 | /**
|
---|
441 | * Replies the list of nadgrids as string (comma separated).
|
---|
442 | * @return the list of nadgrids as string (comma separated)
|
---|
443 | * @since 8533
|
---|
444 | */
|
---|
445 | public static String listNadgrids() {
|
---|
446 | return listKeys(nadgrids);
|
---|
447 | }
|
---|
448 | }
|
---|