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

Last change on this file since 8512 was 8451, checked in by Don-vip, 9 years ago

fix #11512 - Add most commonly used ellipsoids (patch by BathoryPeter)

  • Property svn:eol-style set to native
File size: 8.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.Collection;
10import java.util.Collections;
11import java.util.HashMap;
12import java.util.HashSet;
13import java.util.Locale;
14import java.util.Map;
15import java.util.Set;
16import java.util.regex.Matcher;
17import java.util.regex.Pattern;
18
19import org.openstreetmap.josm.Main;
20import org.openstreetmap.josm.data.coor.EastNorth;
21import org.openstreetmap.josm.data.coor.LatLon;
22import org.openstreetmap.josm.data.projection.datum.Datum;
23import org.openstreetmap.josm.data.projection.datum.GRS80Datum;
24import org.openstreetmap.josm.data.projection.datum.NTV2GridShiftFileWrapper;
25import org.openstreetmap.josm.data.projection.datum.WGS84Datum;
26import org.openstreetmap.josm.data.projection.proj.ClassProjFactory;
27import org.openstreetmap.josm.data.projection.proj.LambertConformalConic;
28import org.openstreetmap.josm.data.projection.proj.LonLat;
29import org.openstreetmap.josm.data.projection.proj.Mercator;
30import org.openstreetmap.josm.data.projection.proj.Proj;
31import org.openstreetmap.josm.data.projection.proj.ProjFactory;
32import org.openstreetmap.josm.data.projection.proj.SwissObliqueMercator;
33import org.openstreetmap.josm.data.projection.proj.TransverseMercator;
34import org.openstreetmap.josm.gui.preferences.projection.ProjectionChoice;
35import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
36import org.openstreetmap.josm.io.CachedFile;
37import org.openstreetmap.josm.tools.Pair;
38
39/**
40 * Class to handle projections
41 *
42 */
43public final class Projections {
44
45 private Projections() {
46 // Hide default constructor for utils classes
47 }
48
49 public static EastNorth project(LatLon ll) {
50 if (ll == null) return null;
51 return Main.getProjection().latlon2eastNorth(ll);
52 }
53
54 public static LatLon inverseProject(EastNorth en) {
55 if (en == null) return null;
56 return Main.getProjection().eastNorth2latlon(en);
57 }
58
59 /*********************************
60 * Registry for custom projection
61 *
62 * should be compatible to PROJ.4
63 */
64 public static final Map<String, ProjFactory> projs = new HashMap<>();
65 public static final Map<String, Ellipsoid> ellipsoids = new HashMap<>();
66 public static final Map<String, Datum> datums = new HashMap<>();
67 public static final Map<String, NTV2GridShiftFileWrapper> nadgrids = new HashMap<>();
68 public static final Map<String, Pair<String, String>> inits = new HashMap<>();
69
70 static {
71 registerBaseProjection("lonlat", LonLat.class, "core");
72 registerBaseProjection("josm:smerc", Mercator.class, "core");
73 registerBaseProjection("lcc", LambertConformalConic.class, "core");
74 registerBaseProjection("somerc", SwissObliqueMercator.class, "core");
75 registerBaseProjection("tmerc", TransverseMercator.class, "core");
76
77 ellipsoids.put("airy", Ellipsoid.Airy);
78 ellipsoids.put("mod_airy", Ellipsoid.AiryMod);
79 ellipsoids.put("aust_SA", Ellipsoid.AustSA);
80 ellipsoids.put("bessel", Ellipsoid.Bessel1841);
81 ellipsoids.put("clrk66", Ellipsoid.Clarke1866);
82 ellipsoids.put("clarkeIGN", Ellipsoid.ClarkeIGN);
83 ellipsoids.put("intl", Ellipsoid.Hayford);
84 ellipsoids.put("helmert", Ellipsoid.Helmert);
85 ellipsoids.put("krass", Ellipsoid.Krassowsky);
86 ellipsoids.put("GRS67", Ellipsoid.GRS67);
87 ellipsoids.put("GRS80", Ellipsoid.GRS80);
88 ellipsoids.put("WGS72", Ellipsoid.WGS72);
89 ellipsoids.put("WGS84", Ellipsoid.WGS84);
90
91 datums.put("WGS84", WGS84Datum.INSTANCE);
92 datums.put("GRS80", GRS80Datum.INSTANCE);
93
94 nadgrids.put("BETA2007.gsb", NTV2GridShiftFileWrapper.BETA2007);
95 nadgrids.put("ntf_r93_b.gsb", NTV2GridShiftFileWrapper.ntf_rgf93);
96
97 loadInits();
98 }
99
100 /**
101 * Plugins can register additional base projections.
102 *
103 * @param id The "official" PROJ.4 id. In case the projection is not supported
104 * by PROJ.4, use some prefix, e.g. josm:myproj or gdal:otherproj.
105 * @param fac The base projection factory.
106 * @param origin Multiple plugins may implement the same base projection.
107 * Provide plugin name or similar string, so it be differentiated.
108 */
109 public static void registerBaseProjection(String id, ProjFactory fac, String origin) {
110 projs.put(id, fac);
111 }
112
113 public static void registerBaseProjection(String id, Class<? extends Proj> projClass, String origin) {
114 registerBaseProjection(id, new ClassProjFactory(projClass), origin);
115 }
116
117 public static Proj getBaseProjection(String id) {
118 ProjFactory fac = projs.get(id);
119 if (fac == null) return null;
120 return fac.createInstance();
121 }
122
123 public static Ellipsoid getEllipsoid(String id) {
124 return ellipsoids.get(id);
125 }
126
127 public static Datum getDatum(String id) {
128 return datums.get(id);
129 }
130
131 public static NTV2GridShiftFileWrapper getNTV2Grid(String id) {
132 return nadgrids.get(id);
133 }
134
135 /**
136 * Get the projection definition string for the given id.
137 * @param id the id
138 * @return the string that can be processed by #{link CustomProjection}.
139 * Null, if the id isn't supported.
140 */
141 public static String getInit(String id) {
142 Pair<String, String> r = inits.get(id.toUpperCase(Locale.ENGLISH));
143 if (r == null) return null;
144 return r.b;
145 }
146
147 /**
148 * Load +init "presets" from file
149 */
150 private static void loadInits() {
151 Pattern epsgPattern = Pattern.compile("<(\\d+)>(.*)<>");
152 try (
153 InputStream in = new CachedFile("resource://data/projection/epsg").getInputStream();
154 BufferedReader r = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
155 ) {
156 String line, lastline = "";
157 while ((line = r.readLine()) != null) {
158 line = line.trim();
159 if (!line.startsWith("#") && !line.isEmpty()) {
160 if (!lastline.startsWith("#")) throw new AssertionError("EPSG file seems corrupted");
161 String name = lastline.substring(1).trim();
162 Matcher m = epsgPattern.matcher(line);
163 if (m.matches()) {
164 inits.put("EPSG:" + m.group(1), Pair.create(name, m.group(2).trim()));
165 } else {
166 Main.warn("Failed to parse line from the EPSG projection definition: "+line);
167 }
168 }
169 lastline = line;
170 }
171 } catch (IOException ex) {
172 throw new RuntimeException(ex);
173 }
174 }
175
176 private static final Set<String> allCodes = new HashSet<>();
177 private static final Map<String, ProjectionChoice> allProjectionChoicesByCode = new HashMap<>();
178 private static final Map<String, Projection> projectionsByCode_cache = new HashMap<>();
179
180 static {
181 for (ProjectionChoice pc : ProjectionPreference.getProjectionChoices()) {
182 for (String code : pc.allCodes()) {
183 allProjectionChoicesByCode.put(code, pc);
184 }
185 }
186 allCodes.addAll(inits.keySet());
187 allCodes.addAll(allProjectionChoicesByCode.keySet());
188 }
189
190 public static Projection getProjectionByCode(String code) {
191 Projection proj = projectionsByCode_cache.get(code);
192 if (proj != null) return proj;
193 ProjectionChoice pc = allProjectionChoicesByCode.get(code);
194 if (pc != null) {
195 Collection<String> pref = pc.getPreferencesFromCode(code);
196 pc.setPreferences(pref);
197 try {
198 proj = pc.getProjection();
199 } catch (Exception e) {
200 String cause = e.getMessage();
201 Main.warn("Unable to get projection "+code+" with "+pc + (cause != null ? ". "+cause : ""));
202 }
203 }
204 if (proj == null) {
205 Pair<String, String> pair = inits.get(code);
206 if (pair == null) return null;
207 String name = pair.a;
208 String init = pair.b;
209 proj = new CustomProjection(name, code, init, null);
210 }
211 projectionsByCode_cache.put(code, proj);
212 return proj;
213 }
214
215 public static Collection<String> getAllProjectionCodes() {
216 return Collections.unmodifiableCollection(allCodes);
217 }
218}
Note: See TracBrowser for help on using the repository browser.