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

Last change on this file since 9532 was 9532, checked in by bastiK, 10 years ago

see #12186 - add Oblique Mercator projection
(imports pieces of code from the Geotools project)

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