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