1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.data.projection;
|
---|
3 |
|
---|
4 | import java.io.BufferedReader;
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.io.InputStream;
|
---|
7 | import java.io.InputStreamReader;
|
---|
8 | import java.util.ArrayList;
|
---|
9 | import java.util.Arrays;
|
---|
10 | import java.util.HashMap;
|
---|
11 | import java.util.Map;
|
---|
12 | import java.util.regex.Matcher;
|
---|
13 | import java.util.regex.Pattern;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.Main;
|
---|
16 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
17 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
18 | import org.openstreetmap.josm.data.projection.datum.Datum;
|
---|
19 | import org.openstreetmap.josm.data.projection.datum.NTV2GridShiftFileWrapper;
|
---|
20 | import org.openstreetmap.josm.data.projection.datum.WGS84Datum;
|
---|
21 | import org.openstreetmap.josm.data.projection.proj.ClassProjFactory;
|
---|
22 | import org.openstreetmap.josm.data.projection.proj.LambertConformalConic;
|
---|
23 | import org.openstreetmap.josm.data.projection.proj.LonLat;
|
---|
24 | import org.openstreetmap.josm.data.projection.proj.Proj;
|
---|
25 | import org.openstreetmap.josm.data.projection.proj.ProjFactory;
|
---|
26 | import org.openstreetmap.josm.data.projection.proj.SwissObliqueMercator;
|
---|
27 | import org.openstreetmap.josm.data.projection.proj.TransverseMercator;
|
---|
28 | import org.openstreetmap.josm.io.MirroredInputStream;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Class to handle projections
|
---|
32 | *
|
---|
33 | */
|
---|
34 | public class Projections {
|
---|
35 | /**
|
---|
36 | * List of all available projections.
|
---|
37 | */
|
---|
38 | private static ArrayList<Projection> allProjections =
|
---|
39 | new ArrayList<Projection>(Arrays.asList(new Projection[] {
|
---|
40 | // global projections
|
---|
41 | new Epsg4326(),
|
---|
42 | new Mercator(),
|
---|
43 | new UTM(),
|
---|
44 | // regional - alphabetical order by country code
|
---|
45 | new BelgianLambert1972(), // BE
|
---|
46 | new BelgianLambert2008(), // BE
|
---|
47 | new SwissGrid(), // CH
|
---|
48 | new GaussKrueger(), // DE
|
---|
49 | new LambertEST(), // EE
|
---|
50 | new Lambert(), // FR
|
---|
51 | new Lambert93(), // FR
|
---|
52 | new LambertCC9Zones(), // FR
|
---|
53 | new UTM_France_DOM(), // FR
|
---|
54 | new TransverseMercatorLV(), // LV
|
---|
55 | new Puwg(), // PL
|
---|
56 | new Epsg3008(), // SE
|
---|
57 | new CustomProjectionPrefGui()
|
---|
58 | }));
|
---|
59 |
|
---|
60 | public static ArrayList<Projection> getProjections() {
|
---|
61 | return allProjections;
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Adds a new projection to the list of known projections.
|
---|
66 | *
|
---|
67 | * For Plugins authors: make sure your plugin is an early plugin, i.e. put
|
---|
68 | * Plugin-Early=true in your Manifest.
|
---|
69 | */
|
---|
70 | public static void addProjection(Projection proj) {
|
---|
71 | allProjections.add(proj);
|
---|
72 | }
|
---|
73 |
|
---|
74 | public static EastNorth project(LatLon ll) {
|
---|
75 | if (ll == null) return null;
|
---|
76 | return Main.getProjection().latlon2eastNorth(ll);
|
---|
77 | }
|
---|
78 |
|
---|
79 | public static LatLon inverseProject(EastNorth en) {
|
---|
80 | if (en == null) return null;
|
---|
81 | return Main.getProjection().eastNorth2latlon(en);
|
---|
82 | }
|
---|
83 |
|
---|
84 | /*********************************
|
---|
85 | * Registry for custom projection
|
---|
86 | *
|
---|
87 | * should be compatible to PROJ.4
|
---|
88 | */
|
---|
89 | public static Map<String, ProjFactory> projs = new HashMap<String, ProjFactory>();
|
---|
90 | public static Map<String, Ellipsoid> ellipsoids = new HashMap<String, Ellipsoid>();
|
---|
91 | public static Map<String, Datum> datums = new HashMap<String, Datum>();
|
---|
92 | public static Map<String, NTV2GridShiftFileWrapper> nadgrids = new HashMap<String, NTV2GridShiftFileWrapper>();
|
---|
93 | public static Map<String, String> inits = new HashMap<String, String>();
|
---|
94 |
|
---|
95 | static {
|
---|
96 | registerBaseProjection("lonlat", LonLat.class, "core");
|
---|
97 | registerBaseProjection("josm:smerc", org.openstreetmap.josm.data.projection.proj.Mercator.class, "core");
|
---|
98 | registerBaseProjection("lcc", LambertConformalConic.class, "core");
|
---|
99 | registerBaseProjection("somerc", SwissObliqueMercator.class, "core");
|
---|
100 | registerBaseProjection("tmerc", TransverseMercator.class, "core");
|
---|
101 |
|
---|
102 | ellipsoids.put("intl", Ellipsoid.hayford);
|
---|
103 | ellipsoids.put("GRS80", Ellipsoid.GRS80);
|
---|
104 | ellipsoids.put("WGS84", Ellipsoid.WGS84);
|
---|
105 | ellipsoids.put("bessel", Ellipsoid.Bessel1841);
|
---|
106 |
|
---|
107 | datums.put("WGS84", WGS84Datum.INSTANCE);
|
---|
108 |
|
---|
109 | nadgrids.put("BETA2007.gsb", NTV2GridShiftFileWrapper.BETA2007);
|
---|
110 | nadgrids.put("ntf_r93_b.gsb", NTV2GridShiftFileWrapper.ntf_rgf93);
|
---|
111 |
|
---|
112 | loadInits();
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Plugins can register additional base projections.
|
---|
117 | *
|
---|
118 | * @param id The "official" PROJ.4 id. In case the projection is not supported
|
---|
119 | * by PROJ.4, use some prefix, e.g. josm:myproj or gdal:otherproj.
|
---|
120 | * @param fac The base projection factory.
|
---|
121 | * @param origin Multiple plugins may implement the same base projection.
|
---|
122 | * Provide plugin name or similar string, so it be differentiated.
|
---|
123 | */
|
---|
124 | public static void registerBaseProjection(String id, ProjFactory fac, String origin) {
|
---|
125 | projs.put(id, fac);
|
---|
126 | }
|
---|
127 |
|
---|
128 | public static void registerBaseProjection(String id, Class<? extends Proj> projClass, String origin) {
|
---|
129 | registerBaseProjection(id, new ClassProjFactory(projClass), origin);
|
---|
130 | }
|
---|
131 |
|
---|
132 | public static Proj getBaseProjection(String id) {
|
---|
133 | ProjFactory fac = projs.get(id);
|
---|
134 | if (fac == null) return null;
|
---|
135 | return fac.createInstance();
|
---|
136 | }
|
---|
137 |
|
---|
138 | public static Ellipsoid getEllipsoid(String id) {
|
---|
139 | return ellipsoids.get(id);
|
---|
140 | }
|
---|
141 |
|
---|
142 | public static Datum getDatum(String id) {
|
---|
143 | return datums.get(id);
|
---|
144 | }
|
---|
145 |
|
---|
146 | public static NTV2GridShiftFileWrapper getNTV2Grid(String id) {
|
---|
147 | return nadgrids.get(id);
|
---|
148 | }
|
---|
149 |
|
---|
150 | public static String getInit(String id) {
|
---|
151 | return inits.get(id);
|
---|
152 | }
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Load +init "presets" from file
|
---|
156 | */
|
---|
157 | private static void loadInits() {
|
---|
158 | Pattern epsgPattern = Pattern.compile("\\A<(\\d+)>(.*)<>\\Z");
|
---|
159 | try {
|
---|
160 | InputStream in = new MirroredInputStream("resource://data/epsg");
|
---|
161 | BufferedReader r = new BufferedReader(new InputStreamReader(in));
|
---|
162 | String line;
|
---|
163 | while ((line = r.readLine()) != null) {
|
---|
164 | line = line.trim();
|
---|
165 | if (!line.startsWith("#")) {
|
---|
166 | Matcher m = epsgPattern.matcher(line);
|
---|
167 | if (m.matches()) {
|
---|
168 | inits.put("epsg:" + m.group(1), m.group(2).trim());
|
---|
169 | } else {
|
---|
170 | System.err.println("Warning: failed to parse line from the epsg projection definition: "+line);
|
---|
171 | }
|
---|
172 | }
|
---|
173 | }
|
---|
174 | } catch (IOException ex) {
|
---|
175 | throw new RuntimeException();
|
---|
176 | }
|
---|
177 | }
|
---|
178 | }
|
---|