001    // License: GPL. Copyright 2007 by Immanuel Scholz and others
002    package org.openstreetmap.josm.data.coor;
003    
004    import org.openstreetmap.josm.Main;
005    import 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     */
016    public 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        public final void setCoor(LatLon coor) {
036            setLocation(coor.lon(), coor.lat());
037            proj = null;
038        }
039    
040        public final void setEastNorth(EastNorth eastNorth) {
041            proj = Main.getProjection();
042            this.eastNorth = eastNorth;
043            LatLon l = proj.eastNorth2latlon(eastNorth);
044            setLocation(l.lon(), l.lat());
045        }
046    
047        /**
048         * Replies the projected east/north coordinates.
049         * 
050         * @return the internally cached east/north coordinates. null, if the globally defined projection is null
051         */
052        public final EastNorth getEastNorth() {
053            if(proj != Main.getProjection())
054            {
055                proj = Main.getProjection();
056                eastNorth = proj.latlon2eastNorth(this);
057            }
058            return eastNorth;
059        }
060        @Override public String toString() {
061            return "CachedLatLon[lat="+lat()+",lon="+lon()+"]";
062        }
063    
064        // Only for Node.get3892DebugInfo()
065        public Projection getProjection() {
066            return proj;
067        }
068    }