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

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

When doing a String.toLowerCase()/toUpperCase() call, use a Locale. This avoids problems with certain locales, i.e. Lithuanian or Turkish. See PMD UseLocaleWithCaseConversions rule and String.toLowerCase() javadoc.

  • Property svn:eol-style set to native
File size: 8.1 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("clrk66", Ellipsoid.clarke1866);
78 ellipsoids.put("clarkeIGN", Ellipsoid.clarkeIGN);
79 ellipsoids.put("intl", Ellipsoid.hayford);
80 ellipsoids.put("GRS67", Ellipsoid.GRS67);
81 ellipsoids.put("GRS80", Ellipsoid.GRS80);
82 ellipsoids.put("WGS84", Ellipsoid.WGS84);
83 ellipsoids.put("bessel", Ellipsoid.Bessel1841);
84
85 datums.put("WGS84", WGS84Datum.INSTANCE);
86 datums.put("GRS80", GRS80Datum.INSTANCE);
87
88 nadgrids.put("BETA2007.gsb", NTV2GridShiftFileWrapper.BETA2007);
89 nadgrids.put("ntf_r93_b.gsb", NTV2GridShiftFileWrapper.ntf_rgf93);
90
91 loadInits();
92 }
93
94 /**
95 * Plugins can register additional base projections.
96 *
97 * @param id The "official" PROJ.4 id. In case the projection is not supported
98 * by PROJ.4, use some prefix, e.g. josm:myproj or gdal:otherproj.
99 * @param fac The base projection factory.
100 * @param origin Multiple plugins may implement the same base projection.
101 * Provide plugin name or similar string, so it be differentiated.
102 */
103 public static void registerBaseProjection(String id, ProjFactory fac, String origin) {
104 projs.put(id, fac);
105 }
106
107 public static void registerBaseProjection(String id, Class<? extends Proj> projClass, String origin) {
108 registerBaseProjection(id, new ClassProjFactory(projClass), origin);
109 }
110
111 public static Proj getBaseProjection(String id) {
112 ProjFactory fac = projs.get(id);
113 if (fac == null) return null;
114 return fac.createInstance();
115 }
116
117 public static Ellipsoid getEllipsoid(String id) {
118 return ellipsoids.get(id);
119 }
120
121 public static Datum getDatum(String id) {
122 return datums.get(id);
123 }
124
125 public static NTV2GridShiftFileWrapper getNTV2Grid(String id) {
126 return nadgrids.get(id);
127 }
128
129 /**
130 * Get the projection definition string for the given id.
131 * @param id the id
132 * @return the string that can be processed by #{link CustomProjection}.
133 * Null, if the id isn't supported.
134 */
135 public static String getInit(String id) {
136 Pair<String, String> r = inits.get(id.toUpperCase(Locale.ENGLISH));
137 if (r == null) return null;
138 return r.b;
139 }
140
141 /**
142 * Load +init "presets" from file
143 */
144 private static void loadInits() {
145 Pattern epsgPattern = Pattern.compile("<(\\d+)>(.*)<>");
146 try (
147 InputStream in = new CachedFile("resource://data/projection/epsg").getInputStream();
148 BufferedReader r = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
149 ) {
150 String line, lastline = "";
151 while ((line = r.readLine()) != null) {
152 line = line.trim();
153 if (!line.startsWith("#") && !line.isEmpty()) {
154 if (!lastline.startsWith("#")) throw new AssertionError("EPSG file seems corrupted");
155 String name = lastline.substring(1).trim();
156 Matcher m = epsgPattern.matcher(line);
157 if (m.matches()) {
158 inits.put("EPSG:" + m.group(1), Pair.create(name, m.group(2).trim()));
159 } else {
160 Main.warn("Failed to parse line from the EPSG projection definition: "+line);
161 }
162 }
163 lastline = line;
164 }
165 } catch (IOException ex) {
166 throw new RuntimeException(ex);
167 }
168 }
169
170 private static final Set<String> allCodes = new HashSet<>();
171 private static final Map<String, ProjectionChoice> allProjectionChoicesByCode = new HashMap<>();
172 private static final Map<String, Projection> projectionsByCode_cache = new HashMap<>();
173
174 static {
175 for (ProjectionChoice pc : ProjectionPreference.getProjectionChoices()) {
176 for (String code : pc.allCodes()) {
177 allProjectionChoicesByCode.put(code, pc);
178 }
179 }
180 allCodes.addAll(inits.keySet());
181 allCodes.addAll(allProjectionChoicesByCode.keySet());
182 }
183
184 public static Projection getProjectionByCode(String code) {
185 Projection proj = projectionsByCode_cache.get(code);
186 if (proj != null) return proj;
187 ProjectionChoice pc = allProjectionChoicesByCode.get(code);
188 if (pc != null) {
189 Collection<String> pref = pc.getPreferencesFromCode(code);
190 pc.setPreferences(pref);
191 try {
192 proj = pc.getProjection();
193 } catch (Exception e) {
194 String cause = e.getMessage();
195 Main.warn("Unable to get projection "+code+" with "+pc + (cause != null ? ". "+cause : ""));
196 }
197 }
198 if (proj == null) {
199 Pair<String, String> pair = inits.get(code);
200 if (pair == null) return null;
201 String name = pair.a;
202 String init = pair.b;
203 proj = new CustomProjection(name, code, init, null);
204 }
205 projectionsByCode_cache.put(code, proj);
206 return proj;
207 }
208
209 public static Collection<String> getAllProjectionCodes() {
210 return Collections.unmodifiableCollection(allCodes);
211 }
212}
Note: See TracBrowser for help on using the repository browser.