1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.projection;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.util.ArrayList;
|
---|
7 | import java.util.HashMap;
|
---|
8 | import java.util.List;
|
---|
9 | import java.util.Map;
|
---|
10 | import java.util.concurrent.ConcurrentHashMap;
|
---|
11 | import java.util.regex.Matcher;
|
---|
12 | import java.util.regex.Pattern;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.Main;
|
---|
15 | import org.openstreetmap.josm.data.Bounds;
|
---|
16 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
17 | import org.openstreetmap.josm.data.projection.datum.CentricDatum;
|
---|
18 | import org.openstreetmap.josm.data.projection.datum.Datum;
|
---|
19 | import org.openstreetmap.josm.data.projection.datum.NTV2Datum;
|
---|
20 | import org.openstreetmap.josm.data.projection.datum.NTV2GridShiftFileWrapper;
|
---|
21 | import org.openstreetmap.josm.data.projection.datum.NullDatum;
|
---|
22 | import org.openstreetmap.josm.data.projection.datum.SevenParameterDatum;
|
---|
23 | import org.openstreetmap.josm.data.projection.datum.ThreeParameterDatum;
|
---|
24 | import org.openstreetmap.josm.data.projection.datum.WGS84Datum;
|
---|
25 | import org.openstreetmap.josm.data.projection.proj.Mercator;
|
---|
26 | import org.openstreetmap.josm.data.projection.proj.Proj;
|
---|
27 | import org.openstreetmap.josm.data.projection.proj.ProjParameters;
|
---|
28 | import org.openstreetmap.josm.tools.Utils;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Custom projection.
|
---|
32 | *
|
---|
33 | * Inspired by PROJ.4 and Proj4J.
|
---|
34 | * @since 5072
|
---|
35 | */
|
---|
36 | public class CustomProjection extends AbstractProjection {
|
---|
37 |
|
---|
38 | private static final double METER_PER_UNIT_DEGREE = 2 * Math.PI * 6370997 / 360;
|
---|
39 | private static final Map<String, Double> UNITS_TO_METERS = getUnitsToMeters();
|
---|
40 | private static final Map<String, Double> PRIME_MERIDANS = getPrimeMeridians();
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * pref String that defines the projection
|
---|
44 | *
|
---|
45 | * null means fall back mode (Mercator)
|
---|
46 | */
|
---|
47 | protected String pref;
|
---|
48 | protected String name;
|
---|
49 | protected String code;
|
---|
50 | protected String cacheDir;
|
---|
51 | protected Bounds bounds;
|
---|
52 | private double metersPerUnit = METER_PER_UNIT_DEGREE; // default to degrees
|
---|
53 | private String axis = "enu"; // default axis orientation is East, North, Up
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Proj4-like projection parameters. See <a href="https://trac.osgeo.org/proj/wiki/GenParms">reference</a>.
|
---|
57 | * @since 7370 (public)
|
---|
58 | */
|
---|
59 | public enum Param {
|
---|
60 |
|
---|
61 | /** False easting */
|
---|
62 | x_0("x_0", true),
|
---|
63 | /** False northing */
|
---|
64 | y_0("y_0", true),
|
---|
65 | /** Central meridian */
|
---|
66 | lon_0("lon_0", true),
|
---|
67 | /** Prime meridian */
|
---|
68 | pm("pm", true),
|
---|
69 | /** Scaling factor */
|
---|
70 | k_0("k_0", true),
|
---|
71 | /** Ellipsoid name (see {@code proj -le}) */
|
---|
72 | ellps("ellps", true),
|
---|
73 | /** Semimajor radius of the ellipsoid axis */
|
---|
74 | a("a", true),
|
---|
75 | /** Eccentricity of the ellipsoid squared */
|
---|
76 | es("es", true),
|
---|
77 | /** Reciprocal of the ellipsoid flattening term (e.g. 298) */
|
---|
78 | rf("rf", true),
|
---|
79 | /** Flattening of the ellipsoid = 1-sqrt(1-e^2) */
|
---|
80 | f("f", true),
|
---|
81 | /** Semiminor radius of the ellipsoid axis */
|
---|
82 | b("b", true),
|
---|
83 | /** Datum name (see {@code proj -ld}) */
|
---|
84 | datum("datum", true),
|
---|
85 | /** 3 or 7 term datum transform parameters */
|
---|
86 | towgs84("towgs84", true),
|
---|
87 | /** Filename of NTv2 grid file to use for datum transforms */
|
---|
88 | nadgrids("nadgrids", true),
|
---|
89 | /** Projection name (see {@code proj -l}) */
|
---|
90 | proj("proj", true),
|
---|
91 | /** Latitude of origin */
|
---|
92 | lat_0("lat_0", true),
|
---|
93 | /** Latitude of first standard parallel */
|
---|
94 | lat_1("lat_1", true),
|
---|
95 | /** Latitude of second standard parallel */
|
---|
96 | lat_2("lat_2", true),
|
---|
97 | /** the exact proj.4 string will be preserved in the WKT representation */
|
---|
98 | wktext("wktext", false), // ignored
|
---|
99 | /** meters, US survey feet, etc. */
|
---|
100 | units("units", true),
|
---|
101 | /** Don't use the /usr/share/proj/proj_def.dat defaults file */
|
---|
102 | no_defs("no_defs", false),
|
---|
103 | init("init", true),
|
---|
104 | /** crs units to meter multiplier */
|
---|
105 | to_meter("to_meter", true),
|
---|
106 | /** definition of axis for projection */
|
---|
107 | axis("axis", true),
|
---|
108 | /** UTM zone */
|
---|
109 | zone("zone", true),
|
---|
110 | /** indicate southern hemisphere for UTM */
|
---|
111 | south("south", false),
|
---|
112 | // JOSM extensions, not present in PROJ.4
|
---|
113 | wmssrs("wmssrs", true),
|
---|
114 | bounds("bounds", true);
|
---|
115 |
|
---|
116 | /** Parameter key */
|
---|
117 | public final String key;
|
---|
118 | /** {@code true} if the parameter has a value */
|
---|
119 | public final boolean hasValue;
|
---|
120 |
|
---|
121 | /** Map of all parameters by key */
|
---|
122 | static final Map<String, Param> paramsByKey = new ConcurrentHashMap<>();
|
---|
123 | static {
|
---|
124 | for (Param p : Param.values()) {
|
---|
125 | paramsByKey.put(p.key, p);
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | Param(String key, boolean hasValue) {
|
---|
130 | this.key = key;
|
---|
131 | this.hasValue = hasValue;
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * Constructs a new empty {@code CustomProjection}.
|
---|
137 | */
|
---|
138 | public CustomProjection() {
|
---|
139 | // contents can be set later with update()
|
---|
140 | }
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Constructs a new {@code CustomProjection} with given parameters.
|
---|
144 | * @param pref String containing projection parameters
|
---|
145 | * (ex: "+proj=tmerc +lon_0=-3 +k_0=0.9996 +x_0=500000 +ellps=WGS84 +datum=WGS84 +bounds=-8,-5,2,85")
|
---|
146 | */
|
---|
147 | public CustomProjection(String pref) {
|
---|
148 | this(null, null, pref, null);
|
---|
149 | }
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * Constructs a new {@code CustomProjection} with given name, code and parameters.
|
---|
153 | *
|
---|
154 | * @param name describe projection in one or two words
|
---|
155 | * @param code unique code for this projection - may be null
|
---|
156 | * @param pref the string that defines the custom projection
|
---|
157 | * @param cacheDir cache directory name
|
---|
158 | */
|
---|
159 | public CustomProjection(String name, String code, String pref, String cacheDir) {
|
---|
160 | this.name = name;
|
---|
161 | this.code = code;
|
---|
162 | this.pref = pref;
|
---|
163 | this.cacheDir = cacheDir;
|
---|
164 | try {
|
---|
165 | update(pref);
|
---|
166 | } catch (ProjectionConfigurationException ex) {
|
---|
167 | try {
|
---|
168 | update(null);
|
---|
169 | } catch (ProjectionConfigurationException ex1) {
|
---|
170 | throw new RuntimeException(ex1);
|
---|
171 | }
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * Updates this {@code CustomProjection} with given parameters.
|
---|
177 | * @param pref String containing projection parameters (ex: "+proj=lonlat +ellps=WGS84 +datum=WGS84 +bounds=-180,-90,180,90")
|
---|
178 | * @throws ProjectionConfigurationException if {@code pref} cannot be parsed properly
|
---|
179 | */
|
---|
180 | public final void update(String pref) throws ProjectionConfigurationException {
|
---|
181 | this.pref = pref;
|
---|
182 | if (pref == null) {
|
---|
183 | ellps = Ellipsoid.WGS84;
|
---|
184 | datum = WGS84Datum.INSTANCE;
|
---|
185 | proj = new Mercator();
|
---|
186 | bounds = new Bounds(
|
---|
187 | -85.05112877980659, -180.0,
|
---|
188 | 85.05112877980659, 180.0, true);
|
---|
189 | } else {
|
---|
190 | Map<String, String> parameters = parseParameterList(pref);
|
---|
191 | ellps = parseEllipsoid(parameters);
|
---|
192 | datum = parseDatum(parameters, ellps);
|
---|
193 | if (ellps == null) {
|
---|
194 | ellps = datum.getEllipsoid();
|
---|
195 | }
|
---|
196 | proj = parseProjection(parameters, ellps);
|
---|
197 | // "utm" is a shortcut for a set of parameters
|
---|
198 | if ("utm".equals(parameters.get(Param.proj.key))) {
|
---|
199 | String zoneStr = parameters.get(Param.zone.key);
|
---|
200 | Integer zone;
|
---|
201 | if (zoneStr == null)
|
---|
202 | throw new ProjectionConfigurationException(tr("UTM projection (''+proj=utm'') requires ''+zone=...'' parameter."));
|
---|
203 | try {
|
---|
204 | zone = Integer.valueOf(zoneStr);
|
---|
205 | } catch (NumberFormatException e) {
|
---|
206 | zone = null;
|
---|
207 | }
|
---|
208 | if (zone == null || zone < 1 || zone > 60)
|
---|
209 | throw new ProjectionConfigurationException(tr("Expected integer value in range 1-60 for ''+zone=...'' parameter."));
|
---|
210 | this.lon0 = 6 * zone - 183;
|
---|
211 | this.k0 = 0.9996;
|
---|
212 | this.x0 = 500000;
|
---|
213 | this.y0 = parameters.containsKey(Param.south.key) ? 10000000 : 0;
|
---|
214 | }
|
---|
215 | String s = parameters.get(Param.x_0.key);
|
---|
216 | if (s != null) {
|
---|
217 | this.x0 = parseDouble(s, Param.x_0.key);
|
---|
218 | }
|
---|
219 | s = parameters.get(Param.y_0.key);
|
---|
220 | if (s != null) {
|
---|
221 | this.y0 = parseDouble(s, Param.y_0.key);
|
---|
222 | }
|
---|
223 | s = parameters.get(Param.lon_0.key);
|
---|
224 | if (s != null) {
|
---|
225 | this.lon0 = parseAngle(s, Param.lon_0.key);
|
---|
226 | }
|
---|
227 | s = parameters.get(Param.pm.key);
|
---|
228 | if (s != null) {
|
---|
229 | if (PRIME_MERIDANS.containsKey(s)) {
|
---|
230 | this.pm = PRIME_MERIDANS.get(s);
|
---|
231 | } else {
|
---|
232 | this.pm = parseAngle(s, Param.pm.key);
|
---|
233 | }
|
---|
234 | }
|
---|
235 | s = parameters.get(Param.k_0.key);
|
---|
236 | if (s != null) {
|
---|
237 | this.k0 = parseDouble(s, Param.k_0.key);
|
---|
238 | }
|
---|
239 | s = parameters.get(Param.bounds.key);
|
---|
240 | if (s != null) {
|
---|
241 | this.bounds = parseBounds(s);
|
---|
242 | }
|
---|
243 | s = parameters.get(Param.wmssrs.key);
|
---|
244 | if (s != null) {
|
---|
245 | this.code = s;
|
---|
246 | }
|
---|
247 | s = parameters.get(Param.units.key);
|
---|
248 | if (s != null) {
|
---|
249 | s = Utils.strip(s, "\"");
|
---|
250 | if (UNITS_TO_METERS.containsKey(s)) {
|
---|
251 | this.metersPerUnit = UNITS_TO_METERS.get(s);
|
---|
252 | } else {
|
---|
253 | Main.warn("No metersPerUnit found for: " + s);
|
---|
254 | }
|
---|
255 | }
|
---|
256 | s = parameters.get(Param.to_meter.key);
|
---|
257 | if (s != null) {
|
---|
258 | this.metersPerUnit = parseDouble(s, Param.to_meter.key);
|
---|
259 | }
|
---|
260 | s = parameters.get(Param.axis.key);
|
---|
261 | if (s != null) {
|
---|
262 | this.axis = s;
|
---|
263 | }
|
---|
264 | }
|
---|
265 | }
|
---|
266 |
|
---|
267 | private Map<String, String> parseParameterList(String pref) throws ProjectionConfigurationException {
|
---|
268 | Map<String, String> parameters = new HashMap<>();
|
---|
269 | String[] parts = Utils.WHITE_SPACES_PATTERN.split(pref.trim());
|
---|
270 | if (pref.trim().isEmpty()) {
|
---|
271 | parts = new String[0];
|
---|
272 | }
|
---|
273 | for (String part : parts) {
|
---|
274 | if (part.isEmpty() || part.charAt(0) != '+')
|
---|
275 | throw new ProjectionConfigurationException(tr("Parameter must begin with a ''+'' character (found ''{0}'')", part));
|
---|
276 | Matcher m = Pattern.compile("\\+([a-zA-Z0-9_]+)(=(.*))?").matcher(part);
|
---|
277 | if (m.matches()) {
|
---|
278 | String key = m.group(1);
|
---|
279 | // alias
|
---|
280 | if ("k".equals(key)) {
|
---|
281 | key = Param.k_0.key;
|
---|
282 | }
|
---|
283 | String value = null;
|
---|
284 | if (m.groupCount() >= 3) {
|
---|
285 | value = m.group(3);
|
---|
286 | // some aliases
|
---|
287 | if (key.equals(Param.proj.key)) {
|
---|
288 | if ("longlat".equals(value) || "latlon".equals(value) || "latlong".equals(value)) {
|
---|
289 | value = "lonlat";
|
---|
290 | }
|
---|
291 | }
|
---|
292 | }
|
---|
293 | if (!Param.paramsByKey.containsKey(key))
|
---|
294 | throw new ProjectionConfigurationException(tr("Unknown parameter: ''{0}''.", key));
|
---|
295 | if (Param.paramsByKey.get(key).hasValue && value == null)
|
---|
296 | throw new ProjectionConfigurationException(tr("Value expected for parameter ''{0}''.", key));
|
---|
297 | if (!Param.paramsByKey.get(key).hasValue && value != null)
|
---|
298 | throw new ProjectionConfigurationException(tr("No value expected for parameter ''{0}''.", key));
|
---|
299 | parameters.put(key, value);
|
---|
300 | } else
|
---|
301 | throw new ProjectionConfigurationException(tr("Unexpected parameter format (''{0}'')", part));
|
---|
302 | }
|
---|
303 | // recursive resolution of +init includes
|
---|
304 | String initKey = parameters.get(Param.init.key);
|
---|
305 | if (initKey != null) {
|
---|
306 | String init = Projections.getInit(initKey);
|
---|
307 | if (init == null)
|
---|
308 | throw new ProjectionConfigurationException(tr("Value ''{0}'' for option +init not supported.", initKey));
|
---|
309 | Map<String, String> initp = null;
|
---|
310 | try {
|
---|
311 | initp = parseParameterList(init);
|
---|
312 | } catch (ProjectionConfigurationException ex) {
|
---|
313 | throw new ProjectionConfigurationException(tr(initKey+": "+ex.getMessage()), ex);
|
---|
314 | }
|
---|
315 | for (Map.Entry<String, String> e : parameters.entrySet()) {
|
---|
316 | initp.put(e.getKey(), e.getValue());
|
---|
317 | }
|
---|
318 | return initp;
|
---|
319 | }
|
---|
320 | return parameters;
|
---|
321 | }
|
---|
322 |
|
---|
323 | public Ellipsoid parseEllipsoid(Map<String, String> parameters) throws ProjectionConfigurationException {
|
---|
324 | String code = parameters.get(Param.ellps.key);
|
---|
325 | if (code != null) {
|
---|
326 | Ellipsoid ellipsoid = Projections.getEllipsoid(code);
|
---|
327 | if (ellipsoid == null) {
|
---|
328 | throw new ProjectionConfigurationException(tr("Ellipsoid ''{0}'' not supported.", code));
|
---|
329 | } else {
|
---|
330 | return ellipsoid;
|
---|
331 | }
|
---|
332 | }
|
---|
333 | String s = parameters.get(Param.a.key);
|
---|
334 | if (s != null) {
|
---|
335 | double a = parseDouble(s, Param.a.key);
|
---|
336 | if (parameters.get(Param.es.key) != null) {
|
---|
337 | double es = parseDouble(parameters, Param.es.key);
|
---|
338 | return Ellipsoid.create_a_es(a, es);
|
---|
339 | }
|
---|
340 | if (parameters.get(Param.rf.key) != null) {
|
---|
341 | double rf = parseDouble(parameters, Param.rf.key);
|
---|
342 | return Ellipsoid.create_a_rf(a, rf);
|
---|
343 | }
|
---|
344 | if (parameters.get(Param.f.key) != null) {
|
---|
345 | double f = parseDouble(parameters, Param.f.key);
|
---|
346 | return Ellipsoid.create_a_f(a, f);
|
---|
347 | }
|
---|
348 | if (parameters.get(Param.b.key) != null) {
|
---|
349 | double b = parseDouble(parameters, Param.b.key);
|
---|
350 | return Ellipsoid.create_a_b(a, b);
|
---|
351 | }
|
---|
352 | }
|
---|
353 | if (parameters.containsKey(Param.a.key) ||
|
---|
354 | parameters.containsKey(Param.es.key) ||
|
---|
355 | parameters.containsKey(Param.rf.key) ||
|
---|
356 | parameters.containsKey(Param.f.key) ||
|
---|
357 | parameters.containsKey(Param.b.key))
|
---|
358 | throw new ProjectionConfigurationException(tr("Combination of ellipsoid parameters is not supported."));
|
---|
359 | return null;
|
---|
360 | }
|
---|
361 |
|
---|
362 | public Datum parseDatum(Map<String, String> parameters, Ellipsoid ellps) throws ProjectionConfigurationException {
|
---|
363 | String datumId = parameters.get(Param.datum.key);
|
---|
364 | if (datumId != null) {
|
---|
365 | Datum datum = Projections.getDatum(datumId);
|
---|
366 | if (datum == null) throw new ProjectionConfigurationException(tr("Unknown datum identifier: ''{0}''", datumId));
|
---|
367 | return datum;
|
---|
368 | }
|
---|
369 | if (ellps == null) {
|
---|
370 | if (parameters.containsKey(Param.no_defs.key))
|
---|
371 | throw new ProjectionConfigurationException(tr("Ellipsoid required (+ellps=* or +a=*, +b=*)"));
|
---|
372 | // nothing specified, use WGS84 as default
|
---|
373 | ellps = Ellipsoid.WGS84;
|
---|
374 | }
|
---|
375 |
|
---|
376 | String nadgridsId = parameters.get(Param.nadgrids.key);
|
---|
377 | if (nadgridsId != null) {
|
---|
378 | if (nadgridsId.startsWith("@")) {
|
---|
379 | nadgridsId = nadgridsId.substring(1);
|
---|
380 | }
|
---|
381 | if ("null".equals(nadgridsId))
|
---|
382 | return new NullDatum(null, ellps);
|
---|
383 | NTV2GridShiftFileWrapper nadgrids = Projections.getNTV2Grid(nadgridsId);
|
---|
384 | if (nadgrids == null)
|
---|
385 | throw new ProjectionConfigurationException(tr("Grid shift file ''{0}'' for option +nadgrids not supported.", nadgridsId));
|
---|
386 | return new NTV2Datum(nadgridsId, null, ellps, nadgrids);
|
---|
387 | }
|
---|
388 |
|
---|
389 | String towgs84 = parameters.get(Param.towgs84.key);
|
---|
390 | if (towgs84 != null)
|
---|
391 | return parseToWGS84(towgs84, ellps);
|
---|
392 |
|
---|
393 | return new NullDatum(null, ellps);
|
---|
394 | }
|
---|
395 |
|
---|
396 | public Datum parseToWGS84(String paramList, Ellipsoid ellps) throws ProjectionConfigurationException {
|
---|
397 | String[] numStr = paramList.split(",");
|
---|
398 |
|
---|
399 | if (numStr.length != 3 && numStr.length != 7)
|
---|
400 | throw new ProjectionConfigurationException(tr("Unexpected number of arguments for parameter ''towgs84'' (must be 3 or 7)"));
|
---|
401 | List<Double> towgs84Param = new ArrayList<>();
|
---|
402 | for (String str : numStr) {
|
---|
403 | try {
|
---|
404 | towgs84Param.add(Double.valueOf(str));
|
---|
405 | } catch (NumberFormatException e) {
|
---|
406 | throw new ProjectionConfigurationException(tr("Unable to parse value of parameter ''towgs84'' (''{0}'')", str), e);
|
---|
407 | }
|
---|
408 | }
|
---|
409 | boolean isCentric = true;
|
---|
410 | for (Double param : towgs84Param) {
|
---|
411 | if (param != 0) {
|
---|
412 | isCentric = false;
|
---|
413 | break;
|
---|
414 | }
|
---|
415 | }
|
---|
416 | if (isCentric)
|
---|
417 | return new CentricDatum(null, null, ellps);
|
---|
418 | boolean is3Param = true;
|
---|
419 | for (int i = 3; i < towgs84Param.size(); i++) {
|
---|
420 | if (towgs84Param.get(i) != 0) {
|
---|
421 | is3Param = false;
|
---|
422 | break;
|
---|
423 | }
|
---|
424 | }
|
---|
425 | if (is3Param)
|
---|
426 | return new ThreeParameterDatum(null, null, ellps,
|
---|
427 | towgs84Param.get(0),
|
---|
428 | towgs84Param.get(1),
|
---|
429 | towgs84Param.get(2));
|
---|
430 | else
|
---|
431 | return new SevenParameterDatum(null, null, ellps,
|
---|
432 | towgs84Param.get(0),
|
---|
433 | towgs84Param.get(1),
|
---|
434 | towgs84Param.get(2),
|
---|
435 | towgs84Param.get(3),
|
---|
436 | towgs84Param.get(4),
|
---|
437 | towgs84Param.get(5),
|
---|
438 | towgs84Param.get(6));
|
---|
439 | }
|
---|
440 |
|
---|
441 | public Proj parseProjection(Map<String, String> parameters, Ellipsoid ellps) throws ProjectionConfigurationException {
|
---|
442 | String id = parameters.get(Param.proj.key);
|
---|
443 | if (id == null) throw new ProjectionConfigurationException(tr("Projection required (+proj=*)"));
|
---|
444 |
|
---|
445 | // "utm" is not a real projection, but a shortcut for a set of parameters
|
---|
446 | if ("utm".equals(id)) {
|
---|
447 | id = "tmerc";
|
---|
448 | }
|
---|
449 | Proj proj = Projections.getBaseProjection(id);
|
---|
450 | if (proj == null) throw new ProjectionConfigurationException(tr("Unknown projection identifier: ''{0}''", id));
|
---|
451 |
|
---|
452 | ProjParameters projParams = new ProjParameters();
|
---|
453 |
|
---|
454 | projParams.ellps = ellps;
|
---|
455 |
|
---|
456 | String s;
|
---|
457 | s = parameters.get(Param.lat_0.key);
|
---|
458 | if (s != null) {
|
---|
459 | projParams.lat0 = parseAngle(s, Param.lat_0.key);
|
---|
460 | }
|
---|
461 | s = parameters.get(Param.lat_1.key);
|
---|
462 | if (s != null) {
|
---|
463 | projParams.lat1 = parseAngle(s, Param.lat_1.key);
|
---|
464 | }
|
---|
465 | s = parameters.get(Param.lat_2.key);
|
---|
466 | if (s != null) {
|
---|
467 | projParams.lat2 = parseAngle(s, Param.lat_2.key);
|
---|
468 | }
|
---|
469 | proj.initialize(projParams);
|
---|
470 | return proj;
|
---|
471 | }
|
---|
472 |
|
---|
473 | public static Bounds parseBounds(String boundsStr) throws ProjectionConfigurationException {
|
---|
474 | String[] numStr = boundsStr.split(",");
|
---|
475 | if (numStr.length != 4)
|
---|
476 | throw new ProjectionConfigurationException(tr("Unexpected number of arguments for parameter ''+bounds'' (must be 4)"));
|
---|
477 | return new Bounds(parseAngle(numStr[1], "minlat (+bounds)"),
|
---|
478 | parseAngle(numStr[0], "minlon (+bounds)"),
|
---|
479 | parseAngle(numStr[3], "maxlat (+bounds)"),
|
---|
480 | parseAngle(numStr[2], "maxlon (+bounds)"), false);
|
---|
481 | }
|
---|
482 |
|
---|
483 | public static double parseDouble(Map<String, String> parameters, String parameterName) throws ProjectionConfigurationException {
|
---|
484 | if (!parameters.containsKey(parameterName))
|
---|
485 | throw new ProjectionConfigurationException(tr("Unknown parameter ''{0}''", parameterName));
|
---|
486 | String doubleStr = parameters.get(parameterName);
|
---|
487 | if (doubleStr == null)
|
---|
488 | throw new ProjectionConfigurationException(
|
---|
489 | tr("Expected number argument for parameter ''{0}''", parameterName));
|
---|
490 | return parseDouble(doubleStr, parameterName);
|
---|
491 | }
|
---|
492 |
|
---|
493 | public static double parseDouble(String doubleStr, String parameterName) throws ProjectionConfigurationException {
|
---|
494 | try {
|
---|
495 | return Double.parseDouble(doubleStr);
|
---|
496 | } catch (NumberFormatException e) {
|
---|
497 | throw new ProjectionConfigurationException(
|
---|
498 | tr("Unable to parse value ''{1}'' of parameter ''{0}'' as number.", parameterName, doubleStr), e);
|
---|
499 | }
|
---|
500 | }
|
---|
501 |
|
---|
502 | public static double parseAngle(String angleStr, String parameterName) throws ProjectionConfigurationException {
|
---|
503 | String s = angleStr;
|
---|
504 | double value = 0;
|
---|
505 | boolean neg = false;
|
---|
506 | Matcher m = Pattern.compile("^-").matcher(s);
|
---|
507 | if (m.find()) {
|
---|
508 | neg = true;
|
---|
509 | s = s.substring(m.end());
|
---|
510 | }
|
---|
511 | final String FLOAT = "(\\d+(\\.\\d*)?)";
|
---|
512 | boolean dms = false;
|
---|
513 | double deg = 0.0, min = 0.0, sec = 0.0;
|
---|
514 | // degrees
|
---|
515 | m = Pattern.compile("^"+FLOAT+"d").matcher(s);
|
---|
516 | if (m.find()) {
|
---|
517 | s = s.substring(m.end());
|
---|
518 | deg = Double.parseDouble(m.group(1));
|
---|
519 | dms = true;
|
---|
520 | }
|
---|
521 | // minutes
|
---|
522 | m = Pattern.compile("^"+FLOAT+"'").matcher(s);
|
---|
523 | if (m.find()) {
|
---|
524 | s = s.substring(m.end());
|
---|
525 | min = Double.parseDouble(m.group(1));
|
---|
526 | dms = true;
|
---|
527 | }
|
---|
528 | // seconds
|
---|
529 | m = Pattern.compile("^"+FLOAT+"\"").matcher(s);
|
---|
530 | if (m.find()) {
|
---|
531 | s = s.substring(m.end());
|
---|
532 | sec = Double.parseDouble(m.group(1));
|
---|
533 | dms = true;
|
---|
534 | }
|
---|
535 | // plain number (in degrees)
|
---|
536 | if (dms) {
|
---|
537 | value = deg + (min/60.0) + (sec/3600.0);
|
---|
538 | } else {
|
---|
539 | m = Pattern.compile("^"+FLOAT).matcher(s);
|
---|
540 | if (m.find()) {
|
---|
541 | s = s.substring(m.end());
|
---|
542 | value += Double.parseDouble(m.group(1));
|
---|
543 | }
|
---|
544 | }
|
---|
545 | m = Pattern.compile("^(N|E)", Pattern.CASE_INSENSITIVE).matcher(s);
|
---|
546 | if (m.find()) {
|
---|
547 | s = s.substring(m.end());
|
---|
548 | } else {
|
---|
549 | m = Pattern.compile("^(S|W)", Pattern.CASE_INSENSITIVE).matcher(s);
|
---|
550 | if (m.find()) {
|
---|
551 | s = s.substring(m.end());
|
---|
552 | neg = !neg;
|
---|
553 | }
|
---|
554 | }
|
---|
555 | if (neg) {
|
---|
556 | value = -value;
|
---|
557 | }
|
---|
558 | if (!s.isEmpty()) {
|
---|
559 | throw new ProjectionConfigurationException(
|
---|
560 | tr("Unable to parse value ''{1}'' of parameter ''{0}'' as coordinate value.", parameterName, angleStr));
|
---|
561 | }
|
---|
562 | return value;
|
---|
563 | }
|
---|
564 |
|
---|
565 | @Override
|
---|
566 | public Integer getEpsgCode() {
|
---|
567 | if (code != null && code.startsWith("EPSG:")) {
|
---|
568 | try {
|
---|
569 | return Integer.valueOf(code.substring(5));
|
---|
570 | } catch (NumberFormatException e) {
|
---|
571 | Main.warn(e);
|
---|
572 | }
|
---|
573 | }
|
---|
574 | return null;
|
---|
575 | }
|
---|
576 |
|
---|
577 | @Override
|
---|
578 | public String toCode() {
|
---|
579 | return code != null ? code : "proj:" + (pref == null ? "ERROR" : pref);
|
---|
580 | }
|
---|
581 |
|
---|
582 | @Override
|
---|
583 | public String getCacheDirectoryName() {
|
---|
584 | return cacheDir != null ? cacheDir : "proj-"+Utils.md5Hex(pref == null ? "" : pref).substring(0, 4);
|
---|
585 | }
|
---|
586 |
|
---|
587 | @Override
|
---|
588 | public Bounds getWorldBoundsLatLon() {
|
---|
589 | if (bounds != null) return bounds;
|
---|
590 | return new Bounds(
|
---|
591 | new LatLon(-90.0, -180.0),
|
---|
592 | new LatLon(90.0, 180.0));
|
---|
593 | }
|
---|
594 |
|
---|
595 | @Override
|
---|
596 | public String toString() {
|
---|
597 | return name != null ? name : tr("Custom Projection");
|
---|
598 | }
|
---|
599 |
|
---|
600 | @Override
|
---|
601 | public double getMetersPerUnit() {
|
---|
602 | return metersPerUnit;
|
---|
603 | }
|
---|
604 |
|
---|
605 | @Override
|
---|
606 | public boolean switchXY() {
|
---|
607 | // TODO: support for other axis orientation such as West South, and Up Down
|
---|
608 | return this.axis.startsWith("ne");
|
---|
609 | }
|
---|
610 |
|
---|
611 | private static Map<String, Double> getUnitsToMeters() {
|
---|
612 | Map<String, Double> ret = new ConcurrentHashMap<>();
|
---|
613 | ret.put("km", 1000d);
|
---|
614 | ret.put("m", 1d);
|
---|
615 | ret.put("dm", 1d/10);
|
---|
616 | ret.put("cm", 1d/100);
|
---|
617 | ret.put("mm", 1d/1000);
|
---|
618 | ret.put("kmi", 1852.0);
|
---|
619 | ret.put("in", 0.0254);
|
---|
620 | ret.put("ft", 0.3048);
|
---|
621 | ret.put("yd", 0.9144);
|
---|
622 | ret.put("mi", 1609.344);
|
---|
623 | ret.put("fathom", 1.8288);
|
---|
624 | ret.put("chain", 20.1168);
|
---|
625 | ret.put("link", 0.201168);
|
---|
626 | ret.put("us-in", 1d/39.37);
|
---|
627 | ret.put("us-ft", 0.304800609601219);
|
---|
628 | ret.put("us-yd", 0.914401828803658);
|
---|
629 | ret.put("us-ch", 20.11684023368047);
|
---|
630 | ret.put("us-mi", 1609.347218694437);
|
---|
631 | ret.put("ind-yd", 0.91439523);
|
---|
632 | ret.put("ind-ft", 0.30479841);
|
---|
633 | ret.put("ind-ch", 20.11669506);
|
---|
634 | ret.put("degree", METER_PER_UNIT_DEGREE);
|
---|
635 | return ret;
|
---|
636 | }
|
---|
637 |
|
---|
638 | private static Map<String, Double> getPrimeMeridians() {
|
---|
639 | Map<String, Double> ret = new ConcurrentHashMap<>();
|
---|
640 | try {
|
---|
641 | ret.put("greenwich", 0.0);
|
---|
642 | ret.put("lisbon", parseAngle("9d07'54.862\"W", null));
|
---|
643 | ret.put("paris", parseAngle("2d20'14.025\"E", null));
|
---|
644 | ret.put("bogota", parseAngle("74d04'51.3\"W", null));
|
---|
645 | ret.put("madrid", parseAngle("3d41'16.58\"W", null));
|
---|
646 | ret.put("rome", parseAngle("12d27'8.4\"E", null));
|
---|
647 | ret.put("bern", parseAngle("7d26'22.5\"E", null));
|
---|
648 | ret.put("jakarta", parseAngle("106d48'27.79\"E", null));
|
---|
649 | ret.put("ferro", parseAngle("17d40'W", null));
|
---|
650 | ret.put("brussels", parseAngle("4d22'4.71\"E", null));
|
---|
651 | ret.put("stockholm", parseAngle("18d3'29.8\"E", null));
|
---|
652 | ret.put("athens", parseAngle("23d42'58.815\"E", null));
|
---|
653 | ret.put("oslo", parseAngle("10d43'22.5\"E", null));
|
---|
654 | } catch (ProjectionConfigurationException ex) {
|
---|
655 | throw new RuntimeException();
|
---|
656 | }
|
---|
657 | return ret;
|
---|
658 | }
|
---|
659 | }
|
---|