001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.projection;
003
004import java.io.BufferedReader;
005import java.io.IOException;
006import java.io.InputStream;
007import java.io.InputStreamReader;
008import java.nio.charset.StandardCharsets;
009import java.util.ArrayList;
010import java.util.Collection;
011import java.util.Collections;
012import java.util.HashMap;
013import java.util.HashSet;
014import java.util.LinkedHashMap;
015import java.util.List;
016import java.util.Locale;
017import java.util.Map;
018import java.util.Set;
019import java.util.regex.Matcher;
020import java.util.regex.Pattern;
021
022import org.openstreetmap.josm.Main;
023import org.openstreetmap.josm.data.coor.EastNorth;
024import org.openstreetmap.josm.data.coor.LatLon;
025import org.openstreetmap.josm.data.projection.datum.Datum;
026import org.openstreetmap.josm.data.projection.datum.GRS80Datum;
027import org.openstreetmap.josm.data.projection.datum.NTV2GridShiftFileWrapper;
028import org.openstreetmap.josm.data.projection.datum.SevenParameterDatum;
029import org.openstreetmap.josm.data.projection.datum.ThreeParameterDatum;
030import org.openstreetmap.josm.data.projection.datum.WGS84Datum;
031import org.openstreetmap.josm.data.projection.proj.AlbersEqualArea;
032import org.openstreetmap.josm.data.projection.proj.CassiniSoldner;
033import org.openstreetmap.josm.data.projection.proj.ClassProjFactory;
034import org.openstreetmap.josm.data.projection.proj.DoubleStereographic;
035import org.openstreetmap.josm.data.projection.proj.LambertAzimuthalEqualArea;
036import org.openstreetmap.josm.data.projection.proj.LambertConformalConic;
037import org.openstreetmap.josm.data.projection.proj.LonLat;
038import org.openstreetmap.josm.data.projection.proj.Mercator;
039import org.openstreetmap.josm.data.projection.proj.ObliqueMercator;
040import org.openstreetmap.josm.data.projection.proj.PolarStereographic;
041import org.openstreetmap.josm.data.projection.proj.Proj;
042import org.openstreetmap.josm.data.projection.proj.ProjFactory;
043import org.openstreetmap.josm.data.projection.proj.Sinusoidal;
044import org.openstreetmap.josm.data.projection.proj.SwissObliqueMercator;
045import org.openstreetmap.josm.data.projection.proj.TransverseMercator;
046import org.openstreetmap.josm.gui.preferences.projection.ProjectionChoice;
047import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
048import org.openstreetmap.josm.io.CachedFile;
049import org.openstreetmap.josm.tools.Utils;
050
051/**
052 * Class to manage projections.
053 *
054 * Use this class to query available projection or register new projections
055 * from a plugin.
056 */
057public final class Projections {
058
059    /**
060     * Class to hold information about one projection.
061     */
062    public static class ProjectionDefinition {
063        public String code;
064        public String name;
065        public String definition;
066
067        public ProjectionDefinition(String code, String name, String definition) {
068            this.code = code;
069            this.name = name;
070            this.definition = definition;
071        }
072    }
073
074    private static final Set<String> allCodes = new HashSet<>();
075    private static final Map<String, ProjectionChoice> allProjectionChoicesByCode = new HashMap<>();
076    private static final Map<String, Projection> projectionsByCode_cache = new HashMap<>();
077
078    /*********************************
079     * Registry for custom projection
080     *
081     * should be compatible to PROJ.4
082     */
083    static final Map<String, ProjFactory> projs = new HashMap<>();
084    static final Map<String, Ellipsoid> ellipsoids = new HashMap<>();
085    static final Map<String, Datum> datums = new HashMap<>();
086    static final Map<String, NTV2GridShiftFileWrapper> nadgrids = new HashMap<>();
087    static final Map<String, ProjectionDefinition> inits;
088
089    static {
090        registerBaseProjection("aea", AlbersEqualArea.class, "core");
091        registerBaseProjection("cass", CassiniSoldner.class, "core");
092        registerBaseProjection("laea", LambertAzimuthalEqualArea.class, "core");
093        registerBaseProjection("lcc", LambertConformalConic.class, "core");
094        registerBaseProjection("lonlat", LonLat.class, "core");
095        registerBaseProjection("merc", Mercator.class, "core");
096        registerBaseProjection("omerc", ObliqueMercator.class, "core");
097        registerBaseProjection("somerc", SwissObliqueMercator.class, "core");
098        registerBaseProjection("sinu", Sinusoidal.class, "core");
099        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        nadgrids.put("BETA2007.gsb", NTV2GridShiftFileWrapper.BETA2007);
146        nadgrids.put("ntf_r93_b.gsb", NTV2GridShiftFileWrapper.ntf_rgf93);
147
148        List<ProjectionDefinition> pds;
149        try {
150            pds = loadProjectionDefinitions("resource://data/projection/custom-epsg");
151        } catch (IOException ex) {
152            throw new RuntimeException(ex);
153        }
154        inits = new LinkedHashMap<>();
155        for (ProjectionDefinition pd : pds) {
156            inits.put(pd.code, pd);
157        }
158
159        for (ProjectionChoice pc : ProjectionPreference.getProjectionChoices()) {
160            for (String code : pc.allCodes()) {
161                allProjectionChoicesByCode.put(code, pc);
162            }
163        }
164        allCodes.addAll(inits.keySet());
165        allCodes.addAll(allProjectionChoicesByCode.keySet());
166    }
167
168    private Projections() {
169        // Hide default constructor for utils classes
170    }
171
172    /**
173     * Convert from lat/lon to easting/northing using the current projection.
174     *
175     * @param ll the geographical point to convert (in WGS84 lat/lon)
176     * @return the corresponding east/north coordinates
177     */
178    public static EastNorth project(LatLon ll) {
179        if (ll == null) return null;
180        return Main.getProjection().latlon2eastNorth(ll);
181    }
182
183    /**
184     * Convert from easting/norting to lat/lon using the current projection.
185     *
186     * @param en the geographical point to convert (in projected coordinates)
187     * @return the corresponding lat/lon (WGS84)
188     */
189    public static LatLon inverseProject(EastNorth en) {
190        if (en == null) return null;
191        return Main.getProjection().eastNorth2latlon(en);
192    }
193
194    /**
195     * Plugins can register additional base projections.
196     *
197     * @param id The "official" PROJ.4 id. In case the projection is not supported
198     * by PROJ.4, use some prefix, e.g. josm:myproj or gdal:otherproj.
199     * @param fac The base projection factory.
200     * @param origin Multiple plugins may implement the same base projection.
201     * Provide plugin name or similar string, so it be differentiated.
202     */
203    public static void registerBaseProjection(String id, ProjFactory fac, String origin) {
204        projs.put(id, fac);
205    }
206
207    public static void registerBaseProjection(String id, Class<? extends Proj> projClass, String origin) {
208        registerBaseProjection(id, new ClassProjFactory(projClass), origin);
209    }
210
211    /**
212     * Get a base projection by id.
213     *
214     * @param id the id, for example "lonlat" or "tmerc"
215     * @return the corresponding base projection if the id is known, null otherwise
216     */
217    public static Proj getBaseProjection(String id) {
218        ProjFactory fac = projs.get(id);
219        if (fac == null) return null;
220        return fac.createInstance();
221    }
222
223    /**
224     * Get an ellipsoid by id.
225     *
226     * @param id the id, for example "bessel" or "WGS84"
227     * @return the corresponding ellipsoid if the id is known, null otherwise
228     */
229    public static Ellipsoid getEllipsoid(String id) {
230        return ellipsoids.get(id);
231    }
232
233    /**
234     * Get a geodetic datum by id.
235     *
236     * @param id the id, for example "potsdam" or "WGS84"
237     * @return the corresponding datum if the id is known, null otherwise
238     */
239    public static Datum getDatum(String id) {
240        return datums.get(id);
241    }
242
243    /**
244     * Get a NTV2 grid database by id.
245     * @param id the id
246     * @return the corresponding NTV2 grid if the id is known, null otherwise
247     */
248    public static NTV2GridShiftFileWrapper getNTV2Grid(String id) {
249        return nadgrids.get(id);
250    }
251
252    /**
253     * Get the projection definition string for the given code.
254     * @param code the code
255     * @return the string that can be processed by #{link CustomProjection}.
256     * Null, if the code isn't supported.
257     */
258    public static String getInit(String code) {
259        ProjectionDefinition pd = inits.get(code.toUpperCase(Locale.ENGLISH));
260        if (pd == null) return null;
261        return pd.definition;
262    }
263
264    /**
265     * Load projection definitions from file.
266     *
267     * @param path the path
268     * @return projection definitions
269     * @throws IOException in case of I/O error
270     */
271    public static List<ProjectionDefinition> loadProjectionDefinitions(String path) throws IOException {
272        try (
273            CachedFile cf = new CachedFile(path);
274            InputStream in = cf.getInputStream();
275            BufferedReader r = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
276        ) {
277            return loadProjectionDefinitions(r);
278        }
279    }
280
281    /**
282     * Load projection definitions from file.
283     *
284     * @param r the reader
285     * @return projection definitions
286     * @throws IOException in case of I/O error
287     */
288    public static List<ProjectionDefinition> loadProjectionDefinitions(BufferedReader r) throws IOException {
289        List<ProjectionDefinition> result = new ArrayList<>();
290        Pattern epsgPattern = Pattern.compile("<(\\d+)>(.*)<>");
291        String line, lastline = "";
292        while ((line = r.readLine()) != null) {
293            line = line.trim();
294            if (!line.startsWith("#") && !line.isEmpty()) {
295                if (!lastline.startsWith("#")) throw new AssertionError("EPSG file seems corrupted");
296                String name = lastline.substring(1).trim();
297                Matcher m = epsgPattern.matcher(line);
298                if (m.matches()) {
299                    String code = "EPSG:" + m.group(1);
300                    String definition = m.group(2).trim();
301                    result.add(new ProjectionDefinition(code, name, definition));
302                } else {
303                    Main.warn("Failed to parse line from the EPSG projection definition: "+line);
304                }
305            }
306            lastline = line;
307        }
308        return result;
309    }
310
311    /**
312     * Get a projection by code.
313     * @param code the code, e.g. "EPSG:2026"
314     * @return the corresponding projection, if the code is known, null otherwise
315     */
316    public static Projection getProjectionByCode(String code) {
317        Projection proj = projectionsByCode_cache.get(code);
318        if (proj != null) return proj;
319        ProjectionChoice pc = allProjectionChoicesByCode.get(code);
320        if (pc != null) {
321            Collection<String> pref = pc.getPreferencesFromCode(code);
322            pc.setPreferences(pref);
323            try {
324                proj = pc.getProjection();
325            } catch (RuntimeException e) {
326                String cause = e.getMessage();
327                Main.warn("Unable to get projection "+code+" with "+pc + (cause != null ? ". "+cause : ""));
328            }
329        }
330        if (proj == null) {
331            ProjectionDefinition pd = inits.get(code);
332            if (pd == null) return null;
333            proj = new CustomProjection(pd.name, code, pd.definition, null);
334        }
335        projectionsByCode_cache.put(code, proj);
336        return proj;
337    }
338
339    /**
340     * Get a list of all supported projection codes.
341     *
342     * @return all supported projection codes
343     * @see #getProjectionByCode(java.lang.String)
344     */
345    public static Collection<String> getAllProjectionCodes() {
346        return Collections.unmodifiableCollection(allCodes);
347    }
348
349    /**
350     * Get a list of ids of all registered base projections.
351     *
352     * @return all registered base projection ids
353     * @see #getBaseProjection(java.lang.String)
354     */
355    public static Collection<String> getAllBaseProjectionIds() {
356        return projs.keySet();
357    }
358
359    private static String listKeys(Map<String, ?> map) {
360        List<String> keys = new ArrayList<>(map.keySet());
361        Collections.sort(keys);
362        return Utils.join(", ", keys);
363    }
364
365    /**
366     * Replies the list of projections as string (comma separated).
367     * @return the list of projections as string (comma separated)
368     * @since 8533
369     */
370    public static String listProjs() {
371        return listKeys(projs);
372    }
373
374    /**
375     * Replies the list of ellipsoids as string (comma separated).
376     * @return the list of ellipsoids as string (comma separated)
377     * @since 8533
378     */
379    public static String listEllipsoids() {
380        return listKeys(ellipsoids);
381    }
382
383    /**
384     * Replies the list of datums as string (comma separated).
385     * @return the list of datums as string (comma separated)
386     * @since 8533
387     */
388    public static String listDatums() {
389        return listKeys(datums);
390    }
391
392    /**
393     * Replies the list of nadgrids as string (comma separated).
394     * @return the list of nadgrids as string (comma separated)
395     * @since 8533
396     */
397    public static String listNadgrids() {
398        return listKeys(nadgrids);
399    }
400}