001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.coor;
003
004import org.openstreetmap.josm.Main;
005import org.openstreetmap.josm.data.projection.Projection;
006
007/**
008 * LatLon class that maintains a cache of projected EastNorth coordinates.
009 *
010 * This class is convenient to use, but has relatively high memory costs.
011 * It keeps a pointer to the last known projection in order to detect projection
012 * changes.
013 *
014 * Node and WayPoint have another, optimized, cache for projected coordinates.
015 */
016public class CachedLatLon extends LatLon {
017    private EastNorth eastNorth;
018    private Projection proj;
019
020    public CachedLatLon(double lat, double lon) {
021        super(lat, lon);
022    }
023
024    public CachedLatLon(LatLon coor) {
025        super(coor.lat(), coor.lon());
026        proj = null;
027    }
028
029    public CachedLatLon(EastNorth eastNorth) {
030        super(Main.getProjection().eastNorth2latlon(eastNorth));
031        proj = Main.getProjection();
032        this.eastNorth = eastNorth;
033    }
034
035    /**
036     * Replies the projected east/north coordinates.
037     *
038     * @return the internally cached east/north coordinates. null, if the globally defined projection is null
039     */
040    public final EastNorth getEastNorth() {
041        if(proj != Main.getProjection())
042        {
043            proj = Main.getProjection();
044            eastNorth = proj.latlon2eastNorth(this);
045        }
046        return eastNorth;
047    }
048    @Override public String toString() {
049        return "CachedLatLon[lat="+lat()+",lon="+lon()+"]";
050    }
051
052    // Only for Node.get3892DebugInfo()
053    public Projection getProjection() {
054        return proj;
055    }
056}