| 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others |
|---|
| 2 | package org.openstreetmap.josm.data.coor; |
|---|
| 3 | |
|---|
| 4 | import org.openstreetmap.josm.Main; |
|---|
| 5 | import org.openstreetmap.josm.data.projection.Projection; |
|---|
| 6 | |
|---|
| 7 | /** |
|---|
| 8 | * LatLon class that maintains a cache of projected EastNorth coordinates. |
|---|
| 9 | * |
|---|
| 10 | * This class is convenient to use, but has relatively high memory costs. |
|---|
| 11 | * It keeps a pointer to the last known projection in order to detect projection |
|---|
| 12 | * changes. |
|---|
| 13 | * |
|---|
| 14 | * Node and WayPoint have another, optimized, cache for projected coordinates. |
|---|
| 15 | */ |
|---|
| 16 | public class CachedLatLon extends LatLon { |
|---|
| 17 | private EastNorth eastNorth; |
|---|
| 18 | private Projection proj; |
|---|
| 19 | |
|---|
| 20 | public CachedLatLon(double lat, double lon) { |
|---|
| 21 | super(lat, lon); |
|---|
| 22 | } |
|---|
| 23 | |
|---|
| 24 | public CachedLatLon(LatLon coor) { |
|---|
| 25 | super(coor.lat(), coor.lon()); |
|---|
| 26 | proj = null; |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | public CachedLatLon(EastNorth eastNorth) { |
|---|
| 30 | super(Main.getProjection().eastNorth2latlon(eastNorth)); |
|---|
| 31 | proj = Main.getProjection(); |
|---|
| 32 | this.eastNorth = eastNorth; |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | public final void setCoor(LatLon coor) { |
|---|
| 36 | setLocation(coor.lon(), coor.lat()); |
|---|
| 37 | proj = null; |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | public final void setEastNorth(EastNorth eastNorth) { |
|---|
| 41 | proj = Main.getProjection(); |
|---|
| 42 | this.eastNorth = eastNorth; |
|---|
| 43 | LatLon l = proj.eastNorth2latlon(eastNorth); |
|---|
| 44 | setLocation(l.lon(), l.lat()); |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | /** |
|---|
| 48 | * Replies the projected east/north coordinates. |
|---|
| 49 | * |
|---|
| 50 | * @return the internally cached east/north coordinates. null, if the globally defined projection is null |
|---|
| 51 | */ |
|---|
| 52 | public final EastNorth getEastNorth() { |
|---|
| 53 | if(proj != Main.getProjection()) |
|---|
| 54 | { |
|---|
| 55 | proj = Main.getProjection(); |
|---|
| 56 | eastNorth = proj.latlon2eastNorth(this); |
|---|
| 57 | } |
|---|
| 58 | return eastNorth; |
|---|
| 59 | } |
|---|
| 60 | @Override public String toString() { |
|---|
| 61 | return "CachedLatLon[lat="+lat()+",lon="+lon()+"]"; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | // Only for Node.get3892DebugInfo() |
|---|
| 65 | public Projection getProjection() { |
|---|
| 66 | return proj; |
|---|
| 67 | } |
|---|
| 68 | } |
|---|