1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.coor;
|
---|
3 |
|
---|
4 | import java.util.Objects;
|
---|
5 |
|
---|
6 | import org.openstreetmap.josm.Main;
|
---|
7 | import org.openstreetmap.josm.data.projection.Projecting;
|
---|
8 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * LatLon class that maintains a cache of projected EastNorth coordinates.
|
---|
12 | *
|
---|
13 | * This class is convenient to use, but has relatively high memory costs.
|
---|
14 | * It keeps a pointer to the last known projection in order to detect projection changes.
|
---|
15 | *
|
---|
16 | * Node and WayPoint have another, optimized, cache for projected coordinates.
|
---|
17 | */
|
---|
18 | public class CachedLatLon extends LatLon {
|
---|
19 |
|
---|
20 | private static final long serialVersionUID = 1L;
|
---|
21 |
|
---|
22 | private EastNorth eastNorth;
|
---|
23 | private transient Object cacheKey;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Constructs a new {@code CachedLatLon}.
|
---|
27 | * @param lat latitude
|
---|
28 | * @param lon longitude
|
---|
29 | */
|
---|
30 | public CachedLatLon(double lat, double lon) {
|
---|
31 | super(lat, lon);
|
---|
32 | }
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Constructs a new {@code CachedLatLon}.
|
---|
36 | * @param coor lat/lon
|
---|
37 | */
|
---|
38 | public CachedLatLon(LatLon coor) {
|
---|
39 | super(coor.lat(), coor.lon());
|
---|
40 | cacheKey = null;
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Constructs a new {@code CachedLatLon}.
|
---|
45 | * @param eastNorth easting/northing
|
---|
46 | */
|
---|
47 | public CachedLatLon(EastNorth eastNorth) {
|
---|
48 | this(eastNorth, Main.getProjection());
|
---|
49 | }
|
---|
50 |
|
---|
51 | private CachedLatLon(EastNorth eastNorth, Projection projection) {
|
---|
52 | super(projection.eastNorth2latlon(eastNorth));
|
---|
53 | cacheKey = projection.getCacheKey();
|
---|
54 | this.eastNorth = eastNorth;
|
---|
55 | }
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Replies the projected east/north coordinates.
|
---|
59 | *
|
---|
60 | * @return the internally cached east/north coordinates. null, if the globally defined projection is null
|
---|
61 | */
|
---|
62 | @Override
|
---|
63 | public final EastNorth getEastNorth(Projecting projecting) {
|
---|
64 | if (!Objects.equals(cacheKey, projecting.getCacheKey())) {
|
---|
65 | cacheKey = projecting.getCacheKey();
|
---|
66 | eastNorth = projecting.latlon2eastNorth(this);
|
---|
67 | }
|
---|
68 | return eastNorth;
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Override
|
---|
72 | public int hashCode() {
|
---|
73 | return Objects.hash(x, y, eastNorth);
|
---|
74 | }
|
---|
75 |
|
---|
76 | @Override
|
---|
77 | public boolean equals(Object obj) {
|
---|
78 | if (!super.equals(obj))
|
---|
79 | return false;
|
---|
80 | CachedLatLon other = (CachedLatLon) obj;
|
---|
81 | return Objects.equals(eastNorth, other.eastNorth);
|
---|
82 | }
|
---|
83 |
|
---|
84 | @Override
|
---|
85 | public String toString() {
|
---|
86 | return "CachedLatLon[lat="+lat()+",lon="+lon()+']';
|
---|
87 | }
|
---|
88 | }
|
---|