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

Last change on this file since 5551 was 5548, checked in by bastiK, 11 years ago

remove Projection classes (replaced by data/epsg file)

concludes the projection rework from ealier this year

File size: 5.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.HashMap;
9import java.util.Map;
10import java.util.regex.Matcher;
11import java.util.regex.Pattern;
12
13import org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.data.coor.EastNorth;
15import org.openstreetmap.josm.data.coor.LatLon;
16import org.openstreetmap.josm.data.projection.datum.Datum;
17import org.openstreetmap.josm.data.projection.datum.NTV2GridShiftFileWrapper;
18import org.openstreetmap.josm.data.projection.datum.WGS84Datum;
19import org.openstreetmap.josm.data.projection.proj.ClassProjFactory;
20import org.openstreetmap.josm.data.projection.proj.LambertConformalConic;
21import org.openstreetmap.josm.data.projection.proj.LonLat;
22import org.openstreetmap.josm.data.projection.proj.Proj;
23import org.openstreetmap.josm.data.projection.proj.ProjFactory;
24import org.openstreetmap.josm.data.projection.proj.SwissObliqueMercator;
25import org.openstreetmap.josm.data.projection.proj.TransverseMercator;
26import org.openstreetmap.josm.io.MirroredInputStream;
27import org.openstreetmap.josm.tools.Pair;
28
29/**
30 * Class to handle projections
31 *
32 */
33public class Projections {
34
35 public static EastNorth project(LatLon ll) {
36 if (ll == null) return null;
37 return Main.getProjection().latlon2eastNorth(ll);
38 }
39
40 public static LatLon inverseProject(EastNorth en) {
41 if (en == null) return null;
42 return Main.getProjection().eastNorth2latlon(en);
43 }
44
45 /*********************************
46 * Registry for custom projection
47 *
48 * should be compatible to PROJ.4
49 */
50 final public static Map<String, ProjFactory> projs = new HashMap<String, ProjFactory>();
51 final public static Map<String, Ellipsoid> ellipsoids = new HashMap<String, Ellipsoid>();
52 final public static Map<String, Datum> datums = new HashMap<String, Datum>();
53 final public static Map<String, NTV2GridShiftFileWrapper> nadgrids = new HashMap<String, NTV2GridShiftFileWrapper>();
54 final public static Map<String, Pair<String, String>> inits = new HashMap<String, Pair<String, String>>();
55
56 static {
57 registerBaseProjection("lonlat", LonLat.class, "core");
58 registerBaseProjection("josm:smerc", org.openstreetmap.josm.data.projection.proj.Mercator.class, "core");
59 registerBaseProjection("lcc", LambertConformalConic.class, "core");
60 registerBaseProjection("somerc", SwissObliqueMercator.class, "core");
61 registerBaseProjection("tmerc", TransverseMercator.class, "core");
62
63 ellipsoids.put("intl", Ellipsoid.hayford);
64 ellipsoids.put("GRS80", Ellipsoid.GRS80);
65 ellipsoids.put("WGS84", Ellipsoid.WGS84);
66 ellipsoids.put("bessel", Ellipsoid.Bessel1841);
67
68 datums.put("WGS84", WGS84Datum.INSTANCE);
69
70 nadgrids.put("BETA2007.gsb", NTV2GridShiftFileWrapper.BETA2007);
71 nadgrids.put("ntf_r93_b.gsb", NTV2GridShiftFileWrapper.ntf_rgf93);
72
73 loadInits();
74 }
75
76 /**
77 * Plugins can register additional base projections.
78 *
79 * @param id The "official" PROJ.4 id. In case the projection is not supported
80 * by PROJ.4, use some prefix, e.g. josm:myproj or gdal:otherproj.
81 * @param fac The base projection factory.
82 * @param origin Multiple plugins may implement the same base projection.
83 * Provide plugin name or similar string, so it be differentiated.
84 */
85 public static void registerBaseProjection(String id, ProjFactory fac, String origin) {
86 projs.put(id, fac);
87 }
88
89 public static void registerBaseProjection(String id, Class<? extends Proj> projClass, String origin) {
90 registerBaseProjection(id, new ClassProjFactory(projClass), origin);
91 }
92
93 public static Proj getBaseProjection(String id) {
94 ProjFactory fac = projs.get(id);
95 if (fac == null) return null;
96 return fac.createInstance();
97 }
98
99 public static Ellipsoid getEllipsoid(String id) {
100 return ellipsoids.get(id);
101 }
102
103 public static Datum getDatum(String id) {
104 return datums.get(id);
105 }
106
107 public static NTV2GridShiftFileWrapper getNTV2Grid(String id) {
108 return nadgrids.get(id);
109 }
110
111 public static String getInit(String id) {
112 return inits.get(id.toLowerCase()).b;
113 }
114
115 /**
116 * Load +init "presets" from file
117 */
118 private static void loadInits() {
119 Pattern epsgPattern = Pattern.compile("<(\\d+)>(.*)<>");
120 try {
121 InputStream in = new MirroredInputStream("resource://data/epsg");
122 BufferedReader r = new BufferedReader(new InputStreamReader(in));
123 String line, lastline = "";
124 while ((line = r.readLine()) != null) {
125 line = line.trim();
126 if (!line.startsWith("#") && !line.isEmpty()) {
127 if (!lastline.startsWith("#")) throw new AssertionError();
128 String name = lastline.substring(1).trim();
129 Matcher m = epsgPattern.matcher(line);
130 if (m.matches()) {
131 inits.put("epsg:" + m.group(1), Pair.create(name, m.group(2).trim()));
132 } else {
133 System.err.println("Warning: failed to parse line from the epsg projection definition: "+line);
134 }
135 }
136 lastline = line;
137 }
138 } catch (IOException ex) {
139 throw new RuntimeException();
140 }
141 }
142}
Note: See TracBrowser for help on using the repository browser.