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

Last change on this file since 5266 was 5237, checked in by bastiK, 12 years ago

projection: minor improvements

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