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

Last change on this file since 9117 was 9108, checked in by Don-vip, 8 years ago

checkstyle

  • Property svn:eol-style set to native
File size: 11.4 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.List;
15import java.util.Locale;
16import java.util.Map;
17import java.util.Set;
18import java.util.regex.Matcher;
19import java.util.regex.Pattern;
20
21import org.openstreetmap.josm.Main;
22import org.openstreetmap.josm.data.coor.EastNorth;
23import org.openstreetmap.josm.data.coor.LatLon;
24import org.openstreetmap.josm.data.projection.datum.Datum;
25import org.openstreetmap.josm.data.projection.datum.GRS80Datum;
26import org.openstreetmap.josm.data.projection.datum.NTV2GridShiftFileWrapper;
27import org.openstreetmap.josm.data.projection.datum.SevenParameterDatum;
28import org.openstreetmap.josm.data.projection.datum.ThreeParameterDatum;
29import org.openstreetmap.josm.data.projection.datum.WGS84Datum;
30import org.openstreetmap.josm.data.projection.proj.ClassProjFactory;
31import org.openstreetmap.josm.data.projection.proj.DoubleStereographic;
32import org.openstreetmap.josm.data.projection.proj.LambertConformalConic;
33import org.openstreetmap.josm.data.projection.proj.LonLat;
34import org.openstreetmap.josm.data.projection.proj.Mercator;
35import org.openstreetmap.josm.data.projection.proj.Proj;
36import org.openstreetmap.josm.data.projection.proj.ProjFactory;
37import org.openstreetmap.josm.data.projection.proj.SwissObliqueMercator;
38import org.openstreetmap.josm.data.projection.proj.TransverseMercator;
39import org.openstreetmap.josm.gui.preferences.projection.ProjectionChoice;
40import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
41import org.openstreetmap.josm.io.CachedFile;
42import org.openstreetmap.josm.tools.Pair;
43import org.openstreetmap.josm.tools.Utils;
44
45/**
46 * Class to handle projections
47 *
48 */
49public final class Projections {
50
51 private Projections() {
52 // Hide default constructor for utils classes
53 }
54
55 public static EastNorth project(LatLon ll) {
56 if (ll == null) return null;
57 return Main.getProjection().latlon2eastNorth(ll);
58 }
59
60 public static LatLon inverseProject(EastNorth en) {
61 if (en == null) return null;
62 return Main.getProjection().eastNorth2latlon(en);
63 }
64
65 /*********************************
66 * Registry for custom projection
67 *
68 * should be compatible to PROJ.4
69 */
70 static final Map<String, ProjFactory> projs = new HashMap<>();
71 static final Map<String, Ellipsoid> ellipsoids = new HashMap<>();
72 static final Map<String, Datum> datums = new HashMap<>();
73 static final Map<String, NTV2GridShiftFileWrapper> nadgrids = new HashMap<>();
74 static final Map<String, Pair<String, String>> inits = new HashMap<>();
75
76 static {
77 registerBaseProjection("lonlat", LonLat.class, "core");
78 registerBaseProjection("josm:smerc", Mercator.class, "core");
79 registerBaseProjection("lcc", LambertConformalConic.class, "core");
80 registerBaseProjection("somerc", SwissObliqueMercator.class, "core");
81 registerBaseProjection("tmerc", TransverseMercator.class, "core");
82 registerBaseProjection("sterea", DoubleStereographic.class, "core");
83
84 ellipsoids.put("airy", Ellipsoid.Airy);
85 ellipsoids.put("mod_airy", Ellipsoid.AiryMod);
86 ellipsoids.put("aust_SA", Ellipsoid.AustSA);
87 ellipsoids.put("bessel", Ellipsoid.Bessel1841);
88 ellipsoids.put("bess_nam", Ellipsoid.BesselNamibia);
89 ellipsoids.put("clrk66", Ellipsoid.Clarke1866);
90 ellipsoids.put("clrk80", Ellipsoid.Clarke1880);
91 ellipsoids.put("clarkeIGN", Ellipsoid.ClarkeIGN);
92 ellipsoids.put("evrstSS", Ellipsoid.EverestSabahSarawak);
93 ellipsoids.put("intl", Ellipsoid.Hayford);
94 ellipsoids.put("helmert", Ellipsoid.Helmert);
95 ellipsoids.put("krass", Ellipsoid.Krassowsky);
96 ellipsoids.put("GRS67", Ellipsoid.GRS67);
97 ellipsoids.put("GRS80", Ellipsoid.GRS80);
98 ellipsoids.put("WGS66", Ellipsoid.WGS66);
99 ellipsoids.put("WGS72", Ellipsoid.WGS72);
100 ellipsoids.put("WGS84", Ellipsoid.WGS84);
101
102 datums.put("WGS84", WGS84Datum.INSTANCE);
103 datums.put("GRS80", GRS80Datum.INSTANCE);
104 datums.put("NAD83", GRS80Datum.INSTANCE);
105 datums.put("carthage", new ThreeParameterDatum(
106 "Carthage 1934 Tunisia", "carthage",
107 Ellipsoid.Clarke1880, -263.0, 6.0, 431.0));
108 datums.put("GGRS87", new ThreeParameterDatum(
109 "Greek Geodetic Reference System 1987", "GGRS87",
110 Ellipsoid.GRS80, -199.87, 74.79, 246.62));
111 datums.put("hermannskogel", new ThreeParameterDatum(
112 "Hermannskogel", "hermannskogel",
113 Ellipsoid.Bessel1841, 653.0, -212.0, 449.0));
114 datums.put("ire65", new SevenParameterDatum(
115 "Ireland 1965", "ire65",
116 Ellipsoid.AiryMod, 482.530, -130.596, 564.557, -1.042, -0.214, -0.631, 8.15));
117 datums.put("nzgd49", new SevenParameterDatum(
118 "New Zealand Geodetic Datum 1949", "nzgd49",
119 Ellipsoid.Hayford, 59.47, -5.04, 187.44, 0.47, -0.1, 1.024, -4.5993));
120 datums.put("OSGB36", new SevenParameterDatum(
121 "Airy 1830", "OSGB36",
122 Ellipsoid.Airy, 446.448, -125.157, 542.060, 0.1502, 0.2470, 0.8421, -20.4894));
123 datums.put("potsdam", new SevenParameterDatum(
124 "Potsdam Rauenberg 1950 DHDN", "potsdam",
125 Ellipsoid.Bessel1841, 598.1, 73.7, 418.2, 0.202, 0.045, -2.455, 6.7));
126
127 nadgrids.put("BETA2007.gsb", NTV2GridShiftFileWrapper.BETA2007);
128 nadgrids.put("ntf_r93_b.gsb", NTV2GridShiftFileWrapper.ntf_rgf93);
129
130 loadInits();
131 }
132
133 /**
134 * Plugins can register additional base projections.
135 *
136 * @param id The "official" PROJ.4 id. In case the projection is not supported
137 * by PROJ.4, use some prefix, e.g. josm:myproj or gdal:otherproj.
138 * @param fac The base projection factory.
139 * @param origin Multiple plugins may implement the same base projection.
140 * Provide plugin name or similar string, so it be differentiated.
141 */
142 public static void registerBaseProjection(String id, ProjFactory fac, String origin) {
143 projs.put(id, fac);
144 }
145
146 public static void registerBaseProjection(String id, Class<? extends Proj> projClass, String origin) {
147 registerBaseProjection(id, new ClassProjFactory(projClass), origin);
148 }
149
150 public static Proj getBaseProjection(String id) {
151 ProjFactory fac = projs.get(id);
152 if (fac == null) return null;
153 return fac.createInstance();
154 }
155
156 public static Ellipsoid getEllipsoid(String id) {
157 return ellipsoids.get(id);
158 }
159
160 public static Datum getDatum(String id) {
161 return datums.get(id);
162 }
163
164 public static NTV2GridShiftFileWrapper getNTV2Grid(String id) {
165 return nadgrids.get(id);
166 }
167
168 /**
169 * Get the projection definition string for the given id.
170 * @param id the id
171 * @return the string that can be processed by #{link CustomProjection}.
172 * Null, if the id isn't supported.
173 */
174 public static String getInit(String id) {
175 Pair<String, String> r = inits.get(id.toUpperCase(Locale.ENGLISH));
176 if (r == null) return null;
177 return r.b;
178 }
179
180 /**
181 * Load +init "presets" from file
182 */
183 private static void loadInits() {
184 Pattern epsgPattern = Pattern.compile("<(\\d+)>(.*)<>");
185 try (
186 InputStream in = new CachedFile("resource://data/projection/epsg").getInputStream();
187 BufferedReader r = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
188 ) {
189 String line, lastline = "";
190 while ((line = r.readLine()) != null) {
191 line = line.trim();
192 if (!line.startsWith("#") && !line.isEmpty()) {
193 if (!lastline.startsWith("#")) throw new AssertionError("EPSG file seems corrupted");
194 String name = lastline.substring(1).trim();
195 Matcher m = epsgPattern.matcher(line);
196 if (m.matches()) {
197 inits.put("EPSG:" + m.group(1), Pair.create(name, m.group(2).trim()));
198 } else {
199 Main.warn("Failed to parse line from the EPSG projection definition: "+line);
200 }
201 }
202 lastline = line;
203 }
204 } catch (IOException ex) {
205 throw new RuntimeException(ex);
206 }
207 }
208
209 private static final Set<String> allCodes = new HashSet<>();
210 private static final Map<String, ProjectionChoice> allProjectionChoicesByCode = new HashMap<>();
211 private static final Map<String, Projection> projectionsByCode_cache = new HashMap<>();
212
213 static {
214 for (ProjectionChoice pc : ProjectionPreference.getProjectionChoices()) {
215 for (String code : pc.allCodes()) {
216 allProjectionChoicesByCode.put(code, pc);
217 }
218 }
219 allCodes.addAll(inits.keySet());
220 allCodes.addAll(allProjectionChoicesByCode.keySet());
221 }
222
223 public static Projection getProjectionByCode(String code) {
224 Projection proj = projectionsByCode_cache.get(code);
225 if (proj != null) return proj;
226 ProjectionChoice pc = allProjectionChoicesByCode.get(code);
227 if (pc != null) {
228 Collection<String> pref = pc.getPreferencesFromCode(code);
229 pc.setPreferences(pref);
230 try {
231 proj = pc.getProjection();
232 } catch (Exception e) {
233 String cause = e.getMessage();
234 Main.warn("Unable to get projection "+code+" with "+pc + (cause != null ? ". "+cause : ""));
235 }
236 }
237 if (proj == null) {
238 Pair<String, String> pair = inits.get(code);
239 if (pair == null) return null;
240 String name = pair.a;
241 String init = pair.b;
242 proj = new CustomProjection(name, code, init, null);
243 }
244 projectionsByCode_cache.put(code, proj);
245 return proj;
246 }
247
248 public static Collection<String> getAllProjectionCodes() {
249 return Collections.unmodifiableCollection(allCodes);
250 }
251
252 private static String listKeys(Map<String, ?> map) {
253 List<String> keys = new ArrayList<>(map.keySet());
254 Collections.sort(keys);
255 return Utils.join(", ", keys);
256 }
257
258 /**
259 * Replies the list of projections as string (comma separated).
260 * @return the list of projections as string (comma separated)
261 * @since 8533
262 */
263 public static String listProjs() {
264 return listKeys(projs);
265 }
266
267 /**
268 * Replies the list of ellipsoids as string (comma separated).
269 * @return the list of ellipsoids as string (comma separated)
270 * @since 8533
271 */
272 public static String listEllipsoids() {
273 return listKeys(ellipsoids);
274 }
275
276 /**
277 * Replies the list of datums as string (comma separated).
278 * @return the list of datums as string (comma separated)
279 * @since 8533
280 */
281 public static String listDatums() {
282 return listKeys(datums);
283 }
284
285 /**
286 * Replies the list of nadgrids as string (comma separated).
287 * @return the list of nadgrids as string (comma separated)
288 * @since 8533
289 */
290 public static String listNadgrids() {
291 return listKeys(nadgrids);
292 }
293}
Note: See TracBrowser for help on using the repository browser.