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

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

add Albers Equal Area Projection and Polar Stereographic Projection (see #12186)
(imports pieces of code from the Geotools project)

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