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

Last change on this file since 12809 was 12630, checked in by Don-vip, 7 years ago

see #15182 - deprecate Main.map and Main.isDisplayingMapView(). Replacements: gui.MainApplication.getMap() / gui.MainApplication.isDisplayingMapView()

  • Property svn:eol-style set to native
File size: 9.5 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.List;
10import java.util.ListIterator;
11import java.util.Map;
12
13import org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.data.Preferences;
15import org.openstreetmap.josm.data.Preferences.pref;
16import org.openstreetmap.josm.data.Preferences.writeExplicitly;
17import org.openstreetmap.josm.data.coor.EastNorth;
18import org.openstreetmap.josm.data.coor.LatLon;
19import org.openstreetmap.josm.data.projection.Projection;
20import org.openstreetmap.josm.data.projection.Projections;
21import org.openstreetmap.josm.gui.MainApplication;
22import org.openstreetmap.josm.gui.layer.AbstractTileSourceLayer;
23import org.openstreetmap.josm.gui.layer.ImageryLayer;
24import org.openstreetmap.josm.tools.Logging;
25
26/**
27 * Class to save a displacement of background imagery as a bookmark.
28 *
29 * Known offset bookmarks will be stored in the preferences and can be
30 * restored by the user in later sessions.
31 */
32public class OffsetBookmark {
33 private static final List<OffsetBookmark> allBookmarks = new ArrayList<>();
34
35 @pref private String projection_code;
36 @pref private String imagery_name;
37 @pref private String name;
38 @pref @writeExplicitly private double dx, dy;
39 @pref private double center_lon, center_lat;
40
41 public boolean isUsable(ImageryLayer layer) {
42 if (projection_code == null) return false;
43 if (!Main.getProjection().toCode().equals(projection_code) && !hasCenter()) return false;
44 return layer.getInfo().getName().equals(imagery_name);
45 }
46
47 /**
48 * Construct new empty OffsetBookmark.
49 *
50 * Only used for preferences handling.
51 */
52 public OffsetBookmark() {
53 // do nothing
54 }
55
56 public OffsetBookmark(String projectionCode, String imageryName, String name, double dx, double dy) {
57 this(projectionCode, imageryName, name, dx, dy, 0, 0);
58 }
59
60 public OffsetBookmark(String projectionCode, String imageryName, String name, double dx, double dy, double centerLon, double centerLat) {
61 this.projection_code = projectionCode;
62 this.imagery_name = imageryName;
63 this.name = name;
64 this.dx = dx;
65 this.dy = dy;
66 this.center_lon = centerLon;
67 this.center_lat = centerLat;
68 }
69
70 public OffsetBookmark(Collection<String> list) {
71 List<String> array = new ArrayList<>(list);
72 this.projection_code = array.get(0);
73 this.imagery_name = array.get(1);
74 this.name = array.get(2);
75 this.dx = Double.parseDouble(array.get(3));
76 this.dy = Double.parseDouble(array.get(4));
77 if (array.size() >= 7) {
78 this.center_lon = Double.parseDouble(array.get(5));
79 this.center_lat = Double.parseDouble(array.get(6));
80 }
81 if (projection_code == null) {
82 Logging.error(tr("Projection ''{0}'' is not found, bookmark ''{1}'' is not usable", projection_code, name));
83 }
84 }
85
86 public String getProjectionCode() {
87 return projection_code;
88 }
89
90 public String getName() {
91 return name;
92 }
93
94 public String getImageryName() {
95 return imagery_name;
96 }
97
98 /**
99 * Get displacement in EastNorth coordinates of the original projection.
100 *
101 * @return the displacement
102 * @see #getProjectionCode()
103 */
104 public EastNorth getDisplacement() {
105 return new EastNorth(dx, dy);
106 }
107
108 /**
109 * Get displacement in EastNorth coordinates of a given projection.
110 *
111 * Displacement will be converted to the given projection, with respect to the
112 * center (reference point) of this bookmark.
113 * @param proj the projection
114 * @return the displacement, converted to that projection
115 */
116 public EastNorth getDisplacement(Projection proj) {
117 if (proj.toCode().equals(projection_code)) {
118 return getDisplacement();
119 }
120 LatLon center = getCenter();
121 Projection offsetProj = Projections.getProjectionByCode(projection_code);
122 EastNorth centerEN = center.getEastNorth(offsetProj);
123 EastNorth shiftedEN = centerEN.add(getDisplacement());
124 LatLon shifted = offsetProj.eastNorth2latlon(shiftedEN);
125 EastNorth centerEN2 = center.getEastNorth(proj);
126 EastNorth shiftedEN2 = shifted.getEastNorth(proj);
127 return shiftedEN2.subtract(centerEN2);
128 }
129
130 /**
131 * Get center/reference point of the bookmark.
132 *
133 * Basically this is the place where it was created and is valid.
134 * The center may be unrecorded (see {@link #hasCenter()}, in which
135 * case a dummy center (0,0) will be returned.
136 * @return the center
137 */
138 public LatLon getCenter() {
139 return new LatLon(center_lat, center_lon);
140 }
141
142 /**
143 * Check if bookmark has a valid center.
144 * @return true if bookmark has a valid center
145 */
146 public boolean hasCenter() {
147 return center_lat != 0 || center_lon != 0;
148 }
149
150 public void setProjectionCode(String projectionCode) {
151 this.projection_code = projectionCode;
152 }
153
154 public void setName(String name) {
155 this.name = name;
156 }
157
158 public void setImageryName(String imageryName) {
159 this.imagery_name = imageryName;
160 }
161
162 public void setDisplacement(EastNorth displacement) {
163 this.dx = displacement.east();
164 this.dy = displacement.north();
165 }
166
167 public static void loadBookmarks() {
168 List<OffsetBookmark> bookmarks = Main.pref.getListOfStructs("imagery.offsetbookmarks", null, OffsetBookmark.class);
169 if (bookmarks == null) {
170 loadBookmarksOld();
171 saveBookmarks();
172 } else {
173 allBookmarks.addAll(bookmarks);
174 }
175 }
176
177 // migration code - remove Nov. 2017
178 private static void loadBookmarksOld() {
179 for (Collection<String> c : Main.pref.getArray("imagery.offsets",
180 Collections.<Collection<String>>emptySet())) {
181 allBookmarks.add(new OffsetBookmark(c));
182 }
183 }
184
185 public static void saveBookmarks() {
186 Main.pref.putListOfStructs("imagery.offsetbookmarks", allBookmarks, OffsetBookmark.class);
187 }
188
189 /**
190 * Returns all bookmarks.
191 * @return all bookmarks (unmodifiable collection)
192 * @since 11651
193 */
194 public static List<OffsetBookmark> getBookmarks() {
195 return Collections.unmodifiableList(allBookmarks);
196 }
197
198 /**
199 * Returns the number of bookmarks.
200 * @return the number of bookmarks
201 * @since 11651
202 */
203 public static int getBookmarksSize() {
204 return allBookmarks.size();
205 }
206
207 /**
208 * Adds a bookmark.
209 * @param ob bookmark to add
210 * @return {@code true}
211 * @since 11651
212 */
213 public static boolean addBookmark(OffsetBookmark ob) {
214 return allBookmarks.add(ob);
215 }
216
217 /**
218 * Removes a bookmark.
219 * @param ob bookmark to remove
220 * @return {@code true} if this list contained the specified element
221 * @since 11651
222 */
223 public static boolean removeBookmark(OffsetBookmark ob) {
224 return allBookmarks.remove(ob);
225 }
226
227 /**
228 * Returns the bookmark at the given index.
229 * @param index bookmark index
230 * @return the bookmark at the given index
231 * @throws IndexOutOfBoundsException if the index is out of range
232 * (<tt>index &lt; 0 || index &gt;= size()</tt>)
233 * @since 11651
234 */
235 public static OffsetBookmark getBookmarkByIndex(int index) {
236 return allBookmarks.get(index);
237 }
238
239 public static OffsetBookmark getBookmarkByName(ImageryLayer layer, String name) {
240 for (OffsetBookmark b : allBookmarks) {
241 if (b.isUsable(layer) && name.equals(b.name))
242 return b;
243 }
244 return null;
245 }
246
247 public static void bookmarkOffset(String name, AbstractTileSourceLayer layer) {
248 LatLon center;
249 if (MainApplication.isDisplayingMapView()) {
250 center = Main.getProjection().eastNorth2latlon(MainApplication.getMap().mapView.getCenter());
251 } else {
252 center = LatLon.ZERO;
253 }
254 OffsetBookmark nb = new OffsetBookmark(
255 Main.getProjection().toCode(), layer.getInfo().getName(),
256 name, layer.getDisplaySettings().getDx(), layer.getDisplaySettings().getDy(), center.lon(), center.lat());
257 for (ListIterator<OffsetBookmark> it = allBookmarks.listIterator(); it.hasNext();) {
258 OffsetBookmark b = it.next();
259 if (b.isUsable(layer) && name.equals(b.name)) {
260 it.set(nb);
261 saveBookmarks();
262 return;
263 }
264 }
265 allBookmarks.add(nb);
266 saveBookmarks();
267 }
268
269 /**
270 * Converts the offset bookmark to a properties map.
271 *
272 * The map contains all the information to restore the offset bookmark.
273 * @return properties map of all data
274 * @see #fromPropertiesMap(java.util.Map)
275 * @since 12134
276 */
277 public Map<String, String> toPropertiesMap() {
278 return Preferences.serializeStruct(this, OffsetBookmark.class);
279 }
280
281 /**
282 * Creates an offset bookmark from a properties map.
283 * @param properties the properties map
284 * @return corresponding offset bookmark
285 * @see #toPropertiesMap()
286 * @since 12134
287 */
288 public static OffsetBookmark fromPropertiesMap(Map<String, String> properties) {
289 return Preferences.deserializeStruct(properties, OffsetBookmark.class);
290 }
291}
Note: See TracBrowser for help on using the repository browser.