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

Last change on this file since 6203 was 6135, checked in by Don-vip, 11 years ago

fix javadoc/warnings

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