| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.data.projection;
|
|---|
| 3 |
|
|---|
| 4 | import java.io.BufferedReader;
|
|---|
| 5 | import java.io.IOException;
|
|---|
| 6 | import java.util.ArrayList;
|
|---|
| 7 | import java.util.Collection;
|
|---|
| 8 | import java.util.Collections;
|
|---|
| 9 | import java.util.HashMap;
|
|---|
| 10 | import java.util.HashSet;
|
|---|
| 11 | import java.util.LinkedHashMap;
|
|---|
| 12 | import java.util.List;
|
|---|
| 13 | import java.util.Locale;
|
|---|
| 14 | import java.util.Map;
|
|---|
| 15 | import java.util.Set;
|
|---|
| 16 | import java.util.function.Supplier;
|
|---|
| 17 | import java.util.regex.Matcher;
|
|---|
| 18 | import java.util.regex.Pattern;
|
|---|
| 19 |
|
|---|
| 20 | import org.openstreetmap.josm.data.projection.datum.Datum;
|
|---|
| 21 | import org.openstreetmap.josm.data.projection.datum.GRS80Datum;
|
|---|
| 22 | import org.openstreetmap.josm.data.projection.datum.NTV2GridShiftFileWrapper;
|
|---|
| 23 | import org.openstreetmap.josm.data.projection.datum.SevenParameterDatum;
|
|---|
| 24 | import org.openstreetmap.josm.data.projection.datum.ThreeParameterDatum;
|
|---|
| 25 | import org.openstreetmap.josm.data.projection.datum.WGS84Datum;
|
|---|
| 26 | import org.openstreetmap.josm.data.projection.proj.AlbersEqualArea;
|
|---|
| 27 | import org.openstreetmap.josm.data.projection.proj.CassiniSoldner;
|
|---|
| 28 | import org.openstreetmap.josm.data.projection.proj.ClassProjFactory;
|
|---|
| 29 | import org.openstreetmap.josm.data.projection.proj.DoubleStereographic;
|
|---|
| 30 | import org.openstreetmap.josm.data.projection.proj.LambertAzimuthalEqualArea;
|
|---|
| 31 | import org.openstreetmap.josm.data.projection.proj.LambertConformalConic;
|
|---|
| 32 | import org.openstreetmap.josm.data.projection.proj.LonLat;
|
|---|
| 33 | import org.openstreetmap.josm.data.projection.proj.Mercator;
|
|---|
| 34 | import org.openstreetmap.josm.data.projection.proj.ObliqueMercator;
|
|---|
| 35 | import org.openstreetmap.josm.data.projection.proj.PolarStereographic;
|
|---|
| 36 | import org.openstreetmap.josm.data.projection.proj.Proj;
|
|---|
| 37 | import org.openstreetmap.josm.data.projection.proj.ProjFactory;
|
|---|
| 38 | import org.openstreetmap.josm.data.projection.proj.Sinusoidal;
|
|---|
| 39 | import org.openstreetmap.josm.data.projection.proj.SwissObliqueMercator;
|
|---|
| 40 | import org.openstreetmap.josm.data.projection.proj.TransverseMercator;
|
|---|
| 41 | import org.openstreetmap.josm.io.CachedFile;
|
|---|
| 42 | import org.openstreetmap.josm.tools.JosmRuntimeException;
|
|---|
| 43 | import org.openstreetmap.josm.tools.Logging;
|
|---|
| 44 | import org.openstreetmap.josm.tools.Utils;
|
|---|
| 45 |
|
|---|
| 46 | /**
|
|---|
| 47 | * Class to manage projections.
|
|---|
| 48 | *
|
|---|
| 49 | * Use this class to query available projection or register new projections from a plugin.
|
|---|
| 50 | */
|
|---|
| 51 | public final class Projections {
|
|---|
| 52 |
|
|---|
| 53 | /**
|
|---|
| 54 | * Class to hold information about one projection.
|
|---|
| 55 | */
|
|---|
| 56 | public static class ProjectionDefinition {
|
|---|
| 57 | public final String code;
|
|---|
| 58 | public final String name;
|
|---|
| 59 | public final String definition;
|
|---|
| 60 |
|
|---|
| 61 | /**
|
|---|
| 62 | * Constructs a new {@code ProjectionDefinition}.
|
|---|
| 63 | * @param code EPSG code
|
|---|
| 64 | * @param name projection name
|
|---|
| 65 | * @param definition projection definition (EPSG format)
|
|---|
| 66 | */
|
|---|
| 67 | public ProjectionDefinition(String code, String name, String definition) {
|
|---|
| 68 | this.code = code;
|
|---|
| 69 | this.name = name;
|
|---|
| 70 | this.definition = definition;
|
|---|
| 71 | }
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | private static final Set<String> allCodes = new HashSet<>();
|
|---|
| 75 | private static final Map<String, Supplier<Projection>> projectionSuppliersByCode = new HashMap<>();
|
|---|
| 76 | private static final Map<String, Projection> projectionsByCode_cache = new HashMap<>();
|
|---|
| 77 |
|
|---|
| 78 | /*********************************
|
|---|
| 79 | * Registry for custom projection
|
|---|
| 80 | *
|
|---|
| 81 | * should be compatible to PROJ.4
|
|---|
| 82 | */
|
|---|
| 83 | private static final Map<String, ProjFactory> projs = new HashMap<>();
|
|---|
| 84 | private static final Map<String, Ellipsoid> ellipsoids = new HashMap<>();
|
|---|
| 85 | private static final Map<String, Datum> datums = new HashMap<>();
|
|---|
| 86 | private static final Map<String, NTV2GridShiftFileWrapper> nadgrids = new HashMap<>();
|
|---|
| 87 | private static final Map<String, ProjectionDefinition> inits;
|
|---|
| 88 |
|
|---|
| 89 | static {
|
|---|
| 90 | registerBaseProjection("aea", AlbersEqualArea.class, "core");
|
|---|
| 91 | registerBaseProjection("cass", CassiniSoldner.class, "core");
|
|---|
| 92 | registerBaseProjection("laea", LambertAzimuthalEqualArea.class, "core");
|
|---|
| 93 | registerBaseProjection("lcc", LambertConformalConic.class, "core");
|
|---|
| 94 | registerBaseProjection("lonlat", LonLat.class, "core");
|
|---|
| 95 | registerBaseProjection("merc", Mercator.class, "core");
|
|---|
| 96 | registerBaseProjection("omerc", ObliqueMercator.class, "core");
|
|---|
| 97 | registerBaseProjection("somerc", SwissObliqueMercator.class, "core");
|
|---|
| 98 | registerBaseProjection("sinu", Sinusoidal.class, "core");
|
|---|
| 99 | registerBaseProjection("stere", PolarStereographic.class, "core");
|
|---|
| 100 | registerBaseProjection("sterea", DoubleStereographic.class, "core");
|
|---|
| 101 | registerBaseProjection("tmerc", TransverseMercator.class, "core");
|
|---|
| 102 |
|
|---|
| 103 | ellipsoids.put("airy", Ellipsoid.Airy);
|
|---|
| 104 | ellipsoids.put("mod_airy", Ellipsoid.AiryMod);
|
|---|
| 105 | ellipsoids.put("aust_SA", Ellipsoid.AustSA);
|
|---|
| 106 | ellipsoids.put("bessel", Ellipsoid.Bessel1841);
|
|---|
| 107 | ellipsoids.put("bess_nam", Ellipsoid.BesselNamibia);
|
|---|
| 108 | ellipsoids.put("clrk66", Ellipsoid.Clarke1866);
|
|---|
| 109 | ellipsoids.put("clrk80", Ellipsoid.Clarke1880);
|
|---|
| 110 | ellipsoids.put("clrk80ign", Ellipsoid.ClarkeIGN);
|
|---|
| 111 | ellipsoids.put("evrstSS", Ellipsoid.EverestSabahSarawak);
|
|---|
| 112 | ellipsoids.put("intl", Ellipsoid.Hayford);
|
|---|
| 113 | ellipsoids.put("helmert", Ellipsoid.Helmert);
|
|---|
| 114 | ellipsoids.put("krass", Ellipsoid.Krassowsky);
|
|---|
| 115 | ellipsoids.put("GRS67", Ellipsoid.GRS67);
|
|---|
| 116 | ellipsoids.put("GRS80", Ellipsoid.GRS80);
|
|---|
| 117 | ellipsoids.put("WGS66", Ellipsoid.WGS66);
|
|---|
| 118 | ellipsoids.put("WGS72", Ellipsoid.WGS72);
|
|---|
| 119 | ellipsoids.put("WGS84", Ellipsoid.WGS84);
|
|---|
| 120 |
|
|---|
| 121 | datums.put("WGS84", WGS84Datum.INSTANCE);
|
|---|
| 122 | datums.put("NAD83", GRS80Datum.INSTANCE);
|
|---|
| 123 | datums.put("carthage", new ThreeParameterDatum(
|
|---|
| 124 | "Carthage 1934 Tunisia", "carthage",
|
|---|
| 125 | Ellipsoid.ClarkeIGN, -263.0, 6.0, 431.0));
|
|---|
| 126 | datums.put("GGRS87", new ThreeParameterDatum(
|
|---|
| 127 | "Greek Geodetic Reference System 1987", "GGRS87",
|
|---|
| 128 | Ellipsoid.GRS80, -199.87, 74.79, 246.62));
|
|---|
| 129 | datums.put("hermannskogel", new SevenParameterDatum(
|
|---|
| 130 | "Hermannskogel", "hermannskogel",
|
|---|
| 131 | Ellipsoid.Bessel1841, 577.326, 90.129, 463.919, 5.137, 1.474, 5.297, 2.4232));
|
|---|
| 132 | datums.put("ire65", new SevenParameterDatum(
|
|---|
| 133 | "Ireland 1965", "ire65",
|
|---|
| 134 | Ellipsoid.AiryMod, 482.530, -130.596, 564.557, -1.042, -0.214, -0.631, 8.15));
|
|---|
| 135 | datums.put("nzgd49", new SevenParameterDatum(
|
|---|
| 136 | "New Zealand Geodetic Datum 1949", "nzgd49",
|
|---|
| 137 | Ellipsoid.Hayford, 59.47, -5.04, 187.44, 0.47, -0.1, 1.024, -4.5993));
|
|---|
| 138 | datums.put("OSGB36", new SevenParameterDatum(
|
|---|
| 139 | "Airy 1830", "OSGB36",
|
|---|
| 140 | Ellipsoid.Airy, 446.448, -125.157, 542.060, 0.1502, 0.2470, 0.8421, -20.4894));
|
|---|
| 141 | datums.put("potsdam", new SevenParameterDatum(
|
|---|
| 142 | "Potsdam Rauenberg 1950 DHDN", "potsdam",
|
|---|
| 143 | Ellipsoid.Bessel1841, 598.1, 73.7, 418.2, 0.202, 0.045, -2.455, 6.7));
|
|---|
| 144 |
|
|---|
| 145 | try {
|
|---|
| 146 | inits = new LinkedHashMap<>();
|
|---|
| 147 | for (ProjectionDefinition pd : loadProjectionDefinitions("resource://data/projection/custom-epsg")) {
|
|---|
| 148 | inits.put(pd.code, pd);
|
|---|
| 149 | loadNadgrids(pd.definition);
|
|---|
| 150 | }
|
|---|
| 151 | } catch (IOException ex) {
|
|---|
| 152 | throw new JosmRuntimeException(ex);
|
|---|
| 153 | }
|
|---|
| 154 | allCodes.addAll(inits.keySet());
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | private Projections() {
|
|---|
| 158 | // Hide default constructor for utils classes
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | private static void loadNadgrids(String definition) {
|
|---|
| 162 | final String key = CustomProjection.Param.nadgrids.key;
|
|---|
| 163 | if (definition.contains(key)) {
|
|---|
| 164 | try {
|
|---|
| 165 | String nadgridsId = CustomProjection.parseParameterList(definition, true).get(key);
|
|---|
| 166 | if (nadgridsId.startsWith("@")) {
|
|---|
| 167 | nadgridsId = nadgridsId.substring(1);
|
|---|
| 168 | }
|
|---|
| 169 | if (!"null".equals(nadgridsId) && !nadgrids.containsKey(nadgridsId)) {
|
|---|
| 170 | nadgrids.put(nadgridsId, new NTV2GridShiftFileWrapper(nadgridsId));
|
|---|
| 171 | }
|
|---|
| 172 | } catch (ProjectionConfigurationException e) {
|
|---|
| 173 | Logging.trace(e);
|
|---|
| 174 | }
|
|---|
| 175 | }
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | /**
|
|---|
| 179 | * Plugins can register additional base projections.
|
|---|
| 180 | *
|
|---|
| 181 | * @param id The "official" PROJ.4 id. In case the projection is not supported
|
|---|
| 182 | * by PROJ.4, use some prefix, e.g. josm:myproj or gdal:otherproj.
|
|---|
| 183 | * @param fac The base projection factory.
|
|---|
| 184 | * @param origin Multiple plugins may implement the same base projection.
|
|---|
| 185 | * Provide plugin name or similar string, so it be differentiated.
|
|---|
| 186 | */
|
|---|
| 187 | public static void registerBaseProjection(String id, ProjFactory fac, String origin) {
|
|---|
| 188 | projs.put(id, fac);
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | public static void registerBaseProjection(String id, Class<? extends Proj> projClass, String origin) {
|
|---|
| 192 | registerBaseProjection(id, new ClassProjFactory(projClass), origin);
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | /**
|
|---|
| 196 | * Register a projection supplier, that is, a factory class for projections.
|
|---|
| 197 | * @param code the code of the projection that will be returned
|
|---|
| 198 | * @param supplier a supplier to return a projection with given code
|
|---|
| 199 | * @since 12786
|
|---|
| 200 | */
|
|---|
| 201 | public static void registerProjectionSupplier(String code, Supplier<Projection> supplier) {
|
|---|
| 202 | projectionSuppliersByCode.put(code, supplier);
|
|---|
| 203 | allCodes.add(code);
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | /**
|
|---|
| 207 | * Get a base projection by id.
|
|---|
| 208 | *
|
|---|
| 209 | * @param id the id, for example "lonlat" or "tmerc"
|
|---|
| 210 | * @return the corresponding base projection if the id is known, null otherwise
|
|---|
| 211 | */
|
|---|
| 212 | public static Proj getBaseProjection(String id) {
|
|---|
| 213 | ProjFactory fac = projs.get(id);
|
|---|
| 214 | if (fac == null) return null;
|
|---|
| 215 | return fac.createInstance();
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | /**
|
|---|
| 219 | * Get an ellipsoid by id.
|
|---|
| 220 | *
|
|---|
| 221 | * @param id the id, for example "bessel" or "WGS84"
|
|---|
| 222 | * @return the corresponding ellipsoid if the id is known, null otherwise
|
|---|
| 223 | */
|
|---|
| 224 | public static Ellipsoid getEllipsoid(String id) {
|
|---|
| 225 | return ellipsoids.get(id);
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | /**
|
|---|
| 229 | * Get a geodetic datum by id.
|
|---|
| 230 | *
|
|---|
| 231 | * @param id the id, for example "potsdam" or "WGS84"
|
|---|
| 232 | * @return the corresponding datum if the id is known, null otherwise
|
|---|
| 233 | */
|
|---|
| 234 | public static Datum getDatum(String id) {
|
|---|
| 235 | return datums.get(id);
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| 238 | /**
|
|---|
| 239 | * Get a NTV2 grid database by id.
|
|---|
| 240 | * @param id the id
|
|---|
| 241 | * @return the corresponding NTV2 grid if the id is known, null otherwise
|
|---|
| 242 | */
|
|---|
| 243 | public static NTV2GridShiftFileWrapper getNTV2Grid(String id) {
|
|---|
| 244 | return nadgrids.get(id);
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | /**
|
|---|
| 248 | * Get the projection definition string for the given code.
|
|---|
| 249 | * @param code the code
|
|---|
| 250 | * @return the string that can be processed by #{link CustomProjection}.
|
|---|
| 251 | * Null, if the code isn't supported.
|
|---|
| 252 | */
|
|---|
| 253 | public static String getInit(String code) {
|
|---|
| 254 | ProjectionDefinition pd = inits.get(code.toUpperCase(Locale.ENGLISH));
|
|---|
| 255 | if (pd == null) return null;
|
|---|
| 256 | return pd.definition;
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 | /**
|
|---|
| 260 | * Load projection definitions from file.
|
|---|
| 261 | *
|
|---|
| 262 | * @param path the path
|
|---|
| 263 | * @return projection definitions
|
|---|
| 264 | * @throws IOException in case of I/O error
|
|---|
| 265 | */
|
|---|
| 266 | public static List<ProjectionDefinition> loadProjectionDefinitions(String path) throws IOException {
|
|---|
| 267 | try (
|
|---|
| 268 | CachedFile cf = new CachedFile(path);
|
|---|
| 269 | BufferedReader r = cf.getContentReader()
|
|---|
| 270 | ) {
|
|---|
| 271 | return loadProjectionDefinitions(r);
|
|---|
| 272 | }
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 | /**
|
|---|
| 276 | * Load projection definitions from file.
|
|---|
| 277 | *
|
|---|
| 278 | * @param r the reader
|
|---|
| 279 | * @return projection definitions
|
|---|
| 280 | * @throws IOException in case of I/O error
|
|---|
| 281 | */
|
|---|
| 282 | public static List<ProjectionDefinition> loadProjectionDefinitions(BufferedReader r) throws IOException {
|
|---|
| 283 | List<ProjectionDefinition> result = new ArrayList<>();
|
|---|
| 284 | Pattern epsgPattern = Pattern.compile("<(\\d+)>(.*)<>");
|
|---|
| 285 | String line, lastline = "";
|
|---|
| 286 | while ((line = r.readLine()) != null) {
|
|---|
| 287 | line = line.trim();
|
|---|
| 288 | if (!line.startsWith("#") && !line.isEmpty()) {
|
|---|
| 289 | if (!lastline.startsWith("#")) throw new AssertionError("EPSG file seems corrupted");
|
|---|
| 290 | String name = lastline.substring(1).trim();
|
|---|
| 291 | Matcher m = epsgPattern.matcher(line);
|
|---|
| 292 | if (m.matches()) {
|
|---|
| 293 | String code = "EPSG:" + m.group(1);
|
|---|
| 294 | String definition = m.group(2).trim();
|
|---|
| 295 | result.add(new ProjectionDefinition(code, name, definition));
|
|---|
| 296 | } else {
|
|---|
| 297 | Logging.warn("Failed to parse line from the EPSG projection definition: "+line);
|
|---|
| 298 | }
|
|---|
| 299 | }
|
|---|
| 300 | lastline = line;
|
|---|
| 301 | }
|
|---|
| 302 | return result;
|
|---|
| 303 | }
|
|---|
| 304 |
|
|---|
| 305 | /**
|
|---|
| 306 | * Get a projection by code.
|
|---|
| 307 | * @param code the code, e.g. "EPSG:2026"
|
|---|
| 308 | * @return the corresponding projection, if the code is known, null otherwise
|
|---|
| 309 | */
|
|---|
| 310 | public static Projection getProjectionByCode(String code) {
|
|---|
| 311 | Projection proj = projectionsByCode_cache.get(code);
|
|---|
| 312 | if (proj != null) return proj;
|
|---|
| 313 |
|
|---|
| 314 | ProjectionDefinition pd = inits.get(code);
|
|---|
| 315 | if (pd != null) {
|
|---|
| 316 | CustomProjection cproj = new CustomProjection(pd.name, code, null);
|
|---|
| 317 | try {
|
|---|
| 318 | cproj.update(pd.definition);
|
|---|
| 319 | } catch (ProjectionConfigurationException ex) {
|
|---|
| 320 | throw new RuntimeException(ex);
|
|---|
| 321 | }
|
|---|
| 322 | proj = cproj;
|
|---|
| 323 | }
|
|---|
| 324 | if (proj == null) {
|
|---|
| 325 | Supplier<Projection> ps = projectionSuppliersByCode.get(code);
|
|---|
| 326 | if (ps != null) {
|
|---|
| 327 | proj = ps.get();
|
|---|
| 328 | }
|
|---|
| 329 | }
|
|---|
| 330 | if (proj != null) {
|
|---|
| 331 | projectionsByCode_cache.put(code, proj);
|
|---|
| 332 | }
|
|---|
| 333 | return proj;
|
|---|
| 334 | }
|
|---|
| 335 |
|
|---|
| 336 | /**
|
|---|
| 337 | * Get a list of all supported projection codes.
|
|---|
| 338 | *
|
|---|
| 339 | * @return all supported projection codes
|
|---|
| 340 | * @see #getProjectionByCode(java.lang.String)
|
|---|
| 341 | */
|
|---|
| 342 | public static Collection<String> getAllProjectionCodes() {
|
|---|
| 343 | return Collections.unmodifiableCollection(allCodes);
|
|---|
| 344 | }
|
|---|
| 345 |
|
|---|
| 346 | /**
|
|---|
| 347 | * Get a list of ids of all registered base projections.
|
|---|
| 348 | *
|
|---|
| 349 | * @return all registered base projection ids
|
|---|
| 350 | * @see #getBaseProjection(java.lang.String)
|
|---|
| 351 | */
|
|---|
| 352 | public static Collection<String> getAllBaseProjectionIds() {
|
|---|
| 353 | return projs.keySet();
|
|---|
| 354 | }
|
|---|
| 355 |
|
|---|
| 356 | private static String listKeys(Map<String, ?> map) {
|
|---|
| 357 | List<String> keys = new ArrayList<>(map.keySet());
|
|---|
| 358 | Collections.sort(keys);
|
|---|
| 359 | return Utils.join(", ", keys);
|
|---|
| 360 | }
|
|---|
| 361 |
|
|---|
| 362 | /**
|
|---|
| 363 | * Replies the list of projections as string (comma separated).
|
|---|
| 364 | * @return the list of projections as string (comma separated)
|
|---|
| 365 | * @since 8533
|
|---|
| 366 | */
|
|---|
| 367 | public static String listProjs() {
|
|---|
| 368 | return listKeys(projs);
|
|---|
| 369 | }
|
|---|
| 370 |
|
|---|
| 371 | /**
|
|---|
| 372 | * Replies the list of ellipsoids as string (comma separated).
|
|---|
| 373 | * @return the list of ellipsoids as string (comma separated)
|
|---|
| 374 | * @since 8533
|
|---|
| 375 | */
|
|---|
| 376 | public static String listEllipsoids() {
|
|---|
| 377 | return listKeys(ellipsoids);
|
|---|
| 378 | }
|
|---|
| 379 |
|
|---|
| 380 | /**
|
|---|
| 381 | * Replies the list of datums as string (comma separated).
|
|---|
| 382 | * @return the list of datums as string (comma separated)
|
|---|
| 383 | * @since 8533
|
|---|
| 384 | */
|
|---|
| 385 | public static String listDatums() {
|
|---|
| 386 | return listKeys(datums);
|
|---|
| 387 | }
|
|---|
| 388 |
|
|---|
| 389 | /**
|
|---|
| 390 | * Replies the list of nadgrids as string (comma separated).
|
|---|
| 391 | * @return the list of nadgrids as string (comma separated)
|
|---|
| 392 | * @since 8533
|
|---|
| 393 | */
|
|---|
| 394 | public static String listNadgrids() {
|
|---|
| 395 | return listKeys(nadgrids);
|
|---|
| 396 | }
|
|---|
| 397 | }
|
|---|