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

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

see #12186 - rework loading of projection definitions
to make it accessible from outside

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