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

Last change on this file since 9100 was 9100, checked in by bastiK, 10 years ago

applied #12181 - Add Netherlands RD projection (patch by vholten)

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