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

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

add 2 standard parallel & non-spherical variants for Mercator projection (see #12186)
(imports pieces of code from the Geotools project)

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