1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.projection;
|
---|
3 |
|
---|
4 | import java.io.BufferedReader;
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.io.InputStream;
|
---|
7 | import java.io.InputStreamReader;
|
---|
8 | import java.nio.charset.StandardCharsets;
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.Collections;
|
---|
12 | import java.util.HashMap;
|
---|
13 | import java.util.HashSet;
|
---|
14 | import java.util.LinkedHashMap;
|
---|
15 | import java.util.List;
|
---|
16 | import java.util.Locale;
|
---|
17 | import java.util.Map;
|
---|
18 | import java.util.Set;
|
---|
19 | import java.util.regex.Matcher;
|
---|
20 | import java.util.regex.Pattern;
|
---|
21 | import org.openstreetmap.josm.Main;
|
---|
22 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
23 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
24 | import org.openstreetmap.josm.data.projection.datum.Datum;
|
---|
25 | import org.openstreetmap.josm.data.projection.datum.GRS80Datum;
|
---|
26 | import org.openstreetmap.josm.data.projection.datum.NTV2GridShiftFileWrapper;
|
---|
27 | import org.openstreetmap.josm.data.projection.datum.SevenParameterDatum;
|
---|
28 | import org.openstreetmap.josm.data.projection.datum.ThreeParameterDatum;
|
---|
29 | import org.openstreetmap.josm.data.projection.datum.WGS84Datum;
|
---|
30 | import org.openstreetmap.josm.data.projection.proj.ClassProjFactory;
|
---|
31 | import org.openstreetmap.josm.data.projection.proj.DoubleStereographic;
|
---|
32 | import org.openstreetmap.josm.data.projection.proj.LambertConformalConic;
|
---|
33 | import org.openstreetmap.josm.data.projection.proj.LonLat;
|
---|
34 | import org.openstreetmap.josm.data.projection.proj.Mercator;
|
---|
35 | import org.openstreetmap.josm.data.projection.proj.Proj;
|
---|
36 | import org.openstreetmap.josm.data.projection.proj.ProjFactory;
|
---|
37 | import org.openstreetmap.josm.data.projection.proj.SwissObliqueMercator;
|
---|
38 | import org.openstreetmap.josm.data.projection.proj.TransverseMercator;
|
---|
39 | import org.openstreetmap.josm.gui.preferences.projection.ProjectionChoice;
|
---|
40 | import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
|
---|
41 | import org.openstreetmap.josm.io.CachedFile;
|
---|
42 | import org.openstreetmap.josm.tools.Utils;
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Class to handle projections
|
---|
46 | *
|
---|
47 | */
|
---|
48 | public 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 | List<ProjectionDefinition> pds;
|
---|
145 | try {
|
---|
146 | pds = loadProjectionDefinitions("resource://data/projection/epsg");
|
---|
147 | } catch (IOException ex) {
|
---|
148 | throw new RuntimeException(ex);
|
---|
149 | }
|
---|
150 | inits = new LinkedHashMap<>();
|
---|
151 | for (ProjectionDefinition pd : pds) {
|
---|
152 | inits.put(pd.code, pd);
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Plugins can register additional base projections.
|
---|
158 | *
|
---|
159 | * @param id The "official" PROJ.4 id. In case the projection is not supported
|
---|
160 | * by PROJ.4, use some prefix, e.g. josm:myproj or gdal:otherproj.
|
---|
161 | * @param fac The base projection factory.
|
---|
162 | * @param origin Multiple plugins may implement the same base projection.
|
---|
163 | * Provide plugin name or similar string, so it be differentiated.
|
---|
164 | */
|
---|
165 | public static void registerBaseProjection(String id, ProjFactory fac, String origin) {
|
---|
166 | projs.put(id, fac);
|
---|
167 | }
|
---|
168 |
|
---|
169 | public static void registerBaseProjection(String id, Class<? extends Proj> projClass, String origin) {
|
---|
170 | registerBaseProjection(id, new ClassProjFactory(projClass), origin);
|
---|
171 | }
|
---|
172 |
|
---|
173 | public static Proj getBaseProjection(String id) {
|
---|
174 | ProjFactory fac = projs.get(id);
|
---|
175 | if (fac == null) return null;
|
---|
176 | return fac.createInstance();
|
---|
177 | }
|
---|
178 |
|
---|
179 | public static Ellipsoid getEllipsoid(String id) {
|
---|
180 | return ellipsoids.get(id);
|
---|
181 | }
|
---|
182 |
|
---|
183 | public static Datum getDatum(String id) {
|
---|
184 | return datums.get(id);
|
---|
185 | }
|
---|
186 |
|
---|
187 | public static NTV2GridShiftFileWrapper getNTV2Grid(String id) {
|
---|
188 | return nadgrids.get(id);
|
---|
189 | }
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Get the projection definition string for the given id.
|
---|
193 | * @param id the id
|
---|
194 | * @return the string that can be processed by #{link CustomProjection}.
|
---|
195 | * Null, if the id isn't supported.
|
---|
196 | */
|
---|
197 | public static String getInit(String id) {
|
---|
198 | ProjectionDefinition pd = inits.get(id.toUpperCase(Locale.ENGLISH));
|
---|
199 | if (pd == null) return null;
|
---|
200 | return pd.definition;
|
---|
201 | }
|
---|
202 |
|
---|
203 | /**
|
---|
204 | * Load projection definitions from file.
|
---|
205 | *
|
---|
206 | * @param path the path
|
---|
207 | * @return projection definitions
|
---|
208 | * @throws java.io.IOException
|
---|
209 | */
|
---|
210 | public static List<ProjectionDefinition> loadProjectionDefinitions(String path) throws IOException {
|
---|
211 | try (
|
---|
212 | InputStream in = new CachedFile(path).getInputStream();
|
---|
213 | BufferedReader r = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
|
---|
214 | ) {
|
---|
215 | return loadProjectionDefinitions(r);
|
---|
216 | } catch (IOException ex) {
|
---|
217 | throw new RuntimeException(ex);
|
---|
218 | }
|
---|
219 | }
|
---|
220 |
|
---|
221 | /**
|
---|
222 | * Load projection definitions from file.
|
---|
223 | *
|
---|
224 | * @param r the reader
|
---|
225 | * @return projection definitions
|
---|
226 | * @throws java.io.IOException
|
---|
227 | */
|
---|
228 | public static List<ProjectionDefinition> loadProjectionDefinitions(BufferedReader r) throws IOException {
|
---|
229 | List<ProjectionDefinition> result = new ArrayList<>();
|
---|
230 | Pattern epsgPattern = Pattern.compile("<(\\d+)>(.*)<>");
|
---|
231 | String line, lastline = "";
|
---|
232 | while ((line = r.readLine()) != null) {
|
---|
233 | line = line.trim();
|
---|
234 | if (!line.startsWith("#") && !line.isEmpty()) {
|
---|
235 | if (!lastline.startsWith("#")) throw new AssertionError("EPSG file seems corrupted");
|
---|
236 | String name = lastline.substring(1).trim();
|
---|
237 | Matcher m = epsgPattern.matcher(line);
|
---|
238 | if (m.matches()) {
|
---|
239 | String code = "EPSG:" + m.group(1);
|
---|
240 | String definition = m.group(2).trim();
|
---|
241 | result.add(new ProjectionDefinition(code, name, definition));
|
---|
242 | } else {
|
---|
243 | Main.warn("Failed to parse line from the EPSG projection definition: "+line);
|
---|
244 | }
|
---|
245 | }
|
---|
246 | lastline = line;
|
---|
247 | }
|
---|
248 | return result;
|
---|
249 | }
|
---|
250 |
|
---|
251 | private static final Set<String> allCodes = new HashSet<>();
|
---|
252 | private static final Map<String, ProjectionChoice> allProjectionChoicesByCode = new HashMap<>();
|
---|
253 | private static final Map<String, Projection> projectionsByCode_cache = new HashMap<>();
|
---|
254 |
|
---|
255 | static {
|
---|
256 | for (ProjectionChoice pc : ProjectionPreference.getProjectionChoices()) {
|
---|
257 | for (String code : pc.allCodes()) {
|
---|
258 | allProjectionChoicesByCode.put(code, pc);
|
---|
259 | }
|
---|
260 | }
|
---|
261 | allCodes.addAll(inits.keySet());
|
---|
262 | allCodes.addAll(allProjectionChoicesByCode.keySet());
|
---|
263 | }
|
---|
264 |
|
---|
265 | public static Projection getProjectionByCode(String code) {
|
---|
266 | Projection proj = projectionsByCode_cache.get(code);
|
---|
267 | if (proj != null) return proj;
|
---|
268 | ProjectionChoice pc = allProjectionChoicesByCode.get(code);
|
---|
269 | if (pc != null) {
|
---|
270 | Collection<String> pref = pc.getPreferencesFromCode(code);
|
---|
271 | pc.setPreferences(pref);
|
---|
272 | try {
|
---|
273 | proj = pc.getProjection();
|
---|
274 | } catch (Exception e) {
|
---|
275 | String cause = e.getMessage();
|
---|
276 | Main.warn("Unable to get projection "+code+" with "+pc + (cause != null ? ". "+cause : ""));
|
---|
277 | }
|
---|
278 | }
|
---|
279 | if (proj == null) {
|
---|
280 | ProjectionDefinition pd = inits.get(code);
|
---|
281 | if (pd == null) return null;
|
---|
282 | proj = new CustomProjection(pd.name, code, pd.definition, null);
|
---|
283 | }
|
---|
284 | projectionsByCode_cache.put(code, proj);
|
---|
285 | return proj;
|
---|
286 | }
|
---|
287 |
|
---|
288 | public static Collection<String> getAllProjectionCodes() {
|
---|
289 | return Collections.unmodifiableCollection(allCodes);
|
---|
290 | }
|
---|
291 |
|
---|
292 | private static String listKeys(Map<String, ?> map) {
|
---|
293 | List<String> keys = new ArrayList<>(map.keySet());
|
---|
294 | Collections.sort(keys);
|
---|
295 | return Utils.join(", ", keys);
|
---|
296 | }
|
---|
297 |
|
---|
298 | /**
|
---|
299 | * Replies the list of projections as string (comma separated).
|
---|
300 | * @return the list of projections as string (comma separated)
|
---|
301 | * @since 8533
|
---|
302 | */
|
---|
303 | public static String listProjs() {
|
---|
304 | return listKeys(projs);
|
---|
305 | }
|
---|
306 |
|
---|
307 | /**
|
---|
308 | * Replies the list of ellipsoids as string (comma separated).
|
---|
309 | * @return the list of ellipsoids as string (comma separated)
|
---|
310 | * @since 8533
|
---|
311 | */
|
---|
312 | public static String listEllipsoids() {
|
---|
313 | return listKeys(ellipsoids);
|
---|
314 | }
|
---|
315 |
|
---|
316 | /**
|
---|
317 | * Replies the list of datums as string (comma separated).
|
---|
318 | * @return the list of datums as string (comma separated)
|
---|
319 | * @since 8533
|
---|
320 | */
|
---|
321 | public static String listDatums() {
|
---|
322 | return listKeys(datums);
|
---|
323 | }
|
---|
324 |
|
---|
325 | /**
|
---|
326 | * Replies the list of nadgrids as string (comma separated).
|
---|
327 | * @return the list of nadgrids as string (comma separated)
|
---|
328 | * @since 8533
|
---|
329 | */
|
---|
330 | public static String listNadgrids() {
|
---|
331 | return listKeys(nadgrids);
|
---|
332 | }
|
---|
333 | }
|
---|