source: josm/trunk/src/org/openstreetmap/josm/data/coor/CachedLatLon.java @ 5241

Revision 4126, 2.0 KB checked in by bastiK, 12 months ago (diff)

memory optimizations for Node & WayPoint (Patch by Gubaer, modified)

The field 'proj' in CachedLatLon is a waste of memory. For the 2 classes where this has the greatest impact, the cache for the projected coordinates is replaced by 2 simple double fields (east & north). On projection change, they have to be invalidated explicitly. This is handled by the DataSet & the GpxLayer.

  • Property svn:eol-style set to native
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.data.coor;
3
4import org.openstreetmap.josm.Main;
5import 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 */
16public 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}
Note: See TracBrowser for help on using the repository browser.