source: josm/trunk/src/org/openstreetmap/josm/data/imagery/OffsetBookmark.java@ 4145

Last change on this file since 4145 was 4126, checked in by bastiK, 13 years ago

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
File size: 5.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.imagery;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.ArrayList;
7import java.util.Collection;
8import java.util.Collections;
9import java.util.LinkedList;
10import java.util.List;
11import java.util.ListIterator;
12
13import org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.data.coor.LatLon;
15import org.openstreetmap.josm.data.projection.Mercator;
16import org.openstreetmap.josm.data.projection.Projection;
17import org.openstreetmap.josm.data.projection.ProjectionInfo;
18import org.openstreetmap.josm.data.projection.Projections;
19import org.openstreetmap.josm.gui.layer.ImageryLayer;
20
21public class OffsetBookmark {
22 public static List<OffsetBookmark> allBookmarks = new ArrayList<OffsetBookmark>();
23
24 public Projection proj;
25 public String layerName;
26 public String name;
27 public double dx, dy;
28 public double centerX, centerY;
29
30 public boolean isUsable(ImageryLayer layer) {
31 if (proj == null) return false;
32 if (!Main.getProjection().toCode().equals(proj.toCode())) return false;
33 return layer.getInfo().getName().equals(layerName);
34 }
35
36 public OffsetBookmark(Projection proj, String layerName, String name, double dx, double dy) {
37 this(proj, layerName, name, dx, dy, 0, 0);
38 }
39
40 public OffsetBookmark(Projection proj, String layerName, String name, double dx, double dy, double centerX, double centerY) {
41 this.proj = proj;
42 this.layerName = layerName;
43 this.name = name;
44 this.dx = dx;
45 this.dy = dy;
46 this.centerX = centerX;
47 this.centerY = centerY;
48 }
49
50 public OffsetBookmark(Collection<String> list) {
51 ArrayList<String> array = new ArrayList<String>(list);
52 String projectionStr = array.get(0);
53 proj = ProjectionInfo.getProjectionByCode(projectionStr);
54 if (proj == null) {
55 for (Projection proj : Projections.getProjections()) {
56 if (proj.getCacheDirectoryName().equals(projectionStr)) {
57 this.proj = proj;
58 break;
59 }
60 }
61 }
62 this.layerName = array.get(1);
63 this.name = array.get(2);
64 this.dx = Double.valueOf(array.get(3));
65 this.dy = Double.valueOf(array.get(4));
66 if (array.size() >= 7) {
67 this.centerX = Double.valueOf(array.get(5));
68 this.centerY = Double.valueOf(array.get(6));
69 }
70 // Mercator scale factor migration
71 if (proj instanceof Mercator && Math.abs(this.dx) < 1E-3 && Math.abs(this.dy) < 1E-3) {
72 this.dx *= 6378137;
73 this.dy *= 6378137;
74 }
75 if (proj == null) {
76 System.err.println(tr("Projection ''{0}'' is not found, bookmark ''{1}'' is not usable", projectionStr, name));
77 }
78 }
79
80 public ArrayList<String> getInfoArray() {
81 ArrayList<String> res = new ArrayList<String>(7);
82 if (proj != null) {
83 res.add(proj.toCode());
84 } else {
85 res.add("");
86 }
87 res.add(layerName);
88 res.add(name);
89 res.add(String.valueOf(dx));
90 res.add(String.valueOf(dy));
91 if (this.centerX != 0 || this.centerY != 0) {
92 res.add(String.valueOf(centerX));
93 res.add(String.valueOf(centerY));
94 }
95 return res;
96 }
97
98 public static void loadBookmarks() {
99 for(Collection<String> c : Main.pref.getArray("imagery.offsets",
100 Collections.<Collection<String>>emptySet())) {
101 allBookmarks.add(new OffsetBookmark(c));
102 }
103 }
104
105 public static void saveBookmarks() {
106 LinkedList<Collection<String>> coll = new LinkedList<Collection<String>>();
107 for (OffsetBookmark b : allBookmarks) {
108 coll.add(b.getInfoArray());
109 }
110 Main.pref.putArray("imagery.offsets", coll);
111 }
112
113 public static OffsetBookmark getBookmarkByName(ImageryLayer layer, String name) {
114 for (OffsetBookmark b : allBookmarks) {
115 if (b.isUsable(layer) && name.equals(b.name))
116 return b;
117 }
118 return null;
119 }
120
121 public static void bookmarkOffset(String name, ImageryLayer layer) {
122 LatLon center;
123 if (Main.map != null && Main.map.mapView != null) {
124 center = Main.getProjection().eastNorth2latlon(Main.map.mapView.getCenter());
125 } else {
126 center = new LatLon(0,0);
127 }
128 OffsetBookmark nb = new OffsetBookmark(
129 Main.getProjection(), layer.getInfo().getName(),
130 name, layer.getDx(), layer.getDy(), center.lon(), center.lat());
131 for (ListIterator<OffsetBookmark> it = allBookmarks.listIterator();it.hasNext();) {
132 OffsetBookmark b = it.next();
133 if (b.isUsable(layer) && name.equals(b.name)) {
134 it.set(nb);
135 saveBookmarks();
136 return;
137 }
138 }
139 allBookmarks.add(nb);
140 saveBookmarks();
141 }
142}
Note: See TracBrowser for help on using the repository browser.