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

Last change on this file since 9549 was 9549, checked in by bastiK, 8 years ago

see #12186 - add Cassini-Soldner Projection
(imports pieces of code from the Geotools project)

  • Property svn:eol-style set to native
File size: 15.1 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.CassiniSoldner;
33import org.openstreetmap.josm.data.projection.proj.ClassProjFactory;
34import org.openstreetmap.josm.data.projection.proj.DoubleStereographic;
35import org.openstreetmap.josm.data.projection.proj.LambertConformalConic;
36import org.openstreetmap.josm.data.projection.proj.LonLat;
37import org.openstreetmap.josm.data.projection.proj.Mercator;
38import org.openstreetmap.josm.data.projection.proj.ObliqueMercator;
39import org.openstreetmap.josm.data.projection.proj.PolarStereographic;
40import org.openstreetmap.josm.data.projection.proj.Proj;
41import org.openstreetmap.josm.data.projection.proj.ProjFactory;
42import org.openstreetmap.josm.data.projection.proj.SwissObliqueMercator;
43import org.openstreetmap.josm.data.projection.proj.TransverseMercator;
44import org.openstreetmap.josm.gui.preferences.projection.ProjectionChoice;
45import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
46import org.openstreetmap.josm.io.CachedFile;
47import org.openstreetmap.josm.tools.Utils;
48
49/**
50 * Class to manage projections.
51 *
52 * Use this class to query available projection or register new projections
53 * from a plugin.
54 */
55public final class Projections {
56
57 /**
58 * Class to hold information about one projection.
59 */
60 public static class ProjectionDefinition {
61 public String code;
62 public String name;
63 public String definition;
64
65 public ProjectionDefinition(String code, String name, String definition) {
66 this.code = code;
67 this.name = name;
68 this.definition = definition;
69 }
70 }
71
72 private static final Set<String> allCodes = new HashSet<>();
73 private static final Map<String, ProjectionChoice> allProjectionChoicesByCode = new HashMap<>();
74 private static final Map<String, Projection> projectionsByCode_cache = new HashMap<>();
75
76 /*********************************
77 * Registry for custom projection
78 *
79 * should be compatible to PROJ.4
80 */
81 static final Map<String, ProjFactory> projs = new HashMap<>();
82 static final Map<String, Ellipsoid> ellipsoids = new HashMap<>();
83 static final Map<String, Datum> datums = new HashMap<>();
84 static final Map<String, NTV2GridShiftFileWrapper> nadgrids = new HashMap<>();
85 static final Map<String, ProjectionDefinition> inits;
86
87 static {
88 registerBaseProjection("aea", AlbersEqualArea.class, "core");
89 registerBaseProjection("cass", CassiniSoldner.class, "core");
90 registerBaseProjection("josm:smerc", Mercator.class, "core");
91 registerBaseProjection("lcc", LambertConformalConic.class, "core");
92 registerBaseProjection("lonlat", LonLat.class, "core");
93 registerBaseProjection("omerc", ObliqueMercator.class, "core");
94 registerBaseProjection("somerc", SwissObliqueMercator.class, "core");
95 registerBaseProjection("stere", PolarStereographic.class, "core");
96 registerBaseProjection("sterea", DoubleStereographic.class, "core");
97 registerBaseProjection("tmerc", TransverseMercator.class, "core");
98
99 ellipsoids.put("airy", Ellipsoid.Airy);
100 ellipsoids.put("mod_airy", Ellipsoid.AiryMod);
101 ellipsoids.put("aust_SA", Ellipsoid.AustSA);
102 ellipsoids.put("bessel", Ellipsoid.Bessel1841);
103 ellipsoids.put("bess_nam", Ellipsoid.BesselNamibia);
104 ellipsoids.put("clrk66", Ellipsoid.Clarke1866);
105 ellipsoids.put("clrk80", Ellipsoid.Clarke1880);
106 ellipsoids.put("clarkeIGN", Ellipsoid.ClarkeIGN);
107 ellipsoids.put("evrstSS", Ellipsoid.EverestSabahSarawak);
108 ellipsoids.put("intl", Ellipsoid.Hayford);
109 ellipsoids.put("helmert", Ellipsoid.Helmert);
110 ellipsoids.put("krass", Ellipsoid.Krassowsky);
111 ellipsoids.put("GRS67", Ellipsoid.GRS67);
112 ellipsoids.put("GRS80", Ellipsoid.GRS80);
113 ellipsoids.put("WGS66", Ellipsoid.WGS66);
114 ellipsoids.put("WGS72", Ellipsoid.WGS72);
115 ellipsoids.put("WGS84", Ellipsoid.WGS84);
116
117 datums.put("WGS84", WGS84Datum.INSTANCE);
118 datums.put("GRS80", GRS80Datum.INSTANCE);
119 datums.put("NAD83", GRS80Datum.INSTANCE);
120 datums.put("carthage", new ThreeParameterDatum(
121 "Carthage 1934 Tunisia", "carthage",
122 Ellipsoid.Clarke1880, -263.0, 6.0, 431.0));
123 datums.put("GGRS87", new ThreeParameterDatum(
124 "Greek Geodetic Reference System 1987", "GGRS87",
125 Ellipsoid.GRS80, -199.87, 74.79, 246.62));
126 datums.put("hermannskogel", new ThreeParameterDatum(
127 "Hermannskogel", "hermannskogel",
128 Ellipsoid.Bessel1841, 653.0, -212.0, 449.0));
129 datums.put("ire65", new SevenParameterDatum(
130 "Ireland 1965", "ire65",
131 Ellipsoid.AiryMod, 482.530, -130.596, 564.557, -1.042, -0.214, -0.631, 8.15));
132 datums.put("nzgd49", new SevenParameterDatum(
133 "New Zealand Geodetic Datum 1949", "nzgd49",
134 Ellipsoid.Hayford, 59.47, -5.04, 187.44, 0.47, -0.1, 1.024, -4.5993));
135 datums.put("OSGB36", new SevenParameterDatum(
136 "Airy 1830", "OSGB36",
137 Ellipsoid.Airy, 446.448, -125.157, 542.060, 0.1502, 0.2470, 0.8421, -20.4894));
138 datums.put("potsdam", new SevenParameterDatum(
139 "Potsdam Rauenberg 1950 DHDN", "potsdam",
140 Ellipsoid.Bessel1841, 598.1, 73.7, 418.2, 0.202, 0.045, -2.455, 6.7));
141
142 nadgrids.put("BETA2007.gsb", NTV2GridShiftFileWrapper.BETA2007);
143 nadgrids.put("ntf_r93_b.gsb", NTV2GridShiftFileWrapper.ntf_rgf93);
144
145 List<ProjectionDefinition> pds;
146 try {
147 pds = loadProjectionDefinitions("resource://data/projection/custom-epsg");
148 } catch (IOException ex) {
149 throw new RuntimeException(ex);
150 }
151 inits = new LinkedHashMap<>();
152 for (ProjectionDefinition pd : pds) {
153 inits.put(pd.code, pd);
154 }
155
156 for (ProjectionChoice pc : ProjectionPreference.getProjectionChoices()) {
157 for (String code : pc.allCodes()) {
158 allProjectionChoicesByCode.put(code, pc);
159 }
160 }
161 allCodes.addAll(inits.keySet());
162 allCodes.addAll(allProjectionChoicesByCode.keySet());
163 }
164
165 private Projections() {
166 // Hide default constructor for utils classes
167 }
168
169 /**
170 * Convert from lat/lon to easting/northing using the current projection.
171 *
172 * @param ll the geographical point to convert (in WGS84 lat/lon)
173 * @return the corresponding east/north coordinates
174 */
175 public static EastNorth project(LatLon ll) {
176 if (ll == null) return null;
177 return Main.getProjection().latlon2eastNorth(ll);
178 }
179
180 /**
181 * Convert from easting/norting to lat/lon using the current projection.
182 *
183 * @param en the geographical point to convert (in projected coordinates)
184 * @return the corresponding lat/lon (WGS84)
185 */
186 public static LatLon inverseProject(EastNorth en) {
187 if (en == null) return null;
188 return Main.getProjection().eastNorth2latlon(en);
189 }
190
191 /**
192 * Plugins can register additional base projections.
193 *
194 * @param id The "official" PROJ.4 id. In case the projection is not supported
195 * by PROJ.4, use some prefix, e.g. josm:myproj or gdal:otherproj.
196 * @param fac The base projection factory.
197 * @param origin Multiple plugins may implement the same base projection.
198 * Provide plugin name or similar string, so it be differentiated.
199 */
200 public static void registerBaseProjection(String id, ProjFactory fac, String origin) {
201 projs.put(id, fac);
202 }
203
204 public static void registerBaseProjection(String id, Class<? extends Proj> projClass, String origin) {
205 registerBaseProjection(id, new ClassProjFactory(projClass), origin);
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 InputStream in = new CachedFile(path).getInputStream();
271 BufferedReader r = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
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 Main.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 ProjectionChoice pc = allProjectionChoicesByCode.get(code);
316 if (pc != null) {
317 Collection<String> pref = pc.getPreferencesFromCode(code);
318 pc.setPreferences(pref);
319 try {
320 proj = pc.getProjection();
321 } catch (Exception e) {
322 String cause = e.getMessage();
323 Main.warn("Unable to get projection "+code+" with "+pc + (cause != null ? ". "+cause : ""));
324 }
325 }
326 if (proj == null) {
327 ProjectionDefinition pd = inits.get(code);
328 if (pd == null) return null;
329 proj = new CustomProjection(pd.name, code, pd.definition, null);
330 }
331 projectionsByCode_cache.put(code, proj);
332 return proj;
333 }
334
335 /**
336 * Get a list of all supported projection codes.
337 *
338 * @return all supported projection codes
339 * @see #getProjectionByCode(java.lang.String)
340 */
341 public static Collection<String> getAllProjectionCodes() {
342 return Collections.unmodifiableCollection(allCodes);
343 }
344
345 /**
346 * Get a list of ids of all registered base projections.
347 *
348 * @return all registered base projection ids
349 * @see #getBaseProjection(java.lang.String)
350 */
351 public static Collection<String> getAllBaseProjectionIds() {
352 return projs.keySet();
353 }
354
355 private static String listKeys(Map<String, ?> map) {
356 List<String> keys = new ArrayList<>(map.keySet());
357 Collections.sort(keys);
358 return Utils.join(", ", keys);
359 }
360
361 /**
362 * Replies the list of projections as string (comma separated).
363 * @return the list of projections as string (comma separated)
364 * @since 8533
365 */
366 public static String listProjs() {
367 return listKeys(projs);
368 }
369
370 /**
371 * Replies the list of ellipsoids as string (comma separated).
372 * @return the list of ellipsoids as string (comma separated)
373 * @since 8533
374 */
375 public static String listEllipsoids() {
376 return listKeys(ellipsoids);
377 }
378
379 /**
380 * Replies the list of datums as string (comma separated).
381 * @return the list of datums as string (comma separated)
382 * @since 8533
383 */
384 public static String listDatums() {
385 return listKeys(datums);
386 }
387
388 /**
389 * Replies the list of nadgrids as string (comma separated).
390 * @return the list of nadgrids as string (comma separated)
391 * @since 8533
392 */
393 public static String listNadgrids() {
394 return listKeys(nadgrids);
395 }
396}
Note: See TracBrowser for help on using the repository browser.