1 | // License: GPL. For details, see LICENSE file.
|
---|
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 changes.
|
---|
12 | *
|
---|
13 | * Node and WayPoint have another, optimized, cache for projected coordinates.
|
---|
14 | */
|
---|
15 | public class CachedLatLon extends LatLon {
|
---|
16 |
|
---|
17 | private static final long serialVersionUID = 1L;
|
---|
18 |
|
---|
19 | private EastNorth eastNorth;
|
---|
20 | private transient Projection proj;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Constructs a new {@code CachedLatLon}.
|
---|
24 | * @param lat latitude
|
---|
25 | * @param lon longitude
|
---|
26 | */
|
---|
27 | public CachedLatLon(double lat, double lon) {
|
---|
28 | super(lat, lon);
|
---|
29 | }
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Constructs a new {@code CachedLatLon}.
|
---|
33 | * @param coor lat/lon
|
---|
34 | */
|
---|
35 | public CachedLatLon(LatLon coor) {
|
---|
36 | super(coor.lat(), coor.lon());
|
---|
37 | proj = null;
|
---|
38 | }
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Constructs a new {@code CachedLatLon}.
|
---|
42 | * @param eastNorth easting/northing
|
---|
43 | */
|
---|
44 | public CachedLatLon(EastNorth eastNorth) {
|
---|
45 | super(Main.getProjection().eastNorth2latlon(eastNorth));
|
---|
46 | proj = Main.getProjection();
|
---|
47 | this.eastNorth = eastNorth;
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Replies the projected east/north coordinates.
|
---|
52 | *
|
---|
53 | * @return the internally cached east/north coordinates. null, if the globally defined projection is null
|
---|
54 | */
|
---|
55 | public final EastNorth getEastNorth() {
|
---|
56 | if (proj != Main.getProjection()) {
|
---|
57 | proj = Main.getProjection();
|
---|
58 | eastNorth = proj.latlon2eastNorth(this);
|
---|
59 | }
|
---|
60 | return eastNorth;
|
---|
61 | }
|
---|
62 |
|
---|
63 | @Override
|
---|
64 | public String toString() {
|
---|
65 | return "CachedLatLon[lat="+lat()+",lon="+lon()+"]";
|
---|
66 | }
|
---|
67 | }
|
---|