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

Last change on this file since 6920 was 6883, checked in by Don-vip, 10 years ago

fix some Sonar issues

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