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

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

update license/copyright information

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