[5072] | 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;
|
---|
[10870] | 7 | import java.util.Arrays;
|
---|
[9559] | 8 | import java.util.EnumMap;
|
---|
[5072] | 9 | import java.util.HashMap;
|
---|
| 10 | import java.util.List;
|
---|
| 11 | import java.util.Map;
|
---|
[11553] | 12 | import java.util.Optional;
|
---|
[8568] | 13 | import java.util.concurrent.ConcurrentHashMap;
|
---|
[5072] | 14 | import java.util.regex.Matcher;
|
---|
| 15 | import java.util.regex.Pattern;
|
---|
[9562] | 16 |
|
---|
[5072] | 17 | import org.openstreetmap.josm.data.Bounds;
|
---|
[9419] | 18 | import org.openstreetmap.josm.data.ProjectionBounds;
|
---|
| 19 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
[5072] | 20 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
[12792] | 21 | import org.openstreetmap.josm.data.coor.conversion.LatLonParser;
|
---|
[5072] | 22 | import org.openstreetmap.josm.data.projection.datum.CentricDatum;
|
---|
| 23 | import org.openstreetmap.josm.data.projection.datum.Datum;
|
---|
[5226] | 24 | import org.openstreetmap.josm.data.projection.datum.NTV2Datum;
|
---|
[5237] | 25 | import org.openstreetmap.josm.data.projection.datum.NullDatum;
|
---|
[5072] | 26 | import org.openstreetmap.josm.data.projection.datum.SevenParameterDatum;
|
---|
| 27 | import org.openstreetmap.josm.data.projection.datum.ThreeParameterDatum;
|
---|
[5226] | 28 | import org.openstreetmap.josm.data.projection.datum.WGS84Datum;
|
---|
[9532] | 29 | import org.openstreetmap.josm.data.projection.proj.ICentralMeridianProvider;
|
---|
[9565] | 30 | import org.openstreetmap.josm.data.projection.proj.IScaleFactorProvider;
|
---|
[5548] | 31 | import org.openstreetmap.josm.data.projection.proj.Mercator;
|
---|
[5072] | 32 | import org.openstreetmap.josm.data.projection.proj.Proj;
|
---|
| 33 | import org.openstreetmap.josm.data.projection.proj.ProjParameters;
|
---|
[11746] | 34 | import org.openstreetmap.josm.tools.JosmRuntimeException;
|
---|
[12620] | 35 | import org.openstreetmap.josm.tools.Logging;
|
---|
[5072] | 36 | import org.openstreetmap.josm.tools.Utils;
|
---|
[10870] | 37 | import org.openstreetmap.josm.tools.bugreport.BugReport;
|
---|
[5072] | 38 |
|
---|
| 39 | /**
|
---|
[7370] | 40 | * Custom projection.
|
---|
[5072] | 41 | *
|
---|
| 42 | * Inspired by PROJ.4 and Proj4J.
|
---|
[7370] | 43 | * @since 5072
|
---|
[5072] | 44 | */
|
---|
[5226] | 45 | public class CustomProjection extends AbstractProjection {
|
---|
[5072] | 46 |
|
---|
[9630] | 47 | /*
|
---|
[9642] | 48 | * Equation for METER_PER_UNIT_DEGREE taken from:
|
---|
| 49 | * https://github.com/openlayers/ol3/blob/master/src/ol/proj/epsg4326projection.js#L58
|
---|
[9630] | 50 | * Value for Radius taken form:
|
---|
[9642] | 51 | * https://github.com/openlayers/ol3/blob/master/src/ol/sphere/wgs84sphere.js#L11
|
---|
[9630] | 52 | */
|
---|
[9642] | 53 | private static final double METER_PER_UNIT_DEGREE = 2 * Math.PI * 6378137.0 / 360;
|
---|
[8570] | 54 | private static final Map<String, Double> UNITS_TO_METERS = getUnitsToMeters();
|
---|
[9105] | 55 | private static final Map<String, Double> PRIME_MERIDANS = getPrimeMeridians();
|
---|
[8568] | 56 |
|
---|
[5226] | 57 | /**
|
---|
| 58 | * pref String that defines the projection
|
---|
| 59 | *
|
---|
| 60 | * null means fall back mode (Mercator)
|
---|
| 61 | */
|
---|
[5234] | 62 | protected String pref;
|
---|
[5548] | 63 | protected String name;
|
---|
| 64 | protected String code;
|
---|
[5228] | 65 | protected Bounds bounds;
|
---|
[9790] | 66 | private double metersPerUnitWMTS;
|
---|
[8584] | 67 | private String axis = "enu"; // default axis orientation is East, North, Up
|
---|
[5072] | 68 |
|
---|
[10870] | 69 | private static final List<String> LON_LAT_VALUES = Arrays.asList("longlat", "latlon", "latlong");
|
---|
| 70 |
|
---|
[7370] | 71 | /**
|
---|
| 72 | * Proj4-like projection parameters. See <a href="https://trac.osgeo.org/proj/wiki/GenParms">reference</a>.
|
---|
| 73 | * @since 7370 (public)
|
---|
| 74 | */
|
---|
[8836] | 75 | public enum Param {
|
---|
[5227] | 76 |
|
---|
[7370] | 77 | /** False easting */
|
---|
[5239] | 78 | x_0("x_0", true),
|
---|
[7370] | 79 | /** False northing */
|
---|
[5239] | 80 | y_0("y_0", true),
|
---|
[7370] | 81 | /** Central meridian */
|
---|
[5239] | 82 | lon_0("lon_0", true),
|
---|
[9105] | 83 | /** Prime meridian */
|
---|
| 84 | pm("pm", true),
|
---|
[7370] | 85 | /** Scaling factor */
|
---|
[5239] | 86 | k_0("k_0", true),
|
---|
[7370] | 87 | /** Ellipsoid name (see {@code proj -le}) */
|
---|
[5239] | 88 | ellps("ellps", true),
|
---|
[7370] | 89 | /** Semimajor radius of the ellipsoid axis */
|
---|
[5239] | 90 | a("a", true),
|
---|
[7370] | 91 | /** Eccentricity of the ellipsoid squared */
|
---|
[5239] | 92 | es("es", true),
|
---|
[7370] | 93 | /** Reciprocal of the ellipsoid flattening term (e.g. 298) */
|
---|
[5239] | 94 | rf("rf", true),
|
---|
[7370] | 95 | /** Flattening of the ellipsoid = 1-sqrt(1-e^2) */
|
---|
[5239] | 96 | f("f", true),
|
---|
[7370] | 97 | /** Semiminor radius of the ellipsoid axis */
|
---|
[5239] | 98 | b("b", true),
|
---|
[7370] | 99 | /** Datum name (see {@code proj -ld}) */
|
---|
[5239] | 100 | datum("datum", true),
|
---|
[7370] | 101 | /** 3 or 7 term datum transform parameters */
|
---|
[5239] | 102 | towgs84("towgs84", true),
|
---|
[7370] | 103 | /** Filename of NTv2 grid file to use for datum transforms */
|
---|
[5239] | 104 | nadgrids("nadgrids", true),
|
---|
[7370] | 105 | /** Projection name (see {@code proj -l}) */
|
---|
[5239] | 106 | proj("proj", true),
|
---|
[7370] | 107 | /** Latitude of origin */
|
---|
[5239] | 108 | lat_0("lat_0", true),
|
---|
[7370] | 109 | /** Latitude of first standard parallel */
|
---|
[5239] | 110 | lat_1("lat_1", true),
|
---|
[7370] | 111 | /** Latitude of second standard parallel */
|
---|
[5239] | 112 | lat_2("lat_2", true),
|
---|
[9532] | 113 | /** Latitude of true scale (Polar Stereographic) */
|
---|
[9419] | 114 | lat_ts("lat_ts", true),
|
---|
[9532] | 115 | /** longitude of the center of the projection (Oblique Mercator) */
|
---|
| 116 | lonc("lonc", true),
|
---|
| 117 | /** azimuth (true) of the center line passing through the center of the
|
---|
| 118 | * projection (Oblique Mercator) */
|
---|
| 119 | alpha("alpha", true),
|
---|
| 120 | /** rectified bearing of the center line (Oblique Mercator) */
|
---|
| 121 | gamma("gamma", true),
|
---|
| 122 | /** select "Hotine" variant of Oblique Mercator */
|
---|
| 123 | no_off("no_off", false),
|
---|
| 124 | /** legacy alias for no_off */
|
---|
| 125 | no_uoff("no_uoff", false),
|
---|
| 126 | /** longitude of first point (Oblique Mercator) */
|
---|
| 127 | lon_1("lon_1", true),
|
---|
| 128 | /** longitude of second point (Oblique Mercator) */
|
---|
| 129 | lon_2("lon_2", true),
|
---|
[7370] | 130 | /** the exact proj.4 string will be preserved in the WKT representation */
|
---|
[5239] | 131 | wktext("wktext", false), // ignored
|
---|
[7370] | 132 | /** meters, US survey feet, etc. */
|
---|
[8568] | 133 | units("units", true),
|
---|
[7370] | 134 | /** Don't use the /usr/share/proj/proj_def.dat defaults file */
|
---|
[5239] | 135 | no_defs("no_defs", false),
|
---|
| 136 | init("init", true),
|
---|
[8584] | 137 | /** crs units to meter multiplier */
|
---|
[8568] | 138 | to_meter("to_meter", true),
|
---|
[8584] | 139 | /** definition of axis for projection */
|
---|
| 140 | axis("axis", true),
|
---|
[8609] | 141 | /** UTM zone */
|
---|
| 142 | zone("zone", true),
|
---|
| 143 | /** indicate southern hemisphere for UTM */
|
---|
| 144 | south("south", false),
|
---|
[9125] | 145 | /** vertical units - ignore, as we don't use height information */
|
---|
| 146 | vunits("vunits", true),
|
---|
[6854] | 147 | // JOSM extensions, not present in PROJ.4
|
---|
| 148 | wmssrs("wmssrs", true),
|
---|
[5239] | 149 | bounds("bounds", true);
|
---|
[5227] | 150 |
|
---|
[7371] | 151 | /** Parameter key */
|
---|
| 152 | public final String key;
|
---|
| 153 | /** {@code true} if the parameter has a value */
|
---|
| 154 | public final boolean hasValue;
|
---|
[5227] | 155 |
|
---|
[7371] | 156 | /** Map of all parameters by key */
|
---|
[8568] | 157 | static final Map<String, Param> paramsByKey = new ConcurrentHashMap<>();
|
---|
[5227] | 158 | static {
|
---|
[5239] | 159 | for (Param p : Param.values()) {
|
---|
[5227] | 160 | paramsByKey.put(p.key, p);
|
---|
| 161 | }
|
---|
[10870] | 162 | // alias
|
---|
| 163 | paramsByKey.put("k", Param.k_0);
|
---|
[5227] | 164 | }
|
---|
| 165 |
|
---|
[5239] | 166 | Param(String key, boolean hasValue) {
|
---|
[5227] | 167 | this.key = key;
|
---|
| 168 | this.hasValue = hasValue;
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 |
|
---|
[11931] | 172 | enum Polarity {
|
---|
[10870] | 173 | NORTH(LatLon.NORTH_POLE),
|
---|
| 174 | SOUTH(LatLon.SOUTH_POLE);
|
---|
[9559] | 175 |
|
---|
[10870] | 176 | private final LatLon latlon;
|
---|
| 177 |
|
---|
| 178 | Polarity(LatLon latlon) {
|
---|
| 179 | this.latlon = latlon;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[11931] | 182 | LatLon getLatLon() {
|
---|
[10870] | 183 | return latlon;
|
---|
| 184 | }
|
---|
[9559] | 185 | }
|
---|
| 186 |
|
---|
[10870] | 187 | private EnumMap<Polarity, EastNorth> polesEN;
|
---|
| 188 |
|
---|
[7370] | 189 | /**
|
---|
| 190 | * Constructs a new empty {@code CustomProjection}.
|
---|
| 191 | */
|
---|
[5234] | 192 | public CustomProjection() {
|
---|
[8415] | 193 | // contents can be set later with update()
|
---|
[5234] | 194 | }
|
---|
| 195 |
|
---|
[7370] | 196 | /**
|
---|
| 197 | * Constructs a new {@code CustomProjection} with given parameters.
|
---|
[8509] | 198 | * @param pref String containing projection parameters
|
---|
| 199 | * (ex: "+proj=tmerc +lon_0=-3 +k_0=0.9996 +x_0=500000 +ellps=WGS84 +datum=WGS84 +bounds=-8,-5,2,85")
|
---|
[7370] | 200 | */
|
---|
[5234] | 201 | public CustomProjection(String pref) {
|
---|
[12294] | 202 | this(null, null, pref);
|
---|
[5548] | 203 | }
|
---|
| 204 |
|
---|
| 205 | /**
|
---|
[7370] | 206 | * Constructs a new {@code CustomProjection} with given name, code and parameters.
|
---|
[5548] | 207 | *
|
---|
| 208 | * @param name describe projection in one or two words
|
---|
| 209 | * @param code unique code for this projection - may be null
|
---|
| 210 | * @param pref the string that defines the custom projection
|
---|
| 211 | */
|
---|
[12293] | 212 | public CustomProjection(String name, String code, String pref) {
|
---|
[5548] | 213 | this.name = name;
|
---|
| 214 | this.code = code;
|
---|
| 215 | this.pref = pref;
|
---|
[5234] | 216 | try {
|
---|
| 217 | update(pref);
|
---|
| 218 | } catch (ProjectionConfigurationException ex) {
|
---|
[12620] | 219 | Logging.trace(ex);
|
---|
[5234] | 220 | try {
|
---|
| 221 | update(null);
|
---|
| 222 | } catch (ProjectionConfigurationException ex1) {
|
---|
[10870] | 223 | throw BugReport.intercept(ex1).put("name", name).put("code", code).put("pref", pref);
|
---|
[5234] | 224 | }
|
---|
| 225 | }
|
---|
| 226 | }
|
---|
| 227 |
|
---|
[7370] | 228 | /**
|
---|
| 229 | * Updates this {@code CustomProjection} with given parameters.
|
---|
| 230 | * @param pref String containing projection parameters (ex: "+proj=lonlat +ellps=WGS84 +datum=WGS84 +bounds=-180,-90,180,90")
|
---|
| 231 | * @throws ProjectionConfigurationException if {@code pref} cannot be parsed properly
|
---|
| 232 | */
|
---|
[6890] | 233 | public final void update(String pref) throws ProjectionConfigurationException {
|
---|
[5228] | 234 | this.pref = pref;
|
---|
[5226] | 235 | if (pref == null) {
|
---|
| 236 | ellps = Ellipsoid.WGS84;
|
---|
| 237 | datum = WGS84Datum.INSTANCE;
|
---|
[5548] | 238 | proj = new Mercator();
|
---|
[5228] | 239 | bounds = new Bounds(
|
---|
[6203] | 240 | -85.05112877980659, -180.0,
|
---|
| 241 | 85.05112877980659, 180.0, true);
|
---|
[5226] | 242 | } else {
|
---|
[9128] | 243 | Map<String, String> parameters = parseParameterList(pref, false);
|
---|
| 244 | parameters = resolveInits(parameters, false);
|
---|
[5072] | 245 | ellps = parseEllipsoid(parameters);
|
---|
| 246 | datum = parseDatum(parameters, ellps);
|
---|
[8610] | 247 | if (ellps == null) {
|
---|
| 248 | ellps = datum.getEllipsoid();
|
---|
| 249 | }
|
---|
[5072] | 250 | proj = parseProjection(parameters, ellps);
|
---|
[8609] | 251 | // "utm" is a shortcut for a set of parameters
|
---|
| 252 | if ("utm".equals(parameters.get(Param.proj.key))) {
|
---|
[10870] | 253 | Integer zone;
|
---|
[8609] | 254 | try {
|
---|
[11553] | 255 | zone = Integer.valueOf(Optional.ofNullable(parameters.get(Param.zone.key)).orElseThrow(
|
---|
| 256 | () -> new ProjectionConfigurationException(tr("UTM projection (''+proj=utm'') requires ''+zone=...'' parameter."))));
|
---|
[8609] | 257 | } catch (NumberFormatException e) {
|
---|
| 258 | zone = null;
|
---|
| 259 | }
|
---|
| 260 | if (zone == null || zone < 1 || zone > 60)
|
---|
[8638] | 261 | throw new ProjectionConfigurationException(tr("Expected integer value in range 1-60 for ''+zone=...'' parameter."));
|
---|
[10181] | 262 | this.lon0 = 6d * zone - 183d;
|
---|
[8609] | 263 | this.k0 = 0.9996;
|
---|
[11100] | 264 | this.x0 = 500_000;
|
---|
| 265 | this.y0 = parameters.containsKey(Param.south.key) ? 10_000_000 : 0;
|
---|
[8609] | 266 | }
|
---|
[5227] | 267 | String s = parameters.get(Param.x_0.key);
|
---|
[5072] | 268 | if (s != null) {
|
---|
[8346] | 269 | this.x0 = parseDouble(s, Param.x_0.key);
|
---|
[5072] | 270 | }
|
---|
[5227] | 271 | s = parameters.get(Param.y_0.key);
|
---|
[5072] | 272 | if (s != null) {
|
---|
[8346] | 273 | this.y0 = parseDouble(s, Param.y_0.key);
|
---|
[5072] | 274 | }
|
---|
[5227] | 275 | s = parameters.get(Param.lon_0.key);
|
---|
[5072] | 276 | if (s != null) {
|
---|
[8346] | 277 | this.lon0 = parseAngle(s, Param.lon_0.key);
|
---|
[5072] | 278 | }
|
---|
[9532] | 279 | if (proj instanceof ICentralMeridianProvider) {
|
---|
| 280 | this.lon0 = ((ICentralMeridianProvider) proj).getCentralMeridian();
|
---|
| 281 | }
|
---|
[9105] | 282 | s = parameters.get(Param.pm.key);
|
---|
| 283 | if (s != null) {
|
---|
| 284 | if (PRIME_MERIDANS.containsKey(s)) {
|
---|
| 285 | this.pm = PRIME_MERIDANS.get(s);
|
---|
| 286 | } else {
|
---|
| 287 | this.pm = parseAngle(s, Param.pm.key);
|
---|
| 288 | }
|
---|
| 289 | }
|
---|
[5227] | 290 | s = parameters.get(Param.k_0.key);
|
---|
[5072] | 291 | if (s != null) {
|
---|
[8346] | 292 | this.k0 = parseDouble(s, Param.k_0.key);
|
---|
[5072] | 293 | }
|
---|
[9565] | 294 | if (proj instanceof IScaleFactorProvider) {
|
---|
| 295 | this.k0 *= ((IScaleFactorProvider) proj).getScaleFactor();
|
---|
| 296 | }
|
---|
[5228] | 297 | s = parameters.get(Param.bounds.key);
|
---|
[13598] | 298 | this.bounds = s != null ? parseBounds(s) : null;
|
---|
[6854] | 299 | s = parameters.get(Param.wmssrs.key);
|
---|
| 300 | if (s != null) {
|
---|
| 301 | this.code = s;
|
---|
| 302 | }
|
---|
[9628] | 303 | boolean defaultUnits = true;
|
---|
[8568] | 304 | s = parameters.get(Param.units.key);
|
---|
| 305 | if (s != null) {
|
---|
[8611] | 306 | s = Utils.strip(s, "\"");
|
---|
| 307 | if (UNITS_TO_METERS.containsKey(s)) {
|
---|
[9790] | 308 | this.toMeter = UNITS_TO_METERS.get(s);
|
---|
| 309 | this.metersPerUnitWMTS = this.toMeter;
|
---|
[9628] | 310 | defaultUnits = false;
|
---|
[8611] | 311 | } else {
|
---|
[9790] | 312 | throw new ProjectionConfigurationException(tr("No unit found for: {0}", s));
|
---|
[8611] | 313 | }
|
---|
[8568] | 314 | }
|
---|
| 315 | s = parameters.get(Param.to_meter.key);
|
---|
| 316 | if (s != null) {
|
---|
[9790] | 317 | this.toMeter = parseDouble(s, Param.to_meter.key);
|
---|
| 318 | this.metersPerUnitWMTS = this.toMeter;
|
---|
[9628] | 319 | defaultUnits = false;
|
---|
[8568] | 320 | }
|
---|
[9628] | 321 | if (defaultUnits) {
|
---|
[9790] | 322 | this.toMeter = 1;
|
---|
| 323 | this.metersPerUnitWMTS = proj.isGeographic() ? METER_PER_UNIT_DEGREE : 1;
|
---|
[9628] | 324 | }
|
---|
[8584] | 325 | s = parameters.get(Param.axis.key);
|
---|
| 326 | if (s != null) {
|
---|
[10378] | 327 | this.axis = s;
|
---|
[8584] | 328 | }
|
---|
[5072] | 329 | }
|
---|
| 330 | }
|
---|
| 331 |
|
---|
[9128] | 332 | /**
|
---|
| 333 | * Parse a parameter list to key=value pairs.
|
---|
| 334 | *
|
---|
| 335 | * @param pref the parameter list
|
---|
| 336 | * @param ignoreUnknownParameter true, if unknown parameter should not raise exception
|
---|
| 337 | * @return parameters map
|
---|
[9135] | 338 | * @throws ProjectionConfigurationException in case of invalid parameter
|
---|
[9128] | 339 | */
|
---|
| 340 | public static Map<String, String> parseParameterList(String pref, boolean ignoreUnknownParameter) throws ProjectionConfigurationException {
|
---|
[7005] | 341 | Map<String, String> parameters = new HashMap<>();
|
---|
[11889] | 342 | String trimmedPref = pref.trim();
|
---|
| 343 | if (trimmedPref.isEmpty()) {
|
---|
[10870] | 344 | return parameters;
|
---|
[5228] | 345 | }
|
---|
[10870] | 346 |
|
---|
| 347 | Pattern keyPattern = Pattern.compile("\\+(?<key>[a-zA-Z0-9_]+)(=(?<value>.*))?");
|
---|
[11889] | 348 | String[] parts = Utils.WHITE_SPACES_PATTERN.split(trimmedPref);
|
---|
[6104] | 349 | for (String part : parts) {
|
---|
[10870] | 350 | Matcher m = keyPattern.matcher(part);
|
---|
[5228] | 351 | if (m.matches()) {
|
---|
[10870] | 352 | String key = m.group("key");
|
---|
| 353 | String value = m.group("value");
|
---|
| 354 | // some aliases
|
---|
| 355 | if (key.equals(Param.proj.key) && LON_LAT_VALUES.contains(value)) {
|
---|
| 356 | value = "lonlat";
|
---|
[5228] | 357 | }
|
---|
[10870] | 358 | Param param = Param.paramsByKey.get(key);
|
---|
| 359 | if (param == null) {
|
---|
[9128] | 360 | if (!ignoreUnknownParameter)
|
---|
| 361 | throw new ProjectionConfigurationException(tr("Unknown parameter: ''{0}''.", key));
|
---|
| 362 | } else {
|
---|
[10870] | 363 | if (param.hasValue && value == null)
|
---|
[9128] | 364 | throw new ProjectionConfigurationException(tr("Value expected for parameter ''{0}''.", key));
|
---|
[10870] | 365 | if (!param.hasValue && value != null)
|
---|
[9128] | 366 | throw new ProjectionConfigurationException(tr("No value expected for parameter ''{0}''.", key));
|
---|
[10870] | 367 | key = param.key; // To be really sure, we might have an alias.
|
---|
[9128] | 368 | }
|
---|
[5228] | 369 | parameters.put(key, value);
|
---|
[10870] | 370 | } else if (!part.startsWith("+")) {
|
---|
| 371 | throw new ProjectionConfigurationException(tr("Parameter must begin with a ''+'' character (found ''{0}'')", part));
|
---|
| 372 | } else {
|
---|
[5228] | 373 | throw new ProjectionConfigurationException(tr("Unexpected parameter format (''{0}'')", part));
|
---|
[10870] | 374 | }
|
---|
[5228] | 375 | }
|
---|
[9128] | 376 | return parameters;
|
---|
| 377 | }
|
---|
| 378 |
|
---|
| 379 | /**
|
---|
| 380 | * Recursive resolution of +init includes.
|
---|
| 381 | *
|
---|
| 382 | * @param parameters parameters map
|
---|
| 383 | * @param ignoreUnknownParameter true, if unknown parameter should not raise exception
|
---|
| 384 | * @return parameters map with +init includes resolved
|
---|
[9135] | 385 | * @throws ProjectionConfigurationException in case of invalid parameter
|
---|
[9128] | 386 | */
|
---|
[9135] | 387 | public static Map<String, String> resolveInits(Map<String, String> parameters, boolean ignoreUnknownParameter)
|
---|
| 388 | throws ProjectionConfigurationException {
|
---|
[5228] | 389 | // recursive resolution of +init includes
|
---|
| 390 | String initKey = parameters.get(Param.init.key);
|
---|
| 391 | if (initKey != null) {
|
---|
[9128] | 392 | Map<String, String> initp;
|
---|
[5228] | 393 | try {
|
---|
[11553] | 394 | initp = parseParameterList(Optional.ofNullable(Projections.getInit(initKey)).orElseThrow(
|
---|
| 395 | () -> new ProjectionConfigurationException(tr("Value ''{0}'' for option +init not supported.", initKey))),
|
---|
| 396 | ignoreUnknownParameter);
|
---|
[9128] | 397 | initp = resolveInits(initp, ignoreUnknownParameter);
|
---|
[5228] | 398 | } catch (ProjectionConfigurationException ex) {
|
---|
[9128] | 399 | throw new ProjectionConfigurationException(initKey+": "+ex.getMessage(), ex);
|
---|
[5228] | 400 | }
|
---|
[9128] | 401 | initp.putAll(parameters);
|
---|
[5228] | 402 | return initp;
|
---|
| 403 | }
|
---|
| 404 | return parameters;
|
---|
| 405 | }
|
---|
| 406 |
|
---|
[10870] | 407 | /**
|
---|
| 408 | * Gets the ellipsoid
|
---|
| 409 | * @param parameters The parameters to get the value from
|
---|
| 410 | * @return The Ellipsoid as specified with the parameters
|
---|
| 411 | * @throws ProjectionConfigurationException in case of invalid parameters
|
---|
| 412 | */
|
---|
[5072] | 413 | public Ellipsoid parseEllipsoid(Map<String, String> parameters) throws ProjectionConfigurationException {
|
---|
[5227] | 414 | String code = parameters.get(Param.ellps.key);
|
---|
[5072] | 415 | if (code != null) {
|
---|
[11553] | 416 | return Optional.ofNullable(Projections.getEllipsoid(code)).orElseThrow(
|
---|
| 417 | () -> new ProjectionConfigurationException(tr("Ellipsoid ''{0}'' not supported.", code)));
|
---|
[5072] | 418 | }
|
---|
[5227] | 419 | String s = parameters.get(Param.a.key);
|
---|
[5072] | 420 | if (s != null) {
|
---|
[5227] | 421 | double a = parseDouble(s, Param.a.key);
|
---|
| 422 | if (parameters.get(Param.es.key) != null) {
|
---|
| 423 | double es = parseDouble(parameters, Param.es.key);
|
---|
[10748] | 424 | return Ellipsoid.createAes(a, es);
|
---|
[5072] | 425 | }
|
---|
[5227] | 426 | if (parameters.get(Param.rf.key) != null) {
|
---|
| 427 | double rf = parseDouble(parameters, Param.rf.key);
|
---|
[10748] | 428 | return Ellipsoid.createArf(a, rf);
|
---|
[5072] | 429 | }
|
---|
[5227] | 430 | if (parameters.get(Param.f.key) != null) {
|
---|
| 431 | double f = parseDouble(parameters, Param.f.key);
|
---|
[10748] | 432 | return Ellipsoid.createAf(a, f);
|
---|
[5072] | 433 | }
|
---|
[5227] | 434 | if (parameters.get(Param.b.key) != null) {
|
---|
| 435 | double b = parseDouble(parameters, Param.b.key);
|
---|
[10748] | 436 | return Ellipsoid.createAb(a, b);
|
---|
[5072] | 437 | }
|
---|
| 438 | }
|
---|
[5227] | 439 | if (parameters.containsKey(Param.a.key) ||
|
---|
| 440 | parameters.containsKey(Param.es.key) ||
|
---|
| 441 | parameters.containsKey(Param.rf.key) ||
|
---|
| 442 | parameters.containsKey(Param.f.key) ||
|
---|
| 443 | parameters.containsKey(Param.b.key))
|
---|
[5072] | 444 | throw new ProjectionConfigurationException(tr("Combination of ellipsoid parameters is not supported."));
|
---|
[8610] | 445 | return null;
|
---|
[5072] | 446 | }
|
---|
| 447 |
|
---|
[10870] | 448 | /**
|
---|
| 449 | * Gets the datum
|
---|
| 450 | * @param parameters The parameters to get the value from
|
---|
| 451 | * @param ellps The ellisoid that was previously computed
|
---|
| 452 | * @return The Datum as specified with the parameters
|
---|
| 453 | * @throws ProjectionConfigurationException in case of invalid parameters
|
---|
| 454 | */
|
---|
[5072] | 455 | public Datum parseDatum(Map<String, String> parameters, Ellipsoid ellps) throws ProjectionConfigurationException {
|
---|
[8610] | 456 | String datumId = parameters.get(Param.datum.key);
|
---|
| 457 | if (datumId != null) {
|
---|
[11553] | 458 | return Optional.ofNullable(Projections.getDatum(datumId)).orElseThrow(
|
---|
| 459 | () -> new ProjectionConfigurationException(tr("Unknown datum identifier: ''{0}''", datumId)));
|
---|
[8610] | 460 | }
|
---|
| 461 | if (ellps == null) {
|
---|
| 462 | if (parameters.containsKey(Param.no_defs.key))
|
---|
| 463 | throw new ProjectionConfigurationException(tr("Ellipsoid required (+ellps=* or +a=*, +b=*)"));
|
---|
| 464 | // nothing specified, use WGS84 as default
|
---|
| 465 | ellps = Ellipsoid.WGS84;
|
---|
| 466 | }
|
---|
[8611] | 467 |
|
---|
[5227] | 468 | String nadgridsId = parameters.get(Param.nadgrids.key);
|
---|
[5226] | 469 | if (nadgridsId != null) {
|
---|
[5237] | 470 | if (nadgridsId.startsWith("@")) {
|
---|
| 471 | nadgridsId = nadgridsId.substring(1);
|
---|
| 472 | }
|
---|
[6990] | 473 | if ("null".equals(nadgridsId))
|
---|
[5237] | 474 | return new NullDatum(null, ellps);
|
---|
[11553] | 475 | final String fNadgridsId = nadgridsId;
|
---|
| 476 | return new NTV2Datum(fNadgridsId, null, ellps, Optional.ofNullable(Projections.getNTV2Grid(fNadgridsId)).orElseThrow(
|
---|
| 477 | () -> new ProjectionConfigurationException(tr("Grid shift file ''{0}'' for option +nadgrids not supported.", fNadgridsId))));
|
---|
[5226] | 478 | }
|
---|
| 479 |
|
---|
[5227] | 480 | String towgs84 = parameters.get(Param.towgs84.key);
|
---|
[5072] | 481 | if (towgs84 != null)
|
---|
| 482 | return parseToWGS84(towgs84, ellps);
|
---|
| 483 |
|
---|
[9106] | 484 | return new NullDatum(null, ellps);
|
---|
[5072] | 485 | }
|
---|
| 486 |
|
---|
[11889] | 487 | /**
|
---|
| 488 | * Parse {@code towgs84} parameter.
|
---|
| 489 | * @param paramList List of parameter arguments (expected: 3 or 7)
|
---|
| 490 | * @param ellps ellipsoid
|
---|
| 491 | * @return parsed datum ({@link ThreeParameterDatum} or {@link SevenParameterDatum})
|
---|
| 492 | * @throws ProjectionConfigurationException if the arguments cannot be parsed
|
---|
| 493 | */
|
---|
[5072] | 494 | public Datum parseToWGS84(String paramList, Ellipsoid ellps) throws ProjectionConfigurationException {
|
---|
| 495 | String[] numStr = paramList.split(",");
|
---|
| 496 |
|
---|
| 497 | if (numStr.length != 3 && numStr.length != 7)
|
---|
| 498 | throw new ProjectionConfigurationException(tr("Unexpected number of arguments for parameter ''towgs84'' (must be 3 or 7)"));
|
---|
[7005] | 499 | List<Double> towgs84Param = new ArrayList<>();
|
---|
[6104] | 500 | for (String str : numStr) {
|
---|
[5072] | 501 | try {
|
---|
[8390] | 502 | towgs84Param.add(Double.valueOf(str));
|
---|
[5072] | 503 | } catch (NumberFormatException e) {
|
---|
[6798] | 504 | throw new ProjectionConfigurationException(tr("Unable to parse value of parameter ''towgs84'' (''{0}'')", str), e);
|
---|
[5072] | 505 | }
|
---|
| 506 | }
|
---|
[5237] | 507 | boolean isCentric = true;
|
---|
[6104] | 508 | for (Double param : towgs84Param) {
|
---|
[8393] | 509 | if (param != 0) {
|
---|
[5237] | 510 | isCentric = false;
|
---|
| 511 | break;
|
---|
| 512 | }
|
---|
| 513 | }
|
---|
| 514 | if (isCentric)
|
---|
| 515 | return new CentricDatum(null, null, ellps);
|
---|
[5072] | 516 | boolean is3Param = true;
|
---|
[8510] | 517 | for (int i = 3; i < towgs84Param.size(); i++) {
|
---|
[8393] | 518 | if (towgs84Param.get(i) != 0) {
|
---|
[5072] | 519 | is3Param = false;
|
---|
| 520 | break;
|
---|
| 521 | }
|
---|
| 522 | }
|
---|
[5237] | 523 | if (is3Param)
|
---|
| 524 | return new ThreeParameterDatum(null, null, ellps,
|
---|
[5072] | 525 | towgs84Param.get(0),
|
---|
| 526 | towgs84Param.get(1),
|
---|
[5237] | 527 | towgs84Param.get(2));
|
---|
| 528 | else
|
---|
| 529 | return new SevenParameterDatum(null, null, ellps,
|
---|
[5072] | 530 | towgs84Param.get(0),
|
---|
| 531 | towgs84Param.get(1),
|
---|
| 532 | towgs84Param.get(2),
|
---|
| 533 | towgs84Param.get(3),
|
---|
| 534 | towgs84Param.get(4),
|
---|
| 535 | towgs84Param.get(5),
|
---|
[5237] | 536 | towgs84Param.get(6));
|
---|
[5072] | 537 | }
|
---|
| 538 |
|
---|
[10870] | 539 | /**
|
---|
| 540 | * Gets a projection using the given ellipsoid
|
---|
| 541 | * @param parameters Additional parameters
|
---|
| 542 | * @param ellps The {@link Ellipsoid}
|
---|
| 543 | * @return The projection
|
---|
| 544 | * @throws ProjectionConfigurationException in case of invalid parameters
|
---|
| 545 | */
|
---|
[5072] | 546 | public Proj parseProjection(Map<String, String> parameters, Ellipsoid ellps) throws ProjectionConfigurationException {
|
---|
[6142] | 547 | String id = parameters.get(Param.proj.key);
|
---|
[5072] | 548 | if (id == null) throw new ProjectionConfigurationException(tr("Projection required (+proj=*)"));
|
---|
| 549 |
|
---|
[8609] | 550 | // "utm" is not a real projection, but a shortcut for a set of parameters
|
---|
| 551 | if ("utm".equals(id)) {
|
---|
| 552 | id = "tmerc";
|
---|
| 553 | }
|
---|
[10378] | 554 | Proj proj = Projections.getBaseProjection(id);
|
---|
[7184] | 555 | if (proj == null) throw new ProjectionConfigurationException(tr("Unknown projection identifier: ''{0}''", id));
|
---|
[5072] | 556 |
|
---|
| 557 | ProjParameters projParams = new ProjParameters();
|
---|
| 558 |
|
---|
| 559 | projParams.ellps = ellps;
|
---|
| 560 |
|
---|
| 561 | String s;
|
---|
[5227] | 562 | s = parameters.get(Param.lat_0.key);
|
---|
[5072] | 563 | if (s != null) {
|
---|
[8346] | 564 | projParams.lat0 = parseAngle(s, Param.lat_0.key);
|
---|
[5072] | 565 | }
|
---|
[5227] | 566 | s = parameters.get(Param.lat_1.key);
|
---|
[5072] | 567 | if (s != null) {
|
---|
[8346] | 568 | projParams.lat1 = parseAngle(s, Param.lat_1.key);
|
---|
[5072] | 569 | }
|
---|
[5227] | 570 | s = parameters.get(Param.lat_2.key);
|
---|
[5072] | 571 | if (s != null) {
|
---|
[8346] | 572 | projParams.lat2 = parseAngle(s, Param.lat_2.key);
|
---|
[5072] | 573 | }
|
---|
[9419] | 574 | s = parameters.get(Param.lat_ts.key);
|
---|
| 575 | if (s != null) {
|
---|
| 576 | projParams.lat_ts = parseAngle(s, Param.lat_ts.key);
|
---|
| 577 | }
|
---|
[9532] | 578 | s = parameters.get(Param.lonc.key);
|
---|
| 579 | if (s != null) {
|
---|
| 580 | projParams.lonc = parseAngle(s, Param.lonc.key);
|
---|
| 581 | }
|
---|
| 582 | s = parameters.get(Param.alpha.key);
|
---|
| 583 | if (s != null) {
|
---|
| 584 | projParams.alpha = parseAngle(s, Param.alpha.key);
|
---|
| 585 | }
|
---|
| 586 | s = parameters.get(Param.gamma.key);
|
---|
| 587 | if (s != null) {
|
---|
| 588 | projParams.gamma = parseAngle(s, Param.gamma.key);
|
---|
| 589 | }
|
---|
[13598] | 590 | s = parameters.get(Param.lon_0.key);
|
---|
| 591 | if (s != null) {
|
---|
| 592 | projParams.lon0 = parseAngle(s, Param.lon_0.key);
|
---|
| 593 | }
|
---|
[9532] | 594 | s = parameters.get(Param.lon_1.key);
|
---|
| 595 | if (s != null) {
|
---|
| 596 | projParams.lon1 = parseAngle(s, Param.lon_1.key);
|
---|
| 597 | }
|
---|
| 598 | s = parameters.get(Param.lon_2.key);
|
---|
| 599 | if (s != null) {
|
---|
| 600 | projParams.lon2 = parseAngle(s, Param.lon_2.key);
|
---|
| 601 | }
|
---|
| 602 | if (parameters.containsKey(Param.no_off.key) || parameters.containsKey(Param.no_uoff.key)) {
|
---|
[10000] | 603 | projParams.no_off = Boolean.TRUE;
|
---|
[9532] | 604 | }
|
---|
[5072] | 605 | proj.initialize(projParams);
|
---|
| 606 | return proj;
|
---|
[5228] | 607 | }
|
---|
[5072] | 608 |
|
---|
[10870] | 609 | /**
|
---|
| 610 | * Converts a string to a bounds object
|
---|
| 611 | * @param boundsStr The string as comma separated list of angles.
|
---|
| 612 | * @return The bounds.
|
---|
| 613 | * @throws ProjectionConfigurationException in case of invalid parameter
|
---|
[10871] | 614 | * @see CustomProjection#parseAngle(String, String)
|
---|
[10870] | 615 | */
|
---|
[5279] | 616 | public static Bounds parseBounds(String boundsStr) throws ProjectionConfigurationException {
|
---|
[5228] | 617 | String[] numStr = boundsStr.split(",");
|
---|
| 618 | if (numStr.length != 4)
|
---|
| 619 | throw new ProjectionConfigurationException(tr("Unexpected number of arguments for parameter ''+bounds'' (must be 4)"));
|
---|
| 620 | return new Bounds(parseAngle(numStr[1], "minlat (+bounds)"),
|
---|
| 621 | parseAngle(numStr[0], "minlon (+bounds)"),
|
---|
| 622 | parseAngle(numStr[3], "maxlat (+bounds)"),
|
---|
[5235] | 623 | parseAngle(numStr[2], "maxlon (+bounds)"), false);
|
---|
[5072] | 624 | }
|
---|
| 625 |
|
---|
[5279] | 626 | public static double parseDouble(Map<String, String> parameters, String parameterName) throws ProjectionConfigurationException {
|
---|
[5889] | 627 | if (!parameters.containsKey(parameterName))
|
---|
[8609] | 628 | throw new ProjectionConfigurationException(tr("Unknown parameter ''{0}''", parameterName));
|
---|
[11553] | 629 | return parseDouble(Optional.ofNullable(parameters.get(parameterName)).orElseThrow(
|
---|
| 630 | () -> new ProjectionConfigurationException(tr("Expected number argument for parameter ''{0}''", parameterName))),
|
---|
| 631 | parameterName);
|
---|
[5072] | 632 | }
|
---|
| 633 |
|
---|
[5279] | 634 | public static double parseDouble(String doubleStr, String parameterName) throws ProjectionConfigurationException {
|
---|
[5072] | 635 | try {
|
---|
| 636 | return Double.parseDouble(doubleStr);
|
---|
| 637 | } catch (NumberFormatException e) {
|
---|
| 638 | throw new ProjectionConfigurationException(
|
---|
[6798] | 639 | tr("Unable to parse value ''{1}'' of parameter ''{0}'' as number.", parameterName, doubleStr), e);
|
---|
[5072] | 640 | }
|
---|
| 641 | }
|
---|
| 642 |
|
---|
[10870] | 643 | /**
|
---|
| 644 | * Convert an angle string to a double value
|
---|
[12792] | 645 | * @param angleStr The string. e.g. -1.1 or 50d10'3"
|
---|
[10870] | 646 | * @param parameterName Only for error message.
|
---|
| 647 | * @return The angle value, in degrees.
|
---|
| 648 | * @throws ProjectionConfigurationException in case of invalid parameter
|
---|
| 649 | */
|
---|
[5279] | 650 | public static double parseAngle(String angleStr, String parameterName) throws ProjectionConfigurationException {
|
---|
[12792] | 651 | try {
|
---|
| 652 | return LatLonParser.parseCoordinate(angleStr);
|
---|
| 653 | } catch (IllegalArgumentException e) {
|
---|
[10870] | 654 | throw new ProjectionConfigurationException(
|
---|
[12868] | 655 | tr("Unable to parse value ''{1}'' of parameter ''{0}'' as coordinate value.", parameterName, angleStr), e);
|
---|
[5072] | 656 | }
|
---|
| 657 | }
|
---|
| 658 |
|
---|
| 659 | @Override
|
---|
| 660 | public Integer getEpsgCode() {
|
---|
[5548] | 661 | if (code != null && code.startsWith("EPSG:")) {
|
---|
| 662 | try {
|
---|
[8390] | 663 | return Integer.valueOf(code.substring(5));
|
---|
[6310] | 664 | } catch (NumberFormatException e) {
|
---|
[12620] | 665 | Logging.warn(e);
|
---|
[6310] | 666 | }
|
---|
[5548] | 667 | }
|
---|
[5072] | 668 | return null;
|
---|
| 669 | }
|
---|
| 670 |
|
---|
| 671 | @Override
|
---|
| 672 | public String toCode() {
|
---|
[10870] | 673 | if (code != null) {
|
---|
| 674 | return code;
|
---|
| 675 | } else if (pref != null) {
|
---|
| 676 | return "proj:" + pref;
|
---|
| 677 | } else {
|
---|
| 678 | return "proj:ERROR";
|
---|
| 679 | }
|
---|
[5072] | 680 | }
|
---|
| 681 |
|
---|
| 682 | @Override
|
---|
| 683 | public Bounds getWorldBoundsLatLon() {
|
---|
[10870] | 684 | if (bounds == null) {
|
---|
| 685 | Bounds ab = proj.getAlgorithmBounds();
|
---|
| 686 | if (ab != null) {
|
---|
| 687 | double minlon = Math.max(ab.getMinLon() + lon0 + pm, -180);
|
---|
| 688 | double maxlon = Math.min(ab.getMaxLon() + lon0 + pm, 180);
|
---|
| 689 | bounds = new Bounds(ab.getMinLat(), minlon, ab.getMaxLat(), maxlon, false);
|
---|
| 690 | } else {
|
---|
| 691 | bounds = new Bounds(
|
---|
| 692 | new LatLon(-90.0, -180.0),
|
---|
| 693 | new LatLon(90.0, 180.0));
|
---|
| 694 | }
|
---|
[9124] | 695 | }
|
---|
[10870] | 696 | return bounds;
|
---|
[5072] | 697 | }
|
---|
| 698 |
|
---|
| 699 | @Override
|
---|
| 700 | public String toString() {
|
---|
[5548] | 701 | return name != null ? name : tr("Custom Projection");
|
---|
[5072] | 702 | }
|
---|
[8568] | 703 |
|
---|
[9790] | 704 | /**
|
---|
| 705 | * Factor to convert units of east/north coordinates to meters.
|
---|
[10000] | 706 | *
|
---|
[9790] | 707 | * When east/north coordinates are in degrees (geographic CRS), the scale
|
---|
| 708 | * at the equator is taken, i.e. 360 degrees corresponds to the length of
|
---|
| 709 | * the equator in meters.
|
---|
[10000] | 710 | *
|
---|
[9790] | 711 | * @return factor to convert units to meter
|
---|
| 712 | */
|
---|
[8568] | 713 | @Override
|
---|
| 714 | public double getMetersPerUnit() {
|
---|
[9790] | 715 | return metersPerUnitWMTS;
|
---|
[8568] | 716 | }
|
---|
[9631] | 717 |
|
---|
[8584] | 718 | @Override
|
---|
| 719 | public boolean switchXY() {
|
---|
| 720 | // TODO: support for other axis orientation such as West South, and Up Down
|
---|
| 721 | return this.axis.startsWith("ne");
|
---|
| 722 | }
|
---|
| 723 |
|
---|
[8568] | 724 | private static Map<String, Double> getUnitsToMeters() {
|
---|
| 725 | Map<String, Double> ret = new ConcurrentHashMap<>();
|
---|
| 726 | ret.put("km", 1000d);
|
---|
| 727 | ret.put("m", 1d);
|
---|
| 728 | ret.put("dm", 1d/10);
|
---|
| 729 | ret.put("cm", 1d/100);
|
---|
| 730 | ret.put("mm", 1d/1000);
|
---|
| 731 | ret.put("kmi", 1852.0);
|
---|
| 732 | ret.put("in", 0.0254);
|
---|
| 733 | ret.put("ft", 0.3048);
|
---|
| 734 | ret.put("yd", 0.9144);
|
---|
| 735 | ret.put("mi", 1609.344);
|
---|
| 736 | ret.put("fathom", 1.8288);
|
---|
| 737 | ret.put("chain", 20.1168);
|
---|
| 738 | ret.put("link", 0.201168);
|
---|
| 739 | ret.put("us-in", 1d/39.37);
|
---|
| 740 | ret.put("us-ft", 0.304800609601219);
|
---|
| 741 | ret.put("us-yd", 0.914401828803658);
|
---|
| 742 | ret.put("us-ch", 20.11684023368047);
|
---|
| 743 | ret.put("us-mi", 1609.347218694437);
|
---|
| 744 | ret.put("ind-yd", 0.91439523);
|
---|
| 745 | ret.put("ind-ft", 0.30479841);
|
---|
| 746 | ret.put("ind-ch", 20.11669506);
|
---|
| 747 | ret.put("degree", METER_PER_UNIT_DEGREE);
|
---|
| 748 | return ret;
|
---|
| 749 | }
|
---|
[9105] | 750 |
|
---|
| 751 | private static Map<String, Double> getPrimeMeridians() {
|
---|
| 752 | Map<String, Double> ret = new ConcurrentHashMap<>();
|
---|
| 753 | try {
|
---|
| 754 | ret.put("greenwich", 0.0);
|
---|
| 755 | ret.put("lisbon", parseAngle("9d07'54.862\"W", null));
|
---|
| 756 | ret.put("paris", parseAngle("2d20'14.025\"E", null));
|
---|
| 757 | ret.put("bogota", parseAngle("74d04'51.3\"W", null));
|
---|
| 758 | ret.put("madrid", parseAngle("3d41'16.58\"W", null));
|
---|
| 759 | ret.put("rome", parseAngle("12d27'8.4\"E", null));
|
---|
| 760 | ret.put("bern", parseAngle("7d26'22.5\"E", null));
|
---|
| 761 | ret.put("jakarta", parseAngle("106d48'27.79\"E", null));
|
---|
| 762 | ret.put("ferro", parseAngle("17d40'W", null));
|
---|
| 763 | ret.put("brussels", parseAngle("4d22'4.71\"E", null));
|
---|
| 764 | ret.put("stockholm", parseAngle("18d3'29.8\"E", null));
|
---|
| 765 | ret.put("athens", parseAngle("23d42'58.815\"E", null));
|
---|
| 766 | ret.put("oslo", parseAngle("10d43'22.5\"E", null));
|
---|
| 767 | } catch (ProjectionConfigurationException ex) {
|
---|
[10235] | 768 | throw new IllegalStateException(ex);
|
---|
[9105] | 769 | }
|
---|
| 770 | return ret;
|
---|
| 771 | }
|
---|
[9419] | 772 |
|
---|
[10001] | 773 | private static EastNorth getPointAlong(int i, int n, ProjectionBounds r) {
|
---|
| 774 | double dEast = (r.maxEast - r.minEast) / n;
|
---|
| 775 | double dNorth = (r.maxNorth - r.minNorth) / n;
|
---|
| 776 | if (i < n) {
|
---|
[9431] | 777 | return new EastNorth(r.minEast + i * dEast, r.minNorth);
|
---|
[10001] | 778 | } else if (i < 2*n) {
|
---|
| 779 | i -= n;
|
---|
[9431] | 780 | return new EastNorth(r.maxEast, r.minNorth + i * dNorth);
|
---|
[10001] | 781 | } else if (i < 3*n) {
|
---|
| 782 | i -= 2*n;
|
---|
[9431] | 783 | return new EastNorth(r.maxEast - i * dEast, r.maxNorth);
|
---|
[10001] | 784 | } else if (i < 4*n) {
|
---|
| 785 | i -= 3*n;
|
---|
[9431] | 786 | return new EastNorth(r.minEast, r.maxNorth - i * dNorth);
|
---|
| 787 | } else {
|
---|
| 788 | throw new AssertionError();
|
---|
| 789 | }
|
---|
| 790 | }
|
---|
| 791 |
|
---|
[9559] | 792 | private EastNorth getPole(Polarity whichPole) {
|
---|
| 793 | if (polesEN == null) {
|
---|
| 794 | polesEN = new EnumMap<>(Polarity.class);
|
---|
| 795 | for (Polarity p : Polarity.values()) {
|
---|
| 796 | polesEN.put(p, null);
|
---|
[10870] | 797 | LatLon ll = p.getLatLon();
|
---|
[9559] | 798 | try {
|
---|
| 799 | EastNorth enPole = latlon2eastNorth(ll);
|
---|
| 800 | if (enPole.isValid()) {
|
---|
| 801 | // project back and check if the result is somewhat reasonable
|
---|
| 802 | LatLon llBack = eastNorth2latlon(enPole);
|
---|
| 803 | if (llBack.isValid() && ll.greatCircleDistance(llBack) < 1000) {
|
---|
| 804 | polesEN.put(p, enPole);
|
---|
| 805 | }
|
---|
| 806 | }
|
---|
[11746] | 807 | } catch (JosmRuntimeException | IllegalArgumentException | IllegalStateException e) {
|
---|
[12620] | 808 | Logging.error(e);
|
---|
[9562] | 809 | }
|
---|
[9559] | 810 | }
|
---|
| 811 | }
|
---|
| 812 | return polesEN.get(whichPole);
|
---|
| 813 | }
|
---|
| 814 |
|
---|
[9419] | 815 | @Override
|
---|
| 816 | public Bounds getLatLonBoundsBox(ProjectionBounds r) {
|
---|
[10001] | 817 | final int n = 10;
|
---|
[9419] | 818 | Bounds result = new Bounds(eastNorth2latlon(r.getMin()));
|
---|
| 819 | result.extend(eastNorth2latlon(r.getMax()));
|
---|
[9431] | 820 | LatLon llPrev = null;
|
---|
[10001] | 821 | for (int i = 0; i < 4*n; i++) {
|
---|
| 822 | LatLon llNow = eastNorth2latlon(getPointAlong(i, n, r));
|
---|
[9431] | 823 | result.extend(llNow);
|
---|
| 824 | // check if segment crosses 180th meridian and if so, make sure
|
---|
| 825 | // to extend bounds to +/-180 degrees longitude
|
---|
| 826 | if (llPrev != null) {
|
---|
| 827 | double lon1 = llPrev.lon();
|
---|
| 828 | double lon2 = llNow.lon();
|
---|
| 829 | if (90 < lon1 && lon1 < 180 && -180 < lon2 && lon2 < -90) {
|
---|
| 830 | result.extend(new LatLon(llPrev.lat(), 180));
|
---|
| 831 | result.extend(new LatLon(llNow.lat(), -180));
|
---|
| 832 | }
|
---|
| 833 | if (90 < lon2 && lon2 < 180 && -180 < lon1 && lon1 < -90) {
|
---|
| 834 | result.extend(new LatLon(llNow.lat(), 180));
|
---|
| 835 | result.extend(new LatLon(llPrev.lat(), -180));
|
---|
| 836 | }
|
---|
| 837 | }
|
---|
| 838 | llPrev = llNow;
|
---|
[9419] | 839 | }
|
---|
| 840 | // if the box contains one of the poles, the above method did not get
|
---|
| 841 | // correct min/max latitude value
|
---|
[9559] | 842 | for (Polarity p : Polarity.values()) {
|
---|
| 843 | EastNorth pole = getPole(p);
|
---|
| 844 | if (pole != null && r.contains(pole)) {
|
---|
[10870] | 845 | result.extend(p.getLatLon());
|
---|
[9419] | 846 | }
|
---|
| 847 | }
|
---|
| 848 | return result;
|
---|
| 849 | }
|
---|
[11858] | 850 |
|
---|
| 851 | @Override
|
---|
| 852 | public ProjectionBounds getEastNorthBoundsBox(ProjectionBounds box, Projection boxProjection) {
|
---|
| 853 | final int n = 8;
|
---|
| 854 | ProjectionBounds result = null;
|
---|
| 855 | for (int i = 0; i < 4*n; i++) {
|
---|
| 856 | EastNorth en = latlon2eastNorth(boxProjection.eastNorth2latlon(getPointAlong(i, n, box)));
|
---|
| 857 | if (result == null) {
|
---|
| 858 | result = new ProjectionBounds(en);
|
---|
| 859 | } else {
|
---|
| 860 | result.extend(en);
|
---|
| 861 | }
|
---|
| 862 | }
|
---|
| 863 | return result;
|
---|
| 864 | }
|
---|
[12792] | 865 |
|
---|
| 866 | /**
|
---|
| 867 | * Return true, if a geographic coordinate reference system is represented.
|
---|
| 868 | *
|
---|
| 869 | * I.e. if it returns latitude/longitude values rather than Cartesian
|
---|
| 870 | * east/north coordinates on a flat surface.
|
---|
| 871 | * @return true, if it is geographic
|
---|
| 872 | * @since 12792
|
---|
| 873 | */
|
---|
| 874 | public boolean isGeographic() {
|
---|
| 875 | return proj.isGeographic();
|
---|
| 876 | }
|
---|
| 877 |
|
---|
[5072] | 878 | }
|
---|