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.Collection;
|
---|
10 | import java.util.Collections;
|
---|
11 | import java.util.HashMap;
|
---|
12 | import java.util.HashSet;
|
---|
13 | import java.util.Locale;
|
---|
14 | import java.util.Map;
|
---|
15 | import java.util.Set;
|
---|
16 | import java.util.regex.Matcher;
|
---|
17 | import java.util.regex.Pattern;
|
---|
18 |
|
---|
19 | import org.openstreetmap.josm.Main;
|
---|
20 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
21 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
22 | import org.openstreetmap.josm.data.projection.datum.Datum;
|
---|
23 | import org.openstreetmap.josm.data.projection.datum.GRS80Datum;
|
---|
24 | import org.openstreetmap.josm.data.projection.datum.NTV2GridShiftFileWrapper;
|
---|
25 | import org.openstreetmap.josm.data.projection.datum.WGS84Datum;
|
---|
26 | import org.openstreetmap.josm.data.projection.proj.ClassProjFactory;
|
---|
27 | import org.openstreetmap.josm.data.projection.proj.LambertConformalConic;
|
---|
28 | import org.openstreetmap.josm.data.projection.proj.LonLat;
|
---|
29 | import org.openstreetmap.josm.data.projection.proj.Mercator;
|
---|
30 | import org.openstreetmap.josm.data.projection.proj.Proj;
|
---|
31 | import org.openstreetmap.josm.data.projection.proj.ProjFactory;
|
---|
32 | import org.openstreetmap.josm.data.projection.proj.SwissObliqueMercator;
|
---|
33 | import org.openstreetmap.josm.data.projection.proj.TransverseMercator;
|
---|
34 | import org.openstreetmap.josm.gui.preferences.projection.ProjectionChoice;
|
---|
35 | import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
|
---|
36 | import org.openstreetmap.josm.io.CachedFile;
|
---|
37 | import org.openstreetmap.josm.tools.Pair;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Class to handle projections
|
---|
41 | *
|
---|
42 | */
|
---|
43 | public 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 | }
|
---|